@dougongjs/reactive 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.
Files changed (2) hide show
  1. package/README.md +80 -5
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,12 +1,87 @@
1
1
  # @dougongjs/reactive
2
2
 
3
- 零依赖的响应式原语:`signal` / `computed` / `batch` / `observe`。
3
+ [简体中文](#简体中文) · [English](#english)
4
4
 
5
- 与 `@dougongjs/core` 互不依赖——`observe()` 是一个作用在结构化 `ObservationOwner` 上的自由函数,因此可以驱动 Core 的 Lifetime,而 Core 不需要知道它存在。
5
+ ---
6
6
 
7
- - 文档站:https://tangerg.github.io/dougong/
8
- - 仓库:https://github.com/Tangerg/dougong
7
+ ## 简体中文
9
8
 
10
- > 早期开发阶段(0.0.x),当前不承诺向后兼容。需要 Node.js >= 22 或等价的 ES2024 宿主。
9
+ **零依赖**的响应式原语:`signal` / `computed` / `batch` / `observe`。
10
+
11
+ ```sh
12
+ npm install @dougongjs/reactive
13
+ ```
14
+
15
+ ```ts
16
+ import { signal, computed, batch, observe } from "@dougongjs/reactive"
17
+
18
+ const count = signal(0)
19
+ const double = computed(() => count.get() * 2)
20
+
21
+ batch(() => count.set(21))
22
+ double.get() // 42
23
+ ```
24
+
25
+ `computed` 是惰性的,依赖动态追踪。`batch` 合并回调内的全部通知。三者都拒绝异步回调——同步追踪和批次边界不能跨越 `await`,与其产生静默的错误结果,不如立刻抛错。
26
+
27
+ ### observe:把值的变化编译成资源的重建
28
+
29
+ ```ts
30
+ observe(source, owner, (value, lifetime) => {
31
+ const socket = new WebSocket(value)
32
+ lifetime.cleanup(() => socket.close())
33
+ })
34
+ ```
35
+
36
+ 每次 `source` 变化:先释放上一个 `lifetime`,再用新值建一个新的。
37
+
38
+ `observe()` 与 `@dougongjs/core` **互不依赖**——它是一个作用在结构化 `ObservationOwner`(提供 `cleanup` / `lifetime` / `spawn`)上的自由函数,所以能驱动 Core 的 Lifetime,而 Core 不需要知道它存在。插件的 `ctx` 恰好满足这个形状。
39
+
40
+ `source` 只需满足结构化的 `Readable<T>`(`get()` + `subscribe()`)——Signal、Core 的 `ExtensionView`、诊断视图,甚至你自己写的对象都可以。
41
+
42
+ ---
43
+
44
+ ## English
45
+
46
+ **Zero-dependency** reactive primitives: `signal` / `computed` / `batch` / `observe`.
47
+
48
+ ```sh
49
+ npm install @dougongjs/reactive
50
+ ```
51
+
52
+ ```ts
53
+ import { signal, computed, batch, observe } from "@dougongjs/reactive"
54
+
55
+ const count = signal(0)
56
+ const double = computed(() => count.get() * 2)
57
+
58
+ batch(() => count.set(21))
59
+ double.get() // 42
60
+ ```
61
+
62
+ `computed` is lazy with dynamic dependency tracking. `batch` coalesces every notification inside the callback. All three reject asynchronous callbacks — synchronous tracking and batch boundaries cannot survive an `await`, and throwing immediately beats producing a silently wrong result.
63
+
64
+ ### observe: compiling value change into resource rebuild
65
+
66
+ ```ts
67
+ observe(source, owner, (value, lifetime) => {
68
+ const socket = new WebSocket(value)
69
+ lifetime.cleanup(() => socket.close())
70
+ })
71
+ ```
72
+
73
+ On each change to `source`: release the previous `lifetime`, then build a new one from the new value.
74
+
75
+ `observe()` and `@dougongjs/core` are **mutually independent** — it is a free function over a structural `ObservationOwner` (anything providing `cleanup` / `lifetime` / `spawn`), so it can drive Core Lifetimes without Core knowing it exists. A plugin's `ctx` happens to satisfy that shape.
76
+
77
+ `source` need only satisfy the structural `Readable<T>` (`get()` + `subscribe()`) — a signal, Core's `ExtensionView`, a diagnostics view, or your own object.
78
+
79
+ ---
80
+
81
+ - 文档站 / Documentation: https://tangerg.github.io/dougong/
82
+ - 仓库 / Repository: https://github.com/Tangerg/dougong
83
+
84
+ > 早期开发阶段(0.0.x),当前不承诺向后兼容。需要 Node.js ≥ 22 或等价的 ES2024 宿主。
85
+ > Early development (0.0.x); no backward-compatibility promises yet. Requires Node.js ≥ 22 or an equivalent ES2024 host.
11
86
 
12
87
  MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dougongjs/reactive",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=22"