-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipc.es
68 lines (61 loc) · 1.37 KB
/
ipc.es
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
//
// ipc: Inter-Plugins Call
//
class IPC {
constructor() {
this.data = new Object()
}
// scope: string
// opts: key-func Object
register = (scope, opts) => {
if (!(scope && opts)) {
console.error("Invalid scope or opts:", scope, opts)
return
}
if (!this.data[scope]) {
this.data[scope]= new Object()
}
this.unregister(scope, Object.keys(opts))
for (const key in opts) {
this.data[scope][key] = opts[key]
}
return
}
// scope: string
// keys: string / Array of string / key-func Object
unregister = (scope, keys) => {
if (!(scope && keys)) {
console.error("Invalid scope or keys:", scope, keys)
return
}
if (!this.data[scope]) {
return
}
if (typeof keys === "string") {
keys = new Array(keys)
}
if (keys instanceof Object && !(keys instanceof Array)) {
keys = Object.keys(keys)
}
for (const key of keys) {
delete this.data[scope][key]
}
return
}
unregisterAll = (scope) => {
delete this.data[scope]
}
access = (scope) => {
return this.data[scope]
}
// key: string
// args: arguments passing to api
foreachCall = (key, ...args) => {
for (const scope in this.data) {
if (this.data[scope].hasOwnProperty(key)){
this.data[key].apply(null, args)
}
}
}
}
export default new IPC()