50 lines
1017 B
JavaScript
50 lines
1017 B
JavaScript
|
import React from 'react';
|
||
|
import ReactDOM from 'react-dom';
|
||
|
import _ from 'lodash';
|
||
|
|
||
|
class UrbitApi {
|
||
|
setAuthTokens(authTokens) {
|
||
|
this.authTokens = authTokens;
|
||
|
this.bindPaths = [];
|
||
|
}
|
||
|
|
||
|
bind(path, method, ship = this.authTokens.ship, appl = "%APPNAME%", success, fail) {
|
||
|
this.bindPaths = _.uniq([...this.bindPaths, path]);
|
||
|
|
||
|
window.subscriptionId = window.urb.subscribe(ship, appl, path,
|
||
|
(err) => {
|
||
|
fail(err);
|
||
|
},
|
||
|
(event) => {
|
||
|
success({
|
||
|
data: event,
|
||
|
from: {
|
||
|
ship,
|
||
|
path
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
(err) => {
|
||
|
fail(err);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
%APPNAME%(data) {
|
||
|
this.action("%APPNAME%", "json", data);
|
||
|
}
|
||
|
|
||
|
action(appl, mark, data) {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
window.urb.poke(ship, appl, mark, data,
|
||
|
(json) => {
|
||
|
resolve(json);
|
||
|
},
|
||
|
(err) => {
|
||
|
reject(err);
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
export let api = new UrbitApi();
|
||
|
window.api = api;
|