@makoojs/core 0.3.1 → 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 -639
- package/README.md +210 -635
- 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 +610 -578
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,714 +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
|
-
capture?: boolean;
|
|
173
|
-
activitySignal?: () => ActivitySignalSource<boolean>;
|
|
174
|
-
};
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
When `id` is omitted, the runtime uses `listener-${listenAt}-${type}` as the task ID.
|
|
178
|
-
`capture` selects the DOM capture phase when `true`; it defaults to the browser behavior of
|
|
179
|
-
`false` (the bubbling phase). Makoo continues to manage the listener's abort signal internally.
|
|
180
|
-
|
|
181
|
-
### Returns
|
|
182
|
-
|
|
183
|
-
Returns `MakooListenerDeclaration`. It can be started as a standalone task or assigned to `ArtifactOptions.on`.
|
|
184
|
-
|
|
185
|
-
### Example
|
|
186
|
-
|
|
187
|
-
```ts
|
|
188
|
-
const declaration = listen({
|
|
189
|
-
id: 'escape-close',
|
|
190
|
-
listenAt: 'body',
|
|
191
|
-
type: 'keydown',
|
|
192
|
-
capture: true,
|
|
193
|
-
callback: onEscape
|
|
194
|
-
});
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
## `createObserverHub()`
|
|
198
|
-
|
|
199
|
-
Creates an event hub. `logger` defaults to `new Logger()`.
|
|
200
|
-
|
|
201
|
-
### Type
|
|
202
|
-
|
|
203
|
-
```ts
|
|
204
|
-
function createObserverHub(logger?: ILogger): ObserverHub;
|
|
205
|
-
```
|
|
206
|
-
|
|
207
|
-
### Parameters
|
|
208
|
-
|
|
209
|
-
- `logger`: optional `ILogger`. A `Logger` is created when omitted.
|
|
210
|
-
|
|
211
|
-
### Returns
|
|
212
|
-
|
|
213
|
-
Returns `ObserverHub`:
|
|
214
|
-
|
|
215
|
-
| Method | Returns | Description |
|
|
216
|
-
| --- | --- | --- |
|
|
217
|
-
| `on(event, hook)` | `() => void` | Registers a hook for an event |
|
|
218
|
-
| `onTask(taskId, event, hook)` | `() => void` | Registers a task-specific hook |
|
|
219
|
-
| `onAny(hook)` | `() => void` | Registers a hook for every event |
|
|
220
|
-
| `off(event, hook?)` | `void` | Removes event hooks |
|
|
221
|
-
| `offTask(taskId, event?, hook?)` | `void` | Removes task hooks |
|
|
222
|
-
| `offAny(hook)` | `void` | Removes a global hook |
|
|
223
|
-
| `clear()` | `void` | Removes all hooks |
|
|
224
|
-
| `hasHooks(event?)` | `boolean` | Checks whether any or a specific event has hooks |
|
|
225
|
-
| `emit(event)` | `void` | Emits an event |
|
|
226
|
-
| `emitOnTask(taskId, event)` | `void` | Emits an event for a task |
|
|
227
|
-
|
|
228
|
-
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.
|
|
229
|
-
|
|
230
|
-
### Example
|
|
231
|
-
|
|
232
|
-
```ts
|
|
233
|
-
const observer = createObserverHub();
|
|
234
|
-
const off = observer.on('artifact:mountSuccess', (event) => {
|
|
235
|
-
console.log(event.taskId);
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
off();
|
|
239
|
-
```
|
|
240
|
-
|
|
241
|
-
## `createActivityStore()`
|
|
242
|
-
|
|
243
|
-
Creates readable, subscribable, and writable state.
|
|
244
|
-
|
|
245
|
-
### Type
|
|
246
|
-
|
|
247
|
-
```ts
|
|
248
|
-
function createActivityStore<T>(initialValue: T): {
|
|
249
|
-
get(): T;
|
|
250
|
-
subscribe(listener: (value: T) => void): SignalUnsubscribe;
|
|
251
|
-
set(value: T): void;
|
|
252
|
-
update(updater: (value: T) => T): void;
|
|
253
|
-
};
|
|
254
|
-
```
|
|
255
|
-
|
|
256
|
-
### Parameters
|
|
257
|
-
|
|
258
|
-
- `initialValue`: the initial state value.
|
|
259
|
-
|
|
260
|
-
### Returns
|
|
261
|
-
|
|
262
|
-
Returns an object with `get()`, `subscribe()`, `set()`, and `update()`.
|
|
263
|
-
|
|
264
|
-
### Details
|
|
265
|
-
|
|
266
|
-
`set()` and `update()` compare the new and current values with `Object.is()` and do not notify subscribers when the value is unchanged.
|
|
267
|
-
|
|
268
|
-
### Example
|
|
269
|
-
|
|
270
|
-
```ts
|
|
271
|
-
const active = createActivityStore(true);
|
|
272
|
-
const unsubscribe = active.subscribe((value) => {
|
|
273
|
-
console.log(value);
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
active.set(false);
|
|
277
|
-
active.update((value) => !value);
|
|
278
|
-
unsubscribe();
|
|
279
|
-
```
|
|
280
|
-
|
|
281
|
-
## `DOMWatcher`
|
|
282
|
-
|
|
283
|
-
### `DOMWatcher.onDomReady()`
|
|
284
|
-
|
|
285
|
-
Finds an existing matching element and observes subsequently added elements with `MutationObserver`.
|
|
286
|
-
|
|
287
|
-
#### Type
|
|
96
|
+
When `id` is omitted, Makoo infers a task id from the artifact and target selector.
|
|
288
97
|
|
|
289
98
|
```ts
|
|
290
|
-
|
|
291
|
-
selector: string,
|
|
292
|
-
callback: (element: HTMLElement, observer?: MutationObserver) => void,
|
|
293
|
-
root: Document | HTMLElement = document,
|
|
294
|
-
options: { once: boolean; timeout?: number } | { once?: boolean; timeout: number },
|
|
295
|
-
runtime?: {
|
|
296
|
-
logger: ILogger;
|
|
297
|
-
emit: (name:
|
|
298
|
-
| 'dom:targetFound'
|
|
299
|
-
| 'dom:targetTimeout'
|
|
300
|
-
| 'dom:targetRemoved'
|
|
301
|
-
| 'dom:targetRestored'
|
|
302
|
-
) => void;
|
|
303
|
-
}
|
|
304
|
-
): () => void;
|
|
305
|
-
```
|
|
99
|
+
import { createMakoo, inject, listen } from '@makoojs/core';
|
|
306
100
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
#### Type
|
|
319
|
-
|
|
320
|
-
```ts
|
|
321
|
-
DOMWatcher.onDomAlive(
|
|
322
|
-
target: HTMLElement,
|
|
323
|
-
selector: string,
|
|
324
|
-
onRemove: () => void,
|
|
325
|
-
onRestore: (element: HTMLElement, observer?: MutationObserver) => void,
|
|
326
|
-
root: Document | HTMLElement = document,
|
|
327
|
-
options: { once: boolean; timeout?: number } | { once?: boolean; timeout: number },
|
|
328
|
-
runtime?: {
|
|
329
|
-
logger: ILogger;
|
|
330
|
-
emit: (name:
|
|
331
|
-
| 'dom:targetFound'
|
|
332
|
-
| 'dom:targetTimeout'
|
|
333
|
-
| 'dom:targetRemoved'
|
|
334
|
-
| 'dom:targetRestored'
|
|
335
|
-
) => 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
|
+
}
|
|
336
112
|
}
|
|
337
|
-
)
|
|
338
|
-
```
|
|
339
|
-
|
|
340
|
-
#### Returns
|
|
341
|
-
|
|
342
|
-
Returns a function that stops both removal and restoration observation.
|
|
343
|
-
|
|
344
|
-
## `Logger`
|
|
345
|
-
|
|
346
|
-
Makoo's default logger implementation.
|
|
347
|
-
|
|
348
|
-
### Type
|
|
349
|
-
|
|
350
|
-
```ts
|
|
351
|
-
class Logger implements ILogger {
|
|
352
|
-
static readonly PREFIX = '[Makoo]';
|
|
353
|
-
|
|
354
|
-
constructor(level?: LoggerLevel);
|
|
355
|
-
setLevel(level: LoggerLevel): void;
|
|
356
|
-
getLevel(): LoggerLevel;
|
|
357
|
-
log(level: LoggerLevel, message: string, ...args: unknown[]): void;
|
|
358
|
-
debug(message: string, ...args: unknown[]): void;
|
|
359
|
-
info(message: string, ...args: unknown[]): void;
|
|
360
|
-
warn(message: string, ...args: unknown[]): void;
|
|
361
|
-
error(message: string, ...args: unknown[]): void;
|
|
362
|
-
}
|
|
363
|
-
```
|
|
364
|
-
|
|
365
|
-
The default level is `info`. Level order is `debug`, `info`, `warn`, and `error`; messages below the current level are not emitted.
|
|
366
|
-
|
|
367
|
-
## Error classes
|
|
368
|
-
|
|
369
|
-
### `MakooError`
|
|
370
|
-
|
|
371
|
-
```ts
|
|
372
|
-
class MakooError extends Error {
|
|
373
|
-
readonly code: string;
|
|
374
|
-
readonly issues: MakooIssue[];
|
|
375
|
-
override readonly cause?: Error;
|
|
376
|
-
|
|
377
|
-
constructor(
|
|
378
|
-
message: string,
|
|
379
|
-
issues?: MakooIssue[],
|
|
380
|
-
code?: string,
|
|
381
|
-
cause?: Error
|
|
382
|
-
);
|
|
383
|
-
}
|
|
384
|
-
```
|
|
385
|
-
|
|
386
|
-
`message` receives a `[makoo]` prefix and includes the `issues` and `cause` error chain.
|
|
387
|
-
|
|
388
|
-
### `AdapterError`
|
|
389
|
-
|
|
390
|
-
```ts
|
|
391
|
-
class AdapterError extends MakooError;
|
|
392
|
-
```
|
|
393
|
-
|
|
394
|
-
Default code: `ErrorCode.ADAPTER_NOT_FOUND`.
|
|
395
|
-
|
|
396
|
-
### `SignalError`
|
|
397
|
-
|
|
398
|
-
```ts
|
|
399
|
-
class SignalError extends MakooError;
|
|
400
|
-
```
|
|
401
|
-
|
|
402
|
-
Default code: `ErrorCode.TASK_SIGNAL_INVALID`.
|
|
403
|
-
|
|
404
|
-
### `TaskError`
|
|
405
|
-
|
|
406
|
-
```ts
|
|
407
|
-
class TaskError extends MakooError;
|
|
408
|
-
```
|
|
409
|
-
|
|
410
|
-
Default code: `ErrorCode.TASK_NO_REGISTERED`.
|
|
411
|
-
|
|
412
|
-
## Constants
|
|
413
|
-
|
|
414
|
-
### `Action`
|
|
415
|
-
|
|
416
|
-
```ts
|
|
417
|
-
enum Action {
|
|
418
|
-
OPEN = 'OPEN',
|
|
419
|
-
CLOSE = 'CLOSE'
|
|
420
|
-
}
|
|
113
|
+
});
|
|
421
114
|
|
|
422
|
-
|
|
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
|
+
]);
|
|
423
153
|
```
|
|
424
154
|
|
|
425
|
-
|
|
155
|
+
`start()` returns `StartedTasks` for batch-scoped control:
|
|
426
156
|
|
|
427
157
|
```ts
|
|
428
|
-
const
|
|
429
|
-
'register:start',
|
|
430
|
-
'register:success',
|
|
431
|
-
'register:duplicate',
|
|
432
|
-
'register:error',
|
|
433
|
-
'start:requested',
|
|
434
|
-
'start:taskScheduled',
|
|
435
|
-
'start:taskSkipped',
|
|
436
|
-
'artifact:mountStart',
|
|
437
|
-
'artifact:mountSuccess',
|
|
438
|
-
'artifact:mountFail',
|
|
439
|
-
'listener:attached',
|
|
440
|
-
'listener:detached',
|
|
441
|
-
'listener:attachFail',
|
|
442
|
-
'alive:enabled',
|
|
443
|
-
'alive:disabled',
|
|
444
|
-
'alive:observerStarted',
|
|
445
|
-
'alive:observerStopped',
|
|
446
|
-
'task:targetReady',
|
|
447
|
-
'task:statusChange',
|
|
448
|
-
'task:beforeReset',
|
|
449
|
-
'task:afterReset',
|
|
450
|
-
'task:beforeDestroy',
|
|
451
|
-
'task:afterDestroy',
|
|
452
|
-
'signal:watcherReleased',
|
|
453
|
-
'resource:listenerReleased',
|
|
454
|
-
'artifact:unmounted',
|
|
455
|
-
'dom:targetFound',
|
|
456
|
-
'dom:targetTimeout',
|
|
457
|
-
'dom:targetRemoved',
|
|
458
|
-
'dom:targetRestored'
|
|
459
|
-
] as const;
|
|
460
|
-
```
|
|
461
|
-
|
|
462
|
-
### `ErrorCode`
|
|
158
|
+
const toolbar = started.get('toolbar');
|
|
463
159
|
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
| `ADAPTER_NOT_FOUND` | `MAKOO_ADAPTER_NOT_FOUND` |
|
|
468
|
-
| `ADAPTER_MOUNT_FAIL` | `MAKOO_ADAPTER_MOUNT_FAIL` |
|
|
469
|
-
| `ADAPTER_UNMOUNT_FAIL` | `MAKOO_ADAPTER_UNMOUNT_FAIL` |
|
|
470
|
-
| `TASK_NO_REGISTERED` | `MAKOO_TASK_NO_REGISTERED` |
|
|
471
|
-
| `TASK_NOT_FOUND` | `MAKOO_TASK_NOT_FOUND` |
|
|
472
|
-
| `TASK_INJECT_FAIL` | `MAKOO_TASK_INJECT_FAIL` |
|
|
473
|
-
| `TASK_ALREADY_MOUNTED` | `MAKOO_TASK_ALREADY_MOUNTED` |
|
|
474
|
-
| `TASK_TARGET_DETACHED` | `MAKOO_TASK_TARGET_DETACHED` |
|
|
475
|
-
| `TASK_LISTENER_ATTACH_FAIL` | `MAKOO_TASK_LISTENER_ATTACH_FAIL` |
|
|
476
|
-
| `TASK_SIGNAL_INVALID` | `MAKOO_TASK_SIGNAL_INVALID` |
|
|
477
|
-
| `TASK_SIGNAL_BIND_FAIL` | `MAKOO_TASK_SIGNAL_BIND_FAIL` |
|
|
478
|
-
| `CLI_SOURCE_DIR_NOT_FOUND` | `MAKOO_CLI_SOURCE_DIR_NOT_FOUND` |
|
|
479
|
-
| `CLI_MANIFEST_LOAD_FAIL` | `MAKOO_CLI_MANIFEST_LOAD_FAIL` |
|
|
480
|
-
| `CLI_MODULE_MANIFEST_LOAD_FAIL` | `MAKOO_CLI_MODULE_MANIFEST_LOAD_FAIL` |
|
|
481
|
-
| `CLI_MANIFEST_NOT_FOUND` | `MAKOO_CLI_MANIFEST_NOT_FOUND` |
|
|
482
|
-
| `CLI_MANIFEST_BINDING_NOT_FOUND` | `MAKOO_CLI_MANIFEST_BINDING_NOT_FOUND` |
|
|
483
|
-
| `CLI_MANIFEST_IMPORT_NOT_FOUND` | `MAKOO_CLI_MANIFEST_IMPORT_NOT_FOUND` |
|
|
484
|
-
| `CLI_FUNCTION_SERIALIZATION_UNSUPPORTED` | `MAKOO_CLI_FUNCTION_SERIALIZATION_UNSUPPORTED` |
|
|
485
|
-
| `CLI_NO_ENABLED_TASKS` | `MAKOO_CLI_NO_ENABLED_TASKS` |
|
|
486
|
-
| `CLI_COMPONENT_NOT_FOUND` | `MAKOO_CLI_COMPONENT_NOT_FOUND` |
|
|
487
|
-
| `CLI_CONFIG_INVALID` | `MAKOO_CLI_CONFIG_INVALID` |
|
|
488
|
-
| `CLI_UNKNOWN_FRAMEWORK` | `MAKOO_CLI_UNKNOWN_FRAMEWORK` |
|
|
489
|
-
| `CLI_UNSUPPORTED_FRAMEWORK` | `MAKOO_CLI_UNSUPPORTED_FRAMEWORK` |
|
|
490
|
-
| `CLI_MODULE_ALREADY_EXISTS` | `MAKOO_CLI_MODULE_ALREADY_EXISTS` |
|
|
491
|
-
| `CLI_MANIFEST_VALIDATION_FAIL` | `MAKOO_CLI_MANIFEST_VALIDATION_FAIL` |
|
|
492
|
-
| `CLI_VITE_CONFIG_NOT_FOUND` | `MAKOO_CLI_VITE_CONFIG_NOT_FOUND` |
|
|
493
|
-
| `CLI_PLUGIN_NOT_FOUND` | `MAKOO_CLI_PLUGIN_NOT_FOUND` |
|
|
494
|
-
| `CLI_RUNTIME_SETUP_NOT_FOUND` | `MAKOO_CLI_RUNTIME_SETUP_NOT_FOUND` |
|
|
495
|
-
|
|
496
|
-
## Adapter types
|
|
497
|
-
|
|
498
|
-
### `MountAdapter`
|
|
499
|
-
|
|
500
|
-
```ts
|
|
501
|
-
interface MountAdapter<TArtifact = unknown, THandle = unknown, TInstance = unknown> {
|
|
502
|
-
name: string;
|
|
503
|
-
mount(input: AdapterMountInput<TArtifact>): AdapterMountResult<THandle, TInstance>;
|
|
504
|
-
unmount(input: AdapterUnmountInput<THandle>): void;
|
|
160
|
+
if (toolbar?.kind === 'component') {
|
|
161
|
+
toolbar.disableAlive();
|
|
162
|
+
toolbar.enableAlive();
|
|
505
163
|
}
|
|
506
|
-
```
|
|
507
164
|
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
```ts
|
|
511
|
-
interface ResolvableMountAdapter<TArtifact = unknown, THandle = unknown, TInstance = unknown>
|
|
512
|
-
extends MountAdapter<TArtifact, THandle, TInstance> {
|
|
513
|
-
matches(artifact: unknown): artifact is TArtifact;
|
|
514
|
-
}
|
|
165
|
+
started.destroyAll();
|
|
515
166
|
```
|
|
516
167
|
|
|
517
|
-
|
|
168
|
+
`started.destroyAll()` only affects tasks created by that start batch. `makoo.destroyAll()` affects the whole runtime.
|
|
518
169
|
|
|
519
|
-
|
|
520
|
-
| --- | --- |
|
|
521
|
-
| `host` | `HTMLElement` |
|
|
522
|
-
| `mountPoint` | `HTMLElement` |
|
|
523
|
-
| `artifact` | `TArtifact` |
|
|
524
|
-
| `taskId` | `string` |
|
|
525
|
-
| `injectAt` | `string` |
|
|
526
|
-
| `makoo` | `MakooContext` |
|
|
170
|
+
## Adapter Contract
|
|
527
171
|
|
|
528
|
-
|
|
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.
|
|
529
173
|
|
|
530
174
|
```ts
|
|
531
|
-
type
|
|
532
|
-
handle: THandle;
|
|
533
|
-
instance?: TInstance;
|
|
534
|
-
};
|
|
535
|
-
```
|
|
536
|
-
|
|
537
|
-
### `AdapterUnmountInput`
|
|
538
|
-
|
|
539
|
-
| Field | Type |
|
|
540
|
-
| --- | --- |
|
|
541
|
-
| `host` | `HTMLElement \| undefined` |
|
|
542
|
-
| `mountPoint` | `HTMLElement` |
|
|
543
|
-
| `handle` | `THandle` |
|
|
544
|
-
| `taskId` | `string` |
|
|
545
|
-
| `injectAt` | `string` |
|
|
546
|
-
| `reason` | `AdapterUnmountReason` |
|
|
175
|
+
import type { ResolvableMountAdapter } from '@makoojs/core';
|
|
547
176
|
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
destroy(): void;
|
|
563
|
-
on(event: ObserveEventName, hook: ObserveHook): () => void;
|
|
564
|
-
onTask(event: ObserveEventName, hook: ObserveHook): () => void;
|
|
565
|
-
off(event: ObserveEventName, hook?: ObserveHook): void;
|
|
566
|
-
offTask(event?: ObserveEventName, hook?: ObserveHook): void;
|
|
567
|
-
getLogger(): ILogger;
|
|
568
|
-
bindListenerSignal(source: ActivitySignalSource<boolean>): boolean;
|
|
569
|
-
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
|
+
}
|
|
570
191
|
};
|
|
571
192
|
```
|
|
572
193
|
|
|
573
|
-
|
|
194
|
+
`mount(input)` receives the target host, generated mount point, artifact, task ID, selector, and task-scoped `makoo` context.
|
|
574
195
|
|
|
575
|
-
|
|
576
|
-
type MakooDefaults = {
|
|
577
|
-
alive: boolean;
|
|
578
|
-
scope: 'local' | 'global';
|
|
579
|
-
timeout: number;
|
|
580
|
-
};
|
|
581
|
-
|
|
582
|
-
type MakooInjectionDeclaration<TArtifact = unknown> = {
|
|
583
|
-
kind: 'component';
|
|
584
|
-
id?: string;
|
|
585
|
-
injectAt: string;
|
|
586
|
-
artifact: TArtifact;
|
|
587
|
-
options?: ArtifactOptions;
|
|
588
|
-
};
|
|
589
|
-
|
|
590
|
-
type MakooListenerDeclaration = {
|
|
591
|
-
kind: 'listener';
|
|
592
|
-
id?: string;
|
|
593
|
-
listenAt: string;
|
|
594
|
-
event: string;
|
|
595
|
-
type: string;
|
|
596
|
-
callback: EventListener;
|
|
597
|
-
capture?: boolean;
|
|
598
|
-
activitySignal?: () => ActivitySignalSource<boolean>;
|
|
599
|
-
};
|
|
196
|
+
## Listener And Activity Signal
|
|
600
197
|
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
```
|
|
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.
|
|
605
201
|
|
|
606
|
-
|
|
202
|
+
`capture` defaults to `false`. Set it to `true` to run the listener during the DOM capture phase.
|
|
607
203
|
|
|
608
204
|
```ts
|
|
609
|
-
|
|
610
|
-
kind: 'component';
|
|
611
|
-
taskId: string;
|
|
612
|
-
enableAlive(): void;
|
|
613
|
-
disableAlive(): void;
|
|
614
|
-
reset(): void;
|
|
615
|
-
destroy(): void;
|
|
616
|
-
};
|
|
205
|
+
import { createActivityStore, createMakoo, listen } from '@makoojs/core';
|
|
617
206
|
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
taskId: string;
|
|
621
|
-
open(): boolean;
|
|
622
|
-
close(): boolean;
|
|
623
|
-
destroy(): void;
|
|
624
|
-
};
|
|
207
|
+
const enabled = createActivityStore(true);
|
|
208
|
+
const makoo = createMakoo();
|
|
625
209
|
|
|
626
|
-
|
|
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
|
+
]);
|
|
627
221
|
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
get(taskId: string): StartedTask | undefined;
|
|
631
|
-
resetAll(): void;
|
|
632
|
-
destroyAll(): void;
|
|
633
|
-
};
|
|
222
|
+
enabled.set(false);
|
|
223
|
+
enabled.set(true);
|
|
634
224
|
```
|
|
635
225
|
|
|
636
|
-
|
|
226
|
+
## Observation Events
|
|
637
227
|
|
|
638
|
-
|
|
228
|
+
core emits observation events during declaration registration, starting, mounting, listener work, alive mode, DOM watching, and task status changes.
|
|
639
229
|
|
|
640
230
|
```ts
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
hooks?: LifecycleHookMap;
|
|
645
|
-
};
|
|
646
|
-
```
|
|
647
|
-
|
|
648
|
-
## Observer types
|
|
649
|
-
|
|
650
|
-
```ts
|
|
651
|
-
type ObserveEventName = (typeof OBSERVE_EVENT_NAMES)[number];
|
|
652
|
-
|
|
653
|
-
type ObserveEvent = {
|
|
654
|
-
name: ObserveEventName;
|
|
655
|
-
ts: number;
|
|
656
|
-
taskId?: string;
|
|
657
|
-
kind?: 'component' | 'listener';
|
|
658
|
-
injectAt?: string;
|
|
659
|
-
status?: 'idle' | 'pending' | 'active';
|
|
660
|
-
durationMs?: number;
|
|
661
|
-
error?: unknown;
|
|
662
|
-
preStatus?: 'idle' | 'pending' | 'active';
|
|
663
|
-
meta?: Record<string, unknown>;
|
|
664
|
-
};
|
|
231
|
+
const off = makoo.on('artifact:mountSuccess', (event) => {
|
|
232
|
+
console.log(event.taskId, event.injectAt);
|
|
233
|
+
});
|
|
665
234
|
|
|
666
|
-
|
|
667
|
-
event:
|
|
668
|
-
|
|
669
|
-
stopPropagation(): void;
|
|
670
|
-
stopImmediatePropagation(): void;
|
|
235
|
+
makoo.onAny((event, ctrl) => {
|
|
236
|
+
if (event.name === 'artifact:mountFail') {
|
|
237
|
+
ctrl.stopPropagation();
|
|
671
238
|
}
|
|
672
|
-
)
|
|
239
|
+
});
|
|
673
240
|
|
|
674
|
-
|
|
675
|
-
Record<ObserveEventName, ObserveHook | ObserveHook[]>
|
|
676
|
-
>;
|
|
241
|
+
off();
|
|
677
242
|
```
|
|
678
243
|
|
|
679
|
-
|
|
244
|
+
Common events include:
|
|
680
245
|
|
|
681
|
-
|
|
682
|
-
|
|
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`
|
|
683
259
|
|
|
684
|
-
|
|
685
|
-
get(): T;
|
|
686
|
-
subscribe(listener: (value: T) => void): SignalUnsubscribe;
|
|
687
|
-
};
|
|
260
|
+
The full event name list is available from `OBSERVE_EVENT_NAMES`.
|
|
688
261
|
|
|
689
|
-
|
|
690
|
-
```
|
|
262
|
+
## DOMWatcher
|
|
691
263
|
|
|
692
|
-
|
|
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.
|
|
693
265
|
|
|
694
|
-
|
|
695
|
-
type LoggerLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
266
|
+
## Logging And Errors
|
|
696
267
|
|
|
697
|
-
|
|
698
|
-
info(message: string, ...args: unknown[]): void;
|
|
699
|
-
warn(message: string, ...args: unknown[]): void;
|
|
700
|
-
error(message: string, ...args: unknown[]): void;
|
|
701
|
-
debug(message: string, ...args: unknown[]): void;
|
|
702
|
-
}
|
|
703
|
-
```
|
|
268
|
+
core uses `Logger` by default and prints logs with the `[Makoo]` prefix. You can pass a custom logger to `createMakoo({ logger })`.
|
|
704
269
|
|
|
705
|
-
|
|
270
|
+
core also exports these error-related types:
|
|
706
271
|
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
272
|
+
- `MakooError`
|
|
273
|
+
- `AdapterError`
|
|
274
|
+
- `TaskError`
|
|
275
|
+
- `SignalError`
|
|
276
|
+
- `ErrorCode`
|
|
277
|
+
- `MakooIssue`
|
|
712
278
|
|
|
713
|
-
|
|
714
|
-
|
|
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` |
|