@micro-cms/core 0.0.1
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/dist/index.d.ts +18 -0
- package/dist/index.js +109 -0
- package/package.json +26 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { CmsModule } from '@micro-cms/types';
|
|
2
|
+
export declare class App {
|
|
3
|
+
private modules;
|
|
4
|
+
private eventBus;
|
|
5
|
+
private stateManager;
|
|
6
|
+
private capabilities;
|
|
7
|
+
private configs;
|
|
8
|
+
use(module: CmsModule, config?: Record<string, any>): this;
|
|
9
|
+
get context(): {
|
|
10
|
+
get: (k: string) => any;
|
|
11
|
+
subscribe: (k: string, c: Function) => void;
|
|
12
|
+
};
|
|
13
|
+
get runtime(): {
|
|
14
|
+
getCapability: <T = any>(cap: string) => T | undefined;
|
|
15
|
+
};
|
|
16
|
+
start(): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
export declare function createApp(): App;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
class EventBus {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.listeners = {};
|
|
4
|
+
}
|
|
5
|
+
subscribe(event, callback, options = {}) {
|
|
6
|
+
const stage = options.stage || 'default';
|
|
7
|
+
const priority = options.priority || 0;
|
|
8
|
+
const parallel = options.parallel || false;
|
|
9
|
+
if (!this.listeners[event]) {
|
|
10
|
+
this.listeners[event] = {
|
|
11
|
+
validation: [],
|
|
12
|
+
processing: [],
|
|
13
|
+
notification: [],
|
|
14
|
+
default: []
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
this.listeners[event][stage].push({ callback, priority, parallel });
|
|
18
|
+
this.listeners[event][stage].sort((a, b) => b.priority - a.priority);
|
|
19
|
+
}
|
|
20
|
+
async emit(event, payload) {
|
|
21
|
+
if (!this.listeners[event])
|
|
22
|
+
return;
|
|
23
|
+
const stages = ['validation', 'processing', 'notification', 'default'];
|
|
24
|
+
for (const stage of stages) {
|
|
25
|
+
const handlers = this.listeners[event][stage];
|
|
26
|
+
const parallelHandlers = handlers.filter(h => h.parallel);
|
|
27
|
+
const sequentialHandlers = handlers.filter(h => !h.parallel);
|
|
28
|
+
// Run sequential first (or in order of priority)
|
|
29
|
+
for (const handler of sequentialHandlers) {
|
|
30
|
+
await handler.callback(payload);
|
|
31
|
+
}
|
|
32
|
+
// Run parallel
|
|
33
|
+
if (parallelHandlers.length > 0) {
|
|
34
|
+
await Promise.all(parallelHandlers.map(h => h.callback(payload)));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
class StateManager {
|
|
40
|
+
constructor() {
|
|
41
|
+
this.state = {};
|
|
42
|
+
this.subscribers = {};
|
|
43
|
+
}
|
|
44
|
+
publish(key, value) {
|
|
45
|
+
this.state[key] = value;
|
|
46
|
+
if (this.subscribers[key]) {
|
|
47
|
+
this.subscribers[key].forEach(cb => cb(value));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
get(key) {
|
|
51
|
+
return this.state[key];
|
|
52
|
+
}
|
|
53
|
+
subscribe(key, callback) {
|
|
54
|
+
if (!this.subscribers[key])
|
|
55
|
+
this.subscribers[key] = [];
|
|
56
|
+
this.subscribers[key].push(callback);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export class App {
|
|
60
|
+
constructor() {
|
|
61
|
+
this.modules = [];
|
|
62
|
+
this.eventBus = new EventBus();
|
|
63
|
+
this.stateManager = new StateManager();
|
|
64
|
+
this.capabilities = {};
|
|
65
|
+
this.configs = {};
|
|
66
|
+
}
|
|
67
|
+
use(module, config = {}) {
|
|
68
|
+
this.modules.push(module);
|
|
69
|
+
this.configs[module.manifest.name] = config;
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
get context() {
|
|
73
|
+
return {
|
|
74
|
+
get: (k) => this.stateManager.get(k),
|
|
75
|
+
subscribe: (k, c) => this.stateManager.subscribe(k, c)
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
get runtime() {
|
|
79
|
+
return {
|
|
80
|
+
getCapability: (cap) => this.capabilities[cap]
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
async start() {
|
|
84
|
+
for (const mod of this.modules) {
|
|
85
|
+
console.log(`Loading module: ${mod.manifest.name} (v${mod.manifest.version})`);
|
|
86
|
+
const context = {
|
|
87
|
+
runtime: {
|
|
88
|
+
register: (cap, impl) => { this.capabilities[cap] = impl; },
|
|
89
|
+
getCapability: (cap) => this.capabilities[cap]
|
|
90
|
+
},
|
|
91
|
+
events: {
|
|
92
|
+
emit: (e, p) => this.eventBus.emit(e, p),
|
|
93
|
+
subscribe: (e, c, o) => this.eventBus.subscribe(e, c, o)
|
|
94
|
+
},
|
|
95
|
+
context: {
|
|
96
|
+
publish: (k, v) => this.stateManager.publish(k, v),
|
|
97
|
+
get: (k) => this.stateManager.get(k),
|
|
98
|
+
subscribe: (k, c) => this.stateManager.subscribe(k, c)
|
|
99
|
+
},
|
|
100
|
+
config: this.configs[mod.manifest.name] || {}
|
|
101
|
+
};
|
|
102
|
+
await mod.load(context);
|
|
103
|
+
}
|
|
104
|
+
console.log('App started with all modules loaded.');
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
export function createApp() {
|
|
108
|
+
return new App();
|
|
109
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@micro-cms/core",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": ["dist"],
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@micro-cms/types": "workspace:*"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"devDependencies": {},
|
|
21
|
+
"typescript": "^5.0.0",
|
|
22
|
+
|
|
23
|
+
"build": "tsc",
|
|
24
|
+
"prepare": "pnpm build"
|
|
25
|
+
}
|
|
26
|
+
}
|