@lynx-js/react 0.105.0 → 0.105.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/CHANGELOG.md +39 -0
- package/README.md +57 -5
- package/package.json +7 -2
- package/refresh/.turbo/turbo-build.log +1 -1
- 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/legacy-react-runtime/index.js +4 -2
- package/runtime/lib/legacy-react-runtime/index.js.map +1 -1
- package/runtime/lib/lifecycle/reload.js +2 -0
- package/runtime/lib/lifecycle/reload.js.map +1 -1
- package/runtime/lib/lynx/tt.js +1 -1
- package/runtime/lib/lynx/tt.js.map +1 -1
- package/runtime/lib/lynx-api.d.ts +2 -1
- package/runtime/lib/lynx-api.js +4 -4
- package/runtime/lib/lynx-api.js.map +1 -1
- package/runtime/lib/worklet/ctx.js +5 -5
- package/runtime/lib/worklet/ctx.js.map +1 -1
- package/runtime/lib/worklet/destroy.d.ts +2 -0
- package/runtime/lib/worklet/destroy.js +13 -0
- package/runtime/lib/worklet/destroy.js.map +1 -0
- package/runtime/lib/worklet/functionCall.d.ts +1 -0
- package/runtime/lib/worklet/functionCall.js +33 -0
- package/runtime/lib/worklet/functionCall.js.map +1 -0
- package/runtime/lib/worklet/functionality.d.ts +2 -1
- package/runtime/lib/worklet/functionality.js +21 -2
- package/runtime/lib/worklet/functionality.js.map +1 -1
- package/runtime/lib/worklet/runOnBackground.d.ts +20 -0
- package/runtime/lib/worklet/runOnBackground.js +105 -0
- package/runtime/lib/worklet/runOnBackground.js.map +1 -0
- package/runtime/lib/worklet/runOnMainThread.d.ts +19 -0
- package/runtime/lib/worklet/runOnMainThread.js +45 -0
- package/runtime/lib/worklet/runOnMainThread.js.map +1 -0
- package/runtime/lib/worklet/transformToWorklet.d.ts +1 -0
- package/runtime/lib/worklet/transformToWorklet.js +21 -0
- package/runtime/lib/worklet/transformToWorklet.js.map +1 -0
- package/runtime/lib/worklet/workletRef.js +2 -3
- package/runtime/lib/worklet/workletRef.js.map +1 -1
- package/runtime/lib/worklet/workletRefPool.d.ts +1 -2
- package/runtime/lib/worklet/workletRefPool.js +14 -11
- package/runtime/lib/worklet/workletRefPool.js.map +1 -1
- package/runtime/src/internal.ts +1 -1
- package/runtime/src/legacy-react-runtime/index.ts +4 -2
- package/runtime/src/lifecycle/reload.ts +2 -0
- package/runtime/src/lynx/tt.ts +1 -1
- package/runtime/src/lynx-api.ts +5 -4
- package/runtime/src/worklet/ctx.ts +5 -5
- package/runtime/src/worklet/destroy.ts +16 -0
- package/runtime/src/worklet/functionCall.ts +42 -0
- package/runtime/src/worklet/functionality.ts +25 -2
- package/runtime/src/worklet/runOnBackground.ts +125 -0
- package/runtime/src/worklet/runOnMainThread.ts +50 -0
- package/runtime/src/worklet/transformToWorklet.ts +26 -0
- package/runtime/src/worklet/workletRef.ts +3 -3
- package/runtime/src/worklet/workletRefPool.ts +16 -12
- package/transform/dist/wasm.cjs +1 -1
- package/transform/index.d.ts +5 -1
- package/worklet-runtime/dist/dev.js +10 -3
- package/worklet-runtime/dist/dev.js.map +2 -2
- package/worklet-runtime/dist/main.js +10 -3
- package/worklet-runtime/dist/main.js.map +2 -2
- package/worklet-runtime/lib/bindings/events.d.ts +12 -1
- package/worklet-runtime/lib/bindings/events.js +1 -0
- package/worklet-runtime/lib/bindings/events.js.map +1 -1
- package/worklet-runtime/lib/bindings/index.d.ts +1 -1
- package/worklet-runtime/lib/bindings/index.js.map +1 -1
- package/worklet-runtime/lib/bindings/types.d.ts +1 -1
- package/worklet-runtime/lib/global.d.ts +1 -1
- package/worklet-runtime/lib/jsFunctionLifecycle.d.ts +2 -2
- package/worklet-runtime/lib/jsFunctionLifecycle.js +2 -2
- package/worklet-runtime/lib/jsFunctionLifecycle.js.map +1 -1
- package/worklet-runtime/lib/listeners.js +9 -2
- package/worklet-runtime/lib/listeners.js.map +1 -1
- package/worklet-runtime/lib/workletRuntime.js +2 -2
- package/worklet-runtime/lib/workletRuntime.js.map +1 -1
- package/runtime/lib/worklet/jsImpl.d.ts +0 -11
- package/runtime/lib/worklet/jsImpl.js +0 -68
- package/runtime/lib/worklet/jsImpl.js.map +0 -1
- package/runtime/lib/worklet/runWorklet.d.ts +0 -14
- package/runtime/lib/worklet/runWorklet.js +0 -82
- package/runtime/lib/worklet/runWorklet.js.map +0 -1
- package/runtime/src/worklet/jsImpl.ts +0 -86
- package/runtime/src/worklet/runWorklet.ts +0 -88
|
@@ -1,68 +0,0 @@
|
|
|
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 { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings';
|
|
5
|
-
import { WorkletExecIdMap } from './execMap.js';
|
|
6
|
-
import { enableRunOnBackground } from './functionality.js';
|
|
7
|
-
/**
|
|
8
|
-
* @internal
|
|
9
|
-
*/
|
|
10
|
-
export function runJSFunction(event) {
|
|
11
|
-
const impl = lynxWorkletJsImpl();
|
|
12
|
-
if (!impl || !impl._workletExecIdMap) {
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
const data = JSON.parse(event.data);
|
|
16
|
-
const obj = impl._workletExecIdMap.findJsFnHandle(data.obj._execId, data.obj._jsFnId);
|
|
17
|
-
const f = obj?._fn;
|
|
18
|
-
if (!f) {
|
|
19
|
-
throw new Error('runOnBackground: JS function not found: ' + JSON.stringify(data.obj));
|
|
20
|
-
}
|
|
21
|
-
f(...data.params);
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* @internal
|
|
25
|
-
*/
|
|
26
|
-
export function removeJsWorklets(event) {
|
|
27
|
-
const impl = lynxWorkletJsImpl();
|
|
28
|
-
if (!impl?._workletExecIdMap) {
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
for (const id of event.data) {
|
|
32
|
-
impl._workletExecIdMap.remove(id);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
function initWorklet() {
|
|
36
|
-
if (lynx.getCoreContext === undefined) {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
lynx.lynxWorkletJsImpl = {
|
|
40
|
-
_workletJsFnLastId: 0,
|
|
41
|
-
_workletRefLastId: 0,
|
|
42
|
-
_workletRefInitValueSet: new Set(),
|
|
43
|
-
_workletRefInitValuePatch: [],
|
|
44
|
-
};
|
|
45
|
-
if (enableRunOnBackground()) {
|
|
46
|
-
lynx.lynxWorkletJsImpl._workletExecIdMap = new WorkletExecIdMap();
|
|
47
|
-
lynx.getCoreContext().addEventListener(WorkletEvents.runOnBackground, runJSFunction);
|
|
48
|
-
lynx.getCoreContext().addEventListener(WorkletEvents.releaseBackgroundWorkletCtx, removeJsWorklets);
|
|
49
|
-
}
|
|
50
|
-
return true;
|
|
51
|
-
}
|
|
52
|
-
export function destroyWorklet() {
|
|
53
|
-
if (!lynx.lynxWorkletJsImpl) {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
lynx.lynxWorkletJsImpl = undefined;
|
|
57
|
-
if (enableRunOnBackground()) {
|
|
58
|
-
lynx.getCoreContext?.().removeEventListener(WorkletEvents.runOnBackground, runJSFunction);
|
|
59
|
-
lynx.getCoreContext?.().removeEventListener(WorkletEvents.releaseBackgroundWorkletCtx, removeJsWorklets);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
export function lynxWorkletJsImpl(shouldInit = true) {
|
|
63
|
-
if (lynx.lynxWorkletJsImpl || (shouldInit && initWorklet())) {
|
|
64
|
-
return lynx.lynxWorkletJsImpl;
|
|
65
|
-
}
|
|
66
|
-
return undefined;
|
|
67
|
-
}
|
|
68
|
-
//# sourceMappingURL=jsImpl.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"jsImpl.js","sourceRoot":"","sources":["../../src/worklet/jsImpl.ts"],"names":[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
|
|
|
1
|
-
/**
|
|
2
|
-
* `runOnMainThread` allows triggering main thread functions on the main thread asynchronously.
|
|
3
|
-
* @param fn - The main thread functions to be called.
|
|
4
|
-
* @returns A function. Calling which with the arguments to be passed to the main thread function to trigger it on the main thread.
|
|
5
|
-
* @public
|
|
6
|
-
*/
|
|
7
|
-
export declare function runOnMainThread<Fn extends (...args: any[]) => any>(fn: Fn): (...args: Parameters<Fn>) => void;
|
|
8
|
-
/**
|
|
9
|
-
* `runOnBackground` allows triggering js functions on the js context asynchronously.
|
|
10
|
-
* @param f - The js function to be called.
|
|
11
|
-
* @returns A function. Calling which with the arguments to be passed to the js function to trigger it on the js context.
|
|
12
|
-
* @public
|
|
13
|
-
*/
|
|
14
|
-
export declare function runOnBackground<Fn extends (...args: any[]) => any>(f: Fn): (...args: Parameters<Fn>) => void;
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings';
|
|
2
|
-
import { onPostWorkletCtx } from './ctx.js';
|
|
3
|
-
import { enableRunOnBackground } from './functionality.js';
|
|
4
|
-
import { lynxWorkletJsImpl } from './jsImpl.js';
|
|
5
|
-
/**
|
|
6
|
-
* transform args of `runOnJS()`.
|
|
7
|
-
*
|
|
8
|
-
* @internal
|
|
9
|
-
*/
|
|
10
|
-
export function transformToWorklet(obj) {
|
|
11
|
-
const impl = lynxWorkletJsImpl();
|
|
12
|
-
const id = impl ? ++impl._workletJsFnLastId : 0;
|
|
13
|
-
if (typeof obj !== 'function') {
|
|
14
|
-
// We save the error message in the object, so that we can throw it later when the function is called on the main thread.
|
|
15
|
-
return {
|
|
16
|
-
_jsFnId: id,
|
|
17
|
-
_error: `Argument of runOnBackground should be a function, but got [${typeof obj}] instead`,
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
return {
|
|
21
|
-
_jsFnId: id,
|
|
22
|
-
_fn: obj,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* `runOnMainThread` allows triggering main thread functions on the main thread asynchronously.
|
|
27
|
-
* @param fn - The main thread functions to be called.
|
|
28
|
-
* @returns A function. Calling which with the arguments to be passed to the main thread function to trigger it on the main thread.
|
|
29
|
-
* @public
|
|
30
|
-
*/
|
|
31
|
-
export function runOnMainThread(fn) {
|
|
32
|
-
if (__LEPUS__) {
|
|
33
|
-
throw new Error('runOnMainThread can only be used on the background thread.');
|
|
34
|
-
}
|
|
35
|
-
const impl = lynxWorkletJsImpl();
|
|
36
|
-
if (!impl) {
|
|
37
|
-
throw new Error('runOnMainThread requires Lynx sdk version 2.14.');
|
|
38
|
-
}
|
|
39
|
-
return (...params) => {
|
|
40
|
-
onPostWorkletCtx(fn);
|
|
41
|
-
lynx.getCoreContext().dispatchEvent({
|
|
42
|
-
type: WorkletEvents.runWorkletCtx,
|
|
43
|
-
data: JSON.stringify({
|
|
44
|
-
worklet: fn,
|
|
45
|
-
params,
|
|
46
|
-
}),
|
|
47
|
-
});
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* `runOnBackground` allows triggering js functions on the js context asynchronously.
|
|
52
|
-
* @param f - The js function to be called.
|
|
53
|
-
* @returns A function. Calling which with the arguments to be passed to the js function to trigger it on the js context.
|
|
54
|
-
* @public
|
|
55
|
-
*/
|
|
56
|
-
export function runOnBackground(f) {
|
|
57
|
-
if (!enableRunOnBackground()) {
|
|
58
|
-
throw new Error('runOnBackground requires Lynx sdk version 2.16.');
|
|
59
|
-
}
|
|
60
|
-
if (__JS__) {
|
|
61
|
-
throw new Error('runOnBackground can not be used on the main thread.');
|
|
62
|
-
}
|
|
63
|
-
const obj = f;
|
|
64
|
-
if (obj._error) {
|
|
65
|
-
throw new Error(obj._error);
|
|
66
|
-
}
|
|
67
|
-
return (...params) => {
|
|
68
|
-
if (lynx.getJSContext) {
|
|
69
|
-
lynx.getJSContext().dispatchEvent({
|
|
70
|
-
type: WorkletEvents.runOnBackground,
|
|
71
|
-
data: JSON.stringify({
|
|
72
|
-
obj: {
|
|
73
|
-
_jsFnId: obj._jsFnId,
|
|
74
|
-
_execId: obj._execId,
|
|
75
|
-
},
|
|
76
|
-
params,
|
|
77
|
-
}),
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
//# sourceMappingURL=runWorklet.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"runWorklet.js","sourceRoot":"","sources":["../../src/worklet/runWorklet.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AAExE,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAC5C,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,GAAa;IAC9C,MAAM,IAAI,GAAG,iBAAiB,EAAE,CAAC;IACjC,MAAM,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,kBAAkB,CAAC,CAAC,CAAC,CAAC,CAAC;IAChD,IAAI,OAAO,GAAG,KAAK,UAAU,EAAE,CAAC;QAC9B,yHAAyH;QACzH,OAAO;YACL,OAAO,EAAE,EAAE;YACX,MAAM,EAAE,8DAA8D,OAAO,GAAG,WAAW;SAC5F,CAAC;IACJ,CAAC;IACD,OAAO;QACL,OAAO,EAAE,EAAE;QACX,GAAG,EAAE,GAAG;KACT,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAqC,EAAM;IACxE,IAAI,SAAS,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CAAC,4DAA4D,CAAC,CAAC;IAChF,CAAC;IACD,MAAM,IAAI,GAAG,iBAAiB,EAAE,CAAC;IACjC,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,MAAM,IA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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 { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings';
|
|
5
|
-
|
|
6
|
-
import { WorkletExecIdMap } from './execMap.js';
|
|
7
|
-
import { enableRunOnBackground } from './functionality.js';
|
|
8
|
-
|
|
9
|
-
interface LynxWorkletJsImpl {
|
|
10
|
-
_workletExecIdMap?: WorkletExecIdMap;
|
|
11
|
-
_workletJsFnLastId: number;
|
|
12
|
-
_workletRefLastId: number;
|
|
13
|
-
_workletRefInitValueSet: Set<number>;
|
|
14
|
-
_workletRefInitValuePatch: [number, unknown][];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @internal
|
|
19
|
-
*/
|
|
20
|
-
export function runJSFunction(event: RuntimeProxy.Event): void {
|
|
21
|
-
const impl = lynxWorkletJsImpl();
|
|
22
|
-
if (!impl || !impl._workletExecIdMap) {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
const data = JSON.parse(event.data);
|
|
26
|
-
const obj = impl._workletExecIdMap.findJsFnHandle(data.obj._execId, data.obj._jsFnId);
|
|
27
|
-
const f = obj?._fn;
|
|
28
|
-
if (!f) {
|
|
29
|
-
throw new Error('runOnBackground: JS function not found: ' + JSON.stringify(data.obj));
|
|
30
|
-
}
|
|
31
|
-
f(...data.params);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* @internal
|
|
36
|
-
*/
|
|
37
|
-
export function removeJsWorklets(event: RuntimeProxy.Event): void {
|
|
38
|
-
const impl = lynxWorkletJsImpl();
|
|
39
|
-
if (!impl?._workletExecIdMap) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
for (const id of event.data) {
|
|
43
|
-
impl._workletExecIdMap.remove(id);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function initWorklet(): boolean {
|
|
48
|
-
if (lynx.getCoreContext === undefined) {
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
lynx.lynxWorkletJsImpl = {
|
|
53
|
-
_workletJsFnLastId: 0,
|
|
54
|
-
_workletRefLastId: 0,
|
|
55
|
-
_workletRefInitValueSet: new Set<number>(),
|
|
56
|
-
_workletRefInitValuePatch: [],
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
if (enableRunOnBackground()) {
|
|
60
|
-
lynx.lynxWorkletJsImpl._workletExecIdMap = new WorkletExecIdMap();
|
|
61
|
-
lynx.getCoreContext().addEventListener(WorkletEvents.runOnBackground, runJSFunction);
|
|
62
|
-
lynx.getCoreContext().addEventListener(WorkletEvents.releaseBackgroundWorkletCtx, removeJsWorklets);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return true;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export function destroyWorklet(): void {
|
|
69
|
-
if (!lynx.lynxWorkletJsImpl) {
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
lynx.lynxWorkletJsImpl = undefined;
|
|
74
|
-
|
|
75
|
-
if (enableRunOnBackground()) {
|
|
76
|
-
lynx.getCoreContext?.().removeEventListener(WorkletEvents.runOnBackground, runJSFunction);
|
|
77
|
-
lynx.getCoreContext?.().removeEventListener(WorkletEvents.releaseBackgroundWorkletCtx, removeJsWorklets);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export function lynxWorkletJsImpl(shouldInit: boolean = true): LynxWorkletJsImpl | undefined {
|
|
82
|
-
if (lynx.lynxWorkletJsImpl || (shouldInit && initWorklet())) {
|
|
83
|
-
return lynx.lynxWorkletJsImpl;
|
|
84
|
-
}
|
|
85
|
-
return undefined;
|
|
86
|
-
}
|
|
@@ -1,88 +0,0 @@
|
|
|
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 type { JsFnHandle, Worklet } from '@lynx-js/react/worklet-runtime/bindings';
|
|
5
|
-
import { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings';
|
|
6
|
-
|
|
7
|
-
import { onPostWorkletCtx } from './ctx.js';
|
|
8
|
-
import { enableRunOnBackground } from './functionality.js';
|
|
9
|
-
import { lynxWorkletJsImpl } from './jsImpl.js';
|
|
10
|
-
/**
|
|
11
|
-
* transform args of `runOnJS()`.
|
|
12
|
-
*
|
|
13
|
-
* @internal
|
|
14
|
-
*/
|
|
15
|
-
export function transformToWorklet(obj: Function): JsFnHandle {
|
|
16
|
-
const impl = lynxWorkletJsImpl();
|
|
17
|
-
const id = impl ? ++impl._workletJsFnLastId : 0;
|
|
18
|
-
if (typeof obj !== 'function') {
|
|
19
|
-
// We save the error message in the object, so that we can throw it later when the function is called on the main thread.
|
|
20
|
-
return {
|
|
21
|
-
_jsFnId: id,
|
|
22
|
-
_error: `Argument of runOnBackground should be a function, but got [${typeof obj}] instead`,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
return {
|
|
26
|
-
_jsFnId: id,
|
|
27
|
-
_fn: obj,
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* `runOnMainThread` allows triggering main thread functions on the main thread asynchronously.
|
|
33
|
-
* @param fn - The main thread functions to be called.
|
|
34
|
-
* @returns A function. Calling which with the arguments to be passed to the main thread function to trigger it on the main thread.
|
|
35
|
-
* @public
|
|
36
|
-
*/
|
|
37
|
-
export function runOnMainThread<Fn extends (...args: any[]) => any>(fn: Fn): (...args: Parameters<Fn>) => void {
|
|
38
|
-
if (__LEPUS__) {
|
|
39
|
-
throw new Error('runOnMainThread can only be used on the background thread.');
|
|
40
|
-
}
|
|
41
|
-
const impl = lynxWorkletJsImpl();
|
|
42
|
-
if (!impl) {
|
|
43
|
-
throw new Error('runOnMainThread requires Lynx sdk version 2.14.');
|
|
44
|
-
}
|
|
45
|
-
return (...params: any[]): void => {
|
|
46
|
-
onPostWorkletCtx(fn as any as Worklet);
|
|
47
|
-
lynx.getCoreContext!().dispatchEvent({
|
|
48
|
-
type: WorkletEvents.runWorkletCtx,
|
|
49
|
-
data: JSON.stringify({
|
|
50
|
-
worklet: fn,
|
|
51
|
-
params,
|
|
52
|
-
}),
|
|
53
|
-
});
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* `runOnBackground` allows triggering js functions on the js context asynchronously.
|
|
59
|
-
* @param f - The js function to be called.
|
|
60
|
-
* @returns A function. Calling which with the arguments to be passed to the js function to trigger it on the js context.
|
|
61
|
-
* @public
|
|
62
|
-
*/
|
|
63
|
-
export function runOnBackground<Fn extends (...args: any[]) => any>(f: Fn): (...args: Parameters<Fn>) => void {
|
|
64
|
-
if (!enableRunOnBackground()) {
|
|
65
|
-
throw new Error('runOnBackground requires Lynx sdk version 2.16.');
|
|
66
|
-
}
|
|
67
|
-
if (__JS__) {
|
|
68
|
-
throw new Error('runOnBackground can not be used on the main thread.');
|
|
69
|
-
}
|
|
70
|
-
const obj = f as any as JsFnHandle;
|
|
71
|
-
if (obj._error) {
|
|
72
|
-
throw new Error(obj._error);
|
|
73
|
-
}
|
|
74
|
-
return (...params: any[]): void => {
|
|
75
|
-
if (lynx.getJSContext) {
|
|
76
|
-
lynx.getJSContext().dispatchEvent({
|
|
77
|
-
type: WorkletEvents.runOnBackground,
|
|
78
|
-
data: JSON.stringify({
|
|
79
|
-
obj: {
|
|
80
|
-
_jsFnId: obj._jsFnId,
|
|
81
|
-
_execId: obj._execId!,
|
|
82
|
-
},
|
|
83
|
-
params,
|
|
84
|
-
}),
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
}
|