@polylith/core 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/EventBus.js +4 -0
- package/Registry.js +50 -0
- package/ServiceObject.js +34 -10
- package/package.json +1 -1
package/EventBus.js
CHANGED
package/Registry.js
CHANGED
|
@@ -26,6 +26,56 @@ class Registry {
|
|
|
26
26
|
return this.services[name];
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
makeService(name, obj, implements) {
|
|
30
|
+
obj.serviceObject = new ServiceOject(name);
|
|
31
|
+
|
|
32
|
+
obj.serviceObject.implementOn(obj, 'fire');
|
|
33
|
+
obj.serviceObject.implementOn(obj, 'listen');
|
|
34
|
+
obj.serviceObject.implementOn(obj, 'unlisten');
|
|
35
|
+
|
|
36
|
+
if (name) {
|
|
37
|
+
registry.register(name, obj.serviceObject);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (implements) {
|
|
41
|
+
var methods = {};
|
|
42
|
+
|
|
43
|
+
implements.forEach(function(name) {
|
|
44
|
+
if (obj[name]) {
|
|
45
|
+
methods[name] = obj[name].bind(obj);
|
|
46
|
+
} else {
|
|
47
|
+
console.warn('method', name, 'not implemented on service', name ? name : '<unnamed service>')
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
obj.serviceObject.implement(methods);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
extendService(name, obj, implements) {
|
|
56
|
+
var serviceObject = this.subscribe(name);
|
|
57
|
+
|
|
58
|
+
obj.serviceObject = serviceObject || new ServiceOject(name);
|
|
59
|
+
|
|
60
|
+
obj.serviceObject.implementOn(obj, 'fire');
|
|
61
|
+
obj.serviceObject.implementOn(obj, 'listen');
|
|
62
|
+
obj.serviceObject.implementOn(obj, 'unlisten');
|
|
63
|
+
|
|
64
|
+
if (implements) {
|
|
65
|
+
var methods = {};
|
|
66
|
+
|
|
67
|
+
implements.forEach(function(name) {
|
|
68
|
+
if (obj[name]) {
|
|
69
|
+
methods[name] = obj[name].bind(obj);
|
|
70
|
+
} else {
|
|
71
|
+
console.warn('method', name, 'not implemented on service', name ? name : '<unnamed service>')
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
obj.serviceObject.implement(methods);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
29
79
|
callAll(serviceNames, which, ...args) {
|
|
30
80
|
var promises = [];
|
|
31
81
|
|
package/ServiceObject.js
CHANGED
|
@@ -6,6 +6,34 @@ export class ServiceOject extends EventBus {
|
|
|
6
6
|
|
|
7
7
|
this.name = name;
|
|
8
8
|
this.bound = true;
|
|
9
|
+
this.methods = [];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
assignMethod(name, method) {
|
|
13
|
+
// if there is already a listener, unbind and force invoking
|
|
14
|
+
var bind = this.bound || !this.listeners[name];xxx
|
|
15
|
+
|
|
16
|
+
if (bind) {
|
|
17
|
+
this[name] = method;
|
|
18
|
+
} else {
|
|
19
|
+
// this may be a reassignment, but that's okay.
|
|
20
|
+
this.name = this.invoke.bind(this, name)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
this.methods.push(name);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
unbindMethod(name) {
|
|
27
|
+
if (this[method]) {
|
|
28
|
+
this.name = this.invoke.bind(this, name)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
unbind() {
|
|
33
|
+
this.bound = false;
|
|
34
|
+
this.methods.forEach(function(name) {
|
|
35
|
+
this.unbindMethod(name);
|
|
36
|
+
}, this)
|
|
9
37
|
}
|
|
10
38
|
|
|
11
39
|
implement(methods) {
|
|
@@ -13,23 +41,19 @@ export class ServiceOject extends EventBus {
|
|
|
13
41
|
|
|
14
42
|
keys.forEach(function(name) {
|
|
15
43
|
if (methods[name]) {
|
|
16
|
-
|
|
44
|
+
this.assignMethod(name, methods[name])
|
|
17
45
|
|
|
46
|
+
// always add this as a listener, in case the method becomes unbound
|
|
18
47
|
this.listen(name, methods[name]);
|
|
19
|
-
if (!isFirst) {
|
|
20
|
-
delete(this[name])
|
|
21
|
-
} else if (this.bound) {
|
|
22
|
-
this[name] = methods[name];
|
|
23
|
-
}
|
|
24
48
|
}
|
|
25
49
|
}, this);
|
|
26
50
|
}
|
|
27
51
|
|
|
28
52
|
invoke(name, ...args) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
53
|
+
return this.fire(name, ...args);
|
|
54
|
+
}
|
|
32
55
|
|
|
33
|
-
|
|
56
|
+
async asyncInvoke(name, ...args) {
|
|
57
|
+
return this.invoke(name, ...args)
|
|
34
58
|
}
|
|
35
59
|
}
|