@micro-cms/core 1.0.5 → 1.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/dist/index.d.mts +28 -0
- package/dist/index.d.ts +6 -4
- package/dist/index.js +151 -124
- package/dist/index.mjs +129 -0
- package/package.json +8 -7
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { CmsModule } from '@micro-cms/types';
|
|
2
|
+
|
|
3
|
+
declare class RouteRegistry {
|
|
4
|
+
private routes;
|
|
5
|
+
register(moduleName: string, routes: any[]): void;
|
|
6
|
+
getAllRoutes(): any[];
|
|
7
|
+
}
|
|
8
|
+
declare class App {
|
|
9
|
+
private modules;
|
|
10
|
+
private eventBus;
|
|
11
|
+
private stateManager;
|
|
12
|
+
private routeRegistry;
|
|
13
|
+
private capabilities;
|
|
14
|
+
private configs;
|
|
15
|
+
use(module: CmsModule, config?: Record<string, any>): this;
|
|
16
|
+
get context(): {
|
|
17
|
+
get: (k: string) => any;
|
|
18
|
+
subscribe: (k: string, c: Function) => void;
|
|
19
|
+
};
|
|
20
|
+
get runtime(): {
|
|
21
|
+
getCapability: <T = any>(cap: string) => T | undefined;
|
|
22
|
+
getRoutes: () => any[];
|
|
23
|
+
};
|
|
24
|
+
start(): Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
declare function createApp(): App;
|
|
27
|
+
|
|
28
|
+
export { App, RouteRegistry, createApp };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { CmsModule } from '@micro-cms/types';
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
declare class RouteRegistry {
|
|
3
4
|
private routes;
|
|
4
5
|
register(moduleName: string, routes: any[]): void;
|
|
5
6
|
getAllRoutes(): any[];
|
|
6
7
|
}
|
|
7
|
-
|
|
8
|
+
declare class App {
|
|
8
9
|
private modules;
|
|
9
10
|
private eventBus;
|
|
10
11
|
private stateManager;
|
|
@@ -22,5 +23,6 @@ export declare class App {
|
|
|
22
23
|
};
|
|
23
24
|
start(): Promise<void>;
|
|
24
25
|
}
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
declare function createApp(): App;
|
|
27
|
+
|
|
28
|
+
export { App, RouteRegistry, createApp };
|
package/dist/index.js
CHANGED
|
@@ -1,129 +1,156 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
App: () => App,
|
|
24
|
+
RouteRegistry: () => RouteRegistry,
|
|
25
|
+
createApp: () => createApp
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
var EventBus = class {
|
|
29
|
+
constructor() {
|
|
30
|
+
this.listeners = {};
|
|
31
|
+
}
|
|
32
|
+
subscribe(event, callback, options = {}) {
|
|
33
|
+
const stage = options.stage || "default";
|
|
34
|
+
const priority = options.priority || 0;
|
|
35
|
+
const parallel = options.parallel || false;
|
|
36
|
+
if (!this.listeners[event]) {
|
|
37
|
+
this.listeners[event] = {
|
|
38
|
+
validation: [],
|
|
39
|
+
processing: [],
|
|
40
|
+
notification: [],
|
|
41
|
+
default: []
|
|
42
|
+
};
|
|
4
43
|
}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
44
|
+
this.listeners[event][stage].push({ callback, priority, parallel });
|
|
45
|
+
this.listeners[event][stage].sort((a, b) => b.priority - a.priority);
|
|
46
|
+
}
|
|
47
|
+
async emit(event, payload) {
|
|
48
|
+
if (!this.listeners[event]) return;
|
|
49
|
+
const stages = ["validation", "processing", "notification", "default"];
|
|
50
|
+
for (const stage of stages) {
|
|
51
|
+
const handlers = this.listeners[event][stage];
|
|
52
|
+
const parallelHandlers = handlers.filter((h) => h.parallel);
|
|
53
|
+
const sequentialHandlers = handlers.filter((h) => !h.parallel);
|
|
54
|
+
for (const handler of sequentialHandlers) {
|
|
55
|
+
await handler.callback(payload);
|
|
56
|
+
}
|
|
57
|
+
if (parallelHandlers.length > 0) {
|
|
58
|
+
await Promise.all(parallelHandlers.map((h) => h.callback(payload)));
|
|
59
|
+
}
|
|
19
60
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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 RouteRegistry {
|
|
60
|
-
constructor() {
|
|
61
|
-
this.routes = {};
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
var StateManager = class {
|
|
64
|
+
constructor() {
|
|
65
|
+
this.state = {};
|
|
66
|
+
this.subscribers = {};
|
|
67
|
+
}
|
|
68
|
+
publish(key, value) {
|
|
69
|
+
this.state[key] = value;
|
|
70
|
+
if (this.subscribers[key]) {
|
|
71
|
+
this.subscribers[key].forEach((cb) => cb(value));
|
|
62
72
|
}
|
|
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
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
73
|
+
}
|
|
74
|
+
get(key) {
|
|
75
|
+
return this.state[key];
|
|
76
|
+
}
|
|
77
|
+
subscribe(key, callback) {
|
|
78
|
+
if (!this.subscribers[key]) this.subscribers[key] = [];
|
|
79
|
+
this.subscribers[key].push(callback);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
var RouteRegistry = class {
|
|
83
|
+
constructor() {
|
|
84
|
+
this.routes = {};
|
|
85
|
+
}
|
|
86
|
+
register(moduleName, routes) {
|
|
87
|
+
this.routes[moduleName] = routes;
|
|
88
|
+
}
|
|
89
|
+
getAllRoutes() {
|
|
90
|
+
return Object.values(this.routes).flat();
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
var App = class {
|
|
94
|
+
constructor() {
|
|
95
|
+
this.modules = [];
|
|
96
|
+
this.eventBus = new EventBus();
|
|
97
|
+
this.stateManager = new StateManager();
|
|
98
|
+
this.routeRegistry = new RouteRegistry();
|
|
99
|
+
this.capabilities = {};
|
|
100
|
+
this.configs = {};
|
|
101
|
+
}
|
|
102
|
+
use(module2, config = {}) {
|
|
103
|
+
this.modules.push(module2);
|
|
104
|
+
this.configs[module2.manifest.name] = config;
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
get context() {
|
|
108
|
+
return {
|
|
109
|
+
get: (k) => this.stateManager.get(k),
|
|
110
|
+
subscribe: (k, c) => this.stateManager.subscribe(k, c)
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
get runtime() {
|
|
114
|
+
return {
|
|
115
|
+
getCapability: (cap) => this.capabilities[cap],
|
|
116
|
+
getRoutes: () => this.routeRegistry.getAllRoutes()
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
async start() {
|
|
120
|
+
for (const mod of this.modules) {
|
|
121
|
+
console.log(`Loading module: ${mod.manifest.name} (v${mod.manifest.version})`);
|
|
122
|
+
const context = {
|
|
123
|
+
runtime: {
|
|
124
|
+
register: (cap, impl) => {
|
|
125
|
+
this.capabilities[cap] = impl;
|
|
126
|
+
if (cap === "route-provider" && typeof impl.getRoutes === "function") {
|
|
127
|
+
this.routeRegistry.register(mod.manifest.name, impl.getRoutes());
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
getCapability: (cap) => this.capabilities[cap]
|
|
131
|
+
},
|
|
132
|
+
events: {
|
|
133
|
+
emit: (e, p) => this.eventBus.emit(e, p),
|
|
134
|
+
subscribe: (e, c, o) => this.eventBus.subscribe(e, c, o)
|
|
135
|
+
},
|
|
136
|
+
context: {
|
|
137
|
+
publish: (k, v) => this.stateManager.publish(k, v),
|
|
138
|
+
get: (k) => this.stateManager.get(k),
|
|
139
|
+
subscribe: (k, c) => this.stateManager.subscribe(k, c)
|
|
140
|
+
},
|
|
141
|
+
config: this.configs[mod.manifest.name] || {}
|
|
142
|
+
};
|
|
143
|
+
await mod.load(context);
|
|
124
144
|
}
|
|
145
|
+
console.log("App started with all modules loaded.");
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
function createApp() {
|
|
149
|
+
return new App();
|
|
125
150
|
}
|
|
126
|
-
export
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
151
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
152
|
+
0 && (module.exports = {
|
|
153
|
+
App,
|
|
154
|
+
RouteRegistry,
|
|
155
|
+
createApp
|
|
156
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var EventBus = class {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.listeners = {};
|
|
5
|
+
}
|
|
6
|
+
subscribe(event, callback, options = {}) {
|
|
7
|
+
const stage = options.stage || "default";
|
|
8
|
+
const priority = options.priority || 0;
|
|
9
|
+
const parallel = options.parallel || false;
|
|
10
|
+
if (!this.listeners[event]) {
|
|
11
|
+
this.listeners[event] = {
|
|
12
|
+
validation: [],
|
|
13
|
+
processing: [],
|
|
14
|
+
notification: [],
|
|
15
|
+
default: []
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
this.listeners[event][stage].push({ callback, priority, parallel });
|
|
19
|
+
this.listeners[event][stage].sort((a, b) => b.priority - a.priority);
|
|
20
|
+
}
|
|
21
|
+
async emit(event, payload) {
|
|
22
|
+
if (!this.listeners[event]) 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
|
+
for (const handler of sequentialHandlers) {
|
|
29
|
+
await handler.callback(payload);
|
|
30
|
+
}
|
|
31
|
+
if (parallelHandlers.length > 0) {
|
|
32
|
+
await Promise.all(parallelHandlers.map((h) => h.callback(payload)));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var StateManager = class {
|
|
38
|
+
constructor() {
|
|
39
|
+
this.state = {};
|
|
40
|
+
this.subscribers = {};
|
|
41
|
+
}
|
|
42
|
+
publish(key, value) {
|
|
43
|
+
this.state[key] = value;
|
|
44
|
+
if (this.subscribers[key]) {
|
|
45
|
+
this.subscribers[key].forEach((cb) => cb(value));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
get(key) {
|
|
49
|
+
return this.state[key];
|
|
50
|
+
}
|
|
51
|
+
subscribe(key, callback) {
|
|
52
|
+
if (!this.subscribers[key]) this.subscribers[key] = [];
|
|
53
|
+
this.subscribers[key].push(callback);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
var RouteRegistry = class {
|
|
57
|
+
constructor() {
|
|
58
|
+
this.routes = {};
|
|
59
|
+
}
|
|
60
|
+
register(moduleName, routes) {
|
|
61
|
+
this.routes[moduleName] = routes;
|
|
62
|
+
}
|
|
63
|
+
getAllRoutes() {
|
|
64
|
+
return Object.values(this.routes).flat();
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
var App = class {
|
|
68
|
+
constructor() {
|
|
69
|
+
this.modules = [];
|
|
70
|
+
this.eventBus = new EventBus();
|
|
71
|
+
this.stateManager = new StateManager();
|
|
72
|
+
this.routeRegistry = new RouteRegistry();
|
|
73
|
+
this.capabilities = {};
|
|
74
|
+
this.configs = {};
|
|
75
|
+
}
|
|
76
|
+
use(module, config = {}) {
|
|
77
|
+
this.modules.push(module);
|
|
78
|
+
this.configs[module.manifest.name] = config;
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
get context() {
|
|
82
|
+
return {
|
|
83
|
+
get: (k) => this.stateManager.get(k),
|
|
84
|
+
subscribe: (k, c) => this.stateManager.subscribe(k, c)
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
get runtime() {
|
|
88
|
+
return {
|
|
89
|
+
getCapability: (cap) => this.capabilities[cap],
|
|
90
|
+
getRoutes: () => this.routeRegistry.getAllRoutes()
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
async start() {
|
|
94
|
+
for (const mod of this.modules) {
|
|
95
|
+
console.log(`Loading module: ${mod.manifest.name} (v${mod.manifest.version})`);
|
|
96
|
+
const context = {
|
|
97
|
+
runtime: {
|
|
98
|
+
register: (cap, impl) => {
|
|
99
|
+
this.capabilities[cap] = impl;
|
|
100
|
+
if (cap === "route-provider" && typeof impl.getRoutes === "function") {
|
|
101
|
+
this.routeRegistry.register(mod.manifest.name, impl.getRoutes());
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
getCapability: (cap) => this.capabilities[cap]
|
|
105
|
+
},
|
|
106
|
+
events: {
|
|
107
|
+
emit: (e, p) => this.eventBus.emit(e, p),
|
|
108
|
+
subscribe: (e, c, o) => this.eventBus.subscribe(e, c, o)
|
|
109
|
+
},
|
|
110
|
+
context: {
|
|
111
|
+
publish: (k, v) => this.stateManager.publish(k, v),
|
|
112
|
+
get: (k) => this.stateManager.get(k),
|
|
113
|
+
subscribe: (k, c) => this.stateManager.subscribe(k, c)
|
|
114
|
+
},
|
|
115
|
+
config: this.configs[mod.manifest.name] || {}
|
|
116
|
+
};
|
|
117
|
+
await mod.load(context);
|
|
118
|
+
}
|
|
119
|
+
console.log("App started with all modules loaded.");
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
function createApp() {
|
|
123
|
+
return new App();
|
|
124
|
+
}
|
|
125
|
+
export {
|
|
126
|
+
App,
|
|
127
|
+
RouteRegistry,
|
|
128
|
+
createApp
|
|
129
|
+
};
|
package/package.json
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@micro-cms/core",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"type": "module",
|
|
3
|
+
"version": "1.0.8",
|
|
5
4
|
"main": "dist/index.js",
|
|
6
|
-
"module": "dist/index.
|
|
5
|
+
"module": "dist/index.mjs",
|
|
7
6
|
"types": "dist/index.d.ts",
|
|
8
7
|
"files": [
|
|
9
8
|
"dist"
|
|
@@ -11,18 +10,20 @@
|
|
|
11
10
|
"exports": {
|
|
12
11
|
".": {
|
|
13
12
|
"types": "./dist/index.d.ts",
|
|
14
|
-
"import": "./dist/index.
|
|
13
|
+
"import": "./dist/index.mjs",
|
|
14
|
+
"require": "./dist/index.js",
|
|
15
15
|
"default": "./dist/index.js"
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@micro-cms/types": "1.0.
|
|
19
|
+
"@micro-cms/types": "^1.0.8"
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
22
|
-
"build": "
|
|
22
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
23
23
|
"prepare": "pnpm build"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"typescript": "^5.0.0"
|
|
26
|
+
"typescript": "^5.0.0",
|
|
27
|
+
"tsup": "^8.0.2"
|
|
27
28
|
}
|
|
28
29
|
}
|