@makoojs/core 0.2.2 → 0.3.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 +635 -189
- package/README.md +631 -189
- package/dist/Makoo/createMakoo.d.ts +2 -3
- package/dist/Makoo/types.d.ts +8 -1
- package/dist/Task/TaskRegister.d.ts +2 -0
- package/dist/Task/types.d.ts +2 -0
- package/dist/error/ErrorCode.d.ts +5 -1
- package/dist/index.cjs +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +59 -52
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,272 +1,714 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Core API Reference
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## API Index
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
### Tasks
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
- [`createMakoo()`](#createmakoo): creates a runtime
|
|
8
|
+
- [`inject()`](#inject): declares a component task
|
|
9
|
+
- [`listen()`](#listen): declares a listener task
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
### Observation and state
|
|
11
12
|
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
- Use low-level tools such as `DOMWatcher` and `createActivityStore` for custom integrations.
|
|
13
|
+
- [`createObserverHub()`](#createobserverhub): creates a lifecycle event hub
|
|
14
|
+
- [`createActivityStore()`](#createactivitystore): creates subscribable state
|
|
15
|
+
- [`DOMWatcher`](#domwatcher): observes DOM targets
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
### Infrastructure
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
- [`Logger`](#logger): logger implementation
|
|
20
|
+
- [Error classes](#error-classes): structured errors
|
|
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;
|
|
23
40
|
```
|
|
24
41
|
|
|
25
|
-
|
|
42
|
+
### Parameters
|
|
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:
|
|
26
57
|
|
|
27
58
|
```ts
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
59
|
+
{
|
|
60
|
+
alive: false,
|
|
61
|
+
scope: 'local',
|
|
62
|
+
timeout: 5000
|
|
63
|
+
}
|
|
64
|
+
```
|
|
33
65
|
|
|
34
|
-
|
|
35
|
-
kind: 'text';
|
|
36
|
-
text: string;
|
|
37
|
-
};
|
|
66
|
+
### Returns
|
|
38
67
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
68
|
+
Returns `MakooRuntime`:
|
|
69
|
+
|
|
70
|
+
| Method | Returns | Description |
|
|
71
|
+
| --- | --- | --- |
|
|
72
|
+
| `start(tasks)` | `StartedTasks` | Registers and starts a batch of `MakooTaskDeclaration`; an empty array throws `TaskError` |
|
|
73
|
+
| `reset(taskId)` | `void` | Resets a task and releases its current resources |
|
|
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.
|
|
88
|
+
|
|
89
|
+
### Example
|
|
59
90
|
|
|
91
|
+
```ts
|
|
60
92
|
const makoo = createMakoo({
|
|
61
|
-
|
|
62
|
-
alive: true,
|
|
63
|
-
scope: 'local',
|
|
64
|
-
timeout: 5000
|
|
65
|
-
},
|
|
66
|
-
adapters: [textAdapter]
|
|
93
|
+
adapters: [adapter]
|
|
67
94
|
});
|
|
68
95
|
|
|
69
|
-
makoo.start([
|
|
70
|
-
inject('#app',
|
|
71
|
-
kind: 'text',
|
|
72
|
-
text: 'Hello from Makoo core'
|
|
73
|
-
})
|
|
96
|
+
const started = makoo.start([
|
|
97
|
+
inject({ injectAt: '#app', artifact })
|
|
74
98
|
]);
|
|
75
99
|
```
|
|
76
100
|
|
|
77
|
-
##
|
|
101
|
+
## `inject()`
|
|
78
102
|
|
|
79
|
-
|
|
103
|
+
Creates a component task declaration. `inject()` does not register or start the task; pass its result to `MakooRuntime.start()`.
|
|
80
104
|
|
|
81
|
-
|
|
105
|
+
### Type
|
|
82
106
|
|
|
83
107
|
```ts
|
|
84
|
-
inject(
|
|
85
|
-
|
|
86
|
-
|
|
108
|
+
function inject<TArtifact>(
|
|
109
|
+
input: MakooInjectionInput<TArtifact>
|
|
110
|
+
): MakooInjectionDeclaration<TArtifact>;
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Parameters
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
type MakooInjectionInput<TArtifact = unknown> = {
|
|
117
|
+
id?: string;
|
|
118
|
+
injectAt: string;
|
|
119
|
+
artifact: TArtifact;
|
|
120
|
+
options?: ArtifactOptions;
|
|
121
|
+
};
|
|
87
122
|
```
|
|
88
123
|
|
|
89
|
-
|
|
124
|
+
When `id` is omitted, the runtime derives a task ID from the artifact and `injectAt`.
|
|
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
|
|
90
141
|
|
|
91
142
|
```ts
|
|
92
|
-
inject({
|
|
143
|
+
const declaration = inject({
|
|
93
144
|
id: 'settings-panel',
|
|
94
145
|
injectAt: '#settings',
|
|
95
|
-
artifact:
|
|
146
|
+
artifact: settingsPanel,
|
|
96
147
|
options: {
|
|
97
|
-
alive: true
|
|
148
|
+
alive: true,
|
|
149
|
+
scope: 'global'
|
|
98
150
|
}
|
|
99
151
|
});
|
|
100
152
|
```
|
|
101
153
|
|
|
102
|
-
|
|
154
|
+
## `listen()`
|
|
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
|
|
103
159
|
|
|
104
160
|
```ts
|
|
105
|
-
|
|
161
|
+
function listen(input: MakooListenerInput): MakooListenerDeclaration;
|
|
162
|
+
```
|
|
106
163
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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
|
|
119
194
|
});
|
|
195
|
+
```
|
|
120
196
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
-
]);
|
|
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;
|
|
140
205
|
```
|
|
141
206
|
|
|
142
|
-
|
|
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
|
|
143
231
|
|
|
144
232
|
```ts
|
|
145
|
-
const
|
|
233
|
+
const observer = createObserverHub();
|
|
234
|
+
const off = observer.on('artifact:mountSuccess', (event) => {
|
|
235
|
+
console.log(event.taskId);
|
|
236
|
+
});
|
|
146
237
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
238
|
+
off();
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
## `createActivityStore()`
|
|
242
|
+
|
|
243
|
+
Creates readable, subscribable, and writable state.
|
|
244
|
+
|
|
245
|
+
### Type
|
|
151
246
|
|
|
152
|
-
|
|
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
|
+
};
|
|
153
254
|
```
|
|
154
255
|
|
|
155
|
-
|
|
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
|
|
156
265
|
|
|
157
|
-
|
|
266
|
+
`set()` and `update()` compare the new and current values with `Object.is()` and do not notify subscribers when the value is unchanged.
|
|
158
267
|
|
|
159
|
-
|
|
268
|
+
### Example
|
|
160
269
|
|
|
161
270
|
```ts
|
|
162
|
-
|
|
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`.
|
|
163
286
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
287
|
+
#### Type
|
|
288
|
+
|
|
289
|
+
```ts
|
|
290
|
+
DOMWatcher.onDomReady(
|
|
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;
|
|
177
303
|
}
|
|
178
|
-
|
|
304
|
+
): () => void;
|
|
179
305
|
```
|
|
180
306
|
|
|
181
|
-
|
|
307
|
+
#### Details
|
|
182
308
|
|
|
183
|
-
|
|
309
|
+
- Calls `callback` when an element is found.
|
|
310
|
+
- Stops after the first match when `once: true`.
|
|
311
|
+
- Stops when `timeout` expires.
|
|
312
|
+
- Returns a function that stops observation.
|
|
184
313
|
|
|
185
|
-
|
|
314
|
+
### `DOMWatcher.onDomAlive()`
|
|
315
|
+
|
|
316
|
+
Observes removal of `target` and calls `onRestore` when a new element matching `selector` appears.
|
|
317
|
+
|
|
318
|
+
#### Type
|
|
186
319
|
|
|
187
320
|
```ts
|
|
188
|
-
|
|
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;
|
|
336
|
+
}
|
|
337
|
+
): () => void;
|
|
338
|
+
```
|
|
189
339
|
|
|
190
|
-
|
|
191
|
-
const makoo = createMakoo();
|
|
340
|
+
#### Returns
|
|
192
341
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
'click',
|
|
197
|
-
() => {
|
|
198
|
-
console.log('save clicked');
|
|
199
|
-
},
|
|
200
|
-
{
|
|
201
|
-
activitySignal: () => enabled
|
|
202
|
-
}
|
|
203
|
-
)
|
|
204
|
-
]);
|
|
342
|
+
Returns a function that stops both removal and restoration observation.
|
|
343
|
+
|
|
344
|
+
## `Logger`
|
|
205
345
|
|
|
206
|
-
|
|
207
|
-
|
|
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
|
+
}
|
|
208
363
|
```
|
|
209
364
|
|
|
210
|
-
|
|
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
|
|
211
368
|
|
|
212
|
-
|
|
369
|
+
### `MakooError`
|
|
213
370
|
|
|
214
371
|
```ts
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
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
|
+
```
|
|
218
385
|
|
|
219
|
-
makoo
|
|
220
|
-
if (event.name === 'artifact:mountFail') {
|
|
221
|
-
ctrl.stopPropagation();
|
|
222
|
-
}
|
|
223
|
-
});
|
|
386
|
+
`message` receives a `[makoo]` prefix and includes the `issues` and `cause` error chain.
|
|
224
387
|
|
|
225
|
-
|
|
388
|
+
### `AdapterError`
|
|
389
|
+
|
|
390
|
+
```ts
|
|
391
|
+
class AdapterError extends MakooError;
|
|
226
392
|
```
|
|
227
393
|
|
|
228
|
-
|
|
394
|
+
Default code: `ErrorCode.ADAPTER_NOT_FOUND`.
|
|
229
395
|
|
|
230
|
-
|
|
231
|
-
- `register:success`
|
|
232
|
-
- `start:requested`
|
|
233
|
-
- `start:taskScheduled`
|
|
234
|
-
- `artifact:mountStart`
|
|
235
|
-
- `artifact:mountSuccess`
|
|
236
|
-
- `artifact:mountFail`
|
|
237
|
-
- `listener:attached`
|
|
238
|
-
- `alive:enabled`
|
|
239
|
-
- `alive:observerStarted`
|
|
240
|
-
- `task:statusChange`
|
|
241
|
-
- `dom:targetFound`
|
|
242
|
-
- `dom:targetTimeout`
|
|
396
|
+
### `SignalError`
|
|
243
397
|
|
|
244
|
-
|
|
398
|
+
```ts
|
|
399
|
+
class SignalError extends MakooError;
|
|
400
|
+
```
|
|
245
401
|
|
|
246
|
-
|
|
402
|
+
Default code: `ErrorCode.TASK_SIGNAL_INVALID`.
|
|
247
403
|
|
|
248
|
-
|
|
404
|
+
### `TaskError`
|
|
249
405
|
|
|
250
|
-
|
|
406
|
+
```ts
|
|
407
|
+
class TaskError extends MakooError;
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
Default code: `ErrorCode.TASK_NO_REGISTERED`.
|
|
411
|
+
|
|
412
|
+
## Constants
|
|
251
413
|
|
|
252
|
-
|
|
414
|
+
### `Action`
|
|
253
415
|
|
|
254
|
-
|
|
416
|
+
```ts
|
|
417
|
+
enum Action {
|
|
418
|
+
OPEN = 'OPEN',
|
|
419
|
+
CLOSE = 'CLOSE'
|
|
420
|
+
}
|
|
255
421
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
- `TaskError`
|
|
259
|
-
- `ErrorCode`
|
|
260
|
-
- `MakooIssue`
|
|
422
|
+
type ActionEvent = `${Action}`;
|
|
423
|
+
```
|
|
261
424
|
|
|
262
|
-
|
|
425
|
+
### `OBSERVE_EVENT_NAMES`
|
|
263
426
|
|
|
264
|
-
|
|
427
|
+
```ts
|
|
428
|
+
const OBSERVE_EVENT_NAMES = [
|
|
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`
|
|
463
|
+
|
|
464
|
+
| Field | Value |
|
|
265
465
|
| --- | --- |
|
|
266
|
-
|
|
|
267
|
-
|
|
|
268
|
-
|
|
|
269
|
-
|
|
|
270
|
-
|
|
|
271
|
-
|
|
|
272
|
-
|
|
|
466
|
+
| `UNKNOWN` | `MAKOO_UNKNOWN` |
|
|
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;
|
|
505
|
+
}
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
### `ResolvableMountAdapter`
|
|
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
|
+
}
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
### `AdapterMountInput`
|
|
518
|
+
|
|
519
|
+
| Field | Type |
|
|
520
|
+
| --- | --- |
|
|
521
|
+
| `host` | `HTMLElement` |
|
|
522
|
+
| `mountPoint` | `HTMLElement` |
|
|
523
|
+
| `artifact` | `TArtifact` |
|
|
524
|
+
| `taskId` | `string` |
|
|
525
|
+
| `injectAt` | `string` |
|
|
526
|
+
| `makoo` | `MakooContext` |
|
|
527
|
+
|
|
528
|
+
### `AdapterMountResult`
|
|
529
|
+
|
|
530
|
+
```ts
|
|
531
|
+
type AdapterMountResult<THandle = unknown, TInstance = unknown> = {
|
|
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` |
|
|
547
|
+
|
|
548
|
+
```ts
|
|
549
|
+
type AdapterUnmountReason = 'destroy' | 'reset' | 'remount' | 'manual';
|
|
550
|
+
type AdapterResolver = (artifact: unknown) => MountAdapter | undefined;
|
|
551
|
+
```
|
|
552
|
+
|
|
553
|
+
### `MakooContext`
|
|
554
|
+
|
|
555
|
+
```ts
|
|
556
|
+
type MakooContext = {
|
|
557
|
+
taskId: string;
|
|
558
|
+
injectAt: string;
|
|
559
|
+
enableAlive(): void;
|
|
560
|
+
disableAlive(): void;
|
|
561
|
+
reset(): void;
|
|
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;
|
|
570
|
+
};
|
|
571
|
+
```
|
|
572
|
+
|
|
573
|
+
## Task types
|
|
574
|
+
|
|
575
|
+
```ts
|
|
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
|
+
};
|
|
600
|
+
|
|
601
|
+
type MakooTaskDeclaration<TArtifact = unknown> =
|
|
602
|
+
| MakooInjectionDeclaration<TArtifact>
|
|
603
|
+
| MakooListenerDeclaration;
|
|
604
|
+
```
|
|
605
|
+
|
|
606
|
+
### `StartedTasks`
|
|
607
|
+
|
|
608
|
+
```ts
|
|
609
|
+
type StartedComponentTask = {
|
|
610
|
+
kind: 'component';
|
|
611
|
+
taskId: string;
|
|
612
|
+
enableAlive(): void;
|
|
613
|
+
disableAlive(): void;
|
|
614
|
+
reset(): void;
|
|
615
|
+
destroy(): void;
|
|
616
|
+
};
|
|
617
|
+
|
|
618
|
+
type StartedListenerTask = {
|
|
619
|
+
kind: 'listener';
|
|
620
|
+
taskId: string;
|
|
621
|
+
open(): boolean;
|
|
622
|
+
close(): boolean;
|
|
623
|
+
destroy(): void;
|
|
624
|
+
};
|
|
625
|
+
|
|
626
|
+
type StartedTask = StartedComponentTask | StartedListenerTask;
|
|
627
|
+
|
|
628
|
+
type StartedTasks = {
|
|
629
|
+
tasks: StartedTask[];
|
|
630
|
+
get(taskId: string): StartedTask | undefined;
|
|
631
|
+
resetAll(): void;
|
|
632
|
+
destroyAll(): void;
|
|
633
|
+
};
|
|
634
|
+
```
|
|
635
|
+
|
|
636
|
+
`StartedTasks.resetAll()` and `StartedTasks.destroyAll()` only affect the task batch returned by that `start()` call.
|
|
637
|
+
|
|
638
|
+
### `InjectionConfig`
|
|
639
|
+
|
|
640
|
+
```ts
|
|
641
|
+
type InjectionConfig = MakooDefaults & {
|
|
642
|
+
logger: ILogger;
|
|
643
|
+
observer?: ObserverHub;
|
|
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
|
+
};
|
|
665
|
+
|
|
666
|
+
type ObserveHook = (
|
|
667
|
+
event: ObserveEvent,
|
|
668
|
+
ctrl: {
|
|
669
|
+
stopPropagation(): void;
|
|
670
|
+
stopImmediatePropagation(): void;
|
|
671
|
+
}
|
|
672
|
+
) => void;
|
|
673
|
+
|
|
674
|
+
type LifecycleHookMap = Partial<
|
|
675
|
+
Record<ObserveEventName, ObserveHook | ObserveHook[]>
|
|
676
|
+
>;
|
|
677
|
+
```
|
|
678
|
+
|
|
679
|
+
## Signal types
|
|
680
|
+
|
|
681
|
+
```ts
|
|
682
|
+
type SignalUnsubscribe = (() => void) | { unsubscribe(): void };
|
|
683
|
+
|
|
684
|
+
type ActivitySignalSource<T = boolean> = {
|
|
685
|
+
get(): T;
|
|
686
|
+
subscribe(listener: (value: T) => void): SignalUnsubscribe;
|
|
687
|
+
};
|
|
688
|
+
|
|
689
|
+
type ActivitySignalSubscribable<T = boolean> = ActivitySignalSource<T>;
|
|
690
|
+
```
|
|
691
|
+
|
|
692
|
+
## Logger types
|
|
693
|
+
|
|
694
|
+
```ts
|
|
695
|
+
type LoggerLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
696
|
+
|
|
697
|
+
interface ILogger {
|
|
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
|
+
```
|
|
704
|
+
|
|
705
|
+
## Error types
|
|
706
|
+
|
|
707
|
+
```ts
|
|
708
|
+
type MakooIssue = {
|
|
709
|
+
path: string;
|
|
710
|
+
message: string;
|
|
711
|
+
};
|
|
712
|
+
|
|
713
|
+
type ErrorCodeValue = (typeof ErrorCode)[keyof typeof ErrorCode];
|
|
714
|
+
```
|