@polylith/core 0.0.6 → 0.0.10

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,6 +26,56 @@ class Registry {
26
26
  return this.services[name];
27
27
  }
28
28
 
29
+ makeService(serviceName, obj, methodsSpec) {
30
+ obj.serviceObject = new ServiceOject(serviceName);
31
+
32
+ obj.serviceObject.implementOn(obj, 'fire');
33
+ obj.serviceObject.implementOn(obj, 'listen');
34
+ obj.serviceObject.implementOn(obj, 'unlisten');
35
+
36
+ if (serviceName) {
37
+ registry.register(serviceName, obj.serviceObject);
38
+ }
39
+
40
+ if (methodsSpec) {
41
+ var methods = {};
42
+
43
+ methodsSpec.forEach(function(methodName) {
44
+ if (obj[methodName]) {
45
+ methods[methodName] = obj[methodName].bind(obj);
46
+ } else {
47
+ console.warn('method', methodName, 'not implemented on service', serviceName ? serviceName : '<unnamed service>')
48
+ }
49
+ });
50
+
51
+ obj.serviceObject.implement(methods);
52
+ }
53
+ }
54
+
55
+ extendService(serviceName, obj, methodsSpec) {
56
+ var serviceObject = this.subscribe(serviceName);
57
+
58
+ obj.serviceObject = serviceObject || new ServiceOject(serviceName);
59
+
60
+ obj.serviceObject.implementOn(obj, 'fire');
61
+ obj.serviceObject.implementOn(obj, 'listen');
62
+ obj.serviceObject.implementOn(obj, 'unlisten');
63
+
64
+ if (methodsSpec) {
65
+ var methods = {};
66
+
67
+ methodsSpec.forEach(function(methodName) {
68
+ if (obj[methodName]) {
69
+ methods[methodName] = obj[methodName].bind(obj);
70
+ } else {
71
+ console.warn('method', methodName, 'not implemented on service', serviceName ? serviceName : '<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
 
@@ -34,7 +84,7 @@ class Registry {
34
84
  if (!serviceObject) return;
35
85
 
36
86
  var result = serviceObject.invoke.apply(serviceObject, [which, ...args]);
37
- if (result.then) {
87
+ if (result && result.then) {
38
88
  promises.push(result);
39
89
  }
40
90
  }, this);
package/Service.js CHANGED
@@ -21,7 +21,7 @@ export class Service {
21
21
  if (this[name]) {
22
22
  methods[name] = this[name].bind(this);
23
23
  } else {
24
- console.warn('method', name, 'not implemented on service', this.name ? name : '<unnamed service>')
24
+ console.warn('method', name, 'not implemented on service', this.name ? this.name : '<unnamed service>')
25
25
  }
26
26
  }, this);
27
27
 
package/ServiceObject.js CHANGED
@@ -6,30 +6,54 @@ 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];
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[name]) {
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) {
12
- var keys = Object.keys(methods);
40
+ var names = Object.keys(methods);
13
41
 
14
- keys.forEach(function(name) {
42
+ names.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 await this.asyncFire(name, ...args);
34
58
  }
35
59
  }
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@polylith/core",
3
+ "version": "0.0.1",
4
+ "lockfileVersion": 2,
5
+ "requires": true,
6
+ "packages": {
7
+ "": {
8
+ "name": "@polylith/core",
9
+ "version": "0.0.1",
10
+ "license": "MIT",
11
+ "dependencies": {
12
+ "uuid": "^8.3.2"
13
+ }
14
+ },
15
+ "node_modules/uuid": {
16
+ "version": "8.3.2",
17
+ "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
18
+ "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
19
+ "bin": {
20
+ "uuid": "dist/bin/uuid"
21
+ }
22
+ }
23
+ },
24
+ "dependencies": {
25
+ "uuid": {
26
+ "version": "8.3.2",
27
+ "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
28
+ "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg=="
29
+ }
30
+ }
31
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@polylith/core",
3
3
  "access": "public",
4
- "version": "0.0.6",
4
+ "version": "0.0.10",
5
5
  "description": "Core of the client-side polylith framework",
6
6
  "main": "index.js",
7
7
  "scripts": {