-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwish-app-inner.js
More file actions
96 lines (76 loc) · 2.64 KB
/
wish-app-inner.js
File metadata and controls
96 lines (76 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* Copyright (C) 2020, ControlThings Oy Ab
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* @license Apache-2.0
*/
var EventEmitter = require("events").EventEmitter;
var inherits = require('util').inherits;
function WishAppInner(addon) {
var self = this;
this.peers = [];
this.requests = {};
this.addon = addon;
addon.on('ready', function(ready) {
self.emit('ready', ready);
if (typeof self.readyCb === 'function') { self.readyCb(ready); }
});
addon.on('online', function(peer) {
if (typeof self.onlineCb === 'function') { self.onlineCb(peer); }
self.peers.push(peer);
});
addon.on('offline', function(peer) {
if (typeof self.offlineCb === 'function') { self.offlineCb(peer); }
});
addon.on('frame', function(peer, data) {
if (typeof self.frameCb === 'function') { self.frameCb(peer, data); }
});
addon.on('wish', function(msg) {
var id = msg.ack || msg.sig || msg.end || msg.err;
if(typeof self.requests[id] === 'function') {
self.requests[id](msg);
if(!msg.sig) {
delete self.requests[id];
}
} else {
console.log('Request not found for response:', id, self, self.requests);
}
});
}
inherits(WishAppInner, EventEmitter);
WishAppInner.prototype.send = function(peer, message, cb) {
this.request('services.send', [peer, message], cb || function() {});
};
WishAppInner.prototype.broadcast = function(message) {
for(var i in this.peers) {
this.request('services.send', [this.peers[i], message], function() {});
}
};
WishAppInner.prototype.request = function(op, args, cb) {
if (typeof cb !== 'function') { console.log("not function:", new Error().stack); }
return this.requestBare(op, args, function(res) {
if(res.err) { return cb(true, res.data); }
cb(null, res.data);
});
};
WishAppInner.prototype.requestBare = function(op, args, cb) {
var id = ++this.addon.sharedRequestId;
var request = { op: op, args: typeof args === 'undefined' ? [] : args, id: id };
// store callback for response
this.requests[id] = cb;
this.addon.request("wish", request);
return id;
};
WishAppInner.prototype.cancel = function(id) {
this.addon.request("wish", { cancel: id });
};
WishAppInner.prototype.shutdown = function() {
this.addon.shutdown();
};
module.exports = {
WishAppInner: WishAppInner };