@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.
Files changed (83) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/README.md +57 -5
  3. package/package.json +7 -2
  4. package/refresh/.turbo/turbo-build.log +1 -1
  5. package/runtime/lib/internal.d.ts +1 -1
  6. package/runtime/lib/internal.js +1 -1
  7. package/runtime/lib/internal.js.map +1 -1
  8. package/runtime/lib/legacy-react-runtime/index.js +4 -2
  9. package/runtime/lib/legacy-react-runtime/index.js.map +1 -1
  10. package/runtime/lib/lifecycle/reload.js +2 -0
  11. package/runtime/lib/lifecycle/reload.js.map +1 -1
  12. package/runtime/lib/lynx/tt.js +1 -1
  13. package/runtime/lib/lynx/tt.js.map +1 -1
  14. package/runtime/lib/lynx-api.d.ts +2 -1
  15. package/runtime/lib/lynx-api.js +4 -4
  16. package/runtime/lib/lynx-api.js.map +1 -1
  17. package/runtime/lib/worklet/ctx.js +5 -5
  18. package/runtime/lib/worklet/ctx.js.map +1 -1
  19. package/runtime/lib/worklet/destroy.d.ts +2 -0
  20. package/runtime/lib/worklet/destroy.js +13 -0
  21. package/runtime/lib/worklet/destroy.js.map +1 -0
  22. package/runtime/lib/worklet/functionCall.d.ts +1 -0
  23. package/runtime/lib/worklet/functionCall.js +33 -0
  24. package/runtime/lib/worklet/functionCall.js.map +1 -0
  25. package/runtime/lib/worklet/functionality.d.ts +2 -1
  26. package/runtime/lib/worklet/functionality.js +21 -2
  27. package/runtime/lib/worklet/functionality.js.map +1 -1
  28. package/runtime/lib/worklet/runOnBackground.d.ts +20 -0
  29. package/runtime/lib/worklet/runOnBackground.js +105 -0
  30. package/runtime/lib/worklet/runOnBackground.js.map +1 -0
  31. package/runtime/lib/worklet/runOnMainThread.d.ts +19 -0
  32. package/runtime/lib/worklet/runOnMainThread.js +45 -0
  33. package/runtime/lib/worklet/runOnMainThread.js.map +1 -0
  34. package/runtime/lib/worklet/transformToWorklet.d.ts +1 -0
  35. package/runtime/lib/worklet/transformToWorklet.js +21 -0
  36. package/runtime/lib/worklet/transformToWorklet.js.map +1 -0
  37. package/runtime/lib/worklet/workletRef.js +2 -3
  38. package/runtime/lib/worklet/workletRef.js.map +1 -1
  39. package/runtime/lib/worklet/workletRefPool.d.ts +1 -2
  40. package/runtime/lib/worklet/workletRefPool.js +14 -11
  41. package/runtime/lib/worklet/workletRefPool.js.map +1 -1
  42. package/runtime/src/internal.ts +1 -1
  43. package/runtime/src/legacy-react-runtime/index.ts +4 -2
  44. package/runtime/src/lifecycle/reload.ts +2 -0
  45. package/runtime/src/lynx/tt.ts +1 -1
  46. package/runtime/src/lynx-api.ts +5 -4
  47. package/runtime/src/worklet/ctx.ts +5 -5
  48. package/runtime/src/worklet/destroy.ts +16 -0
  49. package/runtime/src/worklet/functionCall.ts +42 -0
  50. package/runtime/src/worklet/functionality.ts +25 -2
  51. package/runtime/src/worklet/runOnBackground.ts +125 -0
  52. package/runtime/src/worklet/runOnMainThread.ts +50 -0
  53. package/runtime/src/worklet/transformToWorklet.ts +26 -0
  54. package/runtime/src/worklet/workletRef.ts +3 -3
  55. package/runtime/src/worklet/workletRefPool.ts +16 -12
  56. package/transform/dist/wasm.cjs +1 -1
  57. package/transform/index.d.ts +5 -1
  58. package/worklet-runtime/dist/dev.js +10 -3
  59. package/worklet-runtime/dist/dev.js.map +2 -2
  60. package/worklet-runtime/dist/main.js +10 -3
  61. package/worklet-runtime/dist/main.js.map +2 -2
  62. package/worklet-runtime/lib/bindings/events.d.ts +12 -1
  63. package/worklet-runtime/lib/bindings/events.js +1 -0
  64. package/worklet-runtime/lib/bindings/events.js.map +1 -1
  65. package/worklet-runtime/lib/bindings/index.d.ts +1 -1
  66. package/worklet-runtime/lib/bindings/index.js.map +1 -1
  67. package/worklet-runtime/lib/bindings/types.d.ts +1 -1
  68. package/worklet-runtime/lib/global.d.ts +1 -1
  69. package/worklet-runtime/lib/jsFunctionLifecycle.d.ts +2 -2
  70. package/worklet-runtime/lib/jsFunctionLifecycle.js +2 -2
  71. package/worklet-runtime/lib/jsFunctionLifecycle.js.map +1 -1
  72. package/worklet-runtime/lib/listeners.js +9 -2
  73. package/worklet-runtime/lib/listeners.js.map +1 -1
  74. package/worklet-runtime/lib/workletRuntime.js +2 -2
  75. package/worklet-runtime/lib/workletRuntime.js.map +1 -1
  76. package/runtime/lib/worklet/jsImpl.d.ts +0 -11
  77. package/runtime/lib/worklet/jsImpl.js +0 -68
  78. package/runtime/lib/worklet/jsImpl.js.map +0 -1
  79. package/runtime/lib/worklet/runWorklet.d.ts +0 -14
  80. package/runtime/lib/worklet/runWorklet.js +0 -82
  81. package/runtime/lib/worklet/runWorklet.js.map +0 -1
  82. package/runtime/src/worklet/jsImpl.ts +0 -86
  83. package/runtime/src/worklet/runWorklet.ts +0 -88
