@makoojs/core 0.1.0 → 0.2.1
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 +137 -178
- package/README.md +122 -163
- package/dist/Makoo/createMakoo.d.ts +5 -0
- package/dist/Makoo/types.d.ts +96 -0
- package/dist/Task/TaskContext.d.ts +4 -84
- package/dist/Task/TaskLifeCycle.d.ts +7 -18
- package/dist/Task/TaskRegister.d.ts +16 -16
- package/dist/Task/TaskRunner.d.ts +6 -19
- package/dist/Task/types.d.ts +2 -6
- package/dist/Task/util.d.ts +7 -0
- package/dist/adapter/Adapter.d.ts +2 -6
- package/dist/adapter/types.d.ts +5 -1
- package/dist/error/ErrorCode.d.ts +7 -0
- package/dist/error/SignalError.d.ts +4 -0
- package/dist/hooks/ObserverHub.d.ts +2 -20
- package/dist/hooks/types.d.ts +13 -1
- package/dist/hooks/util.d.ts +1 -2
- package/dist/index.cjs +2 -2
- package/dist/index.d.ts +6 -6
- package/dist/index.js +1245 -1124
- package/dist/payload/buildRunObservePayload.d.ts +7 -7
- package/dist/runtime/createRuntime.d.ts +3 -0
- package/dist/runtime/types.d.ts +13 -0
- package/dist/signal/observeActivitySignal.d.ts +0 -4
- package/package.json +1 -1
- package/dist/Injector/Injector.d.ts +0 -48
- package/dist/Injector/types.d.ts +0 -29
package/README.CN.md
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
# @makoojs/core
|
|
2
2
|
|
|
3
|
-
`@makoojs/core` 是 Makoo
|
|
3
|
+
`@makoojs/core` 是 Makoo 的框架无关运行时核心。它负责声明注入任务、启动任务批次、等待目标 DOM、挂载 artifact、管理 alive 重新注入、绑定事件监听,并提供生命周期观察、日志和错误基础设施。
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
普通项目通常从 `@makoojs/cli` 开始。只有在你想绕过 CLI 生成入口、手动集成运行时时,才需要直接使用 core。
|
|
6
6
|
|
|
7
7
|
> [!NOTE]
|
|
8
8
|
> `@makoojs/core`包是其他包的父包,其他的包都直接或间接的依赖该包的类型或功能
|
|
9
9
|
|
|
10
|
-
##
|
|
10
|
+
## 使用场景
|
|
11
11
|
|
|
12
|
-
- 编写自定义 `ResolvableMountAdapter`,让 Makoo
|
|
13
|
-
-
|
|
12
|
+
- 编写自定义 `ResolvableMountAdapter`,让 Makoo 挂载新的 artifact 类型。
|
|
13
|
+
- 使用 `createMakoo()` 创建运行时,并显式启动任务声明。
|
|
14
14
|
- 监听注入生命周期事件,用于调试、埋点、错误上报或可视化开发工具。
|
|
15
|
-
- 使用 `DOMWatcher`、`createActivityStore`
|
|
15
|
+
- 使用 `DOMWatcher`、`createActivityStore` 等底层工具构建自定义运行时集成。
|
|
16
16
|
|
|
17
17
|
## 安装
|
|
18
18
|
|
|
@@ -24,10 +24,12 @@ pnpm add @makoojs/core
|
|
|
24
24
|
|
|
25
25
|
## 最小运行时示例
|
|
26
26
|
|
|
27
|
-
下面的示例展示了 core 的基本路径:创建 `Injector`,注册 adapter,注册 artifact,然后调用 `run()` 等待目标 DOM 并挂载。
|
|
28
|
-
|
|
29
27
|
```ts
|
|
30
|
-
import {
|
|
28
|
+
import {
|
|
29
|
+
createMakoo,
|
|
30
|
+
inject,
|
|
31
|
+
type ResolvableMountAdapter
|
|
32
|
+
} from '@makoojs/core';
|
|
31
33
|
|
|
32
34
|
type TextArtifact = {
|
|
33
35
|
kind: 'text';
|
|
@@ -55,42 +57,106 @@ const textAdapter: ResolvableMountAdapter<TextArtifact, HTMLElement> = {
|
|
|
55
57
|
}
|
|
56
58
|
};
|
|
57
59
|
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
const makoo = createMakoo({
|
|
61
|
+
defaults: {
|
|
62
|
+
alive: true,
|
|
63
|
+
scope: 'local',
|
|
64
|
+
timeout: 5000
|
|
65
|
+
},
|
|
66
|
+
adapters: [textAdapter]
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
makoo.start([
|
|
70
|
+
inject('#app', {
|
|
71
|
+
kind: 'text',
|
|
72
|
+
text: 'Hello from Makoo core'
|
|
73
|
+
})
|
|
74
|
+
]);
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## 运行时基础
|
|
78
|
+
|
|
79
|
+
`inject()` 和 `listen()` 是声明 helper。它们不会触碰 DOM,也不会自己注册任务。`makoo.start([...])` 会注册传入批次里的声明,并立即调度这些任务。
|
|
80
|
+
|
|
81
|
+
`inject()` 有两种写法。参数形式比较简洁:
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
inject('#toolbar', toolbarArtifact, {
|
|
85
|
+
alive: true
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
对象形式会把声明字段放在一起。里面可选的 `id` 会作为任务 ID,适合后续需要查找或控制这个任务的场景:
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
inject({
|
|
93
|
+
id: 'settings-panel',
|
|
94
|
+
injectAt: '#settings',
|
|
95
|
+
artifact: settingsArtifact,
|
|
96
|
+
options: {
|
|
97
|
+
alive: true
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
没有传 `id` 时,Makoo 会根据 artifact 和目标 selector 推导任务 ID。
|
|
63
103
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
104
|
+
```ts
|
|
105
|
+
import { createMakoo, inject, listen } from '@makoojs/core';
|
|
106
|
+
|
|
107
|
+
const makoo = createMakoo({
|
|
108
|
+
defaults: {
|
|
109
|
+
alive: false,
|
|
110
|
+
scope: 'local',
|
|
111
|
+
timeout: 5000
|
|
112
|
+
},
|
|
113
|
+
adapters: [myAdapter],
|
|
114
|
+
hooks: {
|
|
115
|
+
'start:requested': (event) => {
|
|
116
|
+
console.log(event.name);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
67
119
|
});
|
|
68
120
|
|
|
69
|
-
|
|
121
|
+
const started = makoo.start([
|
|
122
|
+
inject('#toolbar', toolbarArtifact, {
|
|
123
|
+
alive: true
|
|
124
|
+
}),
|
|
125
|
+
inject({
|
|
126
|
+
id: 'settings-panel',
|
|
127
|
+
injectAt: '#settings',
|
|
128
|
+
artifact: settingsArtifact,
|
|
129
|
+
options: {
|
|
130
|
+
alive: true
|
|
131
|
+
}
|
|
132
|
+
}),
|
|
133
|
+
inject('#save-tip', saveTipArtifact, {
|
|
134
|
+
on: listen('#save', 'click', () => {
|
|
135
|
+
console.log('save clicked');
|
|
136
|
+
})
|
|
137
|
+
}),
|
|
138
|
+
listen('#escape', 'keydown', onEscape)
|
|
139
|
+
]);
|
|
70
140
|
```
|
|
71
141
|
|
|
72
|
-
|
|
142
|
+
`start()` 返回 `StartedTasks`,用于控制本批次任务:
|
|
73
143
|
|
|
74
|
-
|
|
144
|
+
```ts
|
|
145
|
+
const toolbar = started.get('Toolbar@#toolbar');
|
|
75
146
|
|
|
76
|
-
|
|
147
|
+
if (toolbar?.kind === 'component') {
|
|
148
|
+
toolbar.disableAlive();
|
|
149
|
+
toolbar.enableAlive();
|
|
150
|
+
}
|
|
77
151
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
| `applyAdapter(adapter)` | 注册一个可解析 artifact 的挂载适配器 |
|
|
81
|
-
| `register(injectAt, artifact, options?)` | 注册一个组件或其他 artifact 注入任务 |
|
|
82
|
-
| `registerListener(listenAt, event, callback, activitySignal?)` | 注册一个独立 DOM 事件监听任务 |
|
|
83
|
-
| `run()` | 开始调度已注册任务 |
|
|
84
|
-
| `enableAlive(taskId)` / `disableAlive(taskId)` | 开启或关闭指定组件任务的 alive 重注入 |
|
|
85
|
-
| `reset(taskId)` / `destroy(taskId)` | 重置或销毁单个任务 |
|
|
86
|
-
| `resetAll()` / `destroyAll()` | 重置或销毁所有任务 |
|
|
87
|
-
| `on()` / `onTask()` / `onAny()` | 监听生命周期观察事件 |
|
|
152
|
+
started.destroyAll();
|
|
153
|
+
```
|
|
88
154
|
|
|
89
|
-
`
|
|
155
|
+
`started.destroyAll()` 只影响本次启动产生的任务。`makoo.destroyAll()` 会影响整个运行时中的全部任务。
|
|
90
156
|
|
|
91
|
-
## Adapter
|
|
157
|
+
## Adapter 协议
|
|
92
158
|
|
|
93
|
-
core 不关心 artifact 是 Vue 组件、React 组件还是其他对象。它只要求 adapter
|
|
159
|
+
core 不关心 artifact 是 Vue 组件、React 组件还是其他对象。它只要求 adapter 实现统一挂载协议。
|
|
94
160
|
|
|
95
161
|
```ts
|
|
96
162
|
import type { ResolvableMountAdapter } from '@makoojs/core';
|
|
@@ -101,110 +167,56 @@ const adapter: ResolvableMountAdapter<MyArtifact, MyHandle, MyInstance> = {
|
|
|
101
167
|
return isMyArtifact(artifact);
|
|
102
168
|
},
|
|
103
169
|
mount(input) {
|
|
104
|
-
// input.host 是 Makoo 创建的宿主元素
|
|
105
|
-
// input.mountPoint 是 adapter 应该挂载内容的位置
|
|
106
|
-
// input.makoo 是当前任务的上下文能力
|
|
107
170
|
return {
|
|
108
171
|
handle,
|
|
109
172
|
instance
|
|
110
173
|
};
|
|
111
174
|
},
|
|
112
175
|
unmount(input) {
|
|
113
|
-
// 根据 input.reason
|
|
176
|
+
// 根据 input.reason 清理资源。
|
|
114
177
|
}
|
|
115
178
|
};
|
|
116
179
|
```
|
|
117
180
|
|
|
118
|
-
`mount(input)`
|
|
119
|
-
|
|
120
|
-
| 字段 | 说明 |
|
|
121
|
-
| --- | --- |
|
|
122
|
-
| `host` | Makoo 创建并插入到目标位置的宿主元素 |
|
|
123
|
-
| `mountPoint` | adapter 应该挂载内容的节点 |
|
|
124
|
-
| `artifact` | 当前被注册的 artifact |
|
|
125
|
-
| `taskId` | 当前任务 ID |
|
|
126
|
-
| `injectAt` | 当前任务的目标选择器 |
|
|
127
|
-
| `makoo` | 当前任务的上下文能力 |
|
|
128
|
-
|
|
129
|
-
`makoo` 上下文可以让 adapter 内部控制当前任务:
|
|
130
|
-
|
|
131
|
-
```ts
|
|
132
|
-
mount({ makoo }) {
|
|
133
|
-
const off = makoo.onTask('artifact:mountSuccess', (event) => {
|
|
134
|
-
makoo.getLogger().debug('mounted', event.taskId);
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
return {
|
|
138
|
-
handle: {
|
|
139
|
-
off
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
## 生命周期与 Alive 模式
|
|
181
|
+
`mount(input)` 会收到目标宿主节点、生成的挂载点、artifact、taskId、selector,以及任务级 `makoo` 上下文。
|
|
146
182
|
|
|
147
|
-
|
|
183
|
+
## Listener 和 activity signal
|
|
148
184
|
|
|
149
|
-
|
|
150
|
-
const result = injector.register('#toolbar', artifact, {
|
|
151
|
-
alive: true,
|
|
152
|
-
scope: 'global',
|
|
153
|
-
timeout: 8000
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
result.disableAlive();
|
|
157
|
-
result.enableAlive();
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
`scope` 支持:
|
|
161
|
-
|
|
162
|
-
| 值 | 说明 |
|
|
163
|
-
| --- | --- |
|
|
164
|
-
| `local` | 在当前挂载区域附近观察 DOM 变化 |
|
|
165
|
-
| `global` | 在整个 document 范围内观察 DOM 变化 |
|
|
166
|
-
|
|
167
|
-
`alive` 只适用于组件或 artifact 注入任务。独立 listener 任务不使用 alive 重注入机制。
|
|
168
|
-
|
|
169
|
-
## Listener 与 Activity Signal
|
|
170
|
-
|
|
171
|
-
除了挂载 artifact,`Injector` 也可以注册独立 DOM 事件监听任务。
|
|
185
|
+
独立监听任务通过 `listen()` 声明。
|
|
172
186
|
|
|
173
187
|
```ts
|
|
174
|
-
import { createActivityStore } from '@makoojs/core';
|
|
188
|
+
import { createActivityStore, createMakoo, listen } from '@makoojs/core';
|
|
175
189
|
|
|
176
190
|
const enabled = createActivityStore(true);
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
191
|
+
const makoo = createMakoo();
|
|
192
|
+
|
|
193
|
+
makoo.start([
|
|
194
|
+
listen(
|
|
195
|
+
'#save',
|
|
196
|
+
'click',
|
|
197
|
+
() => {
|
|
198
|
+
console.log('save clicked');
|
|
199
|
+
},
|
|
200
|
+
{
|
|
201
|
+
activitySignal: () => enabled
|
|
202
|
+
}
|
|
203
|
+
)
|
|
204
|
+
]);
|
|
188
205
|
|
|
189
206
|
enabled.set(false);
|
|
190
207
|
enabled.set(true);
|
|
191
208
|
```
|
|
192
209
|
|
|
193
|
-
`createActivityStore()` 返回一个简单的可订阅布尔状态。传入 listener 后,Makoo 会根据状态自动 attach 或 detach 事件监听器。
|
|
194
|
-
|
|
195
|
-
> [!NOTE]
|
|
196
|
-
> 现在`@makoojs/cli`是无法解析这种listener的挂载形式,只支持**`component+listener`**的形式
|
|
197
|
-
|
|
198
210
|
## 观察事件
|
|
199
211
|
|
|
200
|
-
core
|
|
212
|
+
core 会在声明注册、启动、挂载、监听、alive 模式、DOM 观察和任务状态变化时发出观察事件。
|
|
201
213
|
|
|
202
214
|
```ts
|
|
203
|
-
const off =
|
|
215
|
+
const off = makoo.on('artifact:mountSuccess', (event) => {
|
|
204
216
|
console.log(event.taskId, event.injectAt);
|
|
205
217
|
});
|
|
206
218
|
|
|
207
|
-
|
|
219
|
+
makoo.onAny((event, ctrl) => {
|
|
208
220
|
if (event.name === 'artifact:mountFail') {
|
|
209
221
|
ctrl.stopPropagation();
|
|
210
222
|
}
|
|
@@ -217,8 +229,8 @@ off();
|
|
|
217
229
|
|
|
218
230
|
- `register:start`
|
|
219
231
|
- `register:success`
|
|
220
|
-
- `
|
|
221
|
-
- `
|
|
232
|
+
- `start:requested`
|
|
233
|
+
- `start:taskScheduled`
|
|
222
234
|
- `artifact:mountStart`
|
|
223
235
|
- `artifact:mountSuccess`
|
|
224
236
|
- `artifact:mountFail`
|
|
@@ -229,52 +241,17 @@ off();
|
|
|
229
241
|
- `dom:targetFound`
|
|
230
242
|
- `dom:targetTimeout`
|
|
231
243
|
|
|
232
|
-
|
|
244
|
+
完整事件名列表可从 `OBSERVE_EVENT_NAMES` 获取。
|
|
233
245
|
|
|
234
246
|
## DOMWatcher
|
|
235
247
|
|
|
236
|
-
`DOMWatcher` 是 core 的底层 DOM 观察工具。通常你不需要直接使用它,因为 `
|
|
237
|
-
|
|
238
|
-
在自定义运行时中,可以直接使用:
|
|
239
|
-
|
|
240
|
-
```ts
|
|
241
|
-
import { DOMWatcher } from '@makoojs/core';
|
|
242
|
-
|
|
243
|
-
const stop = DOMWatcher.onDomReady(
|
|
244
|
-
'#app',
|
|
245
|
-
(el) => {
|
|
246
|
-
console.log('target ready', el);
|
|
247
|
-
},
|
|
248
|
-
document,
|
|
249
|
-
{
|
|
250
|
-
once: true,
|
|
251
|
-
timeout: 5000
|
|
252
|
-
}
|
|
253
|
-
);
|
|
254
|
-
|
|
255
|
-
stop();
|
|
256
|
-
```
|
|
257
|
-
|
|
258
|
-
`DOMWatcher.onDomAlive()` 可以观察一个已存在节点的移除和恢复,主要用于实现重注入能力。
|
|
259
|
-
|
|
260
|
-
## 日志与错误
|
|
261
|
-
|
|
262
|
-
core 默认使用 `Logger` 输出带 `[Makoo]` 前缀的日志。你可以传入自定义 logger。
|
|
263
|
-
|
|
264
|
-
```ts
|
|
265
|
-
import { Injector, type ILogger } from '@makoojs/core';
|
|
248
|
+
`DOMWatcher` 是 core 的底层 DOM 观察工具。通常你不需要直接使用它,因为 `makoo.start()` 和 alive 模式已经封装了目标等待和恢复逻辑。
|
|
266
249
|
|
|
267
|
-
|
|
268
|
-
info: console.info,
|
|
269
|
-
warn: console.warn,
|
|
270
|
-
error: console.error,
|
|
271
|
-
debug: console.debug
|
|
272
|
-
};
|
|
250
|
+
## 日志和错误
|
|
273
251
|
|
|
274
|
-
|
|
275
|
-
```
|
|
252
|
+
core 默认使用 `Logger`,并用 `[Makoo]` 前缀打印日志。你可以通过 `createMakoo({ logger })` 传入自定义 logger。
|
|
276
253
|
|
|
277
|
-
core
|
|
254
|
+
core 也导出这些错误相关类型:
|
|
278
255
|
|
|
279
256
|
- `MakooError`
|
|
280
257
|
- `AdapterError`
|
|
@@ -282,32 +259,14 @@ core 还导出以下错误相关类型:
|
|
|
282
259
|
- `ErrorCode`
|
|
283
260
|
- `MakooIssue`
|
|
284
261
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
## 公开能力概览
|
|
288
|
-
|
|
289
|
-
`@makoojs/core` 的主入口导出以下几类能力:
|
|
262
|
+
## Public Exports 概览
|
|
290
263
|
|
|
291
|
-
|
|
|
264
|
+
| 分类 | 代表导出 |
|
|
292
265
|
| --- | --- |
|
|
293
|
-
|
|
|
294
|
-
| Adapter 协议 | `MountAdapter
|
|
295
|
-
| 生命周期观察 | `ObserverHub
|
|
266
|
+
| 运行时 API | `createMakoo`, `inject`, `listen`, `MakooRuntime`, `StartedTasks` |
|
|
267
|
+
| Adapter 协议 | `MountAdapter`, `ResolvableMountAdapter`, `AdapterMountInput`, `AdapterUnmountInput`, `MakooContext` |
|
|
268
|
+
| 生命周期观察 | `ObserverHub`, `OBSERVE_EVENT_NAMES`, `ObserveEvent`, `ObserveHook`, `LifecycleHookMap` |
|
|
296
269
|
| DOM 观察 | `DOMWatcher` |
|
|
297
|
-
|
|
|
298
|
-
| 日志 | `Logger
|
|
299
|
-
| 错误 | `MakooError
|
|
300
|
-
|
|
301
|
-
完整 API 参考后续会放到独立文档站中。
|
|
302
|
-
|
|
303
|
-
## 与其他包的关系
|
|
304
|
-
|
|
305
|
-
| 包 | 职责 |
|
|
306
|
-
| --- | --- |
|
|
307
|
-
| `@makoojs/core` | 框架无关的注入运行时内核 |
|
|
308
|
-
| `@makoojs/vue` | Vue adapter 与 Vue 插件注册辅助 |
|
|
309
|
-
| `@makoojs/react` | React adapter |
|
|
310
|
-
| `@makoojs/cli` | Vite 插件、配置解析、扫描、代码生成和 userscript 构建接入 |
|
|
311
|
-
| `@makoojs/create-makoo` | 项目脚手架 |
|
|
312
|
-
|
|
313
|
-
如果你只是开发普通 userscript 项目,优先使用 `@makoojs/cli`+`@makoojs/core`。如果你要扩展 Makoo 的运行时能力,再直接使用 `@makoojs/core`。
|
|
270
|
+
| 监听 signal | `createActivityStore`, `ActivitySignalSource` |
|
|
271
|
+
| 日志 | `Logger`, `ILogger`, `LoggerLevel` |
|
|
272
|
+
| 错误 | `MakooError`, `AdapterError`, `TaskError`, `ErrorCode` |
|