Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SYNTACTIC SUGAR ON TOP OF deepstream CLIENT API
- var deepstream = require('deepstream.io-client-js');
- String.prototype.hash = function() {
- var hash = 0, i, chr;
- if (this.length === 0) return hash;
- for (i = 0; i < this.length; i++) {
- chr = this.charCodeAt(i);
- hash = ((hash << 5) - hash) + chr;
- hash |= 0; // Convert to 32bit integer
- }
- return hash;
- };
- var _ = require('lodash');
- module.exports = class Client {
- constructor({host = 'localhost', port = 6020}) {
- this.instance = deepstream(host+':'+port);
- }
- authenticate({username = null, password = null}) {
- return new Promise((resolve, reject) => {
- this.instance.login({username, password}, (success, data) => {
- if (success) {
- this.id = username;
- resolve();
- }
- else reject();
- })
- });
- }
- subscribe_once({type, id, path, on_change}) {
- return new Promise((resolve, reject) => {
- this.instance.record.getRecord(type+'/'+id).whenReady((r)=> {
- r.subscribe(path, async () => {
- await on_change();
- r.discard();
- }, false);
- resolve({
- unsubscribe: ()=>{
- r.discard();
- },
- data: r.get(path)
- });
- });
- });
- }
- subscribe({type, id, path, on_change}) {
- return new Promise((resolve, reject) => {
- this.instance.record.getRecord(type+'/'+id).whenReady((r)=> {
- r.subscribe(path, async()=> {await on_change();}, false);
- resolve({
- unsubscribe: ()=>{
- r.discard();
- },
- data: r.get(path)
- });
- });
- });
- }
- exists({type, id}) {
- return new Promise((resolve) => {
- this.instance.record.has(type+'/'+id, (error, result) => {
- resolve(result);
- })
- })
- }
- request({service, payload = {}}) {
- return new Promise((resolve, reject) => {
- if (!payload.origin) delete payload.origin; // (so that "null" and "undefined" values of origin do not override the default value).
- this.instance.rpc.make(service, { origin: {id: this.id}, ...payload}, (error, response) => {
- if (error) {
- reject(error);
- } else {
- resolve(response);
- }
- })
- })
- }
- delete({type, id}) {
- return this.request({service: 'record/delete', payload: {record: {type, id}, origin}});
- }
- set({type, id}, {path, value}, origin) {
- return this.request({service: 'record/set', payload: {record: {type, id}, path, value, origin}});
- }
- create({type, id}, origin) {
- return this.request({service: 'record/create', payload: {record: {type, id}, origin}});
- }
- get_record({type, id}) {
- return new Promise((resolve, reject) => {
- this.instance.record.getRecord(type+'/'+id).whenReady((r) => {
- resolve(r);
- });
- });
- }
- get({type, id, path}) {
- return new Promise((resolve, reject)=>{
- this.instance.record.getRecord(type+'/'+id).whenReady((r)=> {
- resolve(r.get(path));
- });
- });
- }
- generate_new_unique_id() {
- return this.instance.getUid();
- }
- get Signal() {
- let client = this;
- return class {
- constructor({name, to, data}) {
- this.name = name;
- this.to = to;
- this.data = data;
- client.instance.event.emit(this.to + '/' + this.name, {data: this.data, origin: client.id});
- }
- }
- }
- get when() {
- let registrar = (signal) => {
- return (handler) => {
- this.instance.event.subscribe('/'+ signal, handler);
- return () => {
- this.instance.event.unsubscribe('/'+ signal, handler);
- }
- }
- }
- registrar.private = (signal) => {
- return (handler) => {
- this.instance.event.subscribe(this.id + '/' + signal, handler);
- return () => {
- this.instance.event.unsubscribe(this.id + '/' + signal, handler);
- }
- }
- }
- return registrar;
- }
- async query(string, on_change) {
- // THIS IS A VERY WEIRD WAY OF SUBSCRIBING TO QUERY RESULT UPDATES.
- // BUT IT IS NOT NECESSARY;
- // REPLACE subscriber WITH ANYTHING YOU LIKE INSTEAD.
- let x = {};
- let data = await this.request({service: 'query', payload: {string}});
- for (key in x) delete x[key];
- _.assign(x, data);
- let subscriber = (data) => {
- for (let key in x) delete x[key];
- _.assign(x, data);
- if (on_change) {
- console.log('change happened');
- on_change(x, () => {
- this.instance.event.unsubscribe(this.id+'/service/query/'+string.hash(), subscriber);
- });
- }
- }
- this.instance.event.subscribe(this.id+'/service/query/'+string.hash(), subscriber);
- return x;
- }
- }
Add Comment
Please, Sign In to add comment