@@ -0,0 +1,19 @@
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. This function returns a promise that resolves to the return value of the main thread function.
5
+ * @example
6
+ * ```ts
7
+ * import { runOnMainThread } from '@lynx-js/react';
8
+ *
9
+ * async function someFunction() {
10
+ * const fn = runOnMainThread(() => {
11
+ * 'main thread';
12
+ * return 'hello';
13
+ * });
14
+ * const result = await fn();
15
+ * }
16
+ * ```
17
+ * @public
18
+ */
19
+ export declare function runOnMainThread<R, Fn extends (...args: any[]) => R>(fn: Fn): (...args: Parameters<Fn>) => Promise<R>;
@@ -0,0 +1,45 @@
1
+ import { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings';
2
+ import { onPostWorkletCtx } from './ctx.js';
3
+ import { isMtsEnabled } from './functionality.js';
4
+ import { onFunctionCall } from './functionCall.js';
5
+ /**
6
+ * `runOnMainThread` allows triggering main thread functions on the main thread asynchronously.
7
+ * @param fn - The main thread functions to be called.
8
+ * @returns A function. Calling which with the arguments to be passed to the main thread function to trigger it on the main thread. This function returns a promise that resolves to the return value of the main thread function.
9
+ * @example
10
+ * ```ts
11
+ * import { runOnMainThread } from '@lynx-js/react';
12
+ *
13
+ * async function someFunction() {
14
+ * const fn = runOnMainThread(() => {
15
+ * 'main thread';
16
+ * return 'hello';
17
+ * });
18
+ * const result = await fn();
19
+ * }
20
+ * ```
21
+ * @public
22
+ */
23
+ export function runOnMainThread(fn) {
24
+ if (__LEPUS__) {
25
+ throw new Error('runOnMainThread can only be used on the background thread.');
26
+ }
27
+ if (!isMtsEnabled()) {
28
+ throw new Error('runOnMainThread requires Lynx sdk version 2.14.');
29
+ }
30
+ return async (...params) => {
31
+ return new Promise((resolve) => {
32
+ onPostWorkletCtx(fn);
33
+ const resolveId = onFunctionCall(resolve);
34
+ lynx.getCoreContext().dispatchEvent({
35
+ type: WorkletEvents.runWorkletCtx,
36
+ data: JSON.stringify({
37
+ worklet: fn,
38
+ params,
39
+ resolveId,
40
+ }),
41
+ });
42
+ });
43
+ };
44
+ }
45
+ //# sourceMappingURL=runOnMainThread.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runOnMainThread.js","sourceRoot":"","sources":["../../src/worklet/runOnMainThread.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AAExE,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEnD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,eAAe,CAAsC,EAAM;IACzE,IAAI,SAAS,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CAAC,4DAA4D,CAAC,CAAC;IAChF,CAAC;IACD,IAAI,CAAC,YAAY,EAAE,EAAE,CAAC;QACpB,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;IACrE,CAAC;IACD,OAAO,KAAK,EAAE,GAAG,MAAa,EAAc,EAAE;QAC5C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC7B,gBAAgB,CAAC,EAAoB,CAAC,CAAC;YACvC,MAAM,SAAS,GAAG,cAAc,CAAC,OAAO,CAAC,CAAC;YAC1C,IAAI,CAAC,cAAe,EAAE,CAAC,aAAa,CAAC;gBACnC,IAAI,EAAE,aAAa,CAAC,aAAa;gBACjC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC;oBACnB,OAAO,EAAE,EAAoB;oBAC7B,MAAM;oBACN,SAAS;iBACW,CAAC;aACxB,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,21 @@
1
+ let lastId = 0;
2
+ /**
3
+ * transform args of `runOnBackground()`.
4
+ *
5
+ * @internal
6
+ */
7
+ export function transformToWorklet(obj) {
8
+ const id = ++lastId;
9
+ if (typeof obj !== 'function') {
10
+ // We save the error message in the object, so that we can throw it later when the function is called on the main thread.
11
+ return {
12
+ _jsFnId: id,
13
+ _error: `Argument of runOnBackground should be a function, but got [${typeof obj}] instead`,
14
+ };
15
+ }
16
+ return {
17
+ _jsFnId: id,
18
+ _fn: obj,
19
+ };
20
+ }
21
+ //# sourceMappingURL=transformToWorklet.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transformToWorklet.js","sourceRoot":"","sources":["../../src/worklet/transformToWorklet.ts"],"names":[],"mappings":"AAKA,IAAI,MAAM,GAAG,CAAC,CAAC;AAEf;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,GAA4B;IAC7D,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC;IACpB,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"}
@@ -1,7 +1,7 @@
1
1
  import { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings';
2
- import { lynxWorkletJsImpl } from './jsImpl.js';
3
2
  import { addWorkletRefInitValue } from './workletRefPool.js';
4
3
  import { useMemo } from '../hooks/react.js';
4
+ let lastId = 0;
5
5
  class WorkletRef {
6
6
  /**
7
7
  * @internal
@@ -24,8 +24,7 @@ class WorkletRef {
24
24
  */
25
25
  constructor(initValue, type) {
26
26
  if (__JS__) {
27
- const impl = lynxWorkletJsImpl();
28
- this._id = impl ? ++impl._workletRefLastId : 0;
27
+ this._id = ++lastId;
29
28
  this._initValue = initValue;
30
29
  this._type = type;
31
30
  addWorkletRefInitValue(this._id, initValue);
@@ -1 +1 @@
1
- {"version":3,"file":"workletRef.js","sourceRoot":"","sources":["../../src/worklet/workletRef.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AAExE,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAC7D,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAE5C,MAAe,UAAU;IACvB;;OAEG;IACO,GAAG,CAAS;IACtB;;OAEG;IACO,UAAU,CAAgB;IACpC;;OAEG;IACO,KAAK,CAAS;IACxB;;OAEG;IACO,kBAAkB,CAAU;IAEtC;;OAEG;IACH,YAAsB,SAAY,EAAE,IAAY;QAC9C,IAAI,MAAM,EAAE,CAAC;YACX,MAAM,IAAI,GAAG,iBAAiB,EAAE,CAAC;YACjC,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC;YAC/C,IAAI,CA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;IACtC,CAAC,EAAE,EAAE,CAAC,CAAC;AACT,CAAC"}
@@ -1,2 +1 @@
1
- export declare function addWorkletRefInitValue(id: number, value: unknown): void;
2
- export declare function takeWorkletRefInitValuePatch(): [number, unknown][];
1
+ export {};
@@ -1,22 +1,25 @@
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 { lynxWorkletJsImpl } from './jsImpl.js';
4
+ import { isMtsEnabled } from './functionality.js';
5
+ let initValuePatch = [];
6
+ const initValueIdSet = /*#__PURE__*/ new Set();
7
+ /**
8
+ * @internal
9
+ */
5
10
  export function addWorkletRefInitValue(id, value) {
6
- const impl = lynxWorkletJsImpl();
7
- if (!impl) {
11
+ if (!isMtsEnabled()) {
8
12
  return;
9
13
  }
10
- impl._workletRefInitValueSet.add(id);
11
- impl._workletRefInitValuePatch.push([id, value]);
14
+ initValueIdSet.add(id);
15
+ initValuePatch.push([id, value]);
12
16
  }
17
+ /**
18
+ * @internal
19
+ */
13
20
  export function takeWorkletRefInitValuePatch() {
14
- const impl = lynxWorkletJsImpl(false);
15
- if (!impl) {
16
- return [];
17
- }
18
- const res = impl._workletRefInitValuePatch;
19
- impl._workletRefInitValuePatch = [];
21
+ const res = initValuePatch;
22
+ initValuePatch = [];
20
23
  return res;
21
24
  }
22
25
  //# sourceMappingURL=workletRefPool.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"workletRefPool.js","sourceRoot":"","sources":["../../src/worklet/workletRefPool.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAC1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhD,MAAM,UAAU,sBAAsB,CAAC,EAAU,EAAE,KAAc;IAC/D,MAAM,IAAI,GAAG,iBAAiB,EAAE,CAAC;IACjC,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,OAAO;IACT,CAAC;IAED,IAAI,CAAC,uBAAuB,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IACrC,IAAI,CAAC,yBAAyB,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC,CAAC;AACnD,CAAC;AAED,MAAM,UAAU,4BAA4B;IAC1C,MAAM,IAAI,GAAG,iBAAiB,CAAC,KAAK,CAAC,CAAC;IACtC,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,MAAM,GAAG,GAAG,IAAI,CAAC,yBAAyB,CAAC;IAC3C,IAAI,CAAC,yBAAyB,GAAG,EAAE,CAAC;IACpC,OAAO,GAAG,CAAC;AACb,CAAC"}
1
+ {"version":3,"file":"workletRefPool.js","sourceRoot":"","sources":["../../src/worklet/workletRefPool.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAE1D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,IAAI,cAAc,GAAwB,EAAE,CAAC;AAC7C,MAAM,cAAc,GAAG,aAAa,CAAC,IAAI,GAAG,EAAU,CAAC;AAEvD;;GAEG;AACH,MAAM,UAAU,sBAAsB,CAAC,EAAU,EAAE,KAAc;IAC/D,IAAI,CAAC,YAAY,EAAE,EAAE,CAAC;QACpB,OAAO;IACT,CAAC;IAED,cAAc,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IACvB,cAAc,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC,CAAC;AACnC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,4BAA4B;IAC1C,MAAM,GAAG,GAAG,cAAc,CAAC;IAC3B,cAAc,GAAG,EAAE,CAAC;IACpB,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -54,7 +54,7 @@ export const __ComponentIsPolyfill: FC<{ is: string }> = /* @__PURE__ */ factory
54
54
 
55
55
  export { loadLazyBundle } from './lynx/lazy-bundle.js';
56
56
 
57
- export { transformToWorklet } from './worklet/runWorklet.js';
57
+ export { transformToWorklet } from './worklet/transformToWorklet.js';
58
58
  export { registerWorkletOnBackground } from './worklet/hmr.js';
59
59
 
60
60
  export { loadWorkletRuntime } from '@lynx-js/react/worklet-runtime/bindings';
@@ -1,14 +1,14 @@
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 { createContext } from 'preact';
4
+ import { createContext, createRef } from 'preact';
5
5
  import { lazy } from 'preact/compat';
6
6
 
7
7
  import {
8
8
  ComponentFromReactRuntime as Component,
9
9
  ComponentFromReactRuntime as PureComponent,
10
10
  } from '../compat/lynxComponent.js';
11
- import { useCallback, useEffect, useMemo, useReducer, useState } from '../hooks/react.js';
11
+ import { useCallback, useEffect, useMemo, useReducer, useState, useRef } from '../hooks/react.js';
12
12
 
13
13
  /* v8 ignore next 3 */
14
14
  function __runInJS<T>(value: T): T | undefined | null {
@@ -30,9 +30,11 @@ export default {
30
30
  Component,
31
31
  PureComponent,
32
32
  createContext,
33
+ createRef,
33
34
  lazy,
34
35
  useState,
35
36
  useReducer,
37
+ useRef,
36
38
  useEffect,
37
39
  useMemo,
38
40
  useCallback,
@@ -14,6 +14,7 @@ import { LifecycleConstant } from '../lifecycleConstant.js';
14
14
  import { __pendingListUpdates } from '../list.js';
15
15
  import { takeGlobalRefPatchMap } from '../snapshot/ref.js';
16
16
  import { deinitGlobalSnapshotPatch } from '../snapshotPatch.js';
17
+ import { destroyWorklet } from '../worklet/destroy.js';
17
18
 
18
19
  function reloadMainThread(data: any, options: UpdatePageOption): void {
19
20
  if (__PROFILE__) {
@@ -26,6 +27,7 @@ function reloadMainThread(data: any, options: UpdatePageOption): void {
26
27
  Object.assign(lynx.__initData, data);
27
28
  }
28
29
 
30
+ destroyWorklet();
29
31
  snapshotInstanceManager.clear();
30
32
  __pendingListUpdates.clear();
31
33
 
@@ -14,7 +14,7 @@ import { CHILDREN } from '../renderToOpcodes/constants.js';
14
14
  import { __root } from '../root.js';
15
15
  import { globalRefsToSet, updateBackgroundRefs } from '../snapshot/ref.js';
16
16
  import { backgroundSnapshotInstanceManager } from '../snapshot.js';
17
- import { destroyWorklet } from '../worklet/jsImpl.js';
17
+ import { destroyWorklet } from '../worklet/destroy.js';
18
18
 
19
19
  function injectTt(): void {
20
20
  // @ts-ignore
@@ -7,9 +7,9 @@ import { useState } from 'preact/hooks';
7
7
  import type { Consumer, FC, ReactNode } from 'react';
8
8
 
9
9
  import { factory, withInitDataInState } from './compat/initData.js';
10
+ import { useLynxGlobalEventListener } from './hooks/useLynxGlobalEventListener.js';
10
11
  import { LifecycleConstant } from './lifecycleConstant.js';
11
12
  import { flushDelayedLifecycleEvents } from './lynx/tt.js';
12
- import { useLynxGlobalEventListener } from './hooks/useLynxGlobalEventListener.js';
13
13
  import { __root } from './root.js';
14
14
 
15
15
  /**
@@ -87,8 +87,8 @@ export const root: Root = {
87
87
  } else {
88
88
  __root.__jsx = jsx;
89
89
  render(jsx, __root as any);
90
- if (__FIRST_SCREEN_SYNC_TIMING__ === 'immediately') {
91
- } else {
90
+ if (__FIRST_SCREEN_SYNC_TIMING__ === 'immediately') {}
91
+ else {
92
92
  lynx.getNativeApp().callLepusMethod(LifecycleConstant.jsReady, {});
93
93
  }
94
94
 
@@ -369,6 +369,7 @@ export interface Lynx {
369
369
  registerDataProcessors: (dataProcessorDefinition: DataProcessorDefinition) => void;
370
370
  }
371
371
 
372
- export { runOnBackground, runOnMainThread } from './worklet/runWorklet.js';
372
+ export { runOnMainThread } from './worklet/runOnMainThread.js';
373
+ export { runOnBackground } from './worklet/runOnBackground.js';
373
374
  export { MainThreadRef, useMainThreadRef } from './worklet/workletRef.js';
374
375
  export { useLynxGlobalEventListener } from './hooks/useLynxGlobalEventListener.js';
@@ -3,20 +3,20 @@
3
3
  // LICENSE file in the root directory of this source tree.
4
4
  import type { Worklet } from '@lynx-js/react/worklet-runtime/bindings';
5
5
 
6
- import { lynxWorkletJsImpl } from './jsImpl.js';
6
+ import { isMtsEnabled, isRunOnBackgroundEnabled } from './functionality.js';
7
+ import { registerWorkletCtx } from './runOnBackground.js';
7
8
 
8
9
  /**
9
10
  * @internal
10
11
  */
11
12
  export function onPostWorkletCtx(afterValue: Worklet | null): Worklet | null {
12
- const impl = lynxWorkletJsImpl();
13
- if (!impl && afterValue) {
13
+ if (!isMtsEnabled() && afterValue) {
14
14
  lynx.reportError(new Error('Main thread script requires Lynx sdk version 2.14'));
15
15
  return null;
16
16
  }
17
- if (!afterValue) {
17
+ if (!afterValue || !isRunOnBackgroundEnabled()) {
18
18
  return afterValue;
19
19
  }
20
- impl?._workletExecIdMap?.add(afterValue);
20
+ registerWorkletCtx(afterValue);
21
21
  return afterValue;
22
22
  }
@@ -0,0 +1,16 @@
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
+
5
+ import { takeWorkletRefInitValuePatch } from './workletRefPool.js';
6
+
7
+ export const destroyTasks: (() => void)[] = [];
8
+
9
+ export function destroyWorklet(): void {
10
+ takeWorkletRefInitValuePatch();
11
+
12
+ for (const task of destroyTasks) {
13
+ task();
14
+ }
15
+ destroyTasks.length = 0;
16
+ }
@@ -0,0 +1,42 @@
1
+ // Copyright 2025 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
+
5
+ import { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings';
6
+ import type { RunWorkletCtxRetData } from '@lynx-js/react/worklet-runtime/bindings';
7
+
8
+ import { destroyTasks } from './destroy.js';
9
+ import { IndexMap } from './indexMap.js';
10
+
11
+ let resolveMap: IndexMap<(value: any) => void> | undefined;
12
+
13
+ function initReturnValueListener(): void {
14
+ const context: RuntimeProxy = __JS__ ? lynx.getCoreContext() : lynx.getJSContext();
15
+
16
+ resolveMap = new IndexMap();
17
+ context.addEventListener(WorkletEvents.FunctionCallRet, onFunctionCallRet);
18
+
19
+ destroyTasks.push(() => {
20
+ context.removeEventListener(WorkletEvents.FunctionCallRet, onFunctionCallRet);
21
+ resolveMap = undefined;
22
+ });
23
+ }
24
+
25
+ /**
26
+ * @internal
27
+ */
28
+ function onFunctionCall(resolve: (value: any) => void): number {
29
+ if (!resolveMap) {
30
+ initReturnValueListener();
31
+ }
32
+ return resolveMap!.add(resolve);
33
+ }
34
+
35
+ function onFunctionCallRet(event: RuntimeProxy.Event): void {
36
+ const data = JSON.parse(event.data as string) as RunWorkletCtxRetData;
37
+ const resolve = resolveMap!.get(data.resolveId)!;
38
+ resolveMap!.remove(data.resolveId);
39
+ resolve(data.returnValue);
40
+ }
41
+
42
+ export { onFunctionCall };
@@ -4,9 +4,32 @@
4
4
 
5
5
  import { isSdkVersionGt } from '../utils.js';
6
6
 
7
+ let mtsEnabled: boolean | undefined;
8
+ let runOnBackgroundEnabled: boolean | undefined;
9
+
7
10
  /**
8
11
  * @internal
9
12
  */
10
- export function enableRunOnBackground(): boolean {
11
- return isSdkVersionGt(2, 15);
13
+ function isMtsEnabled(): boolean {
14
+ if (mtsEnabled === undefined) {
15
+ mtsEnabled = isSdkVersionGt(2, 13);
16
+ }
17
+ return mtsEnabled;
12
18
  }
19
+
20
+ /**
21
+ * @internal
22
+ */
23
+ function isRunOnBackgroundEnabled(): boolean {
24
+ if (runOnBackgroundEnabled === undefined) {
25
+ runOnBackgroundEnabled = isSdkVersionGt(2, 15);
26
+ }
27
+ return runOnBackgroundEnabled;
28
+ }
29
+
30
+ function clearConfigCacheForTesting(): void {
31
+ mtsEnabled = undefined;
32
+ runOnBackgroundEnabled = undefined;
33
+ }
34
+
35
+ export { isMtsEnabled, isRunOnBackgroundEnabled, clearConfigCacheForTesting };
@@ -0,0 +1,125 @@
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
+
5
+ import type { JsFnHandle, RunWorkletCtxRetData, Worklet } from '@lynx-js/react/worklet-runtime/bindings';
6
+ import { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings';
7
+
8
+ import { destroyTasks } from './destroy.js';
9
+ import { WorkletExecIdMap } from './execMap.js';
10
+ import { isRunOnBackgroundEnabled } from './functionality.js';
11
+ import { onFunctionCall } from './functionCall.js';
12
+
13
+ /**
14
+ * @internal
15
+ */
16
+ interface RunOnBackgroundData {
17
+ obj: JsFnHandle;
18
+ params: unknown[];
19
+ resolveId: number;
20
+ }
21
+
22
+ let execIdMap: WorkletExecIdMap | undefined;
23
+
24
+ function init() {
25
+ 'background only';
26
+ if (execIdMap) {
27
+ return;
28
+ }
29
+
30
+ execIdMap = new WorkletExecIdMap();
31
+ lynx.getCoreContext().addEventListener(WorkletEvents.runOnBackground, runJSFunction);
32
+ lynx.getCoreContext().addEventListener(WorkletEvents.releaseBackgroundWorkletCtx, releaseBackgroundWorkletCtx);
33
+
34
+ destroyTasks.push(() => {
35
+ lynx.getCoreContext().removeEventListener(WorkletEvents.runOnBackground, runJSFunction);
36
+ lynx.getCoreContext().removeEventListener(WorkletEvents.releaseBackgroundWorkletCtx, releaseBackgroundWorkletCtx);
37
+ execIdMap = undefined;
38
+ });
39
+ }
40
+
41
+ /**
42
+ * @internal
43
+ */
44
+ function runJSFunction(event: RuntimeProxy.Event): void {
45
+ 'background only';
46
+ const data = JSON.parse(event.data as string) as RunOnBackgroundData;
47
+ const obj = execIdMap!.findJsFnHandle(data.obj._execId!, data.obj._jsFnId);
48
+ const f = obj?._fn;
49
+ if (!f) {
50
+ throw new Error('runOnBackground: JS function not found: ' + JSON.stringify(data.obj));
51
+ }
52
+ const returnValue = f(...data.params);
53
+ lynx.getCoreContext!().dispatchEvent({
54
+ type: WorkletEvents.FunctionCallRet,
55
+ data: JSON.stringify({
56
+ resolveId: data.resolveId,
57
+ returnValue,
58
+ } as RunWorkletCtxRetData),
59
+ });
60
+ }
61
+
62
+ function releaseBackgroundWorkletCtx(event: RuntimeProxy.Event): void {
63
+ 'background only';
64
+ for (const id of event.data) {
65
+ execIdMap!.remove(id as number);
66
+ }
67
+ }
68
+
69
+ /**
70
+ * @internal
71
+ */
72
+ function registerWorkletCtx(ctx: Worklet): void {
73
+ 'background only';
74
+ init();
75
+ execIdMap!.add(ctx);
76
+ }
77
+
78
+ /**
79
+ * `runOnBackground` allows triggering js functions on the background thread asynchronously.
80
+ * @param f - The js function to be called.
81
+ * @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.
82
+ * @example
83
+ * ```ts
84
+ * import { runOnBackground } from '@lynx-js/react';
85
+ *
86
+ * async function someMainthreadFunction() {
87
+ * 'main thread';
88
+ * const fn = runOnBackground(() => {
89
+ * return 'hello';
90
+ * });
91
+ * const result = await fn();
92
+ }
93
+ * ```
94
+ * @public
95
+ */
96
+ function runOnBackground<R, Fn extends (...args: any[]) => R>(f: Fn): (...args: Parameters<Fn>) => Promise<R> {
97
+ if (!isRunOnBackgroundEnabled()) {
98
+ throw new Error('runOnBackground requires Lynx sdk version 2.16.');
99
+ }
100
+ if (__JS__) {
101
+ throw new Error('runOnBackground can only be used on the main thread.');
102
+ }
103
+ const obj = f as any as JsFnHandle;
104
+ if (obj._error) {
105
+ throw new Error(obj._error);
106
+ }
107
+ return async (...params: any[]): Promise<R> => {
108
+ return new Promise((resolve) => {
109
+ const resolveId = onFunctionCall(resolve);
110
+ lynx.getJSContext!().dispatchEvent({
111
+ type: WorkletEvents.runOnBackground,
112
+ data: JSON.stringify({
113
+ obj: {
114
+ _jsFnId: obj._jsFnId,
115
+ _execId: obj._execId!,
116
+ },
117
+ params,
118
+ resolveId,
119
+ } as RunOnBackgroundData),
120
+ });
121
+ });
122
+ };
123
+ }
124
+
125
+ export { registerWorkletCtx, runJSFunction, runOnBackground };
@@ -0,0 +1,50 @@
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 { RunWorkletCtxData, 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 { isMtsEnabled } from './functionality.js';
9
+ import { onFunctionCall } from './functionCall.js';
10
+
11
+ /**
12
+ * `runOnMainThread` allows triggering main thread functions on the main thread asynchronously.
13
+ * @param fn - The main thread functions to be called.
14
+ * @returns A function. Calling which with the arguments to be passed to the main thread function to trigger it on the main thread. This function returns a promise that resolves to the return value of the main thread function.
15
+ * @example
16
+ * ```ts
17
+ * import { runOnMainThread } from '@lynx-js/react';
18
+ *
19
+ * async function someFunction() {
20
+ * const fn = runOnMainThread(() => {
21
+ * 'main thread';
22
+ * return 'hello';
23
+ * });
24
+ * const result = await fn();
25
+ * }
26
+ * ```
27
+ * @public
28
+ */
29
+ export function runOnMainThread<R, Fn extends (...args: any[]) => R>(fn: Fn): (...args: Parameters<Fn>) => Promise<R> {
30
+ if (__LEPUS__) {
31
+ throw new Error('runOnMainThread can only be used on the background thread.');
32
+ }
33
+ if (!isMtsEnabled()) {
34
+ throw new Error('runOnMainThread requires Lynx sdk version 2.14.');
35
+ }
36
+ return async (...params: any[]): Promise<R> => {
37
+ return new Promise((resolve) => {
38
+ onPostWorkletCtx(fn as any as Worklet);
39
+ const resolveId = onFunctionCall(resolve);
40
+ lynx.getCoreContext!().dispatchEvent({
41
+ type: WorkletEvents.runWorkletCtx,
42
+ data: JSON.stringify({
43
+ worklet: fn as any as Worklet,
44
+ params,
45
+ resolveId,
46
+ } as RunWorkletCtxData),
47
+ });
48
+ });
49
+ };
50
+ }
@@ -0,0 +1,26 @@
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 } from '@lynx-js/react/worklet-runtime/bindings';
5
+
6
+ let lastId = 0;
7
+
8
+ /**
9
+ * transform args of `runOnBackground()`.
10
+ *
11
+ * @internal
12
+ */
13
+ export function transformToWorklet(obj: (...args: any[]) => any): JsFnHandle {
14
+ const id = ++lastId;
15
+ if (typeof obj !== 'function') {
16
+ // We save the error message in the object, so that we can throw it later when the function is called on the main thread.
17
+ return {
18
+ _jsFnId: id,
19
+ _error: `Argument of runOnBackground should be a function, but got [${typeof obj}] instead`,
20
+ };
21
+ }
22
+ return {
23
+ _jsFnId: id,
24
+ _fn: obj,
25
+ };
26
+ }
@@ -6,10 +6,11 @@ import type { RefObject } from 'react';
6
6
  import type { WorkletRefImpl } from '@lynx-js/react/worklet-runtime/bindings';
7
7
  import { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings';
8
8
 
9
- import { lynxWorkletJsImpl } from './jsImpl.js';
10
9
  import { addWorkletRefInitValue } from './workletRefPool.js';
11
10
  import { useMemo } from '../hooks/react.js';
12
11
 
12
+ let lastId = 0;
13
+
13
14
  abstract class WorkletRef<T> {
14
15
  /**
15
16
  * @internal
@@ -33,8 +34,7 @@ abstract class WorkletRef<T> {
33
34
  */
34
35
  protected constructor(initValue: T, type: string) {
35
36
  if (__JS__) {
36
- const impl = lynxWorkletJsImpl();
37
- this._id = impl ? ++impl._workletRefLastId : 0;
37
+ this._id = ++lastId;
38
38
  this._initValue = initValue;
39
39
  this._type = type;
40
40
  addWorkletRefInitValue(this._id, initValue);