1 | 'use strict';
|
2 | import type {
|
3 | MapperRawInputs,
|
4 | MapperOutputs,
|
5 | SharedValue,
|
6 | } from './commonTypes';
|
7 | import { isJest } from './PlatformChecker';
|
8 | import { runOnUI } from './threads';
|
9 | import { isSharedValue } from './isSharedValue';
|
10 |
|
11 | const IS_JEST = isJest();
|
12 |
|
13 | type MapperExtractedInputs = SharedValue[];
|
14 |
|
15 | type Mapper = {
|
16 | id: number;
|
17 | dirty: boolean;
|
18 | worklet: () => void;
|
19 | inputs: MapperExtractedInputs;
|
20 | outputs?: MapperOutputs;
|
21 | };
|
22 |
|
23 | function createMapperRegistry() {
|
24 | 'worklet';
|
25 | const mappers = new Map<number, Mapper>();
|
26 | let sortedMappers: Mapper[] = [];
|
27 |
|
28 | let runRequested = false;
|
29 | let processingMappers = false;
|
30 |
|
31 | function updateMappersOrder() {
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | const pre = new Map();
|
53 | mappers.forEach((mapper) => {
|
54 | if (mapper.outputs) {
|
55 | for (const output of mapper.outputs) {
|
56 | const preMappers = pre.get(output);
|
57 | if (preMappers === undefined) {
|
58 | pre.set(output, [mapper]);
|
59 | } else {
|
60 | preMappers.push(mapper);
|
61 | }
|
62 | }
|
63 | }
|
64 | });
|
65 | const visited = new Set();
|
66 | const newOrder: Mapper[] = [];
|
67 | function dfs(mapper: Mapper) {
|
68 | visited.add(mapper);
|
69 | for (const input of mapper.inputs) {
|
70 | const preMappers = pre.get(input);
|
71 | if (preMappers) {
|
72 | for (const preMapper of preMappers) {
|
73 | if (!visited.has(preMapper)) {
|
74 | dfs(preMapper);
|
75 | }
|
76 | }
|
77 | }
|
78 | }
|
79 | newOrder.push(mapper);
|
80 | }
|
81 | mappers.forEach((mapper) => {
|
82 | if (!visited.has(mapper)) {
|
83 | dfs(mapper);
|
84 | }
|
85 | });
|
86 | sortedMappers = newOrder;
|
87 | }
|
88 |
|
89 | function mapperRun() {
|
90 | runRequested = false;
|
91 | if (processingMappers) {
|
92 | return;
|
93 | }
|
94 | try {
|
95 | processingMappers = true;
|
96 | if (mappers.size !== sortedMappers.length) {
|
97 | updateMappersOrder();
|
98 | }
|
99 | for (const mapper of sortedMappers) {
|
100 | if (mapper.dirty) {
|
101 | mapper.dirty = false;
|
102 | mapper.worklet();
|
103 | }
|
104 | }
|
105 | } finally {
|
106 | processingMappers = false;
|
107 | }
|
108 | }
|
109 |
|
110 | function maybeRequestUpdates() {
|
111 | if (IS_JEST) {
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | mapperRun();
|
119 | } else if (!runRequested) {
|
120 | if (processingMappers) {
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | requestAnimationFrame(mapperRun);
|
133 | } else {
|
134 | queueMicrotask(mapperRun);
|
135 | }
|
136 | runRequested = true;
|
137 | }
|
138 | }
|
139 |
|
140 | function extractInputs(
|
141 | inputs: unknown,
|
142 | resultArray: MapperExtractedInputs
|
143 | ): MapperExtractedInputs {
|
144 | if (Array.isArray(inputs)) {
|
145 | for (const input of inputs) {
|
146 | input && extractInputs(input, resultArray);
|
147 | }
|
148 | } else if (isSharedValue(inputs)) {
|
149 | resultArray.push(inputs);
|
150 | } else if (Object.getPrototypeOf(inputs) === Object.prototype) {
|
151 |
|
152 |
|
153 |
|
154 | for (const element of Object.values(inputs as Record<string, unknown>)) {
|
155 | element && extractInputs(element, resultArray);
|
156 | }
|
157 | }
|
158 | return resultArray;
|
159 | }
|
160 |
|
161 | return {
|
162 | start: (
|
163 | mapperID: number,
|
164 | worklet: () => void,
|
165 | inputs: MapperRawInputs,
|
166 | outputs?: MapperOutputs
|
167 | ) => {
|
168 | const mapper: Mapper = {
|
169 | id: mapperID,
|
170 | dirty: true,
|
171 | worklet,
|
172 | inputs: extractInputs(inputs, []),
|
173 | outputs,
|
174 | };
|
175 | mappers.set(mapper.id, mapper);
|
176 | sortedMappers = [];
|
177 | for (const sv of mapper.inputs) {
|
178 | sv.addListener(mapper.id, () => {
|
179 | mapper.dirty = true;
|
180 | maybeRequestUpdates();
|
181 | });
|
182 | }
|
183 | maybeRequestUpdates();
|
184 | },
|
185 | stop: (mapperID: number) => {
|
186 | const mapper = mappers.get(mapperID);
|
187 | if (mapper) {
|
188 | mappers.delete(mapper.id);
|
189 | sortedMappers = [];
|
190 | for (const sv of mapper.inputs) {
|
191 | sv.removeListener(mapper.id);
|
192 | }
|
193 | }
|
194 | },
|
195 | };
|
196 | }
|
197 |
|
198 | let MAPPER_ID = 9999;
|
199 |
|
200 | export function startMapper(
|
201 | worklet: () => void,
|
202 | inputs: MapperRawInputs = [],
|
203 | outputs: MapperOutputs = []
|
204 | ): number {
|
205 | const mapperID = (MAPPER_ID += 1);
|
206 |
|
207 | runOnUI(() => {
|
208 | let mapperRegistry = global.__mapperRegistry;
|
209 | if (mapperRegistry === undefined) {
|
210 | mapperRegistry = global.__mapperRegistry = createMapperRegistry();
|
211 | }
|
212 | mapperRegistry.start(mapperID, worklet, inputs, outputs);
|
213 | })();
|
214 |
|
215 | return mapperID;
|
216 | }
|
217 |
|
218 | export function stopMapper(mapperID: number): void {
|
219 | runOnUI(() => {
|
220 | const mapperRegistry = global.__mapperRegistry;
|
221 | mapperRegistry?.stop(mapperID);
|
222 | })();
|
223 | }
|