@polylith/core 0.0.4 → 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 CHANGED
@@ -79,6 +79,10 @@ export class EventBus {
79
79
  }.bind(this));
80
80
  }
81
81
 
82
+ async asyncFire(eventName, ...args) {
83
+ return this.fire(eventName, ...args)
84
+ }
85
+
82
86
  implementOn(obj, name) {
83
87
  obj[name] = this[name].bind(this);
84
88
  }
package/Registry.js CHANGED
@@ -26,15 +26,65 @@ class Registry {
26
26
  return this.services[name];
27
27
  }
28
28
 
29
- callAll(serviceNames, ...args) {
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
+
79
+ callAll(serviceNames, which, ...args) {
30
80
  var promises = [];
31
81
 
32
82
  serviceNames.forEach(function (name) {
33
83
  var serviceObject = this.services[name];
34
84
  if (!serviceObject) return;
35
85
 
36
- var result = serviceObject.invoke.apply(this, args);
37
- if (result.then) {
86
+ var result = serviceObject.invoke.apply(serviceObject, [which, ...args]);
87
+ if (result && result.then) {
38
88
  promises.push(result);
39
89
  }
40
90
  }, this);
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
- var isFirst = !this.listeners[name] || this.listeners[name].length === 0;
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
- if (this[name]) {
30
- return this[name].apply(this, args)
31
- }
53
+ return this.fire(name, ...args);
54
+ }
32
55
 
33
- return this.fire.apply(this, [name, ...args]);
56
+ async asyncInvoke(name, ...args) {
57
+ return this.invoke(name, ...args)
34
58
  }
35
59
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@polylith/core",
3
3
  "access": "public",
4
- "version": "0.0.4",
4
+ "version": "0.0.8",
5
5
  "description": "Core of the client-side polylith framework",
6
6
  "main": "index.js",
7
7
  "scripts": {