2020-05-01 05:54:12 +03:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
2020-05-09 06:51:18 +03:00
|
|
|
export default class Api {
|
2021-05-06 07:20:34 +03:00
|
|
|
ship: any;
|
|
|
|
channel: any;
|
|
|
|
bindPaths: any[];
|
2020-05-09 06:51:18 +03:00
|
|
|
constructor(ship, channel) {
|
|
|
|
this.ship = ship;
|
2020-05-01 05:54:12 +03:00
|
|
|
this.channel = channel;
|
|
|
|
this.bindPaths = [];
|
|
|
|
}
|
|
|
|
|
2020-11-09 22:27:41 +03:00
|
|
|
bind(path, method, ship = this.ship, appl = 'herm', success, fail) {
|
2020-05-01 05:54:12 +03:00
|
|
|
this.bindPaths = _.uniq([...this.bindPaths, path]);
|
|
|
|
|
2021-05-06 07:20:34 +03:00
|
|
|
(window as any).subscriptionId = this.channel.subscribe(ship, appl, path,
|
2020-05-01 05:54:12 +03:00
|
|
|
(err) => {
|
|
|
|
fail(err);
|
|
|
|
},
|
|
|
|
(event) => {
|
|
|
|
success({
|
|
|
|
data: event,
|
|
|
|
from: {
|
|
|
|
ship,
|
|
|
|
path
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
fail(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-24 02:25:44 +03:00
|
|
|
belt(belt) {
|
|
|
|
return this.action('herm', 'belt', belt);
|
2020-05-01 05:54:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
action(appl, mark, data) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.channel.poke(window.ship, appl, mark, data,
|
|
|
|
(json) => {
|
|
|
|
resolve(json);
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|