@makoojs/core 0.4.0 → 0.6.0
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.CN.md +76 -252
- package/README.md +76 -254
- package/dist/adapter/types.d.ts +42 -51
- package/dist/error/MakooError.d.ts +22 -11
- package/dist/error/coreErrors.d.ts +6793 -0
- package/dist/error/defineMakooErrors.d.ts +55 -0
- package/dist/index.cjs +4 -5
- package/dist/index.d.ts +16 -19
- package/dist/index.js +923 -1707
- package/dist/injections/inject/declaration.d.ts +32 -0
- package/dist/injections/inject/inject.d.ts +38 -0
- package/dist/injections/injectEach/declaration.d.ts +25 -0
- package/dist/injections/injectEach/injectEach.d.ts +43 -0
- package/dist/injections/kinds.d.ts +12 -0
- package/dist/injections/listen/declaration.d.ts +34 -0
- package/dist/injections/listen/listen.d.ts +36 -0
- package/dist/injections/mount.d.ts +17 -0
- package/dist/instance/createMakoo.d.ts +3 -0
- package/dist/instance/residue.d.ts +7 -0
- package/dist/instance/types.d.ts +81 -0
- package/dist/runtime/injection.d.ts +94 -0
- package/dist/runtime/match.d.ts +50 -0
- package/dist/runtime/observer.d.ts +9 -0
- package/dist/runtime/scope.d.ts +28 -0
- package/dist/runtime/selector.d.ts +1 -0
- package/dist/runtime/stateNode.d.ts +51 -0
- package/package.json +1 -1
- package/dist/Makoo/createMakoo.d.ts +0 -4
- package/dist/Makoo/types.d.ts +0 -103
- package/dist/Task/TaskContext.d.ts +0 -25
- package/dist/Task/TaskLifeCycle.d.ts +0 -7
- package/dist/Task/TaskRegister.d.ts +0 -19
- package/dist/Task/TaskRunner.d.ts +0 -7
- package/dist/Task/types.d.ts +0 -76
- package/dist/Task/util.d.ts +0 -11
- package/dist/adapter/Adapter.d.ts +0 -2
- package/dist/error/AdapterError.d.ts +0 -4
- package/dist/error/ErrorCode.d.ts +0 -19
- package/dist/error/SignalError.d.ts +0 -4
- package/dist/error/TaskError.d.ts +0 -4
- package/dist/error/formatMakooError.d.ts +0 -2
- package/dist/hooks/ObserverHub.d.ts +0 -3
- package/dist/hooks/types.d.ts +0 -39
- package/dist/hooks/util.d.ts +0 -5
- package/dist/logger/Logger.d.ts +0 -14
- package/dist/logger/types.d.ts +0 -7
- package/dist/payload/buildAliveObservePayload.d.ts +0 -51
- package/dist/payload/buildDomObservePayload.d.ts +0 -46
- package/dist/payload/buildInjectObservePayload.d.ts +0 -54
- package/dist/payload/buildListenerObservePayload.d.ts +0 -44
- package/dist/payload/buildObservePayload.d.ts +0 -10
- package/dist/payload/buildRegisterObservePayload.d.ts +0 -44
- package/dist/payload/buildResourceObservePayload.d.ts +0 -45
- package/dist/payload/buildRunObservePayload.d.ts +0 -57
- package/dist/payload/buildTaskObservePayload.d.ts +0 -44
- package/dist/payload/createDomObserveEmitFactory.d.ts +0 -12
- package/dist/runtime/createRuntime.d.ts +0 -3
- package/dist/runtime/types.d.ts +0 -13
- package/dist/signal/observeActivitySignal.d.ts +0 -4
- package/dist/signal/types.d.ts +0 -15
- package/dist/util/getArtifactName.d.ts +0 -1
- package/dist/util/uuid.d.ts +0 -1
- package/dist/watcher/DomWatcher.d.ts +0 -13
- package/dist/watcher/types.d.ts +0 -15
package/README.CN.md
CHANGED
|
@@ -1,287 +1,111 @@
|
|
|
1
1
|
# @makoojs/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p>
|
|
4
|
+
<a href="https://www.npmjs.com/package/@makoojs/core"><img alt="npm" src="https://img.shields.io/npm/v/@makoojs/core?style=flat-square"></a>
|
|
5
|
+
<a href="https://github.com/makoojs/Makoo/blob/main/LICENSE"><img alt="License" src="https://img.shields.io/badge/License-MIT-yellow?style=flat-square"></a>
|
|
6
|
+
</p>
|
|
4
7
|
|
|
5
|
-
>
|
|
6
|
-
|
|
8
|
+
<p>
|
|
9
|
+
<a href="https://makoojs.github.io/Makoo/zh/api/core">API 参考</a> •
|
|
10
|
+
<a href="https://github.com/makoojs/Makoo">Makoo</a> •
|
|
11
|
+
<a href="https://github.com/makoojs/Makoo/blob/main/packages/core/README.md">English</a>
|
|
12
|
+
</p>
|
|
7
13
|
|
|
8
|
-
|
|
14
|
+
Makoo 的核心包,运行在浏览器中。你用它创建 Core 实例,声明组件和网页事件监听,再控制它们、读取它们的状态。等待目标元素、挂载、节点被替换后恢复和清理都在这里完成。
|
|
9
15
|
|
|
10
|
-
|
|
11
|
-
- 使用 `createMakoo()` 创建运行时,并显式启动任务声明。
|
|
12
|
-
- 监听注入生命周期事件,用于调试、埋点、错误上报或可视化开发工具。
|
|
13
|
-
- 使用 `DOMWatcher`、`createActivityStore` 等底层工具构建自定义运行时集成。
|
|
16
|
+
它不依赖任何前端框架,组件通过 [`@makoojs/vue`](https://www.npmjs.com/package/@makoojs/vue) 或 [`@makoojs/react`](https://www.npmjs.com/package/@makoojs/react) 提供的适配器挂载。
|
|
14
17
|
|
|
15
18
|
## 安装
|
|
16
19
|
|
|
17
|
-
```
|
|
18
|
-
# npm install @makoojs/core
|
|
19
|
-
# yarn add @makoojs/core
|
|
20
|
+
```sh
|
|
20
21
|
pnpm add @makoojs/core
|
|
21
22
|
```
|
|
22
23
|
|
|
23
|
-
|
|
24
|
+
> [!TIP]
|
|
25
|
+
> 用 `pnpm dlx @makoojs/create-makoo` 创建的项目已经装好了它,不需要单独安装。
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
import {
|
|
27
|
-
createMakoo,
|
|
28
|
-
inject,
|
|
29
|
-
type ResolvableMountAdapter
|
|
30
|
-
} from '@makoojs/core';
|
|
31
|
-
|
|
32
|
-
type TextArtifact = {
|
|
33
|
-
kind: 'text';
|
|
34
|
-
text: string;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
const textAdapter: ResolvableMountAdapter<TextArtifact, HTMLElement> = {
|
|
38
|
-
name: 'text',
|
|
39
|
-
matches(artifact): artifact is TextArtifact {
|
|
40
|
-
return (
|
|
41
|
-
typeof artifact === 'object' &&
|
|
42
|
-
artifact !== null &&
|
|
43
|
-
(artifact as { kind?: unknown }).kind === 'text'
|
|
44
|
-
);
|
|
45
|
-
},
|
|
46
|
-
mount({ mountPoint, artifact }) {
|
|
47
|
-
const el = document.createElement('span');
|
|
48
|
-
el.textContent = artifact.text;
|
|
49
|
-
mountPoint.appendChild(el);
|
|
50
|
-
|
|
51
|
-
return { handle: el };
|
|
52
|
-
},
|
|
53
|
-
unmount({ handle }) {
|
|
54
|
-
handle.remove();
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
const makoo = createMakoo({
|
|
59
|
-
defaults: {
|
|
60
|
-
alive: true,
|
|
61
|
-
scope: 'local',
|
|
62
|
-
timeout: 5000
|
|
63
|
-
},
|
|
64
|
-
adapters: [textAdapter]
|
|
65
|
-
});
|
|
27
|
+
## 用法
|
|
66
28
|
|
|
67
|
-
|
|
68
|
-
inject({
|
|
69
|
-
id: 'text',
|
|
70
|
-
injectAt: '#app',
|
|
71
|
-
artifact: {
|
|
72
|
-
kind: 'text',
|
|
73
|
-
text: 'Hello from Makoo core'
|
|
74
|
-
}
|
|
75
|
-
})
|
|
76
|
-
]);
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
## 运行时基础
|
|
80
|
-
|
|
81
|
-
`inject()` 和 `listen()` 用于声明任务。它们不会触碰 DOM,也不会自己注册任务。`makoo.start([...])` 会注册传入批次里的声明,并立即调度这些任务。
|
|
82
|
-
|
|
83
|
-
`inject()` 接收包含目标、artifact 和任务选项的对象。可选的 `id` 会作为任务 ID,适合后续需要查找或控制这个任务的场景:
|
|
84
|
-
|
|
85
|
-
```ts
|
|
86
|
-
inject({
|
|
87
|
-
id: 'settings-panel',
|
|
88
|
-
injectAt: '#settings',
|
|
89
|
-
artifact: settingsArtifact,
|
|
90
|
-
options: {
|
|
91
|
-
alive: true
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
没有传 `id` 时,Makoo 会根据 artifact 和目标 selector 推导任务 ID。
|
|
29
|
+
### 声明组件与监听
|
|
97
30
|
|
|
98
31
|
```ts
|
|
99
32
|
import { createMakoo, inject, listen } from '@makoojs/core';
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
inject({
|
|
133
|
-
id: 'save-tip',
|
|
134
|
-
injectAt: '#save-tip',
|
|
135
|
-
artifact: saveTipArtifact,
|
|
136
|
-
options: {
|
|
137
|
-
on: listen({
|
|
138
|
-
listenAt: '#save',
|
|
139
|
-
type: 'click',
|
|
140
|
-
callback: () => {
|
|
141
|
-
console.log('save clicked');
|
|
142
|
-
}
|
|
143
|
-
})
|
|
144
|
-
}
|
|
145
|
-
}),
|
|
146
|
-
listen({
|
|
147
|
-
id: 'escape-close',
|
|
148
|
-
listenAt: '#escape',
|
|
149
|
-
type: 'keydown',
|
|
150
|
-
callback: onEscape
|
|
151
|
-
})
|
|
33
|
+
import { vueAdapter } from '@makoojs/vue';
|
|
34
|
+
import Panel from './Panel.vue';
|
|
35
|
+
|
|
36
|
+
const core = createMakoo();
|
|
37
|
+
|
|
38
|
+
core.apply([
|
|
39
|
+
inject({
|
|
40
|
+
name: 'panel',
|
|
41
|
+
injectAt: '#sidebar',
|
|
42
|
+
adapter: vueAdapter,
|
|
43
|
+
component: Panel,
|
|
44
|
+
// 网站替换掉 #sidebar 后重新挂载
|
|
45
|
+
reinject: true,
|
|
46
|
+
listeners: [
|
|
47
|
+
// 附属监听:随 panel 一起启动和停止
|
|
48
|
+
listen({
|
|
49
|
+
name: 'search',
|
|
50
|
+
listenAt: '#search-form',
|
|
51
|
+
type: 'submit',
|
|
52
|
+
callback: () => console.log('提交了搜索')
|
|
53
|
+
})
|
|
54
|
+
]
|
|
55
|
+
}),
|
|
56
|
+
// 独立监听:不属于任何组件
|
|
57
|
+
listen({
|
|
58
|
+
name: 'escape',
|
|
59
|
+
listenAt: 'body',
|
|
60
|
+
type: 'keydown',
|
|
61
|
+
callback: (event) => {
|
|
62
|
+
if ((event as KeyboardEvent).key === 'Escape') console.log('按下了 Escape');
|
|
63
|
+
}
|
|
64
|
+
})
|
|
152
65
|
]);
|
|
153
66
|
```
|
|
154
67
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
```ts
|
|
158
|
-
const toolbar = started.get('toolbar');
|
|
159
|
-
|
|
160
|
-
if (toolbar?.kind === 'component') {
|
|
161
|
-
toolbar.disableAlive();
|
|
162
|
-
toolbar.enableAlive();
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
started.destroyAll();
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
`started.destroyAll()` 只影响本次启动产生的任务。`makoo.destroyAll()` 会影响整个运行时中的全部任务。
|
|
169
|
-
|
|
170
|
-
## Adapter 协议
|
|
171
|
-
|
|
172
|
-
core 不关心 artifact 是 Vue 组件、React 组件还是其他对象。它只要求 adapter 实现统一挂载协议。
|
|
173
|
-
|
|
174
|
-
```ts
|
|
175
|
-
import type { ResolvableMountAdapter } from '@makoojs/core';
|
|
176
|
-
|
|
177
|
-
const adapter: ResolvableMountAdapter<MyArtifact, MyHandle, MyInstance> = {
|
|
178
|
-
name: 'my-adapter',
|
|
179
|
-
matches(artifact): artifact is MyArtifact {
|
|
180
|
-
return isMyArtifact(artifact);
|
|
181
|
-
},
|
|
182
|
-
mount(input) {
|
|
183
|
-
return {
|
|
184
|
-
handle,
|
|
185
|
-
instance
|
|
186
|
-
};
|
|
187
|
-
},
|
|
188
|
-
unmount(input) {
|
|
189
|
-
// 根据 input.reason 清理资源。
|
|
190
|
-
}
|
|
191
|
-
};
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
`mount(input)` 会收到目标宿主节点、生成的挂载点、artifact、taskId、selector,以及任务级 `makoo` 上下文。
|
|
68
|
+
### 在每个匹配元素上挂载
|
|
195
69
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
独立监听任务使用 `listen()` 的对象形式声明。可选的 `id` 是任务 ID;后续需要稳定地查找或控制任务时应提供它。组件的 `on` listener 归属于组件任务本身,不需要单独的 ID。
|
|
199
|
-
|
|
200
|
-
`capture` 默认为 `false`;设置为 `true` 后,监听器会在 DOM 捕获阶段执行。
|
|
70
|
+
`injectEach()` 把同一个组件挂载到每个匹配 `injectAt` 的元素上,之后出现的元素也会挂载;一个都没匹配到时会持续监视:
|
|
201
71
|
|
|
202
72
|
```ts
|
|
203
|
-
import {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
console.log('save clicked');
|
|
215
|
-
},
|
|
216
|
-
activitySignal: () => enabled
|
|
217
|
-
})
|
|
73
|
+
import { injectEach } from '@makoojs/core';
|
|
74
|
+
import { vueAdapter } from '@makoojs/vue';
|
|
75
|
+
import Badge from './Badge.vue';
|
|
76
|
+
|
|
77
|
+
core.apply([
|
|
78
|
+
injectEach({
|
|
79
|
+
name: 'badges',
|
|
80
|
+
injectAt: '.comment .author',
|
|
81
|
+
adapter: vueAdapter,
|
|
82
|
+
component: Badge
|
|
83
|
+
})
|
|
218
84
|
]);
|
|
219
|
-
|
|
220
|
-
enabled.set(false);
|
|
221
|
-
enabled.set(true);
|
|
222
85
|
```
|
|
223
86
|
|
|
224
|
-
|
|
87
|
+
### 控制注入与读取状态
|
|
225
88
|
|
|
226
|
-
core
|
|
89
|
+
通过名称获取注入对象:组件注入和 Each 注入使用 `core.injection(name)`,独立监听使用 `core.listener(name)`。可以通过 `kind` 区分组件 (`component`) 和 Each 注入 (`component-each`)。Each 注入会通过 `children` 按文档顺序列出子注入。
|
|
227
90
|
|
|
228
91
|
```ts
|
|
229
|
-
const
|
|
230
|
-
|
|
231
|
-
});
|
|
92
|
+
const panel = core.injection('panel');
|
|
93
|
+
const escape = core.listener('escape');
|
|
232
94
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
ctrl.stopPropagation();
|
|
236
|
-
}
|
|
95
|
+
panel.subscribe(() => {
|
|
96
|
+
console.log(panel.status, panel.lastError);
|
|
237
97
|
});
|
|
238
98
|
|
|
239
|
-
|
|
99
|
+
panel.stop();
|
|
100
|
+
panel.start();
|
|
101
|
+
escape.stop();
|
|
240
102
|
```
|
|
241
103
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
- `register:start`
|
|
245
|
-
- `register:success`
|
|
246
|
-
- `start:requested`
|
|
247
|
-
- `start:taskScheduled`
|
|
248
|
-
- `artifact:mountStart`
|
|
249
|
-
- `artifact:mountSuccess`
|
|
250
|
-
- `artifact:mountFail`
|
|
251
|
-
- `listener:attached`
|
|
252
|
-
- `alive:enabled`
|
|
253
|
-
- `alive:observerStarted`
|
|
254
|
-
- `task:statusChange`
|
|
255
|
-
- `dom:targetFound`
|
|
256
|
-
- `dom:targetTimeout`
|
|
257
|
-
|
|
258
|
-
完整事件名列表可从 `OBSERVE_EVENT_NAMES` 获取。
|
|
259
|
-
|
|
260
|
-
## DOMWatcher
|
|
261
|
-
|
|
262
|
-
`DOMWatcher` 是 core 的底层 DOM 观察工具。通常你不需要直接使用它,因为 `makoo.start()` 和 alive 模式已经封装了目标等待和恢复逻辑。
|
|
263
|
-
|
|
264
|
-
## 日志和错误
|
|
265
|
-
|
|
266
|
-
core 默认使用 `Logger`,并用 `[Makoo]` 前缀打印日志。你可以通过 `createMakoo({ logger })` 传入自定义 logger。
|
|
267
|
-
|
|
268
|
-
core 也导出这些错误相关类型:
|
|
269
|
-
|
|
270
|
-
- `MakooError`
|
|
271
|
-
- `AdapterError`
|
|
272
|
-
- `TaskError`
|
|
273
|
-
- `SignalError`
|
|
274
|
-
- `ErrorCode`
|
|
275
|
-
- `MakooIssue`
|
|
104
|
+
顶层注入的 `start()`、`stop()` 和 `remove()` 都不返回结果。`start()` 和 `stop()` 的结果通过 `status` 和 `lastError` 读取;`remove()` 之后,该名称不再注册在实例上。
|
|
276
105
|
|
|
277
|
-
##
|
|
106
|
+
## 文档
|
|
278
107
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
| 生命周期观察 | `ObserverHub`, `OBSERVE_EVENT_NAMES`, `ObserveEvent`, `ObserveHook`, `LifecycleHookMap` |
|
|
284
|
-
| DOM 观察 | `DOMWatcher` |
|
|
285
|
-
| 监听 signal | `createActivityStore`, `ActivitySignalSource` |
|
|
286
|
-
| 日志 | `Logger`, `ILogger`, `LoggerLevel` |
|
|
287
|
-
| 错误 | `MakooError`, `AdapterError`, `TaskError`, `SignalError`, `ErrorCode` |
|
|
108
|
+
- [Core 实例](https://makoojs.github.io/Makoo/zh/api/instance):`createMakoo()` 与实例上的方法。
|
|
109
|
+
- [声明](https://makoojs.github.io/Makoo/zh/api/declarations):`inject()`、`injectEach()` 与 `listen()` 的全部字段。
|
|
110
|
+
- [控制与状态](https://makoojs.github.io/Makoo/zh/api/status):启动、停止、移除,以及状态值的含义。
|
|
111
|
+
- [错误与错误码](https://makoojs.github.io/Makoo/zh/api/errors):错误的种类和处理方式。
|