@polylith/core 0.0.10 → 0.1.0
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/App.js +14 -0
- package/EventBus.js +86 -86
- package/Eventable.js +5 -5
- package/Registry.js +32 -32
- package/Service.js +22 -22
- package/ServiceObject.js +56 -56
- package/index.js +3 -1
- package/package.json +1 -1
- package/package-lock.json +0 -31
package/App.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export default class App {
|
|
2
|
+
static loadCss(cssFiles) {
|
|
3
|
+
cssFiles.forEach(function(uri) {
|
|
4
|
+
if (typeof uri !== 'string') return;
|
|
5
|
+
|
|
6
|
+
var link = document.createElement('link');
|
|
7
|
+
link.rel = 'stylesheet';
|
|
8
|
+
link.type = 'text/css';
|
|
9
|
+
link.href = uri;
|
|
10
|
+
|
|
11
|
+
document.getElementsByTagName('HEAD')[0].appendChild(link);
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
}
|
package/EventBus.js
CHANGED
|
@@ -1,89 +1,89 @@
|
|
|
1
1
|
import * as uuid from 'uuid';
|
|
2
2
|
|
|
3
3
|
export class EventBus {
|
|
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
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
4
|
+
constructor (prefix = '') {
|
|
5
|
+
this.listeners = {};
|
|
6
|
+
this.prefix = prefix;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
listen (eventName, cb) {
|
|
10
|
+
var listenerId = uuid.v1();
|
|
11
|
+
var name = this.prefix + eventName;
|
|
12
|
+
|
|
13
|
+
this.listeners[name] = this.listeners[name] || [];
|
|
14
|
+
this.listeners[name].push({cb, listenerId});
|
|
15
|
+
|
|
16
|
+
return listenerId;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
unlisten(eventName, listenerId) {
|
|
20
|
+
var name = this.prefix + eventName;
|
|
21
|
+
var listeners = this.listeners[name] || [];
|
|
22
|
+
|
|
23
|
+
var index = listeners.findIndex(function (listener) {
|
|
24
|
+
return listener.listenerId === listenerId;
|
|
25
|
+
}, this);
|
|
26
|
+
|
|
27
|
+
// if found, remove it from the array
|
|
28
|
+
if (index !== -1) {
|
|
29
|
+
listeners.splice(index, 1);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/*
|
|
34
|
+
This will fire all of the listeners for an event.
|
|
35
|
+
|
|
36
|
+
The first listener to return a value is the result of this method.
|
|
37
|
+
If one or more listeners returns a promise, this method will return a promise that will resolve to the return result.
|
|
38
|
+
*/
|
|
39
|
+
fire(eventName, ...args) {
|
|
40
|
+
var name = this.prefix + eventName;
|
|
41
|
+
var listeners = this.listeners[name];
|
|
42
|
+
var promises = [];
|
|
43
|
+
var firstResult;
|
|
44
|
+
|
|
45
|
+
if (!listeners) return;
|
|
46
|
+
|
|
47
|
+
listeners.forEach(function(listener) {
|
|
48
|
+
var result = listener.cb.apply(this, args);
|
|
49
|
+
var isPromise = result && result.then;
|
|
50
|
+
|
|
51
|
+
firstResult = firstResult !== undefined ? firstResult : (result !== undefined && !isPromise) ? result : undefined;
|
|
52
|
+
if (isPromise) promises.push(result);
|
|
53
|
+
}, this)
|
|
54
|
+
|
|
55
|
+
// if there are no promises, just return the firstResult
|
|
56
|
+
if (promises.length === 0) {
|
|
57
|
+
return firstResult;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// if there are promises, wait until they have all completed, then return the first result, or the value of the first fulfilled promise
|
|
61
|
+
return Promise.allSettled(promises)
|
|
62
|
+
.then(function(results) {
|
|
63
|
+
var promisesResult
|
|
64
|
+
|
|
65
|
+
//report on errors
|
|
66
|
+
results.forEach(function(callResult) {
|
|
67
|
+
if (callResult.status !== 'fulfilled') {
|
|
68
|
+
console.warn(callResult.reason);
|
|
69
|
+
}
|
|
70
|
+
}, this);
|
|
71
|
+
|
|
72
|
+
var found = results.find(function(callResult) {
|
|
73
|
+
return callResult.status === 'fulfilled' && callResult.value !== undefined;
|
|
74
|
+
}, this);
|
|
75
|
+
|
|
76
|
+
promisesResult = found ? found.value : undefined;
|
|
77
|
+
|
|
78
|
+
return firstResult !== undefined ? firstResult : promisesResult;
|
|
79
|
+
}.bind(this));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async asyncFire(eventName, ...args) {
|
|
83
|
+
return await this.fire(eventName, ...args);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
implementOn(obj, name) {
|
|
87
|
+
obj[name] = this[name].bind(this);
|
|
88
|
+
}
|
|
89
|
+
}
|
package/Eventable.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { EventBus } from "./EventBus";
|
|
2
2
|
|
|
3
3
|
export function makeEventable(obj) {
|
|
4
|
-
|
|
4
|
+
obj.eventBus = new EventBus('eventable:');
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
6
|
+
obj.eventBus.implementOn(obj, 'fire');
|
|
7
|
+
obj.eventBus.implementOn(obj, 'listen');
|
|
8
|
+
obj.eventBus.implementOn(obj, 'unlisten');
|
|
9
|
+
}
|
package/Registry.js
CHANGED
|
@@ -2,11 +2,11 @@ import { ServiceOject } from "./ServiceObject";
|
|
|
2
2
|
import { makeEventable} from "./Eventable";
|
|
3
3
|
|
|
4
4
|
class Registry {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
constructor () {
|
|
6
|
+
this.services = {};
|
|
7
7
|
|
|
8
8
|
makeEventable(this);
|
|
9
|
-
|
|
9
|
+
}
|
|
10
10
|
|
|
11
11
|
createServiceObject(name) {
|
|
12
12
|
var result = new ServiceOject(name);
|
|
@@ -14,33 +14,33 @@ class Registry {
|
|
|
14
14
|
return result;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
register(name, serviceObject) {
|
|
18
|
+
this.services[name] = serviceObject;
|
|
19
|
+
}
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
unregister(name) {
|
|
22
|
+
delete this.services[name];
|
|
23
|
+
}
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
subscribe(name) {
|
|
26
|
+
return this.services[name];
|
|
27
|
+
}
|
|
28
28
|
|
|
29
|
-
makeService(serviceName, obj,
|
|
29
|
+
makeService(serviceName, obj, methodList) {
|
|
30
30
|
obj.serviceObject = new ServiceOject(serviceName);
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
obj.serviceObject.implementOn(obj, 'fire');
|
|
33
|
+
obj.serviceObject.implementOn(obj, 'listen');
|
|
34
|
+
obj.serviceObject.implementOn(obj, 'unlisten');
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
if (serviceName) {
|
|
37
|
+
registry.register(serviceName, obj.serviceObject);
|
|
38
|
+
}
|
|
39
39
|
|
|
40
|
-
if (
|
|
40
|
+
if (methodList) {
|
|
41
41
|
var methods = {};
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
methodList.forEach(function(methodName) {
|
|
44
44
|
if (obj[methodName]) {
|
|
45
45
|
methods[methodName] = obj[methodName].bind(obj);
|
|
46
46
|
} else {
|
|
@@ -52,19 +52,19 @@ class Registry {
|
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
extendService(serviceName, obj,
|
|
55
|
+
extendService(serviceName, obj, methodList) {
|
|
56
56
|
var serviceObject = this.subscribe(serviceName);
|
|
57
57
|
|
|
58
58
|
obj.serviceObject = serviceObject || new ServiceOject(serviceName);
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
obj.serviceObject.implementOn(obj, 'fire');
|
|
61
|
+
obj.serviceObject.implementOn(obj, 'listen');
|
|
62
|
+
obj.serviceObject.implementOn(obj, 'unlisten');
|
|
63
63
|
|
|
64
|
-
if (
|
|
64
|
+
if (methodList) {
|
|
65
65
|
var methods = {};
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
methodList.forEach(function(methodName) {
|
|
68
68
|
if (obj[methodName]) {
|
|
69
69
|
methods[methodName] = obj[methodName].bind(obj);
|
|
70
70
|
} else {
|
|
@@ -92,10 +92,10 @@ class Registry {
|
|
|
92
92
|
return promises;
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
|
|
96
|
-
|
|
95
|
+
async start(prefix = '') {
|
|
96
|
+
var names = Object.keys(this.services);
|
|
97
97
|
|
|
98
|
-
|
|
98
|
+
var services = names.filter(function(name) {
|
|
99
99
|
return name.indexOf(prefix) === 0;
|
|
100
100
|
}, this);
|
|
101
101
|
|
|
@@ -104,7 +104,7 @@ class Registry {
|
|
|
104
104
|
.then(function () {
|
|
105
105
|
this.callAll(services, 'ready');
|
|
106
106
|
}.bind(this));
|
|
107
|
-
|
|
107
|
+
}
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
export var registry = new Registry();
|
|
110
|
+
export var registry = new Registry();
|
package/Service.js
CHANGED
|
@@ -2,29 +2,29 @@ import { ServiceOject } from "./ServiceObject";
|
|
|
2
2
|
import { registry } from "./Registry";
|
|
3
3
|
|
|
4
4
|
export class Service {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
this.serviceObject.implementOn(this, 'fire');
|
|
9
|
-
this.serviceObject.implementOn(this, 'listen');
|
|
10
|
-
this.serviceObject.implementOn(this, 'unlisten');
|
|
5
|
+
constructor (name) {
|
|
6
|
+
this.serviceObject = new ServiceOject(name);
|
|
11
7
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
8
|
+
this.serviceObject.implementOn(this, 'fire');
|
|
9
|
+
this.serviceObject.implementOn(this, 'listen');
|
|
10
|
+
this.serviceObject.implementOn(this, 'unlisten');
|
|
16
11
|
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
if (name) {
|
|
13
|
+
registry.register(name, this.serviceObject);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
19
16
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
methods[name] = this[name].bind(this);
|
|
23
|
-
} else {
|
|
24
|
-
console.warn('method', name, 'not implemented on service', this.name ? this.name : '<unnamed service>')
|
|
25
|
-
}
|
|
26
|
-
}, this);
|
|
17
|
+
implement (names) {
|
|
18
|
+
var methods = {};
|
|
27
19
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
20
|
+
names.forEach(function(name) {
|
|
21
|
+
if (this[name]) {
|
|
22
|
+
methods[name] = this[name].bind(this);
|
|
23
|
+
} else {
|
|
24
|
+
console.warn('method', name, 'not implemented on service', this.name ? this.name : '<unnamed service>')
|
|
25
|
+
}
|
|
26
|
+
}, this);
|
|
27
|
+
|
|
28
|
+
this.serviceObject.implement(methods);
|
|
29
|
+
}
|
|
30
|
+
}
|
package/ServiceObject.js
CHANGED
|
@@ -1,59 +1,59 @@
|
|
|
1
1
|
import { EventBus } from './EventBus.js';
|
|
2
2
|
|
|
3
3
|
export class ServiceOject extends EventBus {
|
|
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
|
-
}
|
|
4
|
+
constructor(name) {
|
|
5
|
+
super('service:');
|
|
6
|
+
|
|
7
|
+
this.name = name;
|
|
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)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
implement(methods) {
|
|
40
|
+
var names = Object.keys(methods);
|
|
41
|
+
|
|
42
|
+
names.forEach(function(name) {
|
|
43
|
+
if (methods[name]) {
|
|
44
|
+
this.assignMethod(name, methods[name])
|
|
45
|
+
|
|
46
|
+
// always add this as a listener, in case the method becomes unbound
|
|
47
|
+
this.listen(name, methods[name]);
|
|
48
|
+
}
|
|
49
|
+
}, this);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
invoke(name, ...args) {
|
|
53
|
+
return this.fire(name, ...args);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async asyncInvoke(name, ...args) {
|
|
57
|
+
return await this.asyncFire(name, ...args);
|
|
58
|
+
}
|
|
59
|
+
}
|
package/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { registry } from './Registry';
|
|
2
2
|
import { makeEventable } from './Eventable';
|
|
3
3
|
import { Service } from './Service';
|
|
4
|
+
import App from './App';
|
|
4
5
|
|
|
5
|
-
export {registry as registry, Service as Service, makeEventable as makeEventable
|
|
6
|
+
export {registry as registry, Service as Service, makeEventable as makeEventable,
|
|
7
|
+
App as App};
|
package/package.json
CHANGED
package/package-lock.json
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
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
|
-
}
|