-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker_manager.js
More file actions
60 lines (54 loc) · 1.48 KB
/
worker_manager.js
File metadata and controls
60 lines (54 loc) · 1.48 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
"use strict";
function ProWorker(account)
{
this.worker = new Worker('worker.js')
this.account = account
this.mhandlers = {}
var pw = this
this.worker.onmessage = function(msg)
{
for (let i in pw.mhandlers[msg.data.name])
pw.mhandlers[msg.data.name][i](msg.data.msg)
}
}
ProWorker.prototype.refresh_account = function(account){
this.account = account
}
ProWorker.prototype.connect = function (mname, mcallback)
{
if (! $.isArray(this.mhandlers[mname]))
this.mhandlers[mname] = []
if ($.inArray(mcallback, this.mhandlers[mname]) == -1)
this.mhandlers[mname].push(mcallback)
}
ProWorker.prototype.disconnect = function (mname, mcallback)
{
if (! $.isArray(this.mhandlers[mname]))
this.mhandlers[mname] = []
if (typeof mcallback == "number")
this.mhandlers[mname].splice(mcallback, 0)
else if ($.inArray(mcallback, this.mhandlers[mname]) == -1)
this.mhandlers[mname] = this.mhandlers[mname]
.filter(function(e){return (e != mcallback)})
}
ProWorker.prototype.connectOneShot = function (mname, mcallback)
{
var pw = this
let oneshotcb = function(data){
mcallback(data)
pw.disconnect(oneshotcb)
}
this.connect(mname, oneshotcb)
}
ProWorker.prototype.justSend = function (name, msg)
{
this.worker.postMessage({name: name, account: this.account, msg: msg})
}
ProWorker.prototype.send = function (name, msg)
{
var pw = this
return new Promise(function(ok, fail){
pw.connectOneShot(name, ok)
pw.justSend(name, msg)
})
}