ranuts 0.2.0-alpha.1 → 0.4.0-alpha.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.
Files changed (119) hide show
  1. package/CLAUDE.md +126 -24
  2. package/README.md +7 -5
  3. package/README.zh-CN.md +7 -5
  4. package/dist/chain-CTywmh_H.js +1 -0
  5. package/dist/{color-CRdlaDqQ.js → color-Dohr9BXX.js} +1 -1
  6. package/dist/enums-BAgbyGdH.js +1 -0
  7. package/dist/i18n-Cf5TtRb4.js +1 -0
  8. package/dist/index.d.ts +1 -5
  9. package/dist/index.js +1 -1
  10. package/dist/src/node/appendFile.d.ts +3 -3
  11. package/dist/src/node/fileInfo.d.ts +2 -2
  12. package/dist/src/node/index.js +1 -1
  13. package/dist/src/node/paresUrl.d.ts +1 -1
  14. package/dist/src/node/readFile.d.ts +3 -3
  15. package/dist/src/node/traverse.d.ts +2 -2
  16. package/dist/src/node/watchFile.d.ts +3 -3
  17. package/dist/src/node/writeFile.d.ts +3 -3
  18. package/dist/src/sw/index.d.ts +109 -0
  19. package/dist/src/sw/index.js +1 -0
  20. package/dist/src/utils/async.d.ts +81 -0
  21. package/dist/src/utils/audioRecorder.d.ts +1 -1
  22. package/dist/src/utils/behavior.d.ts +12 -3
  23. package/dist/src/utils/bom.d.ts +68 -178
  24. package/dist/src/utils/bridge.d.ts +153 -0
  25. package/dist/src/utils/canvas.d.ts +54 -0
  26. package/dist/src/utils/chain.d.ts +80 -0
  27. package/dist/src/utils/color.d.ts +76 -4
  28. package/dist/src/utils/compose.d.ts +1 -1
  29. package/dist/src/utils/console.d.ts +19 -1
  30. package/dist/src/utils/debounce.d.ts +27 -5
  31. package/dist/src/utils/device.d.ts +31 -3
  32. package/dist/src/utils/dom.d.ts +5 -84
  33. package/dist/src/utils/error.d.ts +18 -1
  34. package/dist/src/utils/event.d.ts +61 -0
  35. package/dist/src/utils/file.d.ts +35 -0
  36. package/dist/src/utils/i18n/index.js +1 -0
  37. package/dist/src/utils/i18n.d.ts +69 -0
  38. package/dist/src/utils/idb.d.ts +218 -0
  39. package/dist/src/utils/img.d.ts +60 -4
  40. package/dist/src/utils/index.d.ts +50 -16
  41. package/dist/src/utils/index.js +1 -1
  42. package/dist/src/utils/lang.d.ts +69 -0
  43. package/dist/src/utils/localePath.d.ts +62 -0
  44. package/dist/src/utils/memoize.d.ts +53 -5
  45. package/dist/src/utils/monitor.d.ts +55 -26
  46. package/dist/src/utils/number.d.ts +46 -9
  47. package/dist/src/utils/obj.d.ts +56 -31
  48. package/dist/src/utils/paginate.d.ts +73 -0
  49. package/dist/src/utils/prefetch.d.ts +81 -0
  50. package/dist/src/utils/queue.d.ts +53 -23
  51. package/dist/src/utils/report.d.ts +51 -3
  52. package/dist/src/utils/request.d.ts +26 -7
  53. package/dist/src/utils/script.d.ts +26 -4
  54. package/dist/src/utils/segment.d.ts +62 -0
  55. package/dist/src/utils/signal.d.ts +35 -3
  56. package/dist/src/utils/storage.d.ts +57 -0
  57. package/dist/src/utils/str.d.ts +112 -88
  58. package/dist/src/utils/style.d.ts +31 -0
  59. package/dist/src/utils/subscribe.d.ts +5 -5
  60. package/dist/src/utils/throttle.d.ts +26 -13
  61. package/dist/src/utils/time.d.ts +38 -6
  62. package/dist/src/utils/tween.d.ts +40 -0
  63. package/dist/src/utils/visual/application.d.ts +4 -4
  64. package/dist/src/utils/visual/enums.d.ts +4 -4
  65. package/dist/src/utils/visual/graphics/graphics.d.ts +25 -25
  66. package/dist/src/utils/visual/graphics/graphicsData.d.ts +1 -1
  67. package/dist/src/utils/visual/graphics/graphicsGeometry.d.ts +6 -6
  68. package/dist/src/utils/visual/index.js +1 -1
  69. package/dist/src/utils/visual/math/matrix.d.ts +8 -8
  70. package/dist/src/utils/visual/math/transform.d.ts +1 -1
  71. package/dist/src/utils/visual/render/batchRenderer.d.ts +20 -18
  72. package/dist/src/utils/visual/render/utils/batch/index.d.ts +13 -13
  73. package/dist/src/utils/visual/render/utils/float.d.ts +13 -13
  74. package/dist/src/utils/visual/render/utils/verticy.d.ts +3 -3
  75. package/dist/src/utils/visual/render/utils/webgl/initShader.d.ts +3 -2
  76. package/dist/src/utils/visual/render/utils/webgpu/shaders.d.ts +2 -2
  77. package/dist/src/utils/visual/render/webGPURenderer.d.ts +8 -8
  78. package/dist/src/utils/visual/vertex/container.d.ts +17 -16
  79. package/dist/src/utils/worker.d.ts +125 -0
  80. package/dist/src/utils/zip.d.ts +162 -0
  81. package/dist/src/vnode/index.d.ts +1 -1
  82. package/dist/src/vnode/index.js +1 -1
  83. package/dist/src/vnode/is.d.ts +4 -0
  84. package/dist/test/canvas.test.d.ts +1 -0
  85. package/dist/test/color.test.d.ts +1 -0
  86. package/dist/test/sw.test.d.ts +1 -0
  87. package/dist/test/tween.test.d.ts +1 -0
  88. package/dist/test/utils/async.test.d.ts +1 -0
  89. package/dist/test/utils/file-read.test.d.ts +1 -0
  90. package/dist/test/utils/format.test.d.ts +1 -0
  91. package/dist/test/utils/idb-collection.test.d.ts +1 -0
  92. package/dist/test/utils/idb.test.d.ts +1 -0
  93. package/dist/test/utils/instrumentation.test.d.ts +1 -0
  94. package/dist/test/utils/lang.test.d.ts +1 -0
  95. package/dist/test/utils/locale-path.test.d.ts +1 -0
  96. package/dist/test/utils/media-query.test.d.ts +1 -0
  97. package/dist/test/utils/number-parse.test.d.ts +1 -0
  98. package/dist/test/utils/paginate.test.d.ts +1 -0
  99. package/dist/test/utils/prefetch.test.d.ts +1 -0
  100. package/dist/test/utils/query-storage.test.d.ts +1 -0
  101. package/dist/test/utils/queue.test.d.ts +1 -0
  102. package/dist/test/utils/report-config.test.d.ts +1 -0
  103. package/dist/test/utils/report-image.test.d.ts +1 -0
  104. package/dist/test/utils/segment.test.d.ts +1 -0
  105. package/dist/test/utils/signal-once.test.d.ts +1 -0
  106. package/dist/test/utils/single-flight.test.d.ts +1 -0
  107. package/dist/test/utils/timing.test.d.ts +1 -0
  108. package/dist/test/utils/worker-client.test.d.ts +1 -0
  109. package/dist/test/utils/worker-serve.test.d.ts +1 -0
  110. package/dist/test/utils/zip.test.d.ts +1 -0
  111. package/dist/umd/index.umd.cjs +1 -1
  112. package/dist/umd/node/node.umd.cjs +1 -1
  113. package/dist/umd/utils/utils.umd.cjs +1 -1
  114. package/dist/utils-8TVtLBjA.js +1 -0
  115. package/docs/API.md +341 -147
  116. package/package.json +13 -6
  117. package/dist/src/vnode/chainDom.d.ts +0 -47
  118. package/dist/subscribe-CINWsor3.js +0 -1
  119. package/dist/utils-_OxsVZmz.js +0 -1
