@mica-js/widget 0.2.2 → 0.3.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.md +20 -2
- package/dist/author.d.ts +226 -0
- package/dist/author.js +0 -0
- package/dist/index.d.ts +241 -206
- package/dist/index.js +600 -1519
- package/dist/index.js.map +1 -1
- package/package.json +10 -6
package/README.md
CHANGED
|
@@ -1,10 +1,28 @@
|
|
|
1
1
|
# @mica-js/widget
|
|
2
2
|
|
|
3
|
-
Widget
|
|
3
|
+
Widget 契约、包加载和运行时,提供实例执行、绘制、音频与端口通信。可作为独立运行时使用,也可由画布编辑器集成。
|
|
4
4
|
|
|
5
5
|
宿主负责提供文件访问、widget 包和沙箱工厂;执行隔离由 `@mica-js/sandbox` 提供。本包不负责器件电气仿真。
|
|
6
6
|
|
|
7
7
|
## 主要入口
|
|
8
8
|
|
|
9
|
-
- `@mica-js/widget`:`
|
|
9
|
+
- `@mica-js/widget`:`createWidgetRuntime`、widget provider 以及内置沙箱和音频 worklet 工厂。
|
|
10
|
+
- `@mica-js/widget/author`:仅 Widget 作者类型(工厂、上下文、输入、资源与音频),不加载宿主或沙箱入口;`@mica-js/sdk/widget` 复用此入口。
|
|
10
11
|
- `@mica-js/widget/vite`:`micaWidgetPlugins()`,用于 widget 构建集成。
|
|
12
|
+
|
|
13
|
+
## IWidgetRuntime(编辑器运行时)
|
|
14
|
+
|
|
15
|
+
`createWidgetRuntime(sandbox, options?)` 创建实例执行层。`sandbox` 由 `createWidgetSandboxFromWorker` 创建;运行时接管其生命周期,`dispose()` 会终止该沙箱。运行时只负责实例执行:不读取文档或文件、不解析 widget 库、不持有仿真时钟,也不接触器件运行时。
|
|
16
|
+
|
|
17
|
+
- `mount(instance)` 接收编辑器已解析好的 `WidgetModule`、尺寸、属性和预取资源;部分失败会清理访客会话、位图与音频会话,可以重试。首帧可能在 `mount` 兑现前就已提交,因此应在挂载前订阅 `onDidRender`。工厂或首帧失败会在报告后拒绝 `mount`。
|
|
18
|
+
- `update(id, update)` 可更新属性、宽高、dpr 和资源;`unmount(id)` 释放实例及位图。
|
|
19
|
+
- `deliver(message)` / `dispatch(id, event)` 先按模块定义校验实例、端口方向与载荷类型,再排队到下一次 `step`;未知实例或端口直接拒绝。
|
|
20
|
+
- `step({ simTime, dt, mode })` 是唯一的时间推进入口。显式步进开启后,访客不再自动重绘,`invalidate()` 标记的实例在该步统一绘制;直连设备发布也排队到该步开始消费。`step` 会等待直连 widget → simulation 投递全部结算后才兑现,避免主通道的下一次 tick 越过直连投递。
|
|
21
|
+
- `onDidRender` 的 bitmap 归运行时所有,保持到同实例下一帧、卸载或运行时释放为止;迟到帧会被关闭。
|
|
22
|
+
- `onDidPublish` 是观察通道;`message.direct === true` 表示访客已通过直连端口把消息交给仿真,观察者不得再次路由。
|
|
23
|
+
- `setBindings(id, bindings)` 只接收编辑器已经校验过的路由表,不做文档绑定校验。
|
|
24
|
+
- 直连端口只使用 `simulation-runtime` 服务,消息为 `{ target: { symbol, port }, payload, simTime }`,时间单位为皮秒。`configure` 在首次挂载前开启显式步进;独立访客未开启显式步进时仍支持自动重绘。
|
|
25
|
+
- `audio` 复用 `WidgetAudioEngine`;未提供 `WidgetAudioOptions` 时为保持可用状态的 no-op 控件。
|
|
26
|
+
- 并发 mount、update、unmount、setBindings 与 step 串行执行;`dispose()` 取消尚未完成的挂载采纳并释放位图、音频和沙箱。
|
|
27
|
+
|
|
28
|
+
公开类型包括 `IWidgetRuntime`、`IWidgetRuntimeRouting`、`IPreparedWidgetInstance`、`IWidgetInstanceUpdate`、`IWidgetMessage`、`IWidgetFrame`、`IWidgetFrameContext`、`IWidgetRuntimeDiagnostic`、`WidgetRuntimeError`,以及直连协议 `widgetSimulationRuntimeService`。
|
package/dist/author.d.ts
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { Disposable } from '@mica-js/core/lifecycle';
|
|
2
|
+
import { Event as Event_2 } from '@mica-js/core/lifecycle';
|
|
3
|
+
|
|
4
|
+
export { Disposable }
|
|
5
|
+
|
|
6
|
+
export { Event_2 as Event }
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Reads resources the host prefetched when the instance was mounted. Guests have no network and
|
|
10
|
+
* no DOM, so every byte a widget sees arrives through this interface.
|
|
11
|
+
*/
|
|
12
|
+
export declare interface WidgetAssetApi {
|
|
13
|
+
/**
|
|
14
|
+
* Resolves to a copy of the prefetched bytes for `src`; the widget may mutate it freely.
|
|
15
|
+
*
|
|
16
|
+
* Rejects when the resource was not prefetched: no `WidgetAssetProvider` was configured, the host
|
|
17
|
+
* failed to read it, or a property introduced it after mount. The first missing read per
|
|
18
|
+
* instance and `src` also reports `widget/asset-failed`. Callers must handle the rejection;
|
|
19
|
+
* ignoring it leaves an unhandled rejection in the guest worker.
|
|
20
|
+
*/
|
|
21
|
+
readBinary(src: string): Promise<Uint8Array>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Image / binary property value from the shared descriptor contract (#7). */
|
|
25
|
+
export declare interface WidgetAssetRef {
|
|
26
|
+
readonly src: string;
|
|
27
|
+
readonly mode: 'file' | 'inline' | 'store';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Instance-level audio API behind `api.audio`. */
|
|
31
|
+
export declare interface WidgetAudioApi {
|
|
32
|
+
readonly state: WidgetAudioState;
|
|
33
|
+
/** Subscribes to availability changes; the returned function unsubscribes. */
|
|
34
|
+
onStateChange(listener: (state: WidgetAudioState) => void): () => void;
|
|
35
|
+
/** Starts a continuous oscillator. */
|
|
36
|
+
tone(options: WidgetAudioToneOptions): WidgetAudioTone;
|
|
37
|
+
/** Opens a PCM stream. */
|
|
38
|
+
createStream(options: WidgetAudioStreamOptions): WidgetAudioStream;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export declare interface WidgetAudioEnded {
|
|
42
|
+
readonly reason: WidgetAudioEndReason;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Why a tone or stream stopped. */
|
|
46
|
+
export declare type WidgetAudioEndReason = 'finished' | 'stopped' | 'dropped' | 'error' | 'unavailable' | 'disposed';
|
|
47
|
+
|
|
48
|
+
/** Sample layout of a PCM stream. */
|
|
49
|
+
export declare type WidgetAudioFormat = 's16' | 'f32';
|
|
50
|
+
|
|
51
|
+
export declare interface WidgetAudioRampOptions {
|
|
52
|
+
/** Smoothing window in milliseconds, `0`–`10000`; `0` when omitted. */
|
|
53
|
+
readonly rampMs?: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Availability of the host audio path. */
|
|
57
|
+
export declare type WidgetAudioState = 'unavailable' | 'suspended' | 'running';
|
|
58
|
+
|
|
59
|
+
export declare interface WidgetAudioStopOptions {
|
|
60
|
+
/** Fade-out in milliseconds, `0`–`10000`; `0` when omitted. */
|
|
61
|
+
readonly fadeMs?: number;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Handle for one PCM stream. Every method is safe to call after the stream closed. */
|
|
65
|
+
export declare interface WidgetAudioStream {
|
|
66
|
+
readonly id: string;
|
|
67
|
+
readonly sampleRate: number;
|
|
68
|
+
readonly channels: 1 | 2;
|
|
69
|
+
readonly format: WidgetAudioFormat;
|
|
70
|
+
readonly capacityFrames: number;
|
|
71
|
+
readonly startFrames: number;
|
|
72
|
+
readonly volume: number;
|
|
73
|
+
/** Accepted frames the worklet has not reported as consumed yet. */
|
|
74
|
+
readonly bufferedFrames: number;
|
|
75
|
+
readonly underruns: number;
|
|
76
|
+
readonly dropped: number;
|
|
77
|
+
readonly state: WidgetAudioStreamState;
|
|
78
|
+
/** Settles when the stream finished, was closed or its instance went away. */
|
|
79
|
+
readonly ended: Promise<WidgetAudioEnded>;
|
|
80
|
+
/**
|
|
81
|
+
* Appends one block of interleaved samples.
|
|
82
|
+
*
|
|
83
|
+
* The promise resolves with the worklet's answer: `true` when the whole block entered the ring
|
|
84
|
+
* buffer, `false` when it lacked room and the block was discarded whole. It never means "played".
|
|
85
|
+
* Shape errors are thrown synchronously, before anything is sent.
|
|
86
|
+
*/
|
|
87
|
+
write(samples: Int16Array | Float32Array): Promise<boolean>;
|
|
88
|
+
/** Drops everything that has not played yet and keeps the stream open. */
|
|
89
|
+
clear(): void;
|
|
90
|
+
/** Plays the remaining data and then ends with `finished`. Idempotent. */
|
|
91
|
+
end(): void;
|
|
92
|
+
/** Stops immediately and discards the remaining data. Idempotent. */
|
|
93
|
+
close(): void;
|
|
94
|
+
/** Fires once each time the buffer drains to half its capacity. */
|
|
95
|
+
onDrain(listener: () => void): () => void;
|
|
96
|
+
/** Fires when `state` or `underruns` changed. */
|
|
97
|
+
onStatus(listener: (status: WidgetAudioStreamStatus) => void): () => void;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export declare interface WidgetAudioStreamOptions {
|
|
101
|
+
/** Integer source rate in Hz, `8000`–`96000`; required. */
|
|
102
|
+
readonly sampleRate: number;
|
|
103
|
+
/** `1` (default) or `2`; multi-channel samples are interleaved. */
|
|
104
|
+
readonly channels?: 1 | 2;
|
|
105
|
+
/** `f32` (default) or `s16`. */
|
|
106
|
+
readonly format?: WidgetAudioFormat;
|
|
107
|
+
/** Worklet buffer window in milliseconds, `50`–`2000`; `500` when omitted. */
|
|
108
|
+
readonly bufferMs?: number;
|
|
109
|
+
/** Frames that must be buffered before (re)starting playback, **not** more than `bufferMs`. */
|
|
110
|
+
readonly startMs?: number;
|
|
111
|
+
/** `0`–`1`; `1` when omitted. */
|
|
112
|
+
readonly volume?: number;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Playback state of a PCM stream, as the guest sees it. */
|
|
116
|
+
export declare type WidgetAudioStreamState = 'buffering' | 'playing' | 'ended' | 'closed';
|
|
117
|
+
|
|
118
|
+
/** Read-only snapshot of one stream. */
|
|
119
|
+
export declare interface WidgetAudioStreamStatus {
|
|
120
|
+
readonly state: WidgetAudioStreamState;
|
|
121
|
+
readonly underruns: number;
|
|
122
|
+
readonly dropped: number;
|
|
123
|
+
readonly bufferedFrames: number;
|
|
124
|
+
readonly capacityFrames: number;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Voice handle of a continuous oscillator, with live parameter updates. */
|
|
128
|
+
export declare interface WidgetAudioTone extends WidgetAudioVoice {
|
|
129
|
+
set(settings: WidgetAudioVoiceSettings, options?: WidgetAudioRampOptions): void;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export declare interface WidgetAudioToneOptions {
|
|
133
|
+
/** `sine` when omitted. */
|
|
134
|
+
readonly type?: WidgetOscillatorType;
|
|
135
|
+
/** Hz, `(0, 24000]`. */
|
|
136
|
+
readonly frequency: number;
|
|
137
|
+
/** `0`–`1`; `1` when omitted. */
|
|
138
|
+
readonly volume?: number;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Handle for one voice; closing it is idempotent. */
|
|
142
|
+
export declare interface WidgetAudioVoice {
|
|
143
|
+
readonly id: string;
|
|
144
|
+
/** Settles once the worklet (or the guest, when audio is unavailable) stopped the voice. */
|
|
145
|
+
readonly ended: Promise<WidgetAudioEnded>;
|
|
146
|
+
stop(options?: WidgetAudioStopOptions): void;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export declare interface WidgetAudioVoiceSettings {
|
|
150
|
+
readonly volume?: number;
|
|
151
|
+
readonly frequency?: number;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Metadata passed to every port listener next to the payload. */
|
|
155
|
+
export declare interface WidgetDeliveryMeta {
|
|
156
|
+
/** Simulation time at which the host routed the payload. */
|
|
157
|
+
readonly simTime: number;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Default export of a widget implementation module. */
|
|
161
|
+
export declare type WidgetFactory = (api: WidgetInstanceApi) => WidgetImplementation | void;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Object a widget factory may return. Every hook is optional, so a pure-logic widget can return
|
|
165
|
+
* nothing at all; an instance without `render` still produces one blank frame per repaint.
|
|
166
|
+
*/
|
|
167
|
+
export declare interface WidgetImplementation {
|
|
168
|
+
/** Draws using logical units; the context is already scaled by the device pixel ratio. */
|
|
169
|
+
render?(context: OffscreenCanvasRenderingContext2D): void;
|
|
170
|
+
input?(event: WidgetInputEvent): void;
|
|
171
|
+
dispose?(): void;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Pointer or keyboard input forwarded to one instance. */
|
|
175
|
+
export declare interface WidgetInputEvent {
|
|
176
|
+
/** DOM event type, e.g. `pointerdown`, `pointermove` or `keydown`. */
|
|
177
|
+
readonly type: string;
|
|
178
|
+
/** Pointer position in logical widget units, for pointer events. */
|
|
179
|
+
readonly x?: number;
|
|
180
|
+
readonly y?: number;
|
|
181
|
+
readonly button?: number;
|
|
182
|
+
readonly buttons?: number;
|
|
183
|
+
/** `KeyboardEvent.key`, for keyboard events. */
|
|
184
|
+
readonly key?: string;
|
|
185
|
+
readonly code?: string;
|
|
186
|
+
readonly repeat?: boolean;
|
|
187
|
+
readonly altKey?: boolean;
|
|
188
|
+
readonly ctrlKey?: boolean;
|
|
189
|
+
readonly metaKey?: boolean;
|
|
190
|
+
readonly shiftKey?: boolean;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Per-instance API handed to a widget factory. Sizes are logical units: the guest runtime applies
|
|
195
|
+
* the device pixel ratio, so widgets never multiply coordinates themselves.
|
|
196
|
+
*/
|
|
197
|
+
export declare interface WidgetInstanceApi {
|
|
198
|
+
readonly instanceId: string;
|
|
199
|
+
readonly width: number;
|
|
200
|
+
readonly height: number;
|
|
201
|
+
/** Document props merged over `definition.properties[*].default`. */
|
|
202
|
+
readonly props: Readonly<Record<string, unknown>>;
|
|
203
|
+
/** Prefetched image / binary resources for this instance. */
|
|
204
|
+
readonly assets: WidgetAssetApi;
|
|
205
|
+
/** Voices, oscillators and PCM streams (RFC 0016). */
|
|
206
|
+
readonly audio: WidgetAudioApi;
|
|
207
|
+
/** Subscribes to one input port; the returned function unsubscribes. */
|
|
208
|
+
subscribe(port: string, listener: WidgetPortListener): () => void;
|
|
209
|
+
/**
|
|
210
|
+
* Sends a payload out of one output port.
|
|
211
|
+
*
|
|
212
|
+
* `simTime` is the simulation time the widget reacted to; an omitted value takes the last time
|
|
213
|
+
* the device host reported.
|
|
214
|
+
*/
|
|
215
|
+
publish(port: string, payload: unknown, simTime?: number): void;
|
|
216
|
+
/** Requests a repaint; repeated calls in the same tick coalesce into one frame. */
|
|
217
|
+
invalidate(): void;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Waveforms the worklet synthesizes for `api.audio.tone()`. */
|
|
221
|
+
export declare type WidgetOscillatorType = 'sine' | 'square' | 'triangle' | 'sawtooth';
|
|
222
|
+
|
|
223
|
+
/** Listener registered for one input port through `WidgetInstanceApi.subscribe`. */
|
|
224
|
+
export declare type WidgetPortListener = (payload: unknown, meta: WidgetDeliveryMeta) => void;
|
|
225
|
+
|
|
226
|
+
export { }
|
package/dist/author.js
ADDED
|
File without changes
|