@makoojs/core 0.3.0 → 0.4.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 +208 -634
- package/README.md +210 -630
- package/dist/Makoo/types.d.ts +2 -0
- package/dist/Task/TaskRegister.d.ts +1 -0
- package/dist/Task/types.d.ts +2 -0
- package/dist/error/ErrorCode.d.ts +4 -18
- package/dist/error/MakooError.d.ts +5 -0
- package/dist/error/formatMakooError.d.ts +2 -0
- package/dist/index.cjs +5 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +637 -598
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,709 +1,289 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @makoojs/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`@makoojs/core` is Makoo's framework-agnostic runtime core. It declares injection tasks, starts task batches, waits for target DOM nodes, mounts artifacts, manages alive reinjection, binds event listeners, and provides lifecycle observation, logging, and error infrastructure.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
> [!NOTE]
|
|
6
|
+
> `@makoojs/core` provides Makoo's public runtime protocol. Other Makoo packages use its types and capabilities directly or indirectly.
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
- [`inject()`](#inject): declares a component task
|
|
9
|
-
- [`listen()`](#listen): declares a listener task
|
|
8
|
+
## Use Cases
|
|
10
9
|
|
|
11
|
-
|
|
10
|
+
- Write a custom `ResolvableMountAdapter` so Makoo can mount a new artifact type.
|
|
11
|
+
- Create a Makoo runtime with `createMakoo()` and start explicit task declarations.
|
|
12
|
+
- Listen to injection lifecycle events for debugging, analytics, error reporting, or visual devtools.
|
|
13
|
+
- Use low-level tools such as `DOMWatcher` and `createActivityStore` for custom integrations.
|
|
12
14
|
|
|
13
|
-
|
|
14
|
-
- [`createActivityStore()`](#createactivitystore): creates subscribable state
|
|
15
|
-
- [`DOMWatcher`](#domwatcher): observes DOM targets
|
|
15
|
+
## Installation
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
- [Constants](#constants): actions, event names, and error codes
|
|
22
|
-
|
|
23
|
-
### TypeScript types
|
|
24
|
-
|
|
25
|
-
- [Adapter types](#adapter-types)
|
|
26
|
-
- [Task types](#task-types)
|
|
27
|
-
- [Observer types](#observer-types)
|
|
28
|
-
- [Signal types](#signal-types)
|
|
29
|
-
- [Logger types](#logger-types)
|
|
30
|
-
- [Error types](#error-types)
|
|
31
|
-
|
|
32
|
-
## `createMakoo()`
|
|
33
|
-
|
|
34
|
-
Creates a Makoo runtime.
|
|
35
|
-
|
|
36
|
-
### Type
|
|
37
|
-
|
|
38
|
-
```ts
|
|
39
|
-
function createMakoo(options?: CreateMakooOptions): MakooRuntime;
|
|
17
|
+
```bash
|
|
18
|
+
# npm install @makoojs/core
|
|
19
|
+
# yarn add @makoojs/core
|
|
20
|
+
pnpm add @makoojs/core
|
|
40
21
|
```
|
|
41
22
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
`options` uses `CreateMakooOptions`:
|
|
45
|
-
|
|
46
|
-
| Field | Type | Default | Description |
|
|
47
|
-
| --- | --- | --- | --- |
|
|
48
|
-
| `defaults` | `Partial<MakooDefaults>` | `{}` | Sets the default injection options |
|
|
49
|
-
| `adapters` | `ResolvableMountAdapter[]` | `[]` | Registers adapters that can mount artifacts |
|
|
50
|
-
| `hooks` | `LifecycleHookMap` | `undefined` | Registers global lifecycle hooks |
|
|
51
|
-
| `logger` | `ILogger` | `new Logger()` | Sets the logger implementation |
|
|
52
|
-
| `observer` | `ObserverHub` | `createObserverHub(logger)` | Sets the lifecycle event hub |
|
|
53
|
-
|
|
54
|
-
### Details
|
|
55
|
-
|
|
56
|
-
The final default values of `defaults` are:
|
|
23
|
+
## Minimal Runtime Example
|
|
57
24
|
|
|
58
25
|
```ts
|
|
59
|
-
{
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
### Returns
|
|
67
|
-
|
|
68
|
-
Returns `MakooRuntime`:
|
|
26
|
+
import {
|
|
27
|
+
createMakoo,
|
|
28
|
+
inject,
|
|
29
|
+
type ResolvableMountAdapter
|
|
30
|
+
} from '@makoojs/core';
|
|
69
31
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
| `destroy(taskId)` | `void` | Destroys and removes a task |
|
|
75
|
-
| `resetAll()` | `void` | Resets every task in the runtime |
|
|
76
|
-
| `destroyAll()` | `void` | Destroys every task in the runtime |
|
|
77
|
-
| `enableAlive(taskId)` | `void` | Enables alive observation for a component task |
|
|
78
|
-
| `disableAlive(taskId)` | `void` | Disables alive observation for a component task |
|
|
79
|
-
| `on(event, hook)` | `() => void` | Subscribes to an event and returns an unsubscribe function |
|
|
80
|
-
| `onTask(taskId, event, hook)` | `() => void` | Subscribes to an event for one task and returns an unsubscribe function |
|
|
81
|
-
| `onAny(hook)` | `() => void` | Subscribes to all events and returns an unsubscribe function |
|
|
82
|
-
| `off(event, hook?)` | `void` | Removes one or all hooks for an event |
|
|
83
|
-
| `offTask(taskId, event?, hook?)` | `void` | Removes task hooks |
|
|
84
|
-
| `offAny(hook)` | `void` | Removes a hook registered with `onAny()` |
|
|
85
|
-
| `getLogger()` | `ILogger` | Returns the current logger |
|
|
86
|
-
|
|
87
|
-
Duplicate task IDs are skipped and are not included in the `StartedTasks.tasks` returned by that `start()` call.
|
|
32
|
+
type TextArtifact = {
|
|
33
|
+
kind: 'text';
|
|
34
|
+
text: string;
|
|
35
|
+
};
|
|
88
36
|
|
|
89
|
-
|
|
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
|
+
};
|
|
90
57
|
|
|
91
|
-
```ts
|
|
92
58
|
const makoo = createMakoo({
|
|
93
|
-
|
|
59
|
+
defaults: {
|
|
60
|
+
alive: true,
|
|
61
|
+
scope: 'local',
|
|
62
|
+
timeout: 5000
|
|
63
|
+
},
|
|
64
|
+
adapters: [textAdapter]
|
|
94
65
|
});
|
|
95
66
|
|
|
96
|
-
|
|
97
|
-
inject({
|
|
67
|
+
makoo.start([
|
|
68
|
+
inject({
|
|
69
|
+
id: 'text',
|
|
70
|
+
injectAt: '#app',
|
|
71
|
+
artifact: {
|
|
72
|
+
kind: 'text',
|
|
73
|
+
text: 'Hello from Makoo core'
|
|
74
|
+
}
|
|
75
|
+
})
|
|
98
76
|
]);
|
|
99
77
|
```
|
|
100
78
|
|
|
101
|
-
##
|
|
102
|
-
|
|
103
|
-
Creates a component task declaration. `inject()` does not register or start the task; pass its result to `MakooRuntime.start()`.
|
|
104
|
-
|
|
105
|
-
### Type
|
|
106
|
-
|
|
107
|
-
```ts
|
|
108
|
-
function inject<TArtifact>(
|
|
109
|
-
input: MakooInjectionInput<TArtifact>
|
|
110
|
-
): MakooInjectionDeclaration<TArtifact>;
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
### Parameters
|
|
79
|
+
## Runtime Basics
|
|
114
80
|
|
|
115
|
-
|
|
116
|
-
type MakooInjectionInput<TArtifact = unknown> = {
|
|
117
|
-
id?: string;
|
|
118
|
-
injectAt: string;
|
|
119
|
-
artifact: TArtifact;
|
|
120
|
-
options?: ArtifactOptions;
|
|
121
|
-
};
|
|
122
|
-
```
|
|
81
|
+
`inject()` and `listen()` declare tasks. They do not touch the DOM or register tasks by themselves. `makoo.start([...])` registers the declarations in the provided batch and immediately schedules those tasks.
|
|
123
82
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
#### `ArtifactOptions`
|
|
127
|
-
|
|
128
|
-
| Field | Type | Description |
|
|
129
|
-
| --- | --- | --- |
|
|
130
|
-
| `alive` | `boolean` | Attempts to mount again after the target is replaced |
|
|
131
|
-
| `scope` | `'local' \| 'global'` | Observation scope used by alive mode |
|
|
132
|
-
| `timeout` | `number` | Milliseconds to wait for the target element |
|
|
133
|
-
| `on` | `MakooListenerDeclaration` | Event listener registered with the component |
|
|
134
|
-
| `hooks` | `LifecycleHookMap` | Lifecycle hooks for this task |
|
|
135
|
-
|
|
136
|
-
### Returns
|
|
137
|
-
|
|
138
|
-
Returns `MakooInjectionDeclaration<TArtifact>`.
|
|
139
|
-
|
|
140
|
-
### Example
|
|
83
|
+
`inject()` accepts an object containing the target, artifact, and task options. Its optional `id` is used as the task ID, which is useful when you want to look up or control that task later:
|
|
141
84
|
|
|
142
85
|
```ts
|
|
143
|
-
|
|
86
|
+
inject({
|
|
144
87
|
id: 'settings-panel',
|
|
145
88
|
injectAt: '#settings',
|
|
146
|
-
artifact:
|
|
89
|
+
artifact: settingsArtifact,
|
|
147
90
|
options: {
|
|
148
|
-
alive: true
|
|
149
|
-
scope: 'global'
|
|
91
|
+
alive: true
|
|
150
92
|
}
|
|
151
93
|
});
|
|
152
94
|
```
|
|
153
95
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
Creates a listener task declaration. `listen()` does not register or start the task. Pass its result to `MakooRuntime.start()` or use it as `ArtifactOptions.on`.
|
|
157
|
-
|
|
158
|
-
### Type
|
|
159
|
-
|
|
160
|
-
```ts
|
|
161
|
-
function listen(input: MakooListenerInput): MakooListenerDeclaration;
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
### Parameters
|
|
165
|
-
|
|
166
|
-
```ts
|
|
167
|
-
type MakooListenerInput = {
|
|
168
|
-
id?: string;
|
|
169
|
-
listenAt: string;
|
|
170
|
-
type: string;
|
|
171
|
-
callback: EventListener;
|
|
172
|
-
activitySignal?: () => ActivitySignalSource<boolean>;
|
|
173
|
-
};
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
When `id` is omitted, the runtime uses `listener-${listenAt}-${type}` as the task ID.
|
|
177
|
-
|
|
178
|
-
### Returns
|
|
179
|
-
|
|
180
|
-
Returns `MakooListenerDeclaration`. It can be started as a standalone task or assigned to `ArtifactOptions.on`.
|
|
181
|
-
|
|
182
|
-
### Example
|
|
183
|
-
|
|
184
|
-
```ts
|
|
185
|
-
const declaration = listen({
|
|
186
|
-
id: 'escape-close',
|
|
187
|
-
listenAt: 'body',
|
|
188
|
-
type: 'keydown',
|
|
189
|
-
callback: onEscape
|
|
190
|
-
});
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
## `createObserverHub()`
|
|
194
|
-
|
|
195
|
-
Creates an event hub. `logger` defaults to `new Logger()`.
|
|
196
|
-
|
|
197
|
-
### Type
|
|
198
|
-
|
|
199
|
-
```ts
|
|
200
|
-
function createObserverHub(logger?: ILogger): ObserverHub;
|
|
201
|
-
```
|
|
202
|
-
|
|
203
|
-
### Parameters
|
|
204
|
-
|
|
205
|
-
- `logger`: optional `ILogger`. A `Logger` is created when omitted.
|
|
206
|
-
|
|
207
|
-
### Returns
|
|
208
|
-
|
|
209
|
-
Returns `ObserverHub`:
|
|
210
|
-
|
|
211
|
-
| Method | Returns | Description |
|
|
212
|
-
| --- | --- | --- |
|
|
213
|
-
| `on(event, hook)` | `() => void` | Registers a hook for an event |
|
|
214
|
-
| `onTask(taskId, event, hook)` | `() => void` | Registers a task-specific hook |
|
|
215
|
-
| `onAny(hook)` | `() => void` | Registers a hook for every event |
|
|
216
|
-
| `off(event, hook?)` | `void` | Removes event hooks |
|
|
217
|
-
| `offTask(taskId, event?, hook?)` | `void` | Removes task hooks |
|
|
218
|
-
| `offAny(hook)` | `void` | Removes a global hook |
|
|
219
|
-
| `clear()` | `void` | Removes all hooks |
|
|
220
|
-
| `hasHooks(event?)` | `boolean` | Checks whether any or a specific event has hooks |
|
|
221
|
-
| `emit(event)` | `void` | Emits an event |
|
|
222
|
-
| `emitOnTask(taskId, event)` | `void` | Emits an event for a task |
|
|
223
|
-
|
|
224
|
-
Task events run task hooks, same-name event hooks, and `onAny()` hooks in that order. `stopPropagation()` prevents the next group from running. `stopImmediatePropagation()` also stops the remaining hooks in the current group. If a hook throws, the error is logged and the other hooks continue.
|
|
225
|
-
|
|
226
|
-
### Example
|
|
227
|
-
|
|
228
|
-
```ts
|
|
229
|
-
const observer = createObserverHub();
|
|
230
|
-
const off = observer.on('artifact:mountSuccess', (event) => {
|
|
231
|
-
console.log(event.taskId);
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
off();
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
## `createActivityStore()`
|
|
238
|
-
|
|
239
|
-
Creates readable, subscribable, and writable state.
|
|
240
|
-
|
|
241
|
-
### Type
|
|
242
|
-
|
|
243
|
-
```ts
|
|
244
|
-
function createActivityStore<T>(initialValue: T): {
|
|
245
|
-
get(): T;
|
|
246
|
-
subscribe(listener: (value: T) => void): SignalUnsubscribe;
|
|
247
|
-
set(value: T): void;
|
|
248
|
-
update(updater: (value: T) => T): void;
|
|
249
|
-
};
|
|
250
|
-
```
|
|
251
|
-
|
|
252
|
-
### Parameters
|
|
253
|
-
|
|
254
|
-
- `initialValue`: the initial state value.
|
|
255
|
-
|
|
256
|
-
### Returns
|
|
257
|
-
|
|
258
|
-
Returns an object with `get()`, `subscribe()`, `set()`, and `update()`.
|
|
259
|
-
|
|
260
|
-
### Details
|
|
261
|
-
|
|
262
|
-
`set()` and `update()` compare the new and current values with `Object.is()` and do not notify subscribers when the value is unchanged.
|
|
263
|
-
|
|
264
|
-
### Example
|
|
265
|
-
|
|
266
|
-
```ts
|
|
267
|
-
const active = createActivityStore(true);
|
|
268
|
-
const unsubscribe = active.subscribe((value) => {
|
|
269
|
-
console.log(value);
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
active.set(false);
|
|
273
|
-
active.update((value) => !value);
|
|
274
|
-
unsubscribe();
|
|
275
|
-
```
|
|
276
|
-
|
|
277
|
-
## `DOMWatcher`
|
|
278
|
-
|
|
279
|
-
### `DOMWatcher.onDomReady()`
|
|
280
|
-
|
|
281
|
-
Finds an existing matching element and observes subsequently added elements with `MutationObserver`.
|
|
282
|
-
|
|
283
|
-
#### Type
|
|
96
|
+
When `id` is omitted, Makoo infers a task id from the artifact and target selector.
|
|
284
97
|
|
|
285
98
|
```ts
|
|
286
|
-
|
|
287
|
-
selector: string,
|
|
288
|
-
callback: (element: HTMLElement, observer?: MutationObserver) => void,
|
|
289
|
-
root: Document | HTMLElement = document,
|
|
290
|
-
options: { once: boolean; timeout?: number } | { once?: boolean; timeout: number },
|
|
291
|
-
runtime?: {
|
|
292
|
-
logger: ILogger;
|
|
293
|
-
emit: (name:
|
|
294
|
-
| 'dom:targetFound'
|
|
295
|
-
| 'dom:targetTimeout'
|
|
296
|
-
| 'dom:targetRemoved'
|
|
297
|
-
| 'dom:targetRestored'
|
|
298
|
-
) => void;
|
|
299
|
-
}
|
|
300
|
-
): () => void;
|
|
301
|
-
```
|
|
99
|
+
import { createMakoo, inject, listen } from '@makoojs/core';
|
|
302
100
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
#### Type
|
|
315
|
-
|
|
316
|
-
```ts
|
|
317
|
-
DOMWatcher.onDomAlive(
|
|
318
|
-
target: HTMLElement,
|
|
319
|
-
selector: string,
|
|
320
|
-
onRemove: () => void,
|
|
321
|
-
onRestore: (element: HTMLElement, observer?: MutationObserver) => void,
|
|
322
|
-
root: Document | HTMLElement = document,
|
|
323
|
-
options: { once: boolean; timeout?: number } | { once?: boolean; timeout: number },
|
|
324
|
-
runtime?: {
|
|
325
|
-
logger: ILogger;
|
|
326
|
-
emit: (name:
|
|
327
|
-
| 'dom:targetFound'
|
|
328
|
-
| 'dom:targetTimeout'
|
|
329
|
-
| 'dom:targetRemoved'
|
|
330
|
-
| 'dom:targetRestored'
|
|
331
|
-
) => void;
|
|
101
|
+
const makoo = createMakoo({
|
|
102
|
+
defaults: {
|
|
103
|
+
alive: false,
|
|
104
|
+
scope: 'local',
|
|
105
|
+
timeout: 5000
|
|
106
|
+
},
|
|
107
|
+
adapters: [myAdapter],
|
|
108
|
+
hooks: {
|
|
109
|
+
'start:requested': (event) => {
|
|
110
|
+
console.log(event.name);
|
|
111
|
+
}
|
|
332
112
|
}
|
|
333
|
-
)
|
|
334
|
-
```
|
|
335
|
-
|
|
336
|
-
#### Returns
|
|
337
|
-
|
|
338
|
-
Returns a function that stops both removal and restoration observation.
|
|
339
|
-
|
|
340
|
-
## `Logger`
|
|
341
|
-
|
|
342
|
-
Makoo's default logger implementation.
|
|
343
|
-
|
|
344
|
-
### Type
|
|
345
|
-
|
|
346
|
-
```ts
|
|
347
|
-
class Logger implements ILogger {
|
|
348
|
-
static readonly PREFIX = '[Makoo]';
|
|
349
|
-
|
|
350
|
-
constructor(level?: LoggerLevel);
|
|
351
|
-
setLevel(level: LoggerLevel): void;
|
|
352
|
-
getLevel(): LoggerLevel;
|
|
353
|
-
log(level: LoggerLevel, message: string, ...args: unknown[]): void;
|
|
354
|
-
debug(message: string, ...args: unknown[]): void;
|
|
355
|
-
info(message: string, ...args: unknown[]): void;
|
|
356
|
-
warn(message: string, ...args: unknown[]): void;
|
|
357
|
-
error(message: string, ...args: unknown[]): void;
|
|
358
|
-
}
|
|
359
|
-
```
|
|
360
|
-
|
|
361
|
-
The default level is `info`. Level order is `debug`, `info`, `warn`, and `error`; messages below the current level are not emitted.
|
|
362
|
-
|
|
363
|
-
## Error classes
|
|
364
|
-
|
|
365
|
-
### `MakooError`
|
|
366
|
-
|
|
367
|
-
```ts
|
|
368
|
-
class MakooError extends Error {
|
|
369
|
-
readonly code: string;
|
|
370
|
-
readonly issues: MakooIssue[];
|
|
371
|
-
override readonly cause?: Error;
|
|
372
|
-
|
|
373
|
-
constructor(
|
|
374
|
-
message: string,
|
|
375
|
-
issues?: MakooIssue[],
|
|
376
|
-
code?: string,
|
|
377
|
-
cause?: Error
|
|
378
|
-
);
|
|
379
|
-
}
|
|
380
|
-
```
|
|
381
|
-
|
|
382
|
-
`message` receives a `[makoo]` prefix and includes the `issues` and `cause` error chain.
|
|
383
|
-
|
|
384
|
-
### `AdapterError`
|
|
385
|
-
|
|
386
|
-
```ts
|
|
387
|
-
class AdapterError extends MakooError;
|
|
388
|
-
```
|
|
389
|
-
|
|
390
|
-
Default code: `ErrorCode.ADAPTER_NOT_FOUND`.
|
|
391
|
-
|
|
392
|
-
### `SignalError`
|
|
393
|
-
|
|
394
|
-
```ts
|
|
395
|
-
class SignalError extends MakooError;
|
|
396
|
-
```
|
|
397
|
-
|
|
398
|
-
Default code: `ErrorCode.TASK_SIGNAL_INVALID`.
|
|
399
|
-
|
|
400
|
-
### `TaskError`
|
|
401
|
-
|
|
402
|
-
```ts
|
|
403
|
-
class TaskError extends MakooError;
|
|
404
|
-
```
|
|
405
|
-
|
|
406
|
-
Default code: `ErrorCode.TASK_NO_REGISTERED`.
|
|
407
|
-
|
|
408
|
-
## Constants
|
|
409
|
-
|
|
410
|
-
### `Action`
|
|
411
|
-
|
|
412
|
-
```ts
|
|
413
|
-
enum Action {
|
|
414
|
-
OPEN = 'OPEN',
|
|
415
|
-
CLOSE = 'CLOSE'
|
|
416
|
-
}
|
|
113
|
+
});
|
|
417
114
|
|
|
418
|
-
|
|
115
|
+
const started = makoo.start([
|
|
116
|
+
inject({
|
|
117
|
+
id: 'toolbar',
|
|
118
|
+
injectAt: '#toolbar',
|
|
119
|
+
artifact: toolbarArtifact,
|
|
120
|
+
options: {
|
|
121
|
+
alive: true
|
|
122
|
+
}
|
|
123
|
+
}),
|
|
124
|
+
inject({
|
|
125
|
+
id: 'settings-panel',
|
|
126
|
+
injectAt: '#settings',
|
|
127
|
+
artifact: settingsArtifact,
|
|
128
|
+
options: {
|
|
129
|
+
alive: true
|
|
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
|
+
})
|
|
152
|
+
]);
|
|
419
153
|
```
|
|
420
154
|
|
|
421
|
-
|
|
155
|
+
`start()` returns `StartedTasks` for batch-scoped control:
|
|
422
156
|
|
|
423
157
|
```ts
|
|
424
|
-
const
|
|
425
|
-
'register:start',
|
|
426
|
-
'register:success',
|
|
427
|
-
'register:duplicate',
|
|
428
|
-
'register:error',
|
|
429
|
-
'start:requested',
|
|
430
|
-
'start:taskScheduled',
|
|
431
|
-
'start:taskSkipped',
|
|
432
|
-
'artifact:mountStart',
|
|
433
|
-
'artifact:mountSuccess',
|
|
434
|
-
'artifact:mountFail',
|
|
435
|
-
'listener:attached',
|
|
436
|
-
'listener:detached',
|
|
437
|
-
'listener:attachFail',
|
|
438
|
-
'alive:enabled',
|
|
439
|
-
'alive:disabled',
|
|
440
|
-
'alive:observerStarted',
|
|
441
|
-
'alive:observerStopped',
|
|
442
|
-
'task:targetReady',
|
|
443
|
-
'task:statusChange',
|
|
444
|
-
'task:beforeReset',
|
|
445
|
-
'task:afterReset',
|
|
446
|
-
'task:beforeDestroy',
|
|
447
|
-
'task:afterDestroy',
|
|
448
|
-
'signal:watcherReleased',
|
|
449
|
-
'resource:listenerReleased',
|
|
450
|
-
'artifact:unmounted',
|
|
451
|
-
'dom:targetFound',
|
|
452
|
-
'dom:targetTimeout',
|
|
453
|
-
'dom:targetRemoved',
|
|
454
|
-
'dom:targetRestored'
|
|
455
|
-
] as const;
|
|
456
|
-
```
|
|
457
|
-
|
|
458
|
-
### `ErrorCode`
|
|
158
|
+
const toolbar = started.get('toolbar');
|
|
459
159
|
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
| `ADAPTER_NOT_FOUND` | `MAKOO_ADAPTER_NOT_FOUND` |
|
|
464
|
-
| `ADAPTER_MOUNT_FAIL` | `MAKOO_ADAPTER_MOUNT_FAIL` |
|
|
465
|
-
| `ADAPTER_UNMOUNT_FAIL` | `MAKOO_ADAPTER_UNMOUNT_FAIL` |
|
|
466
|
-
| `TASK_NO_REGISTERED` | `MAKOO_TASK_NO_REGISTERED` |
|
|
467
|
-
| `TASK_NOT_FOUND` | `MAKOO_TASK_NOT_FOUND` |
|
|
468
|
-
| `TASK_INJECT_FAIL` | `MAKOO_TASK_INJECT_FAIL` |
|
|
469
|
-
| `TASK_ALREADY_MOUNTED` | `MAKOO_TASK_ALREADY_MOUNTED` |
|
|
470
|
-
| `TASK_TARGET_DETACHED` | `MAKOO_TASK_TARGET_DETACHED` |
|
|
471
|
-
| `TASK_LISTENER_ATTACH_FAIL` | `MAKOO_TASK_LISTENER_ATTACH_FAIL` |
|
|
472
|
-
| `TASK_SIGNAL_INVALID` | `MAKOO_TASK_SIGNAL_INVALID` |
|
|
473
|
-
| `TASK_SIGNAL_BIND_FAIL` | `MAKOO_TASK_SIGNAL_BIND_FAIL` |
|
|
474
|
-
| `CLI_SOURCE_DIR_NOT_FOUND` | `MAKOO_CLI_SOURCE_DIR_NOT_FOUND` |
|
|
475
|
-
| `CLI_MANIFEST_LOAD_FAIL` | `MAKOO_CLI_MANIFEST_LOAD_FAIL` |
|
|
476
|
-
| `CLI_MODULE_MANIFEST_LOAD_FAIL` | `MAKOO_CLI_MODULE_MANIFEST_LOAD_FAIL` |
|
|
477
|
-
| `CLI_MANIFEST_NOT_FOUND` | `MAKOO_CLI_MANIFEST_NOT_FOUND` |
|
|
478
|
-
| `CLI_MANIFEST_BINDING_NOT_FOUND` | `MAKOO_CLI_MANIFEST_BINDING_NOT_FOUND` |
|
|
479
|
-
| `CLI_MANIFEST_IMPORT_NOT_FOUND` | `MAKOO_CLI_MANIFEST_IMPORT_NOT_FOUND` |
|
|
480
|
-
| `CLI_FUNCTION_SERIALIZATION_UNSUPPORTED` | `MAKOO_CLI_FUNCTION_SERIALIZATION_UNSUPPORTED` |
|
|
481
|
-
| `CLI_NO_ENABLED_TASKS` | `MAKOO_CLI_NO_ENABLED_TASKS` |
|
|
482
|
-
| `CLI_COMPONENT_NOT_FOUND` | `MAKOO_CLI_COMPONENT_NOT_FOUND` |
|
|
483
|
-
| `CLI_CONFIG_INVALID` | `MAKOO_CLI_CONFIG_INVALID` |
|
|
484
|
-
| `CLI_UNKNOWN_FRAMEWORK` | `MAKOO_CLI_UNKNOWN_FRAMEWORK` |
|
|
485
|
-
| `CLI_UNSUPPORTED_FRAMEWORK` | `MAKOO_CLI_UNSUPPORTED_FRAMEWORK` |
|
|
486
|
-
| `CLI_MODULE_ALREADY_EXISTS` | `MAKOO_CLI_MODULE_ALREADY_EXISTS` |
|
|
487
|
-
| `CLI_MANIFEST_VALIDATION_FAIL` | `MAKOO_CLI_MANIFEST_VALIDATION_FAIL` |
|
|
488
|
-
| `CLI_VITE_CONFIG_NOT_FOUND` | `MAKOO_CLI_VITE_CONFIG_NOT_FOUND` |
|
|
489
|
-
| `CLI_PLUGIN_NOT_FOUND` | `MAKOO_CLI_PLUGIN_NOT_FOUND` |
|
|
490
|
-
| `CLI_RUNTIME_SETUP_NOT_FOUND` | `MAKOO_CLI_RUNTIME_SETUP_NOT_FOUND` |
|
|
491
|
-
|
|
492
|
-
## Adapter types
|
|
493
|
-
|
|
494
|
-
### `MountAdapter`
|
|
495
|
-
|
|
496
|
-
```ts
|
|
497
|
-
interface MountAdapter<TArtifact = unknown, THandle = unknown, TInstance = unknown> {
|
|
498
|
-
name: string;
|
|
499
|
-
mount(input: AdapterMountInput<TArtifact>): AdapterMountResult<THandle, TInstance>;
|
|
500
|
-
unmount(input: AdapterUnmountInput<THandle>): void;
|
|
160
|
+
if (toolbar?.kind === 'component') {
|
|
161
|
+
toolbar.disableAlive();
|
|
162
|
+
toolbar.enableAlive();
|
|
501
163
|
}
|
|
502
|
-
```
|
|
503
164
|
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
```ts
|
|
507
|
-
interface ResolvableMountAdapter<TArtifact = unknown, THandle = unknown, TInstance = unknown>
|
|
508
|
-
extends MountAdapter<TArtifact, THandle, TInstance> {
|
|
509
|
-
matches(artifact: unknown): artifact is TArtifact;
|
|
510
|
-
}
|
|
165
|
+
started.destroyAll();
|
|
511
166
|
```
|
|
512
167
|
|
|
513
|
-
|
|
168
|
+
`started.destroyAll()` only affects tasks created by that start batch. `makoo.destroyAll()` affects the whole runtime.
|
|
514
169
|
|
|
515
|
-
|
|
516
|
-
| --- | --- |
|
|
517
|
-
| `host` | `HTMLElement` |
|
|
518
|
-
| `mountPoint` | `HTMLElement` |
|
|
519
|
-
| `artifact` | `TArtifact` |
|
|
520
|
-
| `taskId` | `string` |
|
|
521
|
-
| `injectAt` | `string` |
|
|
522
|
-
| `makoo` | `MakooContext` |
|
|
170
|
+
## Adapter Contract
|
|
523
171
|
|
|
524
|
-
|
|
172
|
+
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.
|
|
525
173
|
|
|
526
174
|
```ts
|
|
527
|
-
type
|
|
528
|
-
handle: THandle;
|
|
529
|
-
instance?: TInstance;
|
|
530
|
-
};
|
|
531
|
-
```
|
|
532
|
-
|
|
533
|
-
### `AdapterUnmountInput`
|
|
534
|
-
|
|
535
|
-
| Field | Type |
|
|
536
|
-
| --- | --- |
|
|
537
|
-
| `host` | `HTMLElement \| undefined` |
|
|
538
|
-
| `mountPoint` | `HTMLElement` |
|
|
539
|
-
| `handle` | `THandle` |
|
|
540
|
-
| `taskId` | `string` |
|
|
541
|
-
| `injectAt` | `string` |
|
|
542
|
-
| `reason` | `AdapterUnmountReason` |
|
|
175
|
+
import type { ResolvableMountAdapter } from '@makoojs/core';
|
|
543
176
|
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
destroy(): void;
|
|
559
|
-
on(event: ObserveEventName, hook: ObserveHook): () => void;
|
|
560
|
-
onTask(event: ObserveEventName, hook: ObserveHook): () => void;
|
|
561
|
-
off(event: ObserveEventName, hook?: ObserveHook): void;
|
|
562
|
-
offTask(event?: ObserveEventName, hook?: ObserveHook): void;
|
|
563
|
-
getLogger(): ILogger;
|
|
564
|
-
bindListenerSignal(source: ActivitySignalSource<boolean>): boolean;
|
|
565
|
-
controlListener(event: ActionEvent): boolean;
|
|
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
|
+
// Clean up according to input.reason.
|
|
190
|
+
}
|
|
566
191
|
};
|
|
567
192
|
```
|
|
568
193
|
|
|
569
|
-
|
|
194
|
+
`mount(input)` receives the target host, generated mount point, artifact, task ID, selector, and task-scoped `makoo` context.
|
|
570
195
|
|
|
571
|
-
|
|
572
|
-
type MakooDefaults = {
|
|
573
|
-
alive: boolean;
|
|
574
|
-
scope: 'local' | 'global';
|
|
575
|
-
timeout: number;
|
|
576
|
-
};
|
|
577
|
-
|
|
578
|
-
type MakooInjectionDeclaration<TArtifact = unknown> = {
|
|
579
|
-
kind: 'component';
|
|
580
|
-
id?: string;
|
|
581
|
-
injectAt: string;
|
|
582
|
-
artifact: TArtifact;
|
|
583
|
-
options?: ArtifactOptions;
|
|
584
|
-
};
|
|
585
|
-
|
|
586
|
-
type MakooListenerDeclaration = {
|
|
587
|
-
kind: 'listener';
|
|
588
|
-
id?: string;
|
|
589
|
-
listenAt: string;
|
|
590
|
-
event: string;
|
|
591
|
-
type: string;
|
|
592
|
-
callback: EventListener;
|
|
593
|
-
activitySignal?: () => ActivitySignalSource<boolean>;
|
|
594
|
-
};
|
|
196
|
+
## Listener And Activity Signal
|
|
595
197
|
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
```
|
|
198
|
+
Standalone listeners use the object form of `listen()`. Their optional `id` is the task ID;
|
|
199
|
+
use it when later task lookup or control needs a stable identity. A component's `on` listener
|
|
200
|
+
is owned by that component task and does not need its own ID.
|
|
600
201
|
|
|
601
|
-
|
|
202
|
+
`capture` defaults to `false`. Set it to `true` to run the listener during the DOM capture phase.
|
|
602
203
|
|
|
603
204
|
```ts
|
|
604
|
-
|
|
605
|
-
kind: 'component';
|
|
606
|
-
taskId: string;
|
|
607
|
-
enableAlive(): void;
|
|
608
|
-
disableAlive(): void;
|
|
609
|
-
reset(): void;
|
|
610
|
-
destroy(): void;
|
|
611
|
-
};
|
|
205
|
+
import { createActivityStore, createMakoo, listen } from '@makoojs/core';
|
|
612
206
|
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
taskId: string;
|
|
616
|
-
open(): boolean;
|
|
617
|
-
close(): boolean;
|
|
618
|
-
destroy(): void;
|
|
619
|
-
};
|
|
207
|
+
const enabled = createActivityStore(true);
|
|
208
|
+
const makoo = createMakoo();
|
|
620
209
|
|
|
621
|
-
|
|
210
|
+
makoo.start([
|
|
211
|
+
listen({
|
|
212
|
+
id: 'save-listener',
|
|
213
|
+
listenAt: '#save',
|
|
214
|
+
type: 'click',
|
|
215
|
+
callback: () => {
|
|
216
|
+
console.log('save clicked');
|
|
217
|
+
},
|
|
218
|
+
activitySignal: () => enabled
|
|
219
|
+
})
|
|
220
|
+
]);
|
|
622
221
|
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
get(taskId: string): StartedTask | undefined;
|
|
626
|
-
resetAll(): void;
|
|
627
|
-
destroyAll(): void;
|
|
628
|
-
};
|
|
222
|
+
enabled.set(false);
|
|
223
|
+
enabled.set(true);
|
|
629
224
|
```
|
|
630
225
|
|
|
631
|
-
|
|
226
|
+
## Observation Events
|
|
632
227
|
|
|
633
|
-
|
|
228
|
+
core emits observation events during declaration registration, starting, mounting, listener work, alive mode, DOM watching, and task status changes.
|
|
634
229
|
|
|
635
230
|
```ts
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
hooks?: LifecycleHookMap;
|
|
640
|
-
};
|
|
641
|
-
```
|
|
642
|
-
|
|
643
|
-
## Observer types
|
|
644
|
-
|
|
645
|
-
```ts
|
|
646
|
-
type ObserveEventName = (typeof OBSERVE_EVENT_NAMES)[number];
|
|
647
|
-
|
|
648
|
-
type ObserveEvent = {
|
|
649
|
-
name: ObserveEventName;
|
|
650
|
-
ts: number;
|
|
651
|
-
taskId?: string;
|
|
652
|
-
kind?: 'component' | 'listener';
|
|
653
|
-
injectAt?: string;
|
|
654
|
-
status?: 'idle' | 'pending' | 'active';
|
|
655
|
-
durationMs?: number;
|
|
656
|
-
error?: unknown;
|
|
657
|
-
preStatus?: 'idle' | 'pending' | 'active';
|
|
658
|
-
meta?: Record<string, unknown>;
|
|
659
|
-
};
|
|
231
|
+
const off = makoo.on('artifact:mountSuccess', (event) => {
|
|
232
|
+
console.log(event.taskId, event.injectAt);
|
|
233
|
+
});
|
|
660
234
|
|
|
661
|
-
|
|
662
|
-
event:
|
|
663
|
-
|
|
664
|
-
stopPropagation(): void;
|
|
665
|
-
stopImmediatePropagation(): void;
|
|
235
|
+
makoo.onAny((event, ctrl) => {
|
|
236
|
+
if (event.name === 'artifact:mountFail') {
|
|
237
|
+
ctrl.stopPropagation();
|
|
666
238
|
}
|
|
667
|
-
)
|
|
239
|
+
});
|
|
668
240
|
|
|
669
|
-
|
|
670
|
-
Record<ObserveEventName, ObserveHook | ObserveHook[]>
|
|
671
|
-
>;
|
|
241
|
+
off();
|
|
672
242
|
```
|
|
673
243
|
|
|
674
|
-
|
|
244
|
+
Common events include:
|
|
675
245
|
|
|
676
|
-
|
|
677
|
-
|
|
246
|
+
- `register:start`
|
|
247
|
+
- `register:success`
|
|
248
|
+
- `start:requested`
|
|
249
|
+
- `start:taskScheduled`
|
|
250
|
+
- `artifact:mountStart`
|
|
251
|
+
- `artifact:mountSuccess`
|
|
252
|
+
- `artifact:mountFail`
|
|
253
|
+
- `listener:attached`
|
|
254
|
+
- `alive:enabled`
|
|
255
|
+
- `alive:observerStarted`
|
|
256
|
+
- `task:statusChange`
|
|
257
|
+
- `dom:targetFound`
|
|
258
|
+
- `dom:targetTimeout`
|
|
678
259
|
|
|
679
|
-
|
|
680
|
-
get(): T;
|
|
681
|
-
subscribe(listener: (value: T) => void): SignalUnsubscribe;
|
|
682
|
-
};
|
|
260
|
+
The full event name list is available from `OBSERVE_EVENT_NAMES`.
|
|
683
261
|
|
|
684
|
-
|
|
685
|
-
```
|
|
262
|
+
## DOMWatcher
|
|
686
263
|
|
|
687
|
-
|
|
264
|
+
`DOMWatcher` is core's low-level DOM observation utility. You usually do not need to use it directly because `makoo.start()` and alive mode already wrap target waiting and restoration.
|
|
688
265
|
|
|
689
|
-
|
|
690
|
-
type LoggerLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
266
|
+
## Logging And Errors
|
|
691
267
|
|
|
692
|
-
|
|
693
|
-
info(message: string, ...args: unknown[]): void;
|
|
694
|
-
warn(message: string, ...args: unknown[]): void;
|
|
695
|
-
error(message: string, ...args: unknown[]): void;
|
|
696
|
-
debug(message: string, ...args: unknown[]): void;
|
|
697
|
-
}
|
|
698
|
-
```
|
|
268
|
+
core uses `Logger` by default and prints logs with the `[Makoo]` prefix. You can pass a custom logger to `createMakoo({ logger })`.
|
|
699
269
|
|
|
700
|
-
|
|
270
|
+
core also exports these error-related types:
|
|
701
271
|
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
272
|
+
- `MakooError`
|
|
273
|
+
- `AdapterError`
|
|
274
|
+
- `TaskError`
|
|
275
|
+
- `SignalError`
|
|
276
|
+
- `ErrorCode`
|
|
277
|
+
- `MakooIssue`
|
|
707
278
|
|
|
708
|
-
|
|
709
|
-
|
|
279
|
+
## Public Exports Overview
|
|
280
|
+
|
|
281
|
+
| Category | Representative exports |
|
|
282
|
+
| --- | --- |
|
|
283
|
+
| Runtime API | `createMakoo`, `inject`, `listen`, `MakooRuntime`, `StartedTasks` |
|
|
284
|
+
| Adapter protocol | `MountAdapter`, `ResolvableMountAdapter`, `AdapterMountInput`, `AdapterUnmountInput`, `MakooContext` |
|
|
285
|
+
| Lifecycle observation | `ObserverHub`, `OBSERVE_EVENT_NAMES`, `ObserveEvent`, `ObserveHook`, `LifecycleHookMap` |
|
|
286
|
+
| DOM observation | `DOMWatcher` |
|
|
287
|
+
| Listener signal | `createActivityStore`, `ActivitySignalSource` |
|
|
288
|
+
| Logging | `Logger`, `ILogger`, `LoggerLevel` |
|
|
289
|
+
| Errors | `MakooError`, `AdapterError`, `TaskError`, `SignalError`, `ErrorCode` |
|