@@ -4,39 +4,41 @@ import type { Batch } from '@/utils/visual/render/utils/batch';
4
4
  import type { IApplicationOptions } from '@/utils/visual/types';
5
5
  export declare abstract class BatchRenderer extends Renderer {
6
6
  /**
7
- * 上次构建大数组时的场景结构版本号(每个渲染器实例独立)。与根节点的 structureVersion
8
- * 不一致时说明场景结构变了(增删节点 / 重绘),需要重建;否则只做逐帧 transform 更新。
9
- * 初值 -1 保证首帧一定构建。基于版本号对比,多个渲染器共享同一场景也不会互相“消费”脏标记。
7
+ * The scene structure version at the last rebuild, held per renderer instance. When it
8
+ * differs from the root's structureVersion the scene changed (a node was added or removed,
9
+ * or a shape redrawn) and the big array must be rebuilt; otherwise only per-frame transform
10
+ * updates are needed. Starting at -1 guarantees a build on the first frame. Comparing
11
+ * versions rather than consuming a dirty flag lets several renderers share one scene.
10
12
  */
11
13
  protected builtVersion: number;
12
14
  /**
13
- * 顶点个数
15
+ * Vertex count
14
16
  */
15
17
  protected vertexCount: number;
16
18
  /**
17
- * 顶点下标个数
19
+ * Index count
18
20
  */
19
21
  protected indexCount: number;
20
22
  protected batches: Array<Batch | undefined>;
21
23
  protected batchesCount: number;
22
24
  /**
23
- * 顶点数组 float32 视图
25
+ * Float32 view over the vertex array
24
26
  */
25
27
  protected vertFloatView: Float32Array;
26
28
  /**
27
- * 顶点数组 Uint32 视图
29
+ * Uint32 view over the vertex array
28
30
  */
29
31
  protected vertIntView: Uint32Array;
30
32
  /**
31
- * 顶点下标数组
33
+ * Index array
32
34
  */
33
35
  protected indexBuffer: Uint32Array;
34
36
  /**
35
- * 当前的 webGL|webGPU vertex buffer 的长度
37
+ * Current length of the WebGL/WebGPU vertex buffer
36
38
  */
37
39
  protected curVertBufferLength: number;
38
40
  /**
39
- * 当前的 webGL|webGPU index buffer 的长度
41
+ * Current length of the WebGL/WebGPU index buffer
40
42
  */
41
43
  protected curIndexBufferLength: number;
42
44
  constructor(options: IApplicationOptions);
@@ -44,35 +46,35 @@ export declare abstract class BatchRenderer extends Renderer {
44
46
  protected startBuild(): void;
45
47
  protected buildEnd(): void;
46
48
  /**
47
- * 如果现有的 typed array 放不下了,则新建一个
49
+ * Allocate a bigger typed array when the current one can no longer hold the data
48
50
  */
49
51
  protected resizeBufferIfNeeded(): void;
50
52
  /**
51
- * 将数据打包到大数组里
53
+ * Pack the data into the big array
52
54
  */
53
55
  protected packData(): void;
54
56
  /**
55
- * 调用 webGL webGPU 的绘制 api 将内容绘制出来
57
+ * Draw via the WebGL or WebGPU drawing API
56
58
  */
57
59
  protected abstract draw(): void;
58
60
  /**
59
- * 更新更新 vertex buffer index buffer
61
+ * Update the vertex and index buffers
60
62
  */
61
63
  protected abstract updateBuffer(): void;
62
64
  /**
63
- * 设置投影矩阵,这是为了适配 canvas 元素的尺寸
65
+ * Set the projection matrix, which adapts to the canvas element's size
64
66
  */
65
67
  protected abstract setProjectionMatrix(): void;
66
68
  /**
67
- * 更新 stage transform 对应的 uniform 变量
69
+ * Update the uniform holding the stage's transform
68
70
  */
69
71
  protected abstract setRootTransform(a: number, b: number, c: number, d: number, tx: number, ty: number): void;
70
72
  /**
71
- * 更新子节点的 transform
73
+ * Update the children's transforms
72
74
  */
73
75
  protected updateChildrenTransform(rootContainer: Container): void;
74
76
  /**
75
- * 更新节点的位置信息并渲染
77
+ * Update node positions and render
76
78
  */
77
79
  render(rootContainer: Container): void;
78
80
  }
@@ -3,44 +3,44 @@ import type { Container } from '@/utils/visual/vertex/container';
3
3
  import type { Fill } from '@/utils/visual/style';
4
4
  import type { Graphics } from '@/utils/visual/graphics';
5
5
  /**
6
- * 构建大数组
6
+ * Build the big array
7
7
  */
8
8
  export declare const buildArray: (batchRenderer: BatchRenderer, container: Container) => void;
9
9
  /**
10
- * 更新大数组
10
+ * Update the big array
11
11
  */
12
12
  export declare const updateArray: (floatView: Float32Array, container: Container) => void;
13
13
  export declare abstract class Batch {
14
14
  /**
15
- * 顶点个数
15
+ * Vertex count
16
16
  */
17
17
  vertexCount: number;
18
18
  /**
19
- * 顶点下标个数
19
+ * Index count
20
20
  */
21
21
  indexCount: number;
22
22
  /**
23
- * rgba 的小端序形式
23
+ * rgba packed little-endian
24
24
  */
25
25
  rgba: number;
26
26
  /**
27
- * 顶点数据在大数组中的起点
27
+ * Where this batch's vertices start in the big array
28
28
  */
29
29
  vertexStart: number;
30
30
  /**
31
- * 顶点下标数据在大数组中的起点
31
+ * Where this batch's indices start in the big array
32
32
  */
33
33
  indexStart: number;
34
34
  /**
35
- * 将顶点数据写入大数组中
35
+ * Write the vertex data into the big array
36
36
  */
37
37
  abstract packVertices(floatView: Float32Array, intView: Uint32Array): void;
38
38
  /**
39
- * 将顶点下标数据写入大数组中
39
+ * Write the index data into the big array
40
40
  */
41
41
  abstract packIndices(int32: Uint32Array): void;
42
42
  /**
43
- * 在大数组中更新顶点位置数据
43
+ * Update the vertex positions inside the big array
44
44
  */
45
45
  abstract updateVertices(floatView: Float32Array): void;
46
46
  }
@@ -66,15 +66,15 @@ export declare class GraphicsBatchPool extends BatchPool {
66
66
  }
67
67
  export declare class GraphicsBatch extends Batch {
68
68
  /**
69
- * 顶点部分在 geometry.vertices 中的起始下标
69
+ * Where this part's vertices start in geometry.vertices
70
70
  */
71
71
  vertexOffset: number;
72
72
  /**
73
- * 顶点下标部分在 geometry.indices 中的起始下标
73
+ * Where this part's indices start in geometry.indices
74
74
  */
75
75
  indexOffset: number;
76
76
  /**
77
- * 对应的 Graphics 实例
77
+ * The Graphics instance this belongs to
78
78
  */
79
79
  graphics: Graphics;
80
80
  packVertices(floatView: Float32Array, intView: Uint32Array): void;
@@ -5,28 +5,28 @@ export declare class CustomFloatArray {
5
5
  private float32;
6
6
  private _data;
7
7
  /**
8
- * 获取长度
8
+ * Current length
9
9
  */
10
10
  get length(): number;
11
11
  /**
12
- * 拼接一个 number 数组
12
+ * Append an array of numbers
13
13
  */
14
14
  concat(arr: number[]): void;
15
15
  /**
16
- * 插入数据
17
- * @param num 要插入的数
16
+ * Push one value
17
+ * @param num the value to push
18
18
  */
19
19
  push(num: number): void;
20
20
  /**
21
- * 扩容
21
+ * Grow the backing buffer
22
22
  */
23
23
  expandCapacity(): void;
24
24
  /**
25
- * 以从 0 this.length 的这段 buffer 为底层,建立 Float32Array 视图并返回
25
+ * A Float32Array view over the buffer from 0 to this.length
26
26
  */
27
27
  get data(): Float32Array;
28
28
  /**
29
- * 清空
29
+ * Clear
30
30
  */
31
31
  clear(): void;
32
32
  }
@@ -37,27 +37,27 @@ export declare class CustomIntArray {
37
37
  private int32;
38
38
  private _data;
39
39
  /**
40
- * 获取长度
40
+ * Current length
41
41
  */
42
42
  get length(): number;
43
43
  /**
44
- * 拼接一个 number 数组
44
+ * Append an array of numbers
45
45
  */
46
46
  concat(arr: number[]): void;
47
47
  /**
48
- * 拼接一个UInt16数组
48
+ * Append a UInt16 array
49
49
  */
50
50
  concatUInt16(uint16: Uint32Array): void;
51
51
  /**
52
- * 扩容
52
+ * Grow the backing buffer
53
53
  */
54
54
  expandCapacity(): void;
55
55
  /**
56
- * 以从 0 this.length 的这段 buffer 为底层,建立 Uint32Array 视图并返回
56
+ * A Uint32Array view over the buffer from 0 to this.length
57
57
  */
58
58
  get data(): Uint32Array;
59
59
  /**
60
- * 清空
60
+ * Clear
61
61
  */
62
62
  clear(): void;
63
63
  }
@@ -1,13 +1,13 @@
1
1
  import type { GraphicsData } from '@/utils/visual/graphics/graphicsData';
2
2
  import type { GraphicsGeometry } from '@/utils/visual/graphics/graphicsGeometry';
3
3
  /**
4
- * 将一个图形顶点化
5
- * @param data 子图形data
4
+ * Turn a shape into vertices
5
+ * @param data the sub-shape's data
6
6
  */
7
7
  export declare const buildVertices: (data: GraphicsData) => void;
8
8
  export declare const triangulateFill: (data: GraphicsData, geometry: GraphicsGeometry) => void;
9
9
  export declare const getNormalVector: (x: number, y: number, lineWidth: number) => [number, number];
10
10
  /**
11
- * 构建描边的顶点 & 对其进行三角剖分
11
+ * Build the stroke's vertices and triangulate them
12
12
  */
13
13
  export declare const triangulateStroke: (data: GraphicsData, geometry: GraphicsGeometry) => void;
@@ -1,7 +1,8 @@
1
1
  import type { WebGLRenderer } from '@/utils/visual/render/webGlRenderer';
2
2
  /**
3
- * 设置顶点属性布局。调用前需保证目标 ARRAY_BUFFER 已经绑定。
4
- * 顶点格式与共享批处理管线(BatchRenderer)一致:position(2×Float32) + color(4×Uint8, 归一化),stride = BYTES_PER_VERTEX。
3
+ * Set the vertex attribute layout. The target ARRAY_BUFFER must already be bound.
4
+ * The vertex format matches the shared batching pipeline (BatchRenderer):
5
+ * position (2×Float32) + colour (4×Uint8, normalised), stride = BYTES_PER_VERTEX.
5
6
  */
6
7
  export declare const setupVertexLayout: (gl: WebGLRenderingContext, program: WebGLProgram) => void;
7
8
  export declare const initShader: (renderer: WebGLRenderer) => WebGLProgram;
@@ -1,8 +1,8 @@
1
1
  /**
2
- * 顶点着色器
2
+ * Vertex shader
3
3
  */
4
4
  export declare const vertexShaderSource = "\n@group(0) @binding(0) var<uniform> u_root_transform: mat3x3<f32>;\n@group(0) @binding(1) var<uniform> u_projection_matrix: mat3x3<f32>;\n\nstruct VertOutput {\n @builtin(position) v_position: vec4<f32>,\n @location(0) v_color : vec4<f32>,\n};\n\n@vertex\nfn main(\n @location(0) a_position: vec2<f32>,\n @location(1) a_color: vec4<f32>,\n) -> VertOutput {\n let v_position = vec4<f32>((u_projection_matrix * u_root_transform * vec3<f32>(a_position, 1.0)).xy, 0.0, 1.0);\n\n let v_color = a_color;\n\n return VertOutput(v_position, v_color);\n}\n";
5
5
  /**
6
- * 片元着色器
6
+ * Fragment shader
7
7
  */
8
8
  export declare const fragmentShaderSource = "\n@fragment\nfn main(\n @location(0) v_color : vec4<f32>,\n) -> @location(0) vec4<f32> {\n return v_color;\n}\n";
@@ -7,23 +7,23 @@ export declare class WebGPURenderer extends BatchRenderer {
7
7
  private renderPassDescriptor;
8
8
  private options;
9
9
  /**
10
- * 顶点的 webGPU buffer
10
+ * WebGPU buffer holding the vertices
11
11
  */
12
12
  private gpuVertexBuffer;
13
13
  /**
14
- * 顶点下标的 webGPU buffer
14
+ * WebGPU buffer holding the vertex indices
15
15
  */
16
16
  private gpuIndexBuffer;
17
17
  /**
18
- * 投影矩阵的 webGPU buffer
18
+ * WebGPU buffer holding the projection matrix
19
19
  */
20
20
  private projectionMatBuffer;
21
21
  /**
22
- * stage 的变换矩阵的 webGPU buffer
22
+ * WebGPU buffer holding the stage's transform matrix
23
23
  */
24
24
  private stageMatBuffer;
25
25
  /**
26
- * uniform 变量的 bind group
26
+ * Bind group for the uniforms
27
27
  */
28
28
  private uniformBindGroup;
29
29
  constructor(options: IApplicationOptions);
@@ -35,15 +35,15 @@ export declare class WebGPURenderer extends BatchRenderer {
35
35
  protected setRootTransform(a: number, b: number, c: number, d: number, tx: number, ty: number): void;
36
36
  protected setProjectionMatrix(): void;
37
37
  /**
38
- * 初始化 pipeline
38
+ * Initialise the pipeline
39
39
  */
40
40
  private createPipeline;
41
41
  /**
42
- * 初始化 device
42
+ * Initialise the device
43
43
  */
44
44
  private initDevice;
45
45
  /**
46
- * 初始化 renderPassDescriptor
46
+ * Initialise the renderPassDescriptor
47
47
  */
48
48
  private initRenderPassDescriptor;
49
49
  }
@@ -7,68 +7,69 @@ export declare class Container extends Vertex {
7
7
  isSort: boolean;
8
8
  type: string;
9
9
  /**
10
- * 所有子元素
10
+ * Every child
11
11
  */
12
12
  readonly children: Container[];
13
13
  /**
14
- * 用来标记 worldTransform 是否发生了改变
14
+ * Marks whether worldTransform changed
15
15
  */
16
16
  protected worldId: number;
17
17
  /**
18
- * 所有 batch
18
+ * Every batch
19
19
  */
20
20
  protected batches: Batch[];
21
21
  /**
22
- * batch 总数
22
+ * Number of batches
23
23
  */
24
24
  protected batchCount: number;
25
25
  /**
26
- * 场景结构版本号(仅根节点上的值有意义)。任何会改变“大数组该长什么样”的操作
27
- * (增删子节点、Graphics 重绘)都会冒泡到根节点并使其 +1,渲染器据此决定是否重建大数组。
26
+ * Scene structure version (only meaningful on the root). Anything that changes what the
27
+ * big array should look like — adding or removing a child, a Graphics redraw — bubbles to
28
+ * the root and increments it, and the renderer uses that to decide whether to rebuild.
28
29
  */
29
30
  structureVersion: number;
30
31
  constructor();
31
32
  /**
32
- * 标记场景结构已变化:冒泡到根节点并递增其版本号,触发下一帧的大数组重建。
33
+ * Mark the scene structure as changed: bubble to the root and bump its version, so the next frame rebuilds the big array.
33
34
  */
34
35
  protected markStructureDirty: () => void;
35
36
  /**
36
- * @description: 添加子元素
37
+ * @description: Add a child
37
38
  * @param {Container} child
38
39
  * @return {*}
39
40
  */
40
41
  addChild: (child: Container) => void;
41
42
  /**
42
- * @description:删除子元素
43
+ * @description: Remove a child
43
44
  * @param {Container} child
44
45
  * @return {void}
45
46
  */
46
47
  removeChild: (child: Container) => void;
47
48
  /**
48
- * @description: 根据 z-index 排序子元素
49
+ * @description: Sort children by z-index
49
50
  * @return {*}
50
51
  */
51
52
  sortChildren: () => void;
52
53
  /**
53
- * 渲染自身,在 container 上面没有东西要渲染,所以这个函数的内容为空
54
+ * Render itself — a container has nothing to render, so this is empty
54
55
  */
55
56
  protected renderCanvas(_render: CanvasRenderer): void;
56
57
  /**
57
- * 递归渲染以自身为根的整棵节点树
58
+ * Recursively render the whole tree rooted here
58
59
  */
59
60
  renderCanvasRecursive: (render: CanvasRenderer) => void;
60
61
  /**
61
- * 递归更新当前元素以及所有子元素的 transform
62
+ * Recursively update this element's transform and every child's
62
63
  */
63
64
  updateTransform: () => void;
64
65
  containsPoint: (p: Point) => boolean;
65
66
  /**
66
- * 构建自身的 batch。组容器自身没有可渲染内容,子类(如 Graphics)会重写此方法。
67
- * WebGL / WebGPU 后端统一通过 BatchRenderer buildArray 递归调用本方法。
67
+ * Build its own batches. A group has no content of its own, so subclasses such as
68
+ * Graphics override this. Both GPU backends reach it through BatchRenderer's buildArray.
68
69
  */
69
70
  buildBatches(_batchRenderer: BatchRenderer): void;
70
71
  /**
71
- * 更新自身的所有 batch 对应的大数组中的顶点
72
+ * Update this node's batched vertices inside the big array
72
73
  */
73
74
  updateBatches(_floatView: Float32Array): void;
74
75
  }
@@ -0,0 +1,125 @@
1
+ /**
2
+ * A request/response wrapper over a Web Worker.
3
+ *
4
+ * A native worker only knows "post a message" and "receive a message" — there is no notion
5
+ * of a request and its response: send two tasks concurrently, get two messages back, and
6
+ * there is no way to tell which is which. The standard fix is to number each request and
7
+ * echo that number in the response; this class packages that correlation logic (ids, a
8
+ * pending table, progress events, rejecting every in-flight request when the worker
9
+ * crashes) into something reusable.
10
+ *
11
+ * It also owns the worker's lifecycle: **the worker is created on the first `send`** (heavy
12
+ * work should not start at page load), and `dispose()` terminates it and rejects in-flight
13
+ * requests; a later `send` rebuilds it.
14
+ */
15
+ /** A response must at least echo the request id so the two can be paired */
16
+ export interface WorkerResponseBase {
17
+ operationId: number;
18
+ type?: string;
19
+ }
20
+ export interface WorkerClientOptions<Res extends WorkerResponseBase, Progress> {
21
+ /** How to create the worker, e.g. `() => new Worker(new URL('./my.worker.ts', import.meta.url), { type: 'module' })` */
22
+ create: () => Worker;
23
+ /** Whether this is a progress message — it does not settle the request, it only calls onProgress. Defaults to `type === 'progress'` */
24
+ isProgress?: (response: Res) => boolean;
25
+ /** Extract the progress payload from a progress message. Defaults to `response.progress` */
26
+ getProgress?: (response: Res) => Progress;
27
+ /** Whether this is an error message. Defaults to `type === 'error'` */
28
+ isError?: (response: Res) => boolean;
29
+ /** Error message text. Defaults to `response.message` */
30
+ getErrorMessage?: (response: Res) => string;
31
+ /** Per-request timeout in milliseconds; omit for none. A timeout only rejects that request, it does not terminate the worker */
32
+ timeout?: number;
33
+ }
34
+ /**
35
+ * @description: A worker client with request ids. The request type `Req` is defined by the
36
+ * caller; the client only fills in `operationId` and routes each response back to the right
37
+ * promise.
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * const client = new WorkerClient<MyRequest, MyResponse, { progress: number }>({
42
+ * create: () => new Worker(new URL('./nlp.worker.ts', import.meta.url), { type: 'module' }),
43
+ * });
44
+ * const loaded = await client.send({ type: 'load', modelId }, (p) => render(p.progress));
45
+ * client.dispose();
46
+ * ```
47
+ * The worker side only has to echo the `operationId` it received:
48
+ * ```ts
49
+ * self.onmessage = ({ data }) => self.postMessage({ operationId: data.operationId, type: 'result', ... });
50
+ * ```
51
+ */
52
+ export declare class WorkerClient<Req extends object, Res extends WorkerResponseBase, Progress = unknown> {
53
+ private worker;
54
+ private nextOperationId;
55
+ private pending;
56
+ private options;
57
+ constructor(options: WorkerClientOptions<Res, Progress>);
58
+ /** Whether the worker has been created (it is created on the first `send`) */
59
+ get active(): boolean;
60
+ /** Number of in-flight requests */
61
+ get pendingCount(): number;
62
+ /**
63
+ * @description: Send one request and await its matching response
64
+ * @param {Req} request request body (`operationId` is filled in by the client — do not pass it)
65
+ * @param {Function} onProgress progress callback, fired on progress messages without settling the request
66
+ * @param {Transferable[]} transfer objects whose ownership is transferred (ArrayBuffer and friends, avoiding the structured-clone copy)
67
+ * @return {Promise<Res>}
68
+ */
69
+ send: (request: Req, onProgress?: (progress: Progress) => void, transfer?: Transferable[]) => Promise<Res>;
70
+ /** @description: Terminate the worker and reject every in-flight request; the next `send` rebuilds it */
71
+ dispose: () => void;
72
+ private getWorker;
73
+ private settle;
74
+ private handleMessage;
75
+ private rejectAll;
76
+ }
77
+ /** A request always carries the id the client stamped on it */
78
+ export interface WorkerRequestBase {
79
+ operationId: number;
80
+ type?: string;
81
+ }
82
+ /** Handed to the handler so it can stream progress for the request it is currently serving */
83
+ export interface WorkerHandlerContext<Progress> {
84
+ /** Post a progress update. Does not settle the request; may be called any number of times. */
85
+ progress: (payload: Progress) => void;
86
+ }
87
+ export interface ServeWorkerOptions {
88
+ /**
89
+ * Where to listen. Defaults to `self` — override for a `MessagePort`, a nested worker, or
90
+ * a test double.
91
+ */
92
+ scope?: {
93
+ addEventListener: (type: 'message', listener: (event: MessageEvent) => void) => void;
94
+ removeEventListener: (type: 'message', listener: (event: MessageEvent) => void) => void;
95
+ postMessage: (message: unknown, transfer?: Transferable[]) => void;
96
+ };
97
+ /** Response `type` for a successful reply that the handler returned bare (non-object). Default `'result'`. */
98
+ resultType?: string;
99
+ }
100
+ /**
101
+ * @description: Serve requests inside a Web Worker, mirroring {@link WorkerClient} on the
102
+ * other end. Reads `operationId` off each request, awaits your handler, and posts the reply
103
+ * back with that same id.
104
+ *
105
+ * Failures become `{ operationId, type: 'error', message }` — the envelope `WorkerClient`
106
+ * rejects on by default. **Both synchronous throws and rejected promises are caught**: a sync
107
+ * throw inside `onmessage` would otherwise escape to the worker's error handler, which carries
108
+ * no id, so the client could only fail *every* in-flight request instead of the one that broke.
109
+ *
110
+ * @param {Function} handler receives the request and a context whose `progress()` streams updates
111
+ * @param {ServeWorkerOptions} options
112
+ * @return {Function} stop — removes the listener
113
+ * @example
114
+ * ```ts
115
+ * // my.worker.ts
116
+ * serveWorker<MyRequest, MyResponse, ModelProgress>(async (request, { progress }) => {
117
+ * if (request.type === 'load') {
118
+ * const device = await load(request.modelId, (p) => progress(p));
119
+ * return { type: 'loaded', device };
120
+ * }
121
+ * return { type: 'result', scores: await classify(request.lines) };
122
+ * });
123
+ * ```
124
+ */
125
+ export declare const serveWorker: <Req extends WorkerRequestBase, Res extends object = object, Progress = unknown>(handler: (request: Req, context: WorkerHandlerContext<Progress>) => Promise<Res> | Res, options?: ServeWorkerOptions) => (() => void);