@dougongjs/core 0.0.1 → 0.0.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 +97 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,18 +1,105 @@
|
|
|
1
1
|
# @dougongjs/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[简体中文](#简体中文) · [English](#english)
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
- `Extension` 可动态增减的开放贡献集合
|
|
7
|
-
- `Event` 不保留状态的瞬时事实
|
|
8
|
-
- `Lifetime` 监听、贡献、任务与资源的结构化所有权
|
|
9
|
-
- `Application` / `ChangeSet` 事务化的安装图,失败回滚或 fail closed
|
|
5
|
+
---
|
|
10
6
|
|
|
11
|
-
|
|
7
|
+
## 简体中文
|
|
12
8
|
|
|
13
|
-
|
|
14
|
-
- 仓库:https://github.com/Tangerg/dougong
|
|
9
|
+
Dougong 的能力组合与结构化生命周期内核。
|
|
15
10
|
|
|
16
|
-
|
|
11
|
+
```sh
|
|
12
|
+
npm install @dougongjs/core
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### 六个原子
|
|
16
|
+
|
|
17
|
+
- **`Service`** —— 稳定的一对一能力。依赖通过 `requires` 显式声明,实例期内不变;提供者变化会重建消费者,不使用 live Proxy。
|
|
18
|
+
- **`Extension`** —— 可动态增删的开放贡献集合。Core 只保存原始贡献;排序、领域 key、覆盖和 pipeline 是高层组合策略。
|
|
19
|
+
- **`Event`** —— 不保留状态的瞬时事实。只有一种分发语义:并发广播并等待全部监听器。
|
|
20
|
+
- **`Lifetime`** —— 监听、贡献、任务、子生命周期与清理的结构化所有权。终态资源自动从父级摘除。
|
|
21
|
+
- **`Plugin`** —— 一次 `setup` 产生一组能力。
|
|
22
|
+
- **`Application`** / **`ChangeSet`** —— 事务化的安装图;失败回滚,无法可靠回滚时 fail closed。
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { createApp, definePlugin, service } from "@dougongjs/core"
|
|
26
|
+
|
|
27
|
+
const DATABASE = service<Database>("app/database")
|
|
28
|
+
|
|
29
|
+
const database = definePlugin({
|
|
30
|
+
name: "app.database",
|
|
31
|
+
provides: { db: DATABASE },
|
|
32
|
+
async setup(ctx) {
|
|
33
|
+
const client = await connect()
|
|
34
|
+
ctx.cleanup(() => client.close())
|
|
35
|
+
return { db: client }
|
|
36
|
+
},
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
const app = createApp({ name: "api" })
|
|
40
|
+
app.install(database)
|
|
41
|
+
await app.start()
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
不使用 Service Locator、环境作用域、原型链注入或 Proxy。运行时依赖只有 `@standard-schema/spec`(类型契约)。
|
|
45
|
+
|
|
46
|
+
### 环境要求
|
|
47
|
+
|
|
48
|
+
Node.js ≥ 22 或等价的 ES2024 宿主。TypeScript 消费者需要
|
|
49
|
+
`"lib": ["ES2024", "DOM", "DOM.Iterable", "ESNext.Disposable"]`。
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## English
|
|
54
|
+
|
|
55
|
+
The capability composition and structured lifetime kernel of Dougong.
|
|
56
|
+
|
|
57
|
+
```sh
|
|
58
|
+
npm install @dougongjs/core
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Six atoms
|
|
62
|
+
|
|
63
|
+
- **`Service`** — a stable one-to-one capability. Dependencies are declared explicitly through `requires` and stay fixed for the instance lifetime; a provider change rebuilds consumers rather than using a live proxy.
|
|
64
|
+
- **`Extension`** — an open contribution set that adds and removes live. Core keeps only raw contributions; ordering, domain keys, override and pipelines are higher-level composition policy.
|
|
65
|
+
- **`Event`** — a transient fact retaining no state, with one dispatch semantic: broadcast concurrently and await every listener.
|
|
66
|
+
- **`Lifetime`** — structured ownership of listeners, contributions, tasks, child lifetimes and cleanups. Terminal resources detach from their parent automatically.
|
|
67
|
+
- **`Plugin`** — one `setup` producing a set of capabilities.
|
|
68
|
+
- **`Application`** / **`ChangeSet`** — a transactional installation graph; a failure rolls back, and fails closed when it cannot roll back reliably.
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { createApp, definePlugin, service } from "@dougongjs/core"
|
|
72
|
+
|
|
73
|
+
const DATABASE = service<Database>("app/database")
|
|
74
|
+
|
|
75
|
+
const database = definePlugin({
|
|
76
|
+
name: "app.database",
|
|
77
|
+
provides: { db: DATABASE },
|
|
78
|
+
async setup(ctx) {
|
|
79
|
+
const client = await connect()
|
|
80
|
+
ctx.cleanup(() => client.close())
|
|
81
|
+
return { db: client }
|
|
82
|
+
},
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
const app = createApp({ name: "api" })
|
|
86
|
+
app.install(database)
|
|
87
|
+
await app.start()
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
No service locator, ambient scope, prototype-chain injection or proxy. The only runtime dependency is `@standard-schema/spec` (a type contract).
|
|
91
|
+
|
|
92
|
+
### Requirements
|
|
93
|
+
|
|
94
|
+
Node.js ≥ 22 or an equivalent ES2024 host. TypeScript consumers need
|
|
95
|
+
`"lib": ["ES2024", "DOM", "DOM.Iterable", "ESNext.Disposable"]`.
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
- 文档站 / Documentation: https://tangerg.github.io/dougong/
|
|
100
|
+
- 仓库 / Repository: https://github.com/Tangerg/dougong
|
|
101
|
+
|
|
102
|
+
> 早期开发阶段(0.0.x),当前不承诺向后兼容。
|
|
103
|
+
> Early development (0.0.x); no backward-compatibility promises yet.
|
|
17
104
|
|
|
18
105
|
MIT
|