@lynx-js/react-canary 0.121.2 → 0.121.3-canary-20260623-04f031e3
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/CHANGELOG.md +56 -61
- package/package.json +1 -1
- package/runtime/lib/core/background-function/exec-map.d.ts +11 -0
- package/runtime/lib/core/background-function/exec-map.js +39 -0
- package/runtime/lib/core/background-function/exec-map.js.map +1 -0
- package/runtime/lib/core/background-function/handle.d.ts +8 -0
- package/runtime/lib/core/background-function/handle.js +25 -0
- package/runtime/lib/core/background-function/handle.js.map +1 -0
- package/runtime/lib/core/background-function/run-on-background.d.ts +8 -0
- package/runtime/lib/{snapshot/worklet/call/runOnBackground.js → core/background-function/run-on-background.js} +39 -33
- package/runtime/lib/core/background-function/run-on-background.js.map +1 -0
- package/runtime/lib/core/initData.js +35 -0
- package/runtime/lib/core/initData.js.map +1 -1
- package/runtime/lib/core/lynx-page-data.js +6 -0
- package/runtime/lib/core/lynx-page-data.js.map +1 -1
- package/runtime/lib/core/lynx-update-data.js +6 -0
- package/runtime/lib/core/lynx-update-data.js.map +1 -1
- package/runtime/lib/core/runtime-destroy.d.ts +4 -0
- package/runtime/lib/core/runtime-destroy.js +18 -0
- package/runtime/lib/core/runtime-destroy.js.map +1 -0
- package/runtime/lib/core/thread-function-call/main-thread.d.ts +5 -0
- package/runtime/lib/core/thread-function-call/main-thread.js +69 -0
- package/runtime/lib/core/thread-function-call/main-thread.js.map +1 -0
- package/runtime/lib/core/thread-function-call/return-value.d.ts +2 -0
- package/runtime/lib/core/thread-function-call/return-value.js +67 -0
- package/runtime/lib/core/thread-function-call/return-value.js.map +1 -0
- package/runtime/lib/element-template/background/attr-slots.d.ts +1 -1
- package/runtime/lib/element-template/background/attr-slots.js +8 -7
- package/runtime/lib/element-template/background/attr-slots.js.map +1 -1
- package/runtime/lib/element-template/background/commit-hook.js +79 -59
- package/runtime/lib/element-template/background/commit-hook.js.map +1 -1
- package/runtime/lib/element-template/background/destroy.js +4 -0
- package/runtime/lib/element-template/background/destroy.js.map +1 -1
- package/runtime/lib/element-template/background/hydrate.js +23 -4
- package/runtime/lib/element-template/background/hydrate.js.map +1 -1
- package/runtime/lib/element-template/background/hydration-listener.js +33 -14
- package/runtime/lib/element-template/background/hydration-listener.js.map +1 -1
- package/runtime/lib/element-template/background/instance.d.ts +1 -1
- package/runtime/lib/element-template/background/instance.js +10 -3
- package/runtime/lib/element-template/background/instance.js.map +1 -1
- package/runtime/lib/element-template/background/render-scope.d.ts +4 -0
- package/runtime/lib/element-template/background/render-scope.js +43 -0
- package/runtime/lib/element-template/background/render-scope.js.map +1 -0
- package/runtime/lib/element-template/index.d.ts +2 -0
- package/runtime/lib/element-template/index.js +2 -0
- package/runtime/lib/element-template/index.js.map +1 -1
- package/runtime/lib/element-template/internal.d.ts +4 -1
- package/runtime/lib/element-template/internal.js +5 -5
- package/runtime/lib/element-template/internal.js.map +1 -1
- package/runtime/lib/element-template/lynx/env.js +2 -0
- package/runtime/lib/element-template/lynx/env.js.map +1 -1
- package/runtime/lib/element-template/native/index.js +2 -0
- package/runtime/lib/element-template/native/index.js.map +1 -1
- package/runtime/lib/element-template/native/mts-destroy.js +4 -0
- package/runtime/lib/element-template/native/mts-destroy.js.map +1 -1
- package/runtime/lib/element-template/native/patch-listener.js +41 -16
- package/runtime/lib/element-template/native/patch-listener.js.map +1 -1
- package/runtime/lib/element-template/native/reload-main-thread.js +4 -0
- package/runtime/lib/element-template/native/reload-main-thread.js.map +1 -1
- package/runtime/lib/element-template/prop-adapters/spread.d.ts +1 -4
- package/runtime/lib/element-template/prop-adapters/spread.js +1 -1
- package/runtime/lib/element-template/prop-adapters/spread.js.map +1 -1
- package/runtime/lib/element-template/protocol/types.d.ts +5 -2
- package/runtime/lib/element-template/protocol/types.js.map +1 -1
- package/runtime/lib/element-template/protocol/update-event.d.ts +15 -0
- package/runtime/lib/element-template/protocol/update-event.js +24 -0
- package/runtime/lib/element-template/protocol/update-event.js.map +1 -0
- package/runtime/lib/element-template/runtime/patch.d.ts +1 -1
- package/runtime/lib/element-template/runtime/patch.js +18 -2
- package/runtime/lib/element-template/runtime/patch.js.map +1 -1
- package/runtime/lib/element-template/runtime/template/attr-slot-plan.d.ts +7 -5
- package/runtime/lib/element-template/runtime/template/attr-slot-plan.js +17 -4
- package/runtime/lib/element-template/runtime/template/attr-slot-plan.js.map +1 -1
- package/runtime/lib/element-template/runtime/template/handle.js +7 -1
- package/runtime/lib/element-template/runtime/template/handle.js.map +1 -1
- package/runtime/lib/element-template/runtime/template/main-thread-background-function.d.ts +10 -0
- package/runtime/lib/element-template/runtime/template/main-thread-background-function.js +30 -0
- package/runtime/lib/element-template/runtime/template/main-thread-background-function.js.map +1 -0
- package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.d.ts +15 -0
- package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js +91 -0
- package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js.map +1 -0
- package/runtime/lib/element-template/runtime/template/main-thread-event-ctx.d.ts +15 -0
- package/runtime/lib/element-template/runtime/template/main-thread-event-ctx.js +29 -0
- package/runtime/lib/element-template/runtime/template/main-thread-event-ctx.js.map +1 -0
- package/runtime/lib/element-template/runtime/template/main-thread-function.d.ts +6 -0
- package/runtime/lib/element-template/runtime/template/main-thread-function.js +24 -0
- package/runtime/lib/element-template/runtime/template/main-thread-function.js.map +1 -0
- package/runtime/lib/internal.d.ts +1 -1
- package/runtime/lib/internal.js +1 -1
- package/runtime/lib/internal.js.map +1 -1
- package/runtime/lib/lynx-api.d.ts +1 -1
- package/runtime/lib/lynx-api.js +1 -1
- package/runtime/lib/lynx-api.js.map +1 -1
- package/runtime/lib/{snapshot/worklet/indexMap.d.ts → shared/index-map.d.ts} +2 -2
- package/runtime/lib/{snapshot/worklet/indexMap.js → shared/index-map.js} +5 -3
- package/runtime/lib/shared/index-map.js.map +1 -0
- package/runtime/lib/snapshot/lifecycle/patch/commit.js +1 -1
- package/runtime/lib/snapshot/lifecycle/patch/commit.js.map +1 -1
- package/runtime/lib/snapshot/lynx/tt.js +1 -1
- package/runtime/lib/snapshot/lynx/tt.js.map +1 -1
- package/runtime/lib/snapshot/worklet/call/runOnMainThread.js +10 -31
- package/runtime/lib/snapshot/worklet/call/runOnMainThread.js.map +1 -1
- package/runtime/lib/snapshot/worklet/ctx.js +2 -2
- package/runtime/lib/snapshot/worklet/ctx.js.map +1 -1
- package/runtime/lib/snapshot/worklet/destroy.d.ts +0 -1
- package/runtime/lib/snapshot/worklet/destroy.js +2 -5
- package/runtime/lib/snapshot/worklet/destroy.js.map +1 -1
- package/runtime/lib/tsconfig.tsbuildinfo +1 -1
- package/runtime/lib/worklet-runtime/bindings/observers.d.ts +7 -0
- package/runtime/lib/worklet-runtime/bindings/observers.js +14 -1
- package/runtime/lib/worklet-runtime/bindings/observers.js.map +1 -1
- package/runtime/lib/worklet-runtime/delayRunOnBackground.d.ts +2 -1
- package/runtime/lib/worklet-runtime/delayRunOnBackground.js +6 -2
- package/runtime/lib/worklet-runtime/delayRunOnBackground.js.map +1 -1
- package/runtime/lib/worklet-runtime/hydrate.js +3 -0
- package/runtime/lib/worklet-runtime/hydrate.js.map +1 -1
- package/runtime/lib/worklet-runtime/jsFunctionLifecycle.d.ts +1 -1
- package/runtime/lib/worklet-runtime/jsFunctionLifecycle.js +1 -1
- package/runtime/lib/worklet-runtime/jsFunctionLifecycle.js.map +1 -1
- package/runtime/lib/worklet-runtime/workletRuntime.js +17 -2
- package/runtime/lib/worklet-runtime/workletRuntime.js.map +1 -1
- package/runtime/worklet-runtime/dev.js +26 -8
- package/runtime/worklet-runtime/main.js +1 -1
- package/transform/dist/wasm.cjs +1 -1
- package/runtime/lib/snapshot/worklet/call/delayedRunOnMainThreadData.d.ts +0 -3
- package/runtime/lib/snapshot/worklet/call/delayedRunOnMainThreadData.js +0 -10
- package/runtime/lib/snapshot/worklet/call/delayedRunOnMainThreadData.js.map +0 -1
- package/runtime/lib/snapshot/worklet/call/execMap.d.ts +0 -1
- package/runtime/lib/snapshot/worklet/call/execMap.js +0 -43
- package/runtime/lib/snapshot/worklet/call/execMap.js.map +0 -1
- package/runtime/lib/snapshot/worklet/call/functionCall.d.ts +0 -1
- package/runtime/lib/snapshot/worklet/call/functionCall.js +0 -33
- package/runtime/lib/snapshot/worklet/call/functionCall.js.map +0 -1
- package/runtime/lib/snapshot/worklet/call/runOnBackground.d.ts +0 -20
- package/runtime/lib/snapshot/worklet/call/runOnBackground.js.map +0 -1
- package/runtime/lib/snapshot/worklet/call/transformToWorklet.d.ts +0 -1
- package/runtime/lib/snapshot/worklet/call/transformToWorklet.js +0 -22
- package/runtime/lib/snapshot/worklet/call/transformToWorklet.js.map +0 -1
- package/runtime/lib/snapshot/worklet/indexMap.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @lynx-js/react
|
|
2
2
|
|
|
3
|
+
## 0.121.3-canary-20260623053631-04f031e362a432f1da692465f0c64b9a2f15fa89
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Report a development error when a page-data reset is combined with `withInitDataInState`. ([#2869](https://github.com/lynx-family/lynx-stack/pull/2869))
|
|
8
|
+
|
|
9
|
+
`withInitDataInState` merges `lynx.__initData` into the wrapped component's state, so resetting the page data — `updatePage(..., { resetPageData: true })` on the main thread, or `updateData(..., { type: 'reset' })` on the background thread — cannot drop the keys the reset removed; the component keeps rendering the stale keys. A dev-only error is now reported (once per reset path) on both threads, pointing to `useInitData()` instead. The check is gated by `__DEV__` and is fully removed from production builds.
|
|
10
|
+
|
|
11
|
+
- Fix `withInitDataInState` not refreshing the component state on a main-thread re-render. ([#2868](https://github.com/lynx-family/lynx-stack/pull/2868))
|
|
12
|
+
|
|
13
|
+
`withInitDataInState` injected `lynx.__initData` into the class component's state only in the constructor, and its `onDataChanged` listener was active on the background thread only. On the main thread the component instance is reused across an `updatePage` re-render (the constructor never re-runs), so the injected state stayed frozen at the data from the first render. This surfaces whenever the main thread re-renders the screen itself before hydration (an `updatePage` while `firstScreenSyncTiming` is `'jsReady'`). The HOC now re-reads `lynx.__initData` on every render via `getDerivedStateFromProps`, composing with — and not overriding — the wrapped component's own `getDerivedStateFromProps`.
|
|
14
|
+
|
|
3
15
|
## 0.121.2
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -9,13 +21,13 @@
|
|
|
9
21
|
Previously, the compat transform could short-circuit while scanning opening attributes and treat:
|
|
10
22
|
|
|
11
23
|
```jsx
|
|
12
|
-
<Component removeComponentElement={true} {...props}
|
|
24
|
+
<Component removeComponentElement={true} {...props} />
|
|
13
25
|
```
|
|
14
26
|
|
|
15
27
|
differently from:
|
|
16
28
|
|
|
17
29
|
```jsx
|
|
18
|
-
<Component {...props} removeComponentElement={true}
|
|
30
|
+
<Component {...props} removeComponentElement={true} />
|
|
19
31
|
```
|
|
20
32
|
|
|
21
33
|
The transform now collects the full opening-attribute state first, so both prop orders compile the same way in normal and `compilerOnly` modes.
|
|
@@ -41,7 +53,7 @@
|
|
|
41
53
|
- Support `React.createElement(type, props, children)` API. ([#2360](https://github.com/lynx-family/lynx-stack/pull/2360))
|
|
42
54
|
|
|
43
55
|
```jsx
|
|
44
|
-
React.createElement(
|
|
56
|
+
React.createElement("view", { style }, <text>hello</text>);
|
|
45
57
|
// equivalent to
|
|
46
58
|
<view style={style}>
|
|
47
59
|
<text>hello</text>
|
|
@@ -103,7 +115,6 @@
|
|
|
103
115
|
- Fix ref callbacks not being cleaned up or re-applied correctly when the ref at the same element slot changes across rerenders that happen before hydration (e.g. a `useEffect` triggering `setState` during the initial background render). ([#2500](https://github.com/lynx-family/lynx-stack/pull/2500))
|
|
104
116
|
|
|
105
117
|
- fix: reduce redundant updates for main-thread handlers and gestures ([#2188](https://github.com/lynx-family/lynx-stack/pull/2188))
|
|
106
|
-
|
|
107
118
|
- Updates are faster when the main-thread event handler or gesture object is stable across rerenders (fewer unnecessary native updates).
|
|
108
119
|
- Spread props rerenders that don't semantically change the handler/gesture no longer trigger redundant updates.
|
|
109
120
|
- Removing a gesture from spread props reliably clears the gesture state on the target element.
|
|
@@ -123,7 +134,6 @@
|
|
|
123
134
|
- Remove stale gestures when gestures are removed ([#2297](https://github.com/lynx-family/lynx-stack/pull/2297))
|
|
124
135
|
|
|
125
136
|
- Trace refactor ([#2466](https://github.com/lynx-family/lynx-stack/pull/2466))
|
|
126
|
-
|
|
127
137
|
- Remove `ReactLynx::renderOpcodes` from the trace
|
|
128
138
|
- Use `ReactLynx::transferRoot` to measure the time spent transferring the root to the background thread
|
|
129
139
|
|
|
@@ -134,8 +144,8 @@
|
|
|
134
144
|
Create a config file `rstest.config.ts` with the following content:
|
|
135
145
|
|
|
136
146
|
```ts
|
|
137
|
-
import { defineConfig } from
|
|
138
|
-
import { withLynxConfig } from
|
|
147
|
+
import { defineConfig } from "@rstest/core";
|
|
148
|
+
import { withLynxConfig } from "@lynx-js/react/testing-library/rstest-config";
|
|
139
149
|
|
|
140
150
|
export default defineConfig({
|
|
141
151
|
extends: withLynxConfig(),
|
|
@@ -185,7 +195,6 @@
|
|
|
185
195
|
### Minor Changes
|
|
186
196
|
|
|
187
197
|
- feat: export `GlobalPropsProvider`, `GlobalPropsConsumer`, `useGlobalProps` and `useGlobalPropsChanged` for `__globalProps` ([#2346](https://github.com/lynx-family/lynx-stack/pull/2346))
|
|
188
|
-
|
|
189
198
|
- `GlobalPropsProvider`: A Provider component that accepts `children`. It is used to provide the `lynx.__globalProps` context.
|
|
190
199
|
- `GlobalPropsConsumer`: A Consumer component that accepts a function as a child. It is used to consume the `lynx.__globalProps` context.
|
|
191
200
|
- `useGlobalProps`: A hook that returns the `lynx.__globalProps` object. It triggers a re-render when `lynx.__globalProps` changes.
|
|
@@ -200,7 +209,6 @@
|
|
|
200
209
|
Upgrade preact from [f7693b72](https://github.com/preactjs/preact/commit/f7693b72ecb4a40c66e6e47f54e2d4edc374c9f0) to [55254ef7](https://github.com/preactjs/preact/commit/55254ef7021e563cc1a86fb816058964a1b6a29a), see diffs at [f7693b72...55254ef7](https://github.com/preactjs/preact/compare/f7693b72ecb4a40c66e6e47f54e2d4edc374c9f0...preactjs:preact:55254ef7021e563cc1a86fb816058964a1b6a29a?expand=1).
|
|
201
210
|
|
|
202
211
|
- feat: add `globalPropsMode` option to `PluginReactLynxOptions` ([#2346](https://github.com/lynx-family/lynx-stack/pull/2346))
|
|
203
|
-
|
|
204
212
|
- When configured to `"event"`, `updateGlobalProps` will only trigger a global event and skip the `runWithForce` flow.
|
|
205
213
|
- Defaults to `"reactive"`, which means `updateGlobalProps` will trigger re-render automatically.
|
|
206
214
|
|
|
@@ -220,7 +228,6 @@
|
|
|
220
228
|
|
|
221
229
|
- Improve React runtime hook profiling. ([#2235](https://github.com/lynx-family/lynx-stack/pull/2235))
|
|
222
230
|
Enable Profiling recording first, then enter the target page so the trace includes full render/hydrate phases.
|
|
223
|
-
|
|
224
231
|
- Record trace events for `useEffect` / `useLayoutEffect` hook entry, callback, and cleanup phases.
|
|
225
232
|
- Log trace events for `useState` setter calls.
|
|
226
233
|
- Wire `profileFlowId` support in debug profile utilities and attach flow IDs to related hook traces.
|
|
@@ -269,7 +276,6 @@
|
|
|
269
276
|
- Bump `swc_core` v56. ([#2154](https://github.com/lynx-family/lynx-stack/pull/2154))
|
|
270
277
|
|
|
271
278
|
- Use `disableDeprecatedWarning` option to suppress BROKEN warnings during compilation. ([#2157](https://github.com/lynx-family/lynx-stack/pull/2157))
|
|
272
|
-
|
|
273
279
|
1. BROKEN: `getNodeRef`/`getNodeRefFromRoot`/`createSelectorQuery` on component instance is broken and MUST be migrated in ReactLynx 3.0, please use ref or lynx.createSelectorQuery instead.
|
|
274
280
|
2. BROKEN: `getElementById` on component instance is broken and MUST be migrated in ReactLynx 3.0, please use ref or lynx.getElementById instead.
|
|
275
281
|
|
|
@@ -307,9 +313,9 @@
|
|
|
307
313
|
|
|
308
314
|
```typescript
|
|
309
315
|
function getStyle(ele: MainThread.Element) {
|
|
310
|
-
|
|
311
|
-
const width = ele.getComputedStyleProperty(
|
|
312
|
-
const transformMatrix = ele.getComputedStyleProperty(
|
|
316
|
+
"main thread";
|
|
317
|
+
const width = ele.getComputedStyleProperty("width"); // Returns 300px
|
|
318
|
+
const transformMatrix = ele.getComputedStyleProperty("transform"); // Returns matrix(2, 0, 0, 2, 200, 400)
|
|
313
319
|
}
|
|
314
320
|
```
|
|
315
321
|
|
|
@@ -324,7 +330,7 @@
|
|
|
324
330
|
```ts
|
|
325
331
|
const LazyComponent = lazy(async () => {
|
|
326
332
|
try {
|
|
327
|
-
const mod = await import(
|
|
333
|
+
const mod = await import("./lazy-bundle");
|
|
328
334
|
return mod.default;
|
|
329
335
|
} catch (error) {
|
|
330
336
|
console.error(`Lazy Bundle load failed message: ${error.message}`);
|
|
@@ -352,14 +358,13 @@
|
|
|
352
358
|
- Auto define lynx.loadLazyBundle when using `import(/* relative path */)`. ([#1956](https://github.com/lynx-family/lynx-stack/pull/1956))
|
|
353
359
|
|
|
354
360
|
- feat: support declaring cross-thread shared modules via Import Attributes, enabling Main Thread Functions to call standard JS functions directly. ([#1968](https://github.com/lynx-family/lynx-stack/pull/1968))
|
|
355
|
-
|
|
356
361
|
- Usage: Add `with { runtime: "shared" }` to the `import` statement. For example:
|
|
357
362
|
|
|
358
363
|
```ts
|
|
359
|
-
import { func } from
|
|
364
|
+
import { func } from "./utils.js" with { runtime: "shared" };
|
|
360
365
|
|
|
361
366
|
function worklet() {
|
|
362
|
-
|
|
367
|
+
"main thread";
|
|
363
368
|
func(); // callable inside a main thread function
|
|
364
369
|
}
|
|
365
370
|
```
|
|
@@ -375,13 +380,11 @@
|
|
|
375
380
|
- **BREAKING CHANGE**: Delay the `createSnapshot` operation to `Snapshot` constructor to speed up IFR. ([#1899](https://github.com/lynx-family/lynx-stack/pull/1899))
|
|
376
381
|
|
|
377
382
|
This change refactors how snapshots are created and registered:
|
|
378
|
-
|
|
379
383
|
- Removed the `entryUniqID` function
|
|
380
384
|
- Snapshots are now lazily created via `snapshotCreatorMap` instead of eagerly at bundle load time
|
|
381
385
|
- Snapshot IDs are generated at compile time and only prefixed with `${globDynamicComponentEntry}:` for standalone lazy bundles
|
|
382
386
|
|
|
383
387
|
**⚠️ Lazy Bundle Compatibility:**
|
|
384
|
-
|
|
385
388
|
- **Backward compatibility (new runtime → old lazy bundles)**: ✅ **Supported**. Old lazy bundles will work with the new runtime.
|
|
386
389
|
|
|
387
390
|
- **Forward compatibility (old runtime → new lazy bundles)**: ❌ **NOT Supported**. Lower version consumers **will not be able to load lazy bundles produced by this version** due to the changed snapshot creation mechanism.
|
|
@@ -436,11 +439,11 @@
|
|
|
436
439
|
- Support testing React Compiler in testing library. Enable React Compiler by setting the `experimental_enableReactCompiler` option of `createVitestConfig` to `true`. ([#1269](https://github.com/lynx-family/lynx-stack/pull/1269))
|
|
437
440
|
|
|
438
441
|
```js
|
|
439
|
-
import { defineConfig, mergeConfig } from
|
|
440
|
-
import { createVitestConfig } from
|
|
442
|
+
import { defineConfig, mergeConfig } from "vitest/config";
|
|
443
|
+
import { createVitestConfig } from "@lynx-js/react/testing-library/vitest-config";
|
|
441
444
|
|
|
442
445
|
const defaultConfig = await createVitestConfig({
|
|
443
|
-
runtimePkgName:
|
|
446
|
+
runtimePkgName: "@lynx-js/react",
|
|
444
447
|
experimental_enableReactCompiler: true,
|
|
445
448
|
});
|
|
446
449
|
|
|
@@ -470,7 +473,7 @@
|
|
|
470
473
|
```tsx
|
|
471
474
|
function App() {
|
|
472
475
|
function handleInnerTap(event: MainThread.TouchEvent) {
|
|
473
|
-
|
|
476
|
+
"main thread";
|
|
474
477
|
event.stopPropagation();
|
|
475
478
|
// Or stop immediate propagation with
|
|
476
479
|
// event.stopImmediatePropagation();
|
|
@@ -568,10 +571,10 @@
|
|
|
568
571
|
- Add `animate` API in Main Thread Script(MTS), so you can now control a CSS animation imperatively ([#1534](https://github.com/lynx-family/lynx-stack/pull/1534))
|
|
569
572
|
|
|
570
573
|
```ts
|
|
571
|
-
import type { MainThread } from
|
|
574
|
+
import type { MainThread } from "@lynx-js/types";
|
|
572
575
|
|
|
573
576
|
function startAnimation(ele: MainThread.Element) {
|
|
574
|
-
|
|
577
|
+
"main thread";
|
|
575
578
|
const animation = ele.animate([{ opacity: 0 }, { opacity: 1 }], {
|
|
576
579
|
duration: 3000,
|
|
577
580
|
});
|
|
@@ -600,13 +603,12 @@
|
|
|
600
603
|
- Supports `recyclable` attribute in `<list-item>` to control whether the list item is recyclable. The `recyclable` attribute depends on Lynx Engine 3.4 or later. ([#1388](https://github.com/lynx-family/lynx-stack/pull/1388))
|
|
601
604
|
|
|
602
605
|
```jsx
|
|
603
|
-
<list-item recyclable={false}
|
|
606
|
+
<list-item recyclable={false} />
|
|
604
607
|
```
|
|
605
608
|
|
|
606
609
|
- feat: Support using a host element as direct child of Suspense ([#1455](https://github.com/lynx-family/lynx-stack/pull/1455))
|
|
607
610
|
|
|
608
611
|
- Add profile in production build: ([#1336](https://github.com/lynx-family/lynx-stack/pull/1336))
|
|
609
|
-
|
|
610
612
|
1. `diff:__COMPONENT_NAME__`: how long ReactLynx diff took.
|
|
611
613
|
2. `render:__COMPONENT_NAME__`: how long your render function took.
|
|
612
614
|
3. `setState`: an instant trace event, indicate when your setState was called.
|
|
@@ -621,7 +623,7 @@
|
|
|
621
623
|
|
|
622
624
|
```ts
|
|
623
625
|
function handleTap() {
|
|
624
|
-
|
|
626
|
+
"main thread";
|
|
625
627
|
// The following check always returned false before this fix
|
|
626
628
|
if (myHandleTap) {
|
|
627
629
|
runOnBackground(myHandleTap)();
|
|
@@ -644,7 +646,6 @@
|
|
|
644
646
|
- feat: Force synchronous rendering for background initial renders to support Suspense fallbacks ([#1323](https://github.com/lynx-family/lynx-stack/pull/1323))
|
|
645
647
|
|
|
646
648
|
- Introduces `@lynx-js/react/compat` submodule exporting Preact implementations of: ([#1316](https://github.com/lynx-family/lynx-stack/pull/1316))
|
|
647
|
-
|
|
648
649
|
- `startTransition`
|
|
649
650
|
- `useTransition`
|
|
650
651
|
|
|
@@ -682,10 +683,10 @@
|
|
|
682
683
|
Add the import to `@lynx-js/react/debug` at the first line of the entry:
|
|
683
684
|
|
|
684
685
|
```js
|
|
685
|
-
import
|
|
686
|
-
import { root } from
|
|
686
|
+
import "@lynx-js/react/debug";
|
|
687
|
+
import { root } from "@lynx-js/react";
|
|
687
688
|
|
|
688
|
-
import { App } from
|
|
689
|
+
import { App } from "./App.jsx";
|
|
689
690
|
|
|
690
691
|
root.render(<App />);
|
|
691
692
|
```
|
|
@@ -695,9 +696,9 @@
|
|
|
695
696
|
For example, you can use it like this:
|
|
696
697
|
|
|
697
698
|
```jsx
|
|
698
|
-
<list-item defer={{ unmountRecycled: true }} item-key=
|
|
699
|
+
<list-item defer={{ unmountRecycled: true }} item-key="1">
|
|
699
700
|
<WillBeUnmountIfRecycled />
|
|
700
|
-
</list-item
|
|
701
|
+
</list-item>
|
|
701
702
|
```
|
|
702
703
|
|
|
703
704
|
Now the component will be unmounted when it is recycled, which can help with performance in certain scenarios.
|
|
@@ -705,7 +706,7 @@
|
|
|
705
706
|
- Avoid some unexpected `__SetAttribute` in hydrate when `undefined` is passed as an attribute value to intrinsic elements, for example: ([#1318](https://github.com/lynx-family/lynx-stack/pull/1318))
|
|
706
707
|
|
|
707
708
|
```jsx
|
|
708
|
-
<image async-mode={undefined}
|
|
709
|
+
<image async-mode={undefined} />
|
|
709
710
|
```
|
|
710
711
|
|
|
711
712
|
## 0.111.1
|
|
@@ -744,14 +745,13 @@
|
|
|
744
745
|
- Fixed: An issue where the `lynxViewDidUpdate` callback did not trigger when data was updated from native. ([#1171](https://github.com/lynx-family/lynx-stack/pull/1171))
|
|
745
746
|
|
|
746
747
|
Notice:
|
|
747
|
-
|
|
748
748
|
- Even if no data changes are actually processed after calling `updateData()`, the `lynxViewDidUpdate` callback will still be triggered.
|
|
749
749
|
- Only one `lynxViewDidUpdate` callback will be triggered per render cycle. Consequently, if multiple `updateData()` calls are made within a single cycle but the data updates are batched, the number of `lynxViewDidUpdate` callbacks triggered may be less than the number of `updateData()` calls.
|
|
750
750
|
|
|
751
751
|
- Supports `act` in testing library. ([#1182](https://github.com/lynx-family/lynx-stack/pull/1182))
|
|
752
752
|
|
|
753
753
|
```js
|
|
754
|
-
import { act } from
|
|
754
|
+
import { act } from "@lynx-js/react/testing-library";
|
|
755
755
|
|
|
756
756
|
act(() => {
|
|
757
757
|
// ...
|
|
@@ -771,14 +771,12 @@
|
|
|
771
771
|
- Fixed closure variable capture issue in effect hooks to prevent stale values and ensured proper execution order between refs, effects, and event handlers. ([#770](https://github.com/lynx-family/lynx-stack/pull/770))
|
|
772
772
|
|
|
773
773
|
**Breaking Changes**:
|
|
774
|
-
|
|
775
774
|
- The execution timing of `ref`, `useEffect()` callback, `componentDidMount`, `componentDidUpdate`, `componentWillUnmount` and the callback of `setState` have been moved forward. These effects will now execute before hydration is complete, rather than waiting for the main thread update to complete.
|
|
776
775
|
- For components inside `<list />`, `ref` callbacks will now be triggered during background thread rendering, regardless of component visibility. If your code depends on component visibility timing, use `main-thread:ref` instead of regular `ref`.
|
|
777
776
|
|
|
778
777
|
### Patch Changes
|
|
779
778
|
|
|
780
779
|
- Fixed two memory leaks: ([#1071](https://github.com/lynx-family/lynx-stack/pull/1071))
|
|
781
|
-
|
|
782
780
|
1. When JSX is rendered on the main thread and removed, FiberElement can still be referenced by `__root.__jsx` through `props.children`;
|
|
783
781
|
|
|
784
782
|
2. When the SnapshotInstance tree is removed from the root node, its child nodes form a cycle reference because the `__previousSibling` and `__nextSibling` properties point to each other, thus causing a FiberElement leak.
|
|
@@ -857,8 +855,7 @@
|
|
|
857
855
|
* 3: Full Resolution - Batch render with async property and element tree resolution for list item subtree
|
|
858
856
|
*/
|
|
859
857
|
experimental-batch-render-strategy={3}
|
|
860
|
-
>
|
|
861
|
-
</list>;
|
|
858
|
+
></list>
|
|
862
859
|
```
|
|
863
860
|
|
|
864
861
|
- rename @lynx-js/test-environment to @lynx-js/testing-environment ([#704](https://github.com/lynx-family/lynx-stack/pull/704))
|
|
@@ -884,7 +881,6 @@
|
|
|
884
881
|
### Minor Changes
|
|
885
882
|
|
|
886
883
|
- Some of the timing keys are renamed to match the naming convention of the Lynx Engine. ([#438](https://github.com/lynx-family/lynx-stack/pull/438))
|
|
887
|
-
|
|
888
884
|
- `update_set_state_trigger` -> `updateSetStateTrigger`
|
|
889
885
|
- `update_diff_vdom_start` -> `updateDiffVdomStart`
|
|
890
886
|
- `update_diff_vdom_end` -> `updateDiffVdomEnd`
|
|
@@ -976,7 +972,7 @@
|
|
|
976
972
|
You can now use `useErrorBoundary` it in TypeScript like this:
|
|
977
973
|
|
|
978
974
|
```tsx
|
|
979
|
-
import { useErrorBoundary } from
|
|
975
|
+
import { useErrorBoundary } from "@lynx-js/react";
|
|
980
976
|
```
|
|
981
977
|
|
|
982
978
|
- Modified the format of data sent from background threads to the main thread. ([#207](https://github.com/lynx-family/lynx-stack/pull/207))
|
|
@@ -1028,13 +1024,13 @@
|
|
|
1028
1024
|
Now you can get the return value from `runOnBackground()` and `runOnMainThread()`, which enables more flexible data flow between the main thread and the background thread.
|
|
1029
1025
|
|
|
1030
1026
|
```js
|
|
1031
|
-
import { runOnBackground } from
|
|
1027
|
+
import { runOnBackground } from "@lynx-js/react";
|
|
1032
1028
|
|
|
1033
1029
|
const onTap = async () => {
|
|
1034
|
-
|
|
1030
|
+
"main thread";
|
|
1035
1031
|
const text = await runOnBackground(() => {
|
|
1036
|
-
|
|
1037
|
-
return
|
|
1032
|
+
"background only";
|
|
1033
|
+
return "Hello, world!";
|
|
1038
1034
|
})();
|
|
1039
1035
|
console.log(text);
|
|
1040
1036
|
};
|
|
@@ -1069,9 +1065,9 @@
|
|
|
1069
1065
|
|
|
1070
1066
|
```ts
|
|
1071
1067
|
// These imports will be removed from the final bundle
|
|
1072
|
-
import type { Foo } from
|
|
1073
|
-
import { type Bar } from
|
|
1074
|
-
import { xyz } from
|
|
1068
|
+
import type { Foo } from "xyz";
|
|
1069
|
+
import { type Bar } from "xyz";
|
|
1070
|
+
import { xyz } from "xyz"; // When xyz is not used
|
|
1075
1071
|
```
|
|
1076
1072
|
|
|
1077
1073
|
See [TypeScript - verbatimModuleSyntax](https://www.typescriptlang.org/tsconfig/#verbatimModuleSyntax) for details.
|
|
@@ -1111,7 +1107,7 @@
|
|
|
1111
1107
|
const f = undefined;
|
|
1112
1108
|
|
|
1113
1109
|
function mts() {
|
|
1114
|
-
|
|
1110
|
+
"main thread";
|
|
1115
1111
|
// throws in background rendering
|
|
1116
1112
|
f && runOnBackground(f)();
|
|
1117
1113
|
}
|
|
@@ -1145,14 +1141,14 @@
|
|
|
1145
1141
|
- a30c83d: Add `compat.removeComponentAttrRegex`.
|
|
1146
1142
|
|
|
1147
1143
|
```js
|
|
1148
|
-
import { pluginReactLynx } from
|
|
1149
|
-
import { defineConfig } from
|
|
1144
|
+
import { pluginReactLynx } from "@lynx-js/react-rsbuild-plugin";
|
|
1145
|
+
import { defineConfig } from "@lynx-js/rspeedy";
|
|
1150
1146
|
|
|
1151
1147
|
export default defineConfig({
|
|
1152
1148
|
plugins: [
|
|
1153
1149
|
pluginReactLynx({
|
|
1154
1150
|
compat: {
|
|
1155
|
-
removeComponentAttrRegex:
|
|
1151
|
+
removeComponentAttrRegex: "YOUR REGEX",
|
|
1156
1152
|
},
|
|
1157
1153
|
}),
|
|
1158
1154
|
],
|
|
@@ -1229,7 +1225,6 @@
|
|
|
1229
1225
|
</table>
|
|
1230
1226
|
|
|
1231
1227
|
This requires `@lynx-js/react-rsbuild-plugin` v0.5.1 to work.
|
|
1232
|
-
|
|
1233
1228
|
- Inject `globDynamicComponentEntry` for background script. ([#311](https://github.com/lynx-wg/lynx-stack/pull/311))
|
|
1234
1229
|
- Inject `globDynamicComponentEntry` for main thread script. ([#312](https://github.com/lynx-wg/lynx-stack/pull/312))
|
|
1235
1230
|
|
|
@@ -1238,22 +1233,22 @@
|
|
|
1238
1233
|
Gesture Handler is a set of gesture handling capabilities built on top of the Main Thread Script. It currently supports drag, inertial scrolling, long press, and tap gestures for `<view>`, `<scroll-view>`, `<list>`, and `<text>`. In the future, it will also support multi-finger zoom, multi-finger rotation, and other gesture capabilities.
|
|
1239
1234
|
|
|
1240
1235
|
```tsx
|
|
1241
|
-
import { useGesture, PanGesture } from
|
|
1236
|
+
import { useGesture, PanGesture } from "@lynx-js/gesture-runtime";
|
|
1242
1237
|
|
|
1243
1238
|
function App() {
|
|
1244
1239
|
const pan = useGesture(PanGesture);
|
|
1245
1240
|
|
|
1246
1241
|
pan
|
|
1247
1242
|
.onBegin((event, stateManager) => {
|
|
1248
|
-
|
|
1243
|
+
"main thread";
|
|
1249
1244
|
// some logic
|
|
1250
1245
|
})
|
|
1251
1246
|
.onUpdate((event, stateManager) => {
|
|
1252
|
-
|
|
1247
|
+
"main thread";
|
|
1253
1248
|
// some logic
|
|
1254
1249
|
})
|
|
1255
1250
|
.onEnd((event, stateManager) => {
|
|
1256
|
-
|
|
1251
|
+
"main thread";
|
|
1257
1252
|
// some logic
|
|
1258
1253
|
});
|
|
1259
1254
|
|
|
@@ -1273,7 +1268,7 @@
|
|
|
1273
1268
|
return;
|
|
1274
1269
|
}
|
|
1275
1270
|
|
|
1276
|
-
console.log(
|
|
1271
|
+
console.log("not __LEPUS__"); // This can be removed now
|
|
1277
1272
|
}
|
|
1278
1273
|
```
|
|
1279
1274
|
|
package/package.json
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { IndexMap } from '../../shared/index-map.js';
|
|
2
|
+
import type { JsFnHandle, Worklet } from '../../worklet-runtime/bindings/types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Keeps the background-thread worklet ctx alive while main thread still holds
|
|
5
|
+
* a handle to one of its background functions.
|
|
6
|
+
*/
|
|
7
|
+
export declare class BackgroundFunctionExecMap extends IndexMap<Worklet> {
|
|
8
|
+
add(worklet: Worklet): number;
|
|
9
|
+
findJsFnHandle(execId: number, fnId: number): JsFnHandle | undefined;
|
|
10
|
+
private findJsFnHandleInValue;
|
|
11
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Copyright 2024 The Lynx Authors. All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
import { IndexMap } from '../../shared/index-map.js';
|
|
5
|
+
/**
|
|
6
|
+
* Keeps the background-thread worklet ctx alive while main thread still holds
|
|
7
|
+
* a handle to one of its background functions.
|
|
8
|
+
*/
|
|
9
|
+
export class BackgroundFunctionExecMap extends IndexMap {
|
|
10
|
+
add(worklet) {
|
|
11
|
+
const execId = super.add(worklet);
|
|
12
|
+
worklet._execId = execId;
|
|
13
|
+
return execId;
|
|
14
|
+
}
|
|
15
|
+
findJsFnHandle(execId, fnId) {
|
|
16
|
+
const worklet = this.get(execId);
|
|
17
|
+
if (!worklet) {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
return this.findJsFnHandleInValue(worklet, fnId);
|
|
21
|
+
}
|
|
22
|
+
findJsFnHandleInValue(value, fnId) {
|
|
23
|
+
if (value === null || typeof value !== 'object') {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
const obj = value;
|
|
27
|
+
if ('_jsFnId' in obj && obj['_jsFnId'] === fnId) {
|
|
28
|
+
return obj;
|
|
29
|
+
}
|
|
30
|
+
for (const i in obj) {
|
|
31
|
+
const result = this.findJsFnHandleInValue(obj[i], fnId);
|
|
32
|
+
if (result) {
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=exec-map.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exec-map.js","sourceRoot":"","sources":["../../../src/core/background-function/exec-map.ts"],"names":[],"mappings":"A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
+
/**
|
|
3
|
+
* Creates the serializable handle used by main-thread code to call a background function.
|
|
4
|
+
*/
|
|
5
|
+
export declare function createBackgroundFunctionHandle(obj: ((...args: any[]) => any) & {
|
|
6
|
+
toJSON?: () => string;
|
|
7
|
+
}): JsFnHandle;
|
|
8
|
+
export declare function resetBackgroundFunctionHandleIdForTesting(): void;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Copyright 2024 The Lynx Authors. All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
let lastId = 0;
|
|
5
|
+
/**
|
|
6
|
+
* Creates the serializable handle used by main-thread code to call a background function.
|
|
7
|
+
*/
|
|
8
|
+
export function createBackgroundFunctionHandle(obj) {
|
|
9
|
+
const id = ++lastId;
|
|
10
|
+
if (typeof obj !== 'function') {
|
|
11
|
+
return {
|
|
12
|
+
_jsFnId: id,
|
|
13
|
+
_error: `Argument of runOnBackground should be a function, but got [${typeof obj}] instead`,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
obj.toJSON ??= () => '[BackgroundFunction]';
|
|
17
|
+
return {
|
|
18
|
+
_jsFnId: id,
|
|
19
|
+
_fn: obj,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export function resetBackgroundFunctionHandleIdForTesting() {
|
|
23
|
+
lastId = 0;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=handle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handle.js","sourceRoot":"","sources":["../../../src/core/background-function/handle.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAI1D,IAAI,MAAM,GAAG,CAAC,CAAC;AAEf;;GAEG;AACH,MAAM,UAAU,8BAA8B,CAC5C,GAA0D;IAE1D,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC;IACpB,IAAI,OAAO,GAAG,KAAK,UAAU,EAAE,CAAC;QAC9B,OAAO;YACL,OAAO,EAAE,EAAE;YACX,MAAM,EAAE,8DAA8D,OAAO,GAAG,WAAW;SAC5F,CAAC;IACJ,CAAC;IACD,GAAG,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC,sBAAsB,CAAC;IAC5C,OAAO;QACL,OAAO,EAAE,EAAE;QACX,GAAG,EAAE,GAAG;KACT,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,yCAAyC;IACvD,MAAM,GAAG,CAAC,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare function resetBackgroundFunctionRuntime(): void;
|
|
2
|
+
/**
|
|
3
|
+
* `runOnBackground` allows triggering js functions on the background thread asynchronously.
|
|
4
|
+
* @param f - The js function to be called.
|
|
5
|
+
* @returns A function. Calling which with the arguments to be passed to the js function to trigger it on the background thread. This function returns a promise that resolves to the return value of the js function.
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
export declare function runOnBackground<R, Fn extends (...args: any[]) => R>(f: Fn): (...args: Parameters<Fn>) => Promise<R>;
|
|
@@ -1,30 +1,35 @@
|
|
|
1
1
|
// Copyright 2024 The Lynx Authors. All rights reserved.
|
|
2
2
|
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
3
|
// LICENSE file in the root directory of this source tree.
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
4
|
+
import { BackgroundFunctionExecMap } from './exec-map.js';
|
|
5
|
+
import { isSdkVersionGt } from '../../utils.js';
|
|
6
|
+
import { delayRunOnBackground } from '../../worklet-runtime/bindings/bindings.js';
|
|
7
|
+
import { WorkletEvents } from '../../worklet-runtime/bindings/events.js';
|
|
8
|
+
import { registerDestroyTask } from '../runtime-destroy.js';
|
|
9
|
+
import { onFunctionCall } from '../thread-function-call/return-value.js';
|
|
9
10
|
let execIdMap;
|
|
10
|
-
|
|
11
|
+
let cleanupBackgroundFunctionRuntime;
|
|
12
|
+
let unregisterBackgroundFunctionCleanup;
|
|
13
|
+
function initBackgroundFunctionRuntime() {
|
|
11
14
|
'background only';
|
|
12
15
|
if (execIdMap) {
|
|
13
16
|
return;
|
|
14
17
|
}
|
|
15
|
-
execIdMap = new
|
|
16
|
-
lynx.getCoreContext()
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
execIdMap = new BackgroundFunctionExecMap();
|
|
19
|
+
const context = lynx.getCoreContext();
|
|
20
|
+
context.addEventListener(WorkletEvents.runOnBackground, runBackgroundFunction);
|
|
21
|
+
context.addEventListener(WorkletEvents.releaseBackgroundWorkletCtx, releaseBackgroundFunctionCtx);
|
|
22
|
+
cleanupBackgroundFunctionRuntime = () => {
|
|
23
|
+
context.removeEventListener(WorkletEvents.runOnBackground, runBackgroundFunction);
|
|
24
|
+
context.removeEventListener(WorkletEvents.releaseBackgroundWorkletCtx, releaseBackgroundFunctionCtx);
|
|
21
25
|
execIdMap = undefined;
|
|
22
|
-
|
|
26
|
+
cleanupBackgroundFunctionRuntime = undefined;
|
|
27
|
+
};
|
|
23
28
|
}
|
|
24
29
|
/**
|
|
25
30
|
* @internal
|
|
26
31
|
*/
|
|
27
|
-
function
|
|
32
|
+
export function runBackgroundFunction(event) {
|
|
28
33
|
'background only';
|
|
29
34
|
const data = JSON.parse(event.data);
|
|
30
35
|
const obj = execIdMap.findJsFnHandle(data.obj._execId, data.obj._jsFnId);
|
|
@@ -41,7 +46,7 @@ function runJSFunction(event) {
|
|
|
41
46
|
}),
|
|
42
47
|
});
|
|
43
48
|
}
|
|
44
|
-
function
|
|
49
|
+
function releaseBackgroundFunctionCtx(event) {
|
|
45
50
|
'background only';
|
|
46
51
|
for (const id of event.data) {
|
|
47
52
|
execIdMap.remove(id);
|
|
@@ -50,31 +55,33 @@ function releaseBackgroundWorkletCtx(event) {
|
|
|
50
55
|
/**
|
|
51
56
|
* @internal
|
|
52
57
|
*/
|
|
53
|
-
function
|
|
58
|
+
export function registerBackgroundFunctionCtx(ctx) {
|
|
54
59
|
'background only';
|
|
55
|
-
|
|
60
|
+
initBackgroundFunctionRuntime();
|
|
61
|
+
ensureBackgroundFunctionCleanup();
|
|
56
62
|
execIdMap.add(ctx);
|
|
57
63
|
}
|
|
64
|
+
function ensureBackgroundFunctionCleanup() {
|
|
65
|
+
if (unregisterBackgroundFunctionCleanup) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
unregisterBackgroundFunctionCleanup = registerDestroyTask(() => {
|
|
69
|
+
resetBackgroundFunctionRuntime();
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
export function resetBackgroundFunctionRuntime() {
|
|
73
|
+
cleanupBackgroundFunctionRuntime?.();
|
|
74
|
+
unregisterBackgroundFunctionCleanup?.();
|
|
75
|
+
unregisterBackgroundFunctionCleanup = undefined;
|
|
76
|
+
}
|
|
58
77
|
/**
|
|
59
78
|
* `runOnBackground` allows triggering js functions on the background thread asynchronously.
|
|
60
79
|
* @param f - The js function to be called.
|
|
61
80
|
* @returns A function. Calling which with the arguments to be passed to the js function to trigger it on the background thread. This function returns a promise that resolves to the return value of the js function.
|
|
62
|
-
* @example
|
|
63
|
-
* ```ts
|
|
64
|
-
* import { runOnBackground } from '@lynx-js/react';
|
|
65
|
-
*
|
|
66
|
-
* async function someMainthreadFunction() {
|
|
67
|
-
* 'main thread';
|
|
68
|
-
* const fn = runOnBackground(() => {
|
|
69
|
-
* return 'hello';
|
|
70
|
-
* });
|
|
71
|
-
* const result = await fn();
|
|
72
|
-
}
|
|
73
|
-
* ```
|
|
74
81
|
* @public
|
|
75
82
|
*/
|
|
76
|
-
function runOnBackground(f) {
|
|
77
|
-
if (!
|
|
83
|
+
export function runOnBackground(f) {
|
|
84
|
+
if (!isSdkVersionGt(2, 15)) {
|
|
78
85
|
throw new Error('runOnBackground requires Lynx sdk version 2.16.');
|
|
79
86
|
}
|
|
80
87
|
if (__JS__) {
|
|
@@ -110,5 +117,4 @@ function dispatchRunBackgroundFunctionEvent(fnId, params, execId, resolveId) {
|
|
|
110
117
|
}),
|
|
111
118
|
});
|
|
112
119
|
}
|
|
113
|
-
|
|
114
|
-
//# sourceMappingURL=runOnBackground.js.map
|
|
120
|
+
//# sourceMappingURL=run-on-background.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-on-background.js","sourceRoot":"","sources":["../../../src/core/background-function/run-on-background.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAE1D,OAAO,EAAE,yBAAyB,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EAAE,oBAAoB,EAAE,MAAM,4CAA4C,CAAC;AAClF,OAAO,EAAE,aAAa,EAAE,MAAM,0CAA0C,CAAC;AAGzE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,yCAAyC,CAAC;AAWzE,IAAI,SAAgD,CAAC;AACrD,IAAI,gCAA0D,CAAC;AAC/D,IAAI,mCAA6D,CAAC;AAElE,SAAS,6BAA6B;IACpC,iBAAiB,CAAC;IAClB,IAAI,SAAS,EAAE,CAAC;QACd,OAAO;IACT,CAAC;IAED,SAAS,GAAG,IAAI,yBAAyB,EAAE,CAAC;IAC5C,MAAM,OAAO,GAAG,IAAI,CAAC,cAAc,EAAE,CAAC;IACtC,OAAO,CAAC,gBAAgB,CAAC,aAAa,CAAC,eAAe,EAAE,qBAAqB,CAAC,CAAC;IAC/E,OAAO,CAAC,gBAAgB,CAAC,aAAa,CAAC,2BAA2B,EAAE,4BAA4B,CAAC,CAAC;IAElG,gCAAgC,GAAG,GAAG,EAAE;QACtC,OAAO,CAAC,mBAAmB,CAAC,aAAa,CAAC,eAAe,EAAE,qBAAqB,CAAC,CAAC;QAClF,OAAO,CAAC,mBAAmB,C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