tsdkarc 1.1.1 → 1.1.3
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/README.md +9 -3
- package/esm/index.d.ts +18 -0
- package/esm/index.js +36 -7
- package/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/index.d.ts +18 -0
- package/lib/index.js +36 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
[](https://www.npmjs.com/package/tsdkarc) [](https://bundlejs.com/?q=tsdkarc&treeshake=%5B%
|
|
11
|
+
[](https://www.npmjs.com/package/tsdkarc) [](https://bundlejs.com/?q=tsdkarc&treeshake=%5B%7Bdefault,defineModule%7D%5D&config=%7B%22esbuild%22:%7B%22external%22:%5B%22react%22,%22react-dom%22,%22react/jsx-runtime%22%5D%7D%7D)  [](https://github.com/suhaotian/tsdkarc/pulls) [](https://github.com/suhaotian/tsdkarc/blob/main/LICENSE) [](https://www.jsdocs.io/package/tsdkarc) 
|
|
12
12
|
|
|
13
13
|
## Why `TsdkArc`
|
|
14
14
|
|
|
@@ -24,7 +24,7 @@ npm install tsdkarc
|
|
|
24
24
|
```
|
|
25
25
|
|
|
26
26
|
```ts
|
|
27
|
-
import start, { defineModule } from "tsdkarc";
|
|
27
|
+
import start, { defineModule, InferContextBy } from "tsdkarc";
|
|
28
28
|
|
|
29
29
|
interface ConfigSlice {
|
|
30
30
|
config: { port: number; env: string };
|
|
@@ -50,6 +50,9 @@ const configModule = defineModule<ConfigSlice>()({
|
|
|
50
50
|
},
|
|
51
51
|
});
|
|
52
52
|
|
|
53
|
+
// get type from module
|
|
54
|
+
type ConfigModuleCtx = InferContextBy<typeof configModule> // same as `ConfigSlice`
|
|
55
|
+
|
|
53
56
|
// Run
|
|
54
57
|
(async () => {
|
|
55
58
|
const app = await start([serverModule], {
|
|
@@ -66,6 +69,9 @@ const configModule = defineModule<ConfigSlice>()({
|
|
|
66
69
|
})();
|
|
67
70
|
```
|
|
68
71
|
|
|
72
|
+
- Online Playground: [Do some practice here](https://stackblitz.com/edit/vitejs-vite-kdennssf?file=src%2Ftsdkarc-demo.ts&terminal=dev)
|
|
73
|
+
- Website & Documentation: https://arc.tsdk.dev/
|
|
74
|
+
|
|
69
75
|
---
|
|
70
76
|
|
|
71
77
|
## Core Concepts
|
|
@@ -217,7 +223,7 @@ beforeBoot → boot → afterBoot → [running] → beforeShutdown → shutdown
|
|
|
217
223
|
| Hook | Purpose |
|
|
218
224
|
| -------------------- | ------------------------------------------------------------------------------ |
|
|
219
225
|
| `beforeBoot` | Pre-setup, Called once before the first module begins booting. |
|
|
220
|
-
| `boot` | Register values on `ctx` via return ctx or call `ctx.set()`
|
|
226
|
+
| `boot` | Register values on `ctx` via return ctx or call `ctx.set()` |
|
|
221
227
|
| `afterBoot` | Called once after the last module has finished booting. |
|
|
222
228
|
| `beforeShutdown` | Called once before the first module begins shutting down. |
|
|
223
229
|
| `shutdown` | Release resources |
|
package/esm/index.d.ts
CHANGED
|
@@ -101,6 +101,24 @@ export interface Module<S extends object, Sl extends object = S> {
|
|
|
101
101
|
/** Called immediately after this module's own shutdown(). */
|
|
102
102
|
afterShutdown?(ctx: ContextWriter<S, Sl>): Promise<void> | void;
|
|
103
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
|
|
106
|
+
type AContext = {
|
|
107
|
+
value: string;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export const moduleA = defineModule<AContext>()({
|
|
111
|
+
name: "A",
|
|
112
|
+
modules: [] as const,
|
|
113
|
+
boot: () => ({ value: "module:A" }),
|
|
114
|
+
async shutdown(ctx) {
|
|
115
|
+
console.log(`shutdown ${ctx.value}`);
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
// Get context type by module
|
|
119
|
+
type AContext2 = InferContextBy<typeof moduleA> // same as above `AContext`
|
|
120
|
+
*/
|
|
121
|
+
export type InferContextBy<M> = M extends Module<any, infer Ctx> ? Ctx : never;
|
|
104
122
|
/**
|
|
105
123
|
* Define a module with full context inferred from its dependency tuple.
|
|
106
124
|
*
|
package/esm/index.js
CHANGED
|
@@ -88,13 +88,26 @@ export default function start(roots_1) {
|
|
|
88
88
|
return;
|
|
89
89
|
stopped = true;
|
|
90
90
|
yield (beforeShutdown === null || beforeShutdown === void 0 ? void 0 : beforeShutdown(writer));
|
|
91
|
-
|
|
91
|
+
const reverseBoot = [...booted].reverse();
|
|
92
|
+
for (const mod of reverseBoot) {
|
|
92
93
|
try {
|
|
93
|
-
yield (beforeEachShutdown === null || beforeEachShutdown === void 0 ? void 0 : beforeEachShutdown(writer, mod));
|
|
94
94
|
yield ((_a = mod.beforeShutdown) === null || _a === void 0 ? void 0 : _a.call(mod, modWriter));
|
|
95
|
+
}
|
|
96
|
+
catch (err) {
|
|
97
|
+
const error = new Error(`Module "${mod.name}" beforeShutdown failed: ${errorMessage(err)}`);
|
|
98
|
+
if (onError) {
|
|
99
|
+
yield (onError === null || onError === void 0 ? void 0 : onError(error, writer, mod));
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
for (const mod of reverseBoot) {
|
|
107
|
+
try {
|
|
108
|
+
yield (beforeEachShutdown === null || beforeEachShutdown === void 0 ? void 0 : beforeEachShutdown(writer, mod));
|
|
95
109
|
yield ((_b = mod.shutdown) === null || _b === void 0 ? void 0 : _b.call(mod, modWriter));
|
|
96
110
|
yield (afterEachShutdown === null || afterEachShutdown === void 0 ? void 0 : afterEachShutdown(writer, mod));
|
|
97
|
-
yield ((_c = mod.afterShutdown) === null || _c === void 0 ? void 0 : _c.call(mod, modWriter));
|
|
98
111
|
}
|
|
99
112
|
catch (err) {
|
|
100
113
|
const error = new Error(`Module "${mod.name}" stop failed: ${errorMessage(err)}`);
|
|
@@ -106,6 +119,20 @@ export default function start(roots_1) {
|
|
|
106
119
|
}
|
|
107
120
|
}
|
|
108
121
|
}
|
|
122
|
+
for (const mod of reverseBoot) {
|
|
123
|
+
try {
|
|
124
|
+
yield ((_c = mod.afterShutdown) === null || _c === void 0 ? void 0 : _c.call(mod, modWriter));
|
|
125
|
+
}
|
|
126
|
+
catch (err) {
|
|
127
|
+
const error = new Error(`Module "${mod.name}" afterShutdown failed: ${errorMessage(err)}`);
|
|
128
|
+
if (onError) {
|
|
129
|
+
yield (onError === null || onError === void 0 ? void 0 : onError(error, writer, mod));
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
throw error;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
109
136
|
yield (afterShutdown === null || afterShutdown === void 0 ? void 0 : afterShutdown(writer));
|
|
110
137
|
});
|
|
111
138
|
}
|
|
@@ -218,11 +245,13 @@ export function rollback(booted, modWriter) {
|
|
|
218
245
|
*/
|
|
219
246
|
function makeWriter(ctx) {
|
|
220
247
|
return Object.assign(ctx, {
|
|
221
|
-
set(
|
|
222
|
-
if (typeof
|
|
223
|
-
return Object.assign(ctx,
|
|
248
|
+
set(keyOrCtx, value) {
|
|
249
|
+
if (typeof keyOrCtx === "object") {
|
|
250
|
+
return Object.assign(ctx, Object.assign(Object.assign({}, keyOrCtx), { set: ctx.set }));
|
|
251
|
+
}
|
|
252
|
+
if (keyOrCtx !== "set") {
|
|
253
|
+
ctx[keyOrCtx] = value;
|
|
224
254
|
}
|
|
225
|
-
ctx[key] = value;
|
|
226
255
|
return ctx;
|
|
227
256
|
},
|
|
228
257
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"fileNames":["../node_modules/typescript/lib/lib.es5.d.ts","../node_modules/typescript/lib/lib.es2015.d.ts","../node_modules/typescript/lib/lib.dom.d.ts","../node_modules/typescript/lib/lib.es2015.core.d.ts","../node_modules/typescript/lib/lib.es2015.collection.d.ts","../node_modules/typescript/lib/lib.es2015.generator.d.ts","../node_modules/typescript/lib/lib.es2015.iterable.d.ts","../node_modules/typescript/lib/lib.es2015.promise.d.ts","../node_modules/typescript/lib/lib.es2015.proxy.d.ts","../node_modules/typescript/lib/lib.es2015.reflect.d.ts","../node_modules/typescript/lib/lib.es2015.symbol.d.ts","../node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../node_modules/typescript/lib/lib.decorators.d.ts","../node_modules/typescript/lib/lib.decorators.legacy.d.ts","../src/index.ts","../node_modules/@types/deep-eql/index.d.ts","../node_modules/assertion-error/index.d.ts","../node_modules/@types/chai/index.d.ts","../node_modules/@types/estree/index.d.ts"],"fileIdsList":[[16,17]],"fileInfos":[{"version":"c430d44666289dae81f30fa7b2edebf186ecc91a2d4c71266ea6ae76388792e1","affectsGlobalScope":true,"impliedFormat":1},{"version":"45b7ab580deca34ae9729e97c13cfd999df04416a79116c3bfb483804f85ded4","impliedFormat":1},{"version":"080941d9f9ff9307f7e27a83bcd888b7c8270716c39af943532438932ec1d0b9","affectsGlobalScope":true,"impliedFormat":1},{"version":"c57796738e7f83dbc4b8e65132f11a377649c00dd3eee333f672b8f0a6bea671","affectsGlobalScope":true,"impliedFormat":1},{"version":"dc2df20b1bcdc8c2d34af4926e2c3ab15ffe1160a63e58b7e09833f616efff44","affectsGlobalScope":true,"impliedFormat":1},{"version":"515d0b7b9bea2e31ea4ec968e9edd2c39d3eebf4a2d5cbd04e88639819ae3b71","affectsGlobalScope":true,"impliedFormat":1},{"version":"0559b1f683ac7505ae451f9a96ce4c3c92bdc71411651ca6ddb0e88baaaad6a3","affectsGlobalScope":true,"impliedFormat":1},{"version":"0dc1e7ceda9b8b9b455c3a2d67b0412feab00bd2f66656cd8850e8831b08b537","affectsGlobalScope":true,"impliedFormat":1},{"version":"ce691fb9e5c64efb9547083e4a34091bcbe5bdb41027e310ebba8f7d96a98671","affectsGlobalScope":true,"impliedFormat":1},{"version":"8d697a2a929a5fcb38b7a65594020fcef05ec1630804a33748829c5ff53640d0","affectsGlobalScope":true,"impliedFormat":1},{"version":"4ff2a353abf8a80ee399af572debb8faab2d33ad38c4b4474cff7f26e7653b8d","affectsGlobalScope":true,"impliedFormat":1},{"version":"fb0f136d372979348d59b3f5020b4cdb81b5504192b1cacff5d1fbba29378aa1","affectsGlobalScope":true,"impliedFormat":1},{"version":"8e7f8264d0fb4c5339605a15daadb037bf238c10b654bb3eee14208f860a32ea","affectsGlobalScope":true,"impliedFormat":1},{"version":"782dec38049b92d4e85c1585fbea5474a219c6984a35b004963b00beb1aab538","affectsGlobalScope":true,"impliedFormat":1},{"version":"
|
|
1
|
+
{"fileNames":["../node_modules/typescript/lib/lib.es5.d.ts","../node_modules/typescript/lib/lib.es2015.d.ts","../node_modules/typescript/lib/lib.dom.d.ts","../node_modules/typescript/lib/lib.es2015.core.d.ts","../node_modules/typescript/lib/lib.es2015.collection.d.ts","../node_modules/typescript/lib/lib.es2015.generator.d.ts","../node_modules/typescript/lib/lib.es2015.iterable.d.ts","../node_modules/typescript/lib/lib.es2015.promise.d.ts","../node_modules/typescript/lib/lib.es2015.proxy.d.ts","../node_modules/typescript/lib/lib.es2015.reflect.d.ts","../node_modules/typescript/lib/lib.es2015.symbol.d.ts","../node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../node_modules/typescript/lib/lib.decorators.d.ts","../node_modules/typescript/lib/lib.decorators.legacy.d.ts","../src/index.ts","../node_modules/@types/deep-eql/index.d.ts","../node_modules/assertion-error/index.d.ts","../node_modules/@types/chai/index.d.ts","../node_modules/@types/estree/index.d.ts"],"fileIdsList":[[16,17]],"fileInfos":[{"version":"c430d44666289dae81f30fa7b2edebf186ecc91a2d4c71266ea6ae76388792e1","affectsGlobalScope":true,"impliedFormat":1},{"version":"45b7ab580deca34ae9729e97c13cfd999df04416a79116c3bfb483804f85ded4","impliedFormat":1},{"version":"080941d9f9ff9307f7e27a83bcd888b7c8270716c39af943532438932ec1d0b9","affectsGlobalScope":true,"impliedFormat":1},{"version":"c57796738e7f83dbc4b8e65132f11a377649c00dd3eee333f672b8f0a6bea671","affectsGlobalScope":true,"impliedFormat":1},{"version":"dc2df20b1bcdc8c2d34af4926e2c3ab15ffe1160a63e58b7e09833f616efff44","affectsGlobalScope":true,"impliedFormat":1},{"version":"515d0b7b9bea2e31ea4ec968e9edd2c39d3eebf4a2d5cbd04e88639819ae3b71","affectsGlobalScope":true,"impliedFormat":1},{"version":"0559b1f683ac7505ae451f9a96ce4c3c92bdc71411651ca6ddb0e88baaaad6a3","affectsGlobalScope":true,"impliedFormat":1},{"version":"0dc1e7ceda9b8b9b455c3a2d67b0412feab00bd2f66656cd8850e8831b08b537","affectsGlobalScope":true,"impliedFormat":1},{"version":"ce691fb9e5c64efb9547083e4a34091bcbe5bdb41027e310ebba8f7d96a98671","affectsGlobalScope":true,"impliedFormat":1},{"version":"8d697a2a929a5fcb38b7a65594020fcef05ec1630804a33748829c5ff53640d0","affectsGlobalScope":true,"impliedFormat":1},{"version":"4ff2a353abf8a80ee399af572debb8faab2d33ad38c4b4474cff7f26e7653b8d","affectsGlobalScope":true,"impliedFormat":1},{"version":"fb0f136d372979348d59b3f5020b4cdb81b5504192b1cacff5d1fbba29378aa1","affectsGlobalScope":true,"impliedFormat":1},{"version":"8e7f8264d0fb4c5339605a15daadb037bf238c10b654bb3eee14208f860a32ea","affectsGlobalScope":true,"impliedFormat":1},{"version":"782dec38049b92d4e85c1585fbea5474a219c6984a35b004963b00beb1aab538","affectsGlobalScope":true,"impliedFormat":1},{"version":"b13291efa2b01b99a49e027dbb0e06e25c1c5b512fe3ef1785a0cbe5c6c0fee8","signature":"2748ed81f3b55d07195cfe0deb4eea0f63acb66947dcdfe3414122013d520986"},{"version":"427fe2004642504828c1476d0af4270e6ad4db6de78c0b5da3e4c5ca95052a99","impliedFormat":1},{"version":"2eeffcee5c1661ddca53353929558037b8cf305ffb86a803512982f99bcab50d","impliedFormat":99},{"version":"9afb4cb864d297e4092a79ee2871b5d3143ea14153f62ef0bb04ede25f432030","affectsGlobalScope":true,"impliedFormat":99},{"version":"151ff381ef9ff8da2da9b9663ebf657eac35c4c9a19183420c05728f31a6761d","impliedFormat":1}],"root":[15],"options":{"declaration":true,"downlevelIteration":true,"emitDecoratorMetadata":true,"esModuleInterop":true,"experimentalDecorators":true,"module":5,"noImplicitAny":true,"outDir":"./","skipLibCheck":true,"strict":true,"strictPropertyInitialization":false,"target":2},"referencedMap":[[18,1]],"version":"5.9.3"}
|
package/lib/index.d.ts
CHANGED
|
@@ -101,6 +101,24 @@ export interface Module<S extends object, Sl extends object = S> {
|
|
|
101
101
|
/** Called immediately after this module's own shutdown(). */
|
|
102
102
|
afterShutdown?(ctx: ContextWriter<S, Sl>): Promise<void> | void;
|
|
103
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
|
|
106
|
+
type AContext = {
|
|
107
|
+
value: string;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export const moduleA = defineModule<AContext>()({
|
|
111
|
+
name: "A",
|
|
112
|
+
modules: [] as const,
|
|
113
|
+
boot: () => ({ value: "module:A" }),
|
|
114
|
+
async shutdown(ctx) {
|
|
115
|
+
console.log(`shutdown ${ctx.value}`);
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
// Get context type by module
|
|
119
|
+
type AContext2 = InferContextBy<typeof moduleA> // same as above `AContext`
|
|
120
|
+
*/
|
|
121
|
+
export type InferContextBy<M> = M extends Module<any, infer Ctx> ? Ctx : never;
|
|
104
122
|
/**
|
|
105
123
|
* Define a module with full context inferred from its dependency tuple.
|
|
106
124
|
*
|
package/lib/index.js
CHANGED
|
@@ -95,13 +95,26 @@ function start(roots_1) {
|
|
|
95
95
|
return;
|
|
96
96
|
stopped = true;
|
|
97
97
|
yield (beforeShutdown === null || beforeShutdown === void 0 ? void 0 : beforeShutdown(writer));
|
|
98
|
-
|
|
98
|
+
const reverseBoot = [...booted].reverse();
|
|
99
|
+
for (const mod of reverseBoot) {
|
|
99
100
|
try {
|
|
100
|
-
yield (beforeEachShutdown === null || beforeEachShutdown === void 0 ? void 0 : beforeEachShutdown(writer, mod));
|
|
101
101
|
yield ((_a = mod.beforeShutdown) === null || _a === void 0 ? void 0 : _a.call(mod, modWriter));
|
|
102
|
+
}
|
|
103
|
+
catch (err) {
|
|
104
|
+
const error = new Error(`Module "${mod.name}" beforeShutdown failed: ${errorMessage(err)}`);
|
|
105
|
+
if (onError) {
|
|
106
|
+
yield (onError === null || onError === void 0 ? void 0 : onError(error, writer, mod));
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
throw error;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
for (const mod of reverseBoot) {
|
|
114
|
+
try {
|
|
115
|
+
yield (beforeEachShutdown === null || beforeEachShutdown === void 0 ? void 0 : beforeEachShutdown(writer, mod));
|
|
102
116
|
yield ((_b = mod.shutdown) === null || _b === void 0 ? void 0 : _b.call(mod, modWriter));
|
|
103
117
|
yield (afterEachShutdown === null || afterEachShutdown === void 0 ? void 0 : afterEachShutdown(writer, mod));
|
|
104
|
-
yield ((_c = mod.afterShutdown) === null || _c === void 0 ? void 0 : _c.call(mod, modWriter));
|
|
105
118
|
}
|
|
106
119
|
catch (err) {
|
|
107
120
|
const error = new Error(`Module "${mod.name}" stop failed: ${errorMessage(err)}`);
|
|
@@ -113,6 +126,20 @@ function start(roots_1) {
|
|
|
113
126
|
}
|
|
114
127
|
}
|
|
115
128
|
}
|
|
129
|
+
for (const mod of reverseBoot) {
|
|
130
|
+
try {
|
|
131
|
+
yield ((_c = mod.afterShutdown) === null || _c === void 0 ? void 0 : _c.call(mod, modWriter));
|
|
132
|
+
}
|
|
133
|
+
catch (err) {
|
|
134
|
+
const error = new Error(`Module "${mod.name}" afterShutdown failed: ${errorMessage(err)}`);
|
|
135
|
+
if (onError) {
|
|
136
|
+
yield (onError === null || onError === void 0 ? void 0 : onError(error, writer, mod));
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
116
143
|
yield (afterShutdown === null || afterShutdown === void 0 ? void 0 : afterShutdown(writer));
|
|
117
144
|
});
|
|
118
145
|
}
|
|
@@ -225,11 +252,13 @@ function rollback(booted, modWriter) {
|
|
|
225
252
|
*/
|
|
226
253
|
function makeWriter(ctx) {
|
|
227
254
|
return Object.assign(ctx, {
|
|
228
|
-
set(
|
|
229
|
-
if (typeof
|
|
230
|
-
return Object.assign(ctx,
|
|
255
|
+
set(keyOrCtx, value) {
|
|
256
|
+
if (typeof keyOrCtx === "object") {
|
|
257
|
+
return Object.assign(ctx, Object.assign(Object.assign({}, keyOrCtx), { set: ctx.set }));
|
|
258
|
+
}
|
|
259
|
+
if (keyOrCtx !== "set") {
|
|
260
|
+
ctx[keyOrCtx] = value;
|
|
231
261
|
}
|
|
232
|
-
ctx[key] = value;
|
|
233
262
|
return ctx;
|
|
234
263
|
},
|
|
235
264
|
});
|
package/package.json
CHANGED