@makoojs/core 0.1.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.
Files changed (42) hide show
  1. package/LICENSE +21 -0
  2. package/README.CN.md +313 -0
  3. package/README.md +313 -0
  4. package/dist/Injector/Injector.d.ts +48 -0
  5. package/dist/Injector/types.d.ts +29 -0
  6. package/dist/Task/TaskContext.d.ts +105 -0
  7. package/dist/Task/TaskLifeCycle.d.ts +18 -0
  8. package/dist/Task/TaskRegister.d.ts +17 -0
  9. package/dist/Task/TaskRunner.d.ts +20 -0
  10. package/dist/Task/types.d.ts +78 -0
  11. package/dist/Task/util.d.ts +4 -0
  12. package/dist/adapter/Adapter.d.ts +6 -0
  13. package/dist/adapter/types.d.ts +49 -0
  14. package/dist/error/AdapterError.d.ts +4 -0
  15. package/dist/error/ErrorCode.d.ts +22 -0
  16. package/dist/error/MakooError.d.ts +10 -0
  17. package/dist/error/TaskError.d.ts +4 -0
  18. package/dist/hooks/ObserverHub.d.ts +21 -0
  19. package/dist/hooks/types.d.ts +27 -0
  20. package/dist/hooks/util.d.ts +6 -0
  21. package/dist/index.cjs +2 -0
  22. package/dist/index.d.ts +19 -0
  23. package/dist/index.js +1632 -0
  24. package/dist/logger/Logger.d.ts +14 -0
  25. package/dist/logger/types.d.ts +7 -0
  26. package/dist/payload/buildAliveObservePayload.d.ts +51 -0
  27. package/dist/payload/buildDomObservePayload.d.ts +46 -0
  28. package/dist/payload/buildInjectObservePayload.d.ts +54 -0
  29. package/dist/payload/buildListenerObservePayload.d.ts +44 -0
  30. package/dist/payload/buildObservePayload.d.ts +10 -0
  31. package/dist/payload/buildRegisterObservePayload.d.ts +44 -0
  32. package/dist/payload/buildResourceObservePayload.d.ts +45 -0
  33. package/dist/payload/buildRunObservePayload.d.ts +57 -0
  34. package/dist/payload/buildTaskObservePayload.d.ts +44 -0
  35. package/dist/payload/createDomObserveEmitFactory.d.ts +12 -0
  36. package/dist/signal/observeActivitySignal.d.ts +8 -0
  37. package/dist/signal/types.d.ts +15 -0
  38. package/dist/util/getArtifactName.d.ts +1 -0
  39. package/dist/util/uuid.d.ts +1 -0
  40. package/dist/watcher/DomWatcher.d.ts +13 -0
  41. package/dist/watcher/types.d.ts +15 -0
  42. package/package.json +33 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 FlowingInk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.CN.md ADDED
