Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions .changeset/busy-hounds-take.md

This file was deleted.

7 changes: 0 additions & 7 deletions .changeset/calm-grass-grow.md

This file was deleted.

7 changes: 0 additions & 7 deletions .changeset/calm-plants-grow.md

This file was deleted.

3 changes: 0 additions & 3 deletions .changeset/chubby-hounds-hug.md

This file was deleted.

19 changes: 0 additions & 19 deletions .changeset/cold-terms-battle.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/common-games-hope.md

This file was deleted.

10 changes: 0 additions & 10 deletions .changeset/five-boats-fix.md

This file was deleted.

18 changes: 0 additions & 18 deletions .changeset/flat-poets-build.md

This file was deleted.

9 changes: 0 additions & 9 deletions .changeset/gold-chefs-obey.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/heavy-pigs-wave.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/late-lies-bow.md

This file was deleted.

22 changes: 0 additions & 22 deletions .changeset/little-candies-carry.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/long-cameras-refuse.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/lucky-socks-play.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/quick-taxes-ring.md

This file was deleted.

8 changes: 0 additions & 8 deletions .changeset/silly-pumas-float.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/smart-drinks-attend.md

This file was deleted.

7 changes: 0 additions & 7 deletions .changeset/sweet-tires-flash.md

This file was deleted.

7 changes: 0 additions & 7 deletions .changeset/tired-lands-drop.md

This file was deleted.

9 changes: 0 additions & 9 deletions .changeset/violet-banks-cheer.md

This file was deleted.

6 changes: 6 additions & 0 deletions packages/react/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @lynx-js/react

## 0.108.1

### Patch Changes

- Bump swc_core v23.2.0. ([#827](https://github.com/lynx-family/lynx-stack/pull/827))

## 0.108.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lynx-js/react",
"version": "0.108.0",
"version": "0.108.1",
"description": "ReactLynx is a framework for developing Lynx applications with familiar React.",
"repository": {
"type": "git",
Expand Down
8 changes: 5 additions & 3 deletions packages/react/runtime/src/snapshot/workletRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
import { runWorkletCtx, updateWorkletRef as update } from '@lynx-js/react/worklet-runtime/bindings';
import type { Element, Worklet, WorkletRefImpl } from '@lynx-js/react/worklet-runtime/bindings';
import type { Element, Worklet, WorkletRefId, WorkletRefImpl } from '@lynx-js/react/worklet-runtime/bindings';

import type { SnapshotInstance } from '../snapshot.js';

function workletUnRef(value: Worklet | WorkletRefImpl<Element>): void {
if ('_wvid' in value) {
if ('_wvid' in value && (value._wvid as WorkletRefId) > 0) {
update(value as WorkletRefImpl<Element>, null);
} else if ('_wkltId' in value) {
if (typeof value._unmount == 'function') {
Expand Down Expand Up @@ -38,7 +38,9 @@ function updateWorkletRef(
if (value === null || value === undefined) {
// do nothing
} else if (value._wvid) {
update(value as WorkletRefImpl<Element>, snapshot.__elements[elementIndex]!);
if ((value._wvid as WorkletRefId) > 0) {
update(value as WorkletRefImpl<Element>, snapshot.__elements[elementIndex]!);
}
} else if ((value as Worklet)._wkltId) {
(value as Worklet)._unmount = runWorkletCtx(value as Worklet, [{
elementRefptr: (snapshot.__elements[elementIndex]!) as any,
Expand Down
25 changes: 12 additions & 13 deletions packages/react/runtime/src/worklet/workletRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ import { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings';
import { addWorkletRefInitValue } from './workletRefPool.js';
import { useMemo } from '../hooks/react.js';

let lastId = 0;
// Split into two variables for testing purposes
let lastIdBG = 0;
let lastIdMT = 0;

export function clearWorkletRefLastIdForTesting(): void {
lastId = 0;
lastIdBG = lastIdMT = 0;
}

abstract class WorkletRef<T> {
/**
* @internal
*/
protected _id: number;
protected _wvid: number;
/**
* @internal
*/
Expand All @@ -37,16 +39,13 @@ abstract class WorkletRef<T> {
* @internal
*/
protected constructor(initValue: T, type: string) {
this._initValue = initValue;
this._type = type;
if (__JS__) {
this._id = ++lastId;
this._initValue = initValue;
this._type = type;
addWorkletRefInitValue(this._id, initValue);
this._wvid = ++lastIdBG;
addWorkletRefInitValue(this._wvid, initValue);
} else {
// Out of the js thread, the `WorkletRef` class here is just a placeholder and should not be accessed directly.
// The real WorkletRef will be generated by the worklet runtime.
this._id = -1;
this._type = '__LEPUS__';
this._wvid = --lastIdMT;
}
}

Expand Down Expand Up @@ -76,7 +75,7 @@ abstract class WorkletRef<T> {
*/
toJSON(): { _wvid: WorkletRefImpl<T>['_wvid'] } {
return {
_wvid: this._id,
_wvid: this._wvid,
};
}
}
Expand All @@ -92,7 +91,7 @@ export class MainThreadRef<T> extends WorkletRef<T> {
constructor(initValue: T) {
super(initValue, 'main-thread');
if (__JS__) {
const id = this._id;
const id = this._wvid;
this._lifecycleObserver = lynx.getNativeApp().createJSObjectDestructionObserver?.(() => {
lynx.getCoreContext?.().dispatchEvent({
type: WorkletEvents.releaseWorkletRef,
Expand Down
16 changes: 16 additions & 0 deletions packages/react/worklet-runtime/__test__/workletRuntime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';

import { initApiEnv } from '../src/api/lynxApi';
import { currentCtx } from '../src/ctxTrace';
import { updateWorkletRefInitValueChanges } from '../src/workletRef';
import { initWorklet } from '../src/workletRuntime';

Expand Down Expand Up @@ -154,6 +155,21 @@ describe('Worklet', () => {
expect(fn).toHaveBeenLastCalledWith(4);
});

it('should track current worklet ctx', async () => {
initWorklet();

const fn = vi.fn(() => {
expect(currentCtx.ctx._wkltId).toEqual('1');
});
registerWorklet('main-thread', '1', fn);
let worklet = {
_wkltId: '1',
};
runWorklet(worklet);
expect(fn).toBeCalled();
expect(currentCtx).toBeUndefined();
});

it('value of a workletRef should be preserved between calls', async () => {
initWorklet();

Expand Down
21 changes: 21 additions & 0 deletions packages/react/worklet-runtime/src/ctxTrace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2024 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.

import type { ClosureValueType, Worklet } from './bindings/types.js';

interface CtxTrace {
ctx: Worklet;
}

export let currentCtx: CtxTrace | undefined;

export function traceCtxCall(ctx: Worklet, _params: ClosureValueType[]): void {
currentCtx = {
ctx,
};
}

export function clearCurrentCtx(): void {
currentCtx = undefined;
}
Loading