Guest User

Untitled

a guest
Mar 19th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. // SYNTACTIC SUGAR ON TOP OF deepstream CLIENT API
  2. var deepstream = require('deepstream.io-client-js');
  3.  
  4. String.prototype.hash = function() {
  5. var hash = 0, i, chr;
  6. if (this.length === 0) return hash;
  7. for (i = 0; i < this.length; i++) {
  8. chr = this.charCodeAt(i);
  9. hash = ((hash << 5) - hash) + chr;
  10. hash |= 0; // Convert to 32bit integer
  11. }
  12. return hash;
  13. };
  14.  
  15. var _ = require('lodash');
  16.  
  17. module.exports = class Client {
  18. constructor({host = 'localhost', port = 6020}) {
  19. this.instance = deepstream(host+':'+port);
  20. }
  21.  
  22. authenticate({username = null, password = null}) {
  23. return new Promise((resolve, reject) => {
  24. this.instance.login({username, password}, (success, data) => {
  25. if (success) {
  26. this.id = username;
  27. resolve();
  28. }
  29. else reject();
  30. })
  31. });
  32. }
  33.  
  34. subscribe_once({type, id, path, on_change}) {
  35. return new Promise((resolve, reject) => {
  36. this.instance.record.getRecord(type+'/'+id).whenReady((r)=> {
  37. r.subscribe(path, async () => {
  38. await on_change();
  39. r.discard();
  40. }, false);
  41. resolve({
  42. unsubscribe: ()=>{
  43. r.discard();
  44. },
  45. data: r.get(path)
  46. });
  47. });
  48. });
  49. }
  50.  
  51. subscribe({type, id, path, on_change}) {
  52. return new Promise((resolve, reject) => {
  53. this.instance.record.getRecord(type+'/'+id).whenReady((r)=> {
  54. r.subscribe(path, async()=> {await on_change();}, false);
  55. resolve({
  56. unsubscribe: ()=>{
  57. r.discard();
  58. },
  59. data: r.get(path)
  60. });
  61. });
  62. });
  63. }
  64.  
  65. exists({type, id}) {
  66. return new Promise((resolve) => {
  67. this.instance.record.has(type+'/'+id, (error, result) => {
  68. resolve(result);
  69. })
  70. })
  71. }
  72.  
  73. request({service, payload = {}}) {
  74. return new Promise((resolve, reject) => {
  75. if (!payload.origin) delete payload.origin; // (so that "null" and "undefined" values of origin do not override the default value).
  76. this.instance.rpc.make(service, { origin: {id: this.id}, ...payload}, (error, response) => {
  77. if (error) {
  78. reject(error);
  79. } else {
  80. resolve(response);
  81. }
  82. })
  83. })
  84. }
  85.  
  86. delete({type, id}) {
  87. return this.request({service: 'record/delete', payload: {record: {type, id}, origin}});
  88. }
  89.  
  90. set({type, id}, {path, value}, origin) {
  91. return this.request({service: 'record/set', payload: {record: {type, id}, path, value, origin}});
  92. }
  93.  
  94. create({type, id}, origin) {
  95. return this.request({service: 'record/create', payload: {record: {type, id}, origin}});
  96. }
  97.  
  98. get_record({type, id}) {
  99. return new Promise((resolve, reject) => {
  100. this.instance.record.getRecord(type+'/'+id).whenReady((r) => {
  101. resolve(r);
  102. });
  103. });
  104. }
  105.  
  106. get({type, id, path}) {
  107. return new Promise((resolve, reject)=>{
  108. this.instance.record.getRecord(type+'/'+id).whenReady((r)=> {
  109. resolve(r.get(path));
  110. });
  111. });
  112. }
  113.  
  114. generate_new_unique_id() {
  115. return this.instance.getUid();
  116. }
  117.  
  118. get Signal() {
  119. let client = this;
  120. return class {
  121. constructor({name, to, data}) {
  122. this.name = name;
  123. this.to = to;
  124. this.data = data;
  125. client.instance.event.emit(this.to + '/' + this.name, {data: this.data, origin: client.id});
  126. }
  127. }
  128. }
  129.  
  130. get when() {
  131. let registrar = (signal) => {
  132. return (handler) => {
  133. this.instance.event.subscribe('/'+ signal, handler);
  134. return () => {
  135. this.instance.event.unsubscribe('/'+ signal, handler);
  136. }
  137. }
  138. }
  139. registrar.private = (signal) => {
  140. return (handler) => {
  141. this.instance.event.subscribe(this.id + '/' + signal, handler);
  142. return () => {
  143. this.instance.event.unsubscribe(this.id + '/' + signal, handler);
  144. }
  145. }
  146. }
  147.  
  148. return registrar;
  149. }
  150.  
  151. async query(string, on_change) {
  152. // THIS IS A VERY WEIRD WAY OF SUBSCRIBING TO QUERY RESULT UPDATES.
  153. // BUT IT IS NOT NECESSARY;
  154. // REPLACE subscriber WITH ANYTHING YOU LIKE INSTEAD.
  155. let x = {};
  156. let data = await this.request({service: 'query', payload: {string}});
  157. for (key in x) delete x[key];
  158. _.assign(x, data);
  159. let subscriber = (data) => {
  160. for (let key in x) delete x[key];
  161. _.assign(x, data);
  162. if (on_change) {
  163. console.log('change happened');
  164. on_change(x, () => {
  165. this.instance.event.unsubscribe(this.id+'/service/query/'+string.hash(), subscriber);
  166. });
  167. }
  168. }
  169. this.instance.event.subscribe(this.id+'/service/query/'+string.hash(), subscriber);
  170.  
  171. return x;
  172. }
  173. }
Add Comment
Please, Sign In to add comment