@@ -0,0 +1,313 @@
1
+ # @makoojs/core
2
+
3
+ `@makoojs/core` 是 Makoo 的框架无关运行时内核。它负责注入任务的注册与调度、等待目标 DOM、挂载 artifact、管理 alive 重注入、绑定事件监听器,并提供生命周期观察事件、日志和错误基础设施。
4
+
5
+ 项目应该从 `@makoojs/cli` 开始,它负责提供 Vite 插件、扫描 `injections` 目录、解析 manifest、生成userscript 入口代码,并把配置透传给 [lisonge/vite-plugin-monkey](https://github.com/lisonge/vite-plugin-monkey) 完成开发、构建和安装流程。`@makoojs/core` 是更底层的运行时包,只有在你想脱离 Makoo 的 CLI 流程,手动集成注入运行时时,才需要直接使用它。`@makoojs/vue` 和 `@makoojs/react` 则是组件挂载适配器,用来把 Vue 或 React 组件接入 `@makoojs/core` 的注入流程。
6
+
7
+ > [!NOTE]
8
+ > `@makoojs/core`包是其他包的父包,其他的包都直接或间接的依赖该包的类型或功能
9
+
10
+ ## 适用场景
11
+
12
+ - 编写自定义 `ResolvableMountAdapter`,让 Makoo 能挂载新的 artifact 类型。
13
+ - 直接创建 `Injector`,在没有 CLI 扫描和代码生成的环境中手动注册任务。
14
+ - 监听注入生命周期事件,用于调试、埋点、错误上报或可视化开发工具。
15
+ - 使用 `DOMWatcher`、`createActivityStore` 等底层工具构建更定制的运行时集成。
16
+
17
+ ## 安装
18
+
19
+ ```bash
20
+ // npm install @makoojs/core
21
+ // yarn add @makoojs/core
22
+ pnpm add @makoojs/core
23
+ ```
24
+
25
+ ## 最小运行时示例
26
+
27
+ 下面的示例展示了 core 的基本路径:创建 `Injector`,注册 adapter,注册 artifact,然后调用 `run()` 等待目标 DOM 并挂载。
28
+
29
+ ```ts
30
+ import { Injector, type ResolvableMountAdapter } 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 injector = new Injector({
59
+ alive: true,
60
+ scope: 'local',
61
+ timeout: 5000
62
+ }).applyAdapter(textAdapter);
63
+
64
+ injector.register('#app', {
65
+ kind: 'text',
66
+ text: 'Hello from Makoo core'
67
+ });
68
+
69
+ injector.run();
70
+ ```
71
+
72
+ ## Injector 基础
73
+
74
+ `Injector` 是 core 的主要入口。它把任务注册、DOM 等待、adapter 解析、挂载、事件绑定和生命周期控制组织在一起。
75
+
76
+ 常用方法:
77
+
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()` | 监听生命周期观察事件 |
88
+
89
+ `register()` 返回 `taskId`、`isSuccess`,以及当前任务的 `enableAlive()` / `disableAlive()` 快捷方法。
90
+
91
+ ## Adapter Contract
92
+
93
+ core 不关心 artifact 是 Vue 组件、React 组件还是其他对象。它只要求 adapter 实现统一的挂载协议。
94
+
95
+ ```ts
96
+ import type { ResolvableMountAdapter } from '@makoojs/core';
97
+
98
+ const adapter: ResolvableMountAdapter<MyArtifact, MyHandle, MyInstance> = {
99
+ name: 'my-adapter',
100
+ matches(artifact): artifact is MyArtifact {
101
+ return isMyArtifact(artifact);
102
+ },
103
+ mount(input) {
104
+ // input.host 是 Makoo 创建的宿主元素
105
+ // input.mountPoint 是 adapter 应该挂载内容的位置
106
+ // input.makoo 是当前任务的上下文能力
107
+ return {
108
+ handle,
109
+ instance
110
+ };
111
+ },
112
+ unmount(input) {
113
+ // 根据 input.reason 做清理
114
+ }
115
+ };
116
+ ```
117
+
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 模式
146
+
147
+ `alive` 用来处理目标 DOM 被页面重新渲染、删除后又出现的场景。开启后,Makoo 会观察已挂载节点的移除,并在目标选择器重新出现时尝试重新注入。
148
+
149
+ ```ts
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 事件监听任务。
172
+
173
+ ```ts
174
+ import { createActivityStore } from '@makoojs/core';
175
+
176
+ const enabled = createActivityStore(true);
177
+
178
+ injector.registerListener(
179
+ '#save',
180
+ 'click',
181
+ () => {
182
+ console.log('save clicked');
183
+ },
184
+ () => enabled
185
+ );
186
+
187
+ injector.run();
188
+
189
+ enabled.set(false);
190
+ enabled.set(true);
191
+ ```
192
+
193
+ `createActivityStore()` 返回一个简单的可订阅布尔状态。传入 listener 后,Makoo 会根据状态自动 attach 或 detach 事件监听器。
194
+
195
+ > [!NOTE]
196
+ > 现在`@makoojs/cli`是无法解析这种listener的挂载形式,只支持**`component+listener`**的形式
197
+
198
+ ## 观察事件
199
+
200
+ core 会在注册、运行、挂载、listener、alive、DOM watcher 和任务状态变化时发出观察事件。你可以通过 `on()`、`onTask()` 或 `onAny()` 监听它们。
201
+
202
+ ```ts
203
+ const off = injector.on('artifact:mountSuccess', (event) => {
204
+ console.log(event.taskId, event.injectAt);
205
+ });
206
+
207
+ injector.onAny((event, ctrl) => {
208
+ if (event.name === 'artifact:mountFail') {
209
+ ctrl.stopPropagation();
210
+ }
211
+ });
212
+
213
+ off();
214
+ ```
215
+
216
+ 常见事件包括:
217
+
218
+ - `register:start`
219
+ - `register:success`
220
+ - `run:start`
221
+ - `run:taskScheduled`
222
+ - `artifact:mountStart`
223
+ - `artifact:mountSuccess`
224
+ - `artifact:mountFail`
225
+ - `listener:attached`
226
+ - `alive:enabled`
227
+ - `alive:observerStarted`
228
+ - `task:statusChange`
229
+ - `dom:targetFound`
230
+ - `dom:targetTimeout`
231
+
232
+ 完整事件名可以从 `OBSERVE_EVENT_NAMES` 获取。
233
+
234
+ ## DOMWatcher
235
+
236
+ `DOMWatcher` 是 core 的底层 DOM 观察工具。通常你不需要直接使用它,因为 `Injector.run()` 和 alive 模式已经封装了目标等待和恢复逻辑。
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';
266
+
267
+ const logger: ILogger = {
268
+ info: console.info,
269
+ warn: console.warn,
270
+ error: console.error,
271
+ debug: console.debug
272
+ };
273
+
274
+ const injector = new Injector({ logger });
275
+ ```
276
+
277
+ core 还导出以下错误相关类型:
278
+
279
+ - `MakooError`
280
+ - `AdapterError`
281
+ - `TaskError`
282
+ - `ErrorCode`
283
+ - `MakooIssue`
284
+
285
+ 这些类型适合在自定义集成中做错误识别、日志归类或用户提示。
286
+
287
+ ## 公开能力概览
288
+
289
+ `@makoojs/core` 的主入口导出以下几类能力:
290
+
291
+ | 类别 | 代表导出 |
292
+ | --- | --- |
293
+ | 注入调度 | `Injector`、`InjectionConfig`、`ArtifactOptions` |
294
+ | Adapter 协议 | `MountAdapter`、`ResolvableMountAdapter`、`AdapterMountInput`、`AdapterUnmountInput`、`MakooContext` |
295
+ | 生命周期观察 | `ObserverHub`、`OBSERVE_EVENT_NAMES`、`ObserveEvent`、`ObserveHook`、`LifecycleHookMap` |
296
+ | DOM 观察 | `DOMWatcher` |
297
+ | Listener signal | `createActivityStore`、`ActivitySignalSource` |
298
+ | 日志 | `Logger`、`ILogger`、`LoggerLevel` |
299
+ | 错误 | `MakooError`、`AdapterError`、`TaskError`、`ErrorCode` |
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`。
package/README.md ADDED
@@ -0,0 +1,313 @@
1
+ # @makoojs/core
2
+
3
+ `@makoojs/core` is Makoo's framework-agnostic runtime core. It registers and schedules injection tasks, waits for target DOM nodes, mounts artifacts, manages alive reinjection, binds event listeners, and provides lifecycle observation events, logging, and error infrastructure.
4
+
5
+ Projects should usually start with `@makoojs/cli`. The CLI provides the Vite plugin, scans the `injections` directory, resolves manifests, generates userscript entry code, and passes configuration through to [lisonge/vite-plugin-monkey](https://github.com/lisonge/vite-plugin-monkey) for development, build, and install flows. `@makoojs/core` is the lower-level runtime package. You only need to use it directly when you want to leave Makoo's CLI flow and manually integrate the injection runtime. `@makoojs/vue` and `@makoojs/react` are component mounting adapters that connect Vue or React components to the `@makoojs/core` injection flow.
6
+
7
+ > [!NOTE]
8
+ > `@makoojs/core` is the parent package for the rest of Makoo. Other packages depend on its types or runtime capabilities either directly or indirectly.
9
+
10
+ ## Use Cases
11
+
12
+ - Write a custom `ResolvableMountAdapter` so Makoo can mount a new artifact type.
13
+ - Create an `Injector` directly and register tasks manually without CLI scanning or code generation.
14
+ - Listen to injection lifecycle events for debugging, analytics, error reporting, or visual devtools.
15
+ - Use low-level tools such as `DOMWatcher` and `createActivityStore` to build a more custom runtime integration.
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ // npm install @makoojs/core
21
+ // yarn add @makoojs/core
22
+ pnpm add @makoojs/core
23
+ ```
24
+
25
+ ## Minimal Runtime Example
26
+
27
+ The following example shows the basic core flow: create an `Injector`, register an adapter, register an artifact, then call `run()` to wait for the target DOM node and mount it.
28
+
29
+ ```ts
30
+ import { Injector, type ResolvableMountAdapter } 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 injector = new Injector({
59
+ alive: true,
60
+ scope: 'local',
61
+ timeout: 5000
62
+ }).applyAdapter(textAdapter);
63
+
64
+ injector.register('#app', {
65
+ kind: 'text',
66
+ text: 'Hello from Makoo core'
67
+ });
68
+
69
+ injector.run();
70
+ ```
71
+
72
+ ## Injector Basics
73
+
74
+ `Injector` is core's main entry point. It brings together task registration, DOM waiting, adapter resolution, mounting, event binding, and lifecycle control.
75
+
76
+ Common methods:
77
+
78
+ | Method | Description |
79
+ | --- | --- |
80
+ | `applyAdapter(adapter)` | Registers a mount adapter that can resolve artifacts |
81
+ | `register(injectAt, artifact, options?)` | Registers a component or other artifact injection task |
82
+ | `registerListener(listenAt, event, callback, activitySignal?)` | Registers a standalone DOM event listener task |
83
+ | `run()` | Starts scheduling registered tasks |
84
+ | `enableAlive(taskId)` / `disableAlive(taskId)` | Enables or disables alive reinjection for a component task |
85
+ | `reset(taskId)` / `destroy(taskId)` | Resets or destroys a single task |
86
+ | `resetAll()` / `destroyAll()` | Resets or destroys all tasks |
87
+ | `on()` / `onTask()` / `onAny()` | Listens to lifecycle observation events |
88
+
89
+ `register()` returns `taskId`, `isSuccess`, and shortcut methods for `enableAlive()` / `disableAlive()` on the current task.
90
+
91
+ ## Adapter Contract
92
+
93
+ core does not care whether an artifact is a Vue component, a React component, or another object. It only requires adapters to implement a unified mounting protocol.
94
+
95
+ ```ts
96
+ import type { ResolvableMountAdapter } from '@makoojs/core';
97
+
98
+ const adapter: ResolvableMountAdapter<MyArtifact, MyHandle, MyInstance> = {
99
+ name: 'my-adapter',
100
+ matches(artifact): artifact is MyArtifact {
101
+ return isMyArtifact(artifact);
102
+ },
103
+ mount(input) {
104
+ // input.host is the host element created by Makoo
105
+ // input.mountPoint is where the adapter should mount content
106
+ // input.makoo contains task-scoped runtime capabilities
107
+ return {
108
+ handle,
109
+ instance
110
+ };
111
+ },
112
+ unmount(input) {
113
+ // Clean up according to input.reason
114
+ }
115
+ };
116
+ ```
117
+
118
+ `mount(input)` receives:
119
+
120
+ | Field | Description |
121
+ | --- | --- |
122
+ | `host` | The host element created and inserted by Makoo |
123
+ | `mountPoint` | The node where the adapter should mount content |
124
+ | `artifact` | The currently registered artifact |
125
+ | `taskId` | Current task ID |
126
+ | `injectAt` | Target selector for the current task |
127
+ | `makoo` | Task-scoped runtime capabilities |
128
+
129
+ The `makoo` context lets adapter internals control the current task:
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
+ ## Lifecycle And Alive Mode
146
+
147
+ `alive` handles cases where the target DOM is re-rendered, removed, and later restored. When enabled, Makoo observes removal of the mounted node and tries to reinject when the target selector appears again.
148
+
149
+ ```ts
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` supports:
161
+
162
+ | Value | Description |
163
+ | --- | --- |
164
+ | `local` | Observes DOM changes near the current mount area |
165
+ | `global` | Observes DOM changes across the whole document |
166
+
167
+ `alive` only applies to component or artifact injection tasks. Standalone listener tasks do not use the alive reinjection mechanism.
168
+
169
+ ## Listener And Activity Signal
170
+
171
+ Besides mounting artifacts, `Injector` can also register standalone DOM event listener tasks.
172
+
173
+ ```ts
174
+ import { createActivityStore } from '@makoojs/core';
175
+
176
+ const enabled = createActivityStore(true);
177
+
178
+ injector.registerListener(
179
+ '#save',
180
+ 'click',
181
+ () => {
182
+ console.log('save clicked');
183
+ },
184
+ () => enabled
185
+ );
186
+
187
+ injector.run();
188
+
189
+ enabled.set(false);
190
+ enabled.set(true);
191
+ ```
192
+
193
+ `createActivityStore()` returns a small subscribable boolean state. When passed to a listener, Makoo automatically attaches or detaches the event listener based on that state.
194
+
195
+ > [!NOTE]
196
+ > `@makoojs/cli` cannot parse this standalone listener mounting form yet. It only supports the **`component + listener`** form.
197
+
198
+ ## Observation Events
199
+
200
+ core emits observation events during registration, running, mounting, listener work, alive mode, DOM watching, and task status changes. You can listen to them with `on()`, `onTask()`, or `onAny()`.
201
+
202
+ ```ts
203
+ const off = injector.on('artifact:mountSuccess', (event) => {
204
+ console.log(event.taskId, event.injectAt);
205
+ });
206
+
207
+ injector.onAny((event, ctrl) => {
208
+ if (event.name === 'artifact:mountFail') {
209
+ ctrl.stopPropagation();
210
+ }
211
+ });
212
+
213
+ off();
214
+ ```
215
+
216
+ Common events include:
217
+
218
+ - `register:start`
219
+ - `register:success`
220
+ - `run:start`
221
+ - `run:taskScheduled`
222
+ - `artifact:mountStart`
223
+ - `artifact:mountSuccess`
224
+ - `artifact:mountFail`
225
+ - `listener:attached`
226
+ - `alive:enabled`
227
+ - `alive:observerStarted`
228
+ - `task:statusChange`
229
+ - `dom:targetFound`
230
+ - `dom:targetTimeout`
231
+
232
+ The full event name list is available from `OBSERVE_EVENT_NAMES`.
233
+
234
+ ## DOMWatcher
235
+
236
+ `DOMWatcher` is core's low-level DOM observation utility. You usually do not need to use it directly because `Injector.run()` and alive mode already wrap target waiting and restoration.
237
+
238
+ In a custom runtime, you can use it directly:
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()` can observe removal and restoration of an existing node, mainly for implementing reinjection behavior.
259
+
260
+ ## Logging And Errors
261
+
262
+ core uses `Logger` by default and prints logs with the `[Makoo]` prefix. You can pass a custom logger.
263
+
264
+ ```ts
265
+ import { Injector, type ILogger } from '@makoojs/core';
266
+
267
+ const logger: ILogger = {
268
+ info: console.info,
269
+ warn: console.warn,
270
+ error: console.error,
271
+ debug: console.debug
272
+ };
273
+
274
+ const injector = new Injector({ logger });
275
+ ```
276
+
277
+ core also exports these error-related types:
278
+
279
+ - `MakooError`
280
+ - `AdapterError`
281
+ - `TaskError`
282
+ - `ErrorCode`
283
+ - `MakooIssue`
284
+
285
+ These types are useful for error identification, log grouping, or user-facing messages in custom integrations.
286
+
287
+ ## Public Exports Overview
288
+
289
+ The main `@makoojs/core` entry exports these groups of capabilities:
290
+
291
+ | Category | Representative exports |
292
+ | --- | --- |
293
+ | Injection scheduling | `Injector`, `InjectionConfig`, `ArtifactOptions` |
294
+ | Adapter protocol | `MountAdapter`, `ResolvableMountAdapter`, `AdapterMountInput`, `AdapterUnmountInput`, `MakooContext` |
295
+ | Lifecycle observation | `ObserverHub`, `OBSERVE_EVENT_NAMES`, `ObserveEvent`, `ObserveHook`, `LifecycleHookMap` |
296
+ | DOM observation | `DOMWatcher` |
297
+ | Listener signal | `createActivityStore`, `ActivitySignalSource` |
298
+ | Logging | `Logger`, `ILogger`, `LoggerLevel` |
299
+ | Errors | `MakooError`, `AdapterError`, `TaskError`, `ErrorCode` |
300
+
301
+ The full API reference will be moved to a dedicated documentation site later.
302
+
303
+ ## Relationship To Other Packages
304
+
305
+ | Package | Responsibility |
306
+ | --- | --- |
307
+ | `@makoojs/core` | Framework-agnostic injection runtime core |
308
+ | `@makoojs/vue` | Vue adapter and Vue plugin registration helpers |
309
+ | `@makoojs/react` | React adapter |
310
+ | `@makoojs/cli` | Vite plugin, config resolution, scanning, code generation, and userscript build integration |
311
+ | `@makoojs/create-makoo` | Project scaffold |
312
+
313
+ If you are building a regular userscript project, prefer `@makoojs/cli` + `@makoojs/core`. If you want to extend Makoo's runtime capabilities, use `@makoojs/core` directly.
@@ -0,0 +1,48 @@
1
+ import { ResolvableMountAdapter } from '../adapter/types';
2
+ import { ObserverHub } from '../hooks/ObserverHub';
3
+ import { ObserveEventName, ObserveHook } from '../hooks/types';
4
+ import { ILogger } from '../logger/types';
5
+ import { ActivitySignalSource } from '../signal/types';
6
+ import { TaskContext } from '../Task/TaskContext';
7
+ import { ListenerRegisterResult, RegisterResult, TaskActivitySignal } from '../Task/types';
8
+ import { ActionEvent, ArtifactOptions, InjectionConfig } from './types';
9
+ export declare class Injector {
10
+ private readonly taskContext;
11
+ private readonly taskRegister;
12
+ private readonly taskRunner;
13
+ private readonly taskLifeCycle;
14
+ private readonly logger;
15
+ private readonly observer;
16
+ private readonly adapterRegistry;
17
+ private readonly injectConfig;
18
+ constructor(config?: Partial<InjectionConfig>);
19
+ private createMakooContext;
20
+ applyAdapter(adapter: ResolvableMountAdapter): this;
21
+ run(): void;
22
+ registerListener(listenAt: string, event: string, callback: EventListener, activitySignal?: TaskActivitySignal): ListenerRegisterResult;
23
+ register<TArtifact>(injectAt: string, artifact: TArtifact, option?: ArtifactOptions): RegisterResult;
24
+ enableAlive(taskId: string): void;
25
+ disableAlive(taskId: string): void;
26
+ /**
27
+ * @internal
28
+ * Obtain the original execution context of the task.
29
+ * * @warning
30
+ * This API is an internal implementation, extremely unstable, and subject to frequent changes due to refactoring.
31
+ * Direct manipulation of the context may bypass the framework's state management and lead to memory leaks or race conditions.
32
+ */
33
+ getContext(): TaskContext | undefined;
34
+ getObserver(): ObserverHub;
35
+ on(event: ObserveEventName, hook: ObserveHook): () => void;
36
+ onTask(taskId: string, event: ObserveEventName, hook: ObserveHook): () => void;
37
+ onAny(hook: ObserveHook): () => void;
38
+ off(event: ObserveEventName, hook?: ObserveHook): void;
39
+ offTask(taskId: string, event?: ObserveEventName, hook?: ObserveHook): void;
40
+ offAny(hook: ObserveHook): void;
41
+ getLogger(): ILogger;
42
+ reset(taskId: string): void;
43
+ resetAll(): void;
44
+ destroy(taskId: string): void;
45
+ destroyAll(): void;
46
+ bindListenerSignal(taskId: string, source: ActivitySignalSource<boolean>): boolean;
47
+ controlListener(taskId: string, event: ActionEvent): boolean;
48
+ }