Skip to content

Commit 0e09187

Browse files
committed
feat: support events
1 parent ac95ff2 commit 0e09187

File tree

5 files changed

+54
-13
lines changed

5 files changed

+54
-13
lines changed

examples/react/src/MTC.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
renderFakeMTCSlot,
66
renderMTCSlot,
77
} from '@lynx-js/react/internal';
8+
import { MainThread } from '@lynx-js/types';
89

910
const componentInstanceId = 1;
1011

@@ -14,6 +15,16 @@ if (__MAIN_THREAD__) {
1415
function $$mtc_RealMTC(props) {
1516
return (
1617
<view>
18+
<text
19+
bindtap={(e: MainThread.TouchEvent) => {
20+
console.log('click');
21+
e.currentTarget.setStyleProperties({
22+
'background-color': 'red',
23+
});
24+
}}
25+
>
26+
MTC
27+
</text>
1728
{renderMTCSlot(props.btc1)}
1829
{renderMTCSlot(props.btc2)}
1930
</view>

packages/react/runtime/src/lifecycle/patch/updateMainThread.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import { updateWorkletRefInitValueChanges } from '@lynx-js/react/worklet-runtime
77
import type { PatchList, PatchOptions } from './commit.js';
88
import { setMainThreadHydrating } from './isMainThreadHydrating.js';
99
import { snapshotPatchApply } from './snapshotPatchApply.js';
10+
import { printSnapshotInstance } from '../../debug/printSnapshot.js';
1011
import { LifecycleConstant } from '../../lifecycleConstant.js';
1112
import { markTiming, setPipeline } from '../../lynx/performance.js';
1213
import { __pendingListUpdates } from '../../pendingListUpdates.js';
1314
import { applyRefQueue } from '../../snapshot/workletRef.js';
14-
import { __page } from '../../snapshot.js';
15+
import { __page, snapshotInstanceManager } from '../../snapshot.js';
1516
import { getReloadVersion } from '../pass.js';
1617

1718
function updateMainThread(
@@ -51,8 +52,8 @@ function updateMainThread(
5152
snapshotPatchApply(snapshotPatch);
5253
}
5354
__pendingListUpdates.flush();
54-
// console.debug('********** Lepus updatePatch:');
55-
// printSnapshotInstance(snapshotInstanceManager.values.get(-1)!);
55+
console.debug('********** Lepus updatePatch:');
56+
printSnapshotInstance(snapshotInstanceManager.values.get(-1)!);
5657
}
5758
} finally {
5859
markTiming('patchChangesEnd');

packages/react/runtime/src/snapshot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ function renderMTC(snapshotInstance: SnapshotInstance, props: MTCProps) {
713713
wrapper.nodeType = 1;
714714
render(vnode, wrapper as unknown as ContainerNode);
715715
wrapper.ensureElements();
716-
console.log('yra renderMTC render-1', vnode.__e, vnode.__e !== undefined);
716+
// console.log('yra renderMTC render-1', vnode.__e, vnode.__e !== undefined);
717717
if (vnode.__e) {
718718
snapshotInstance.insertBefore(vnode.__e as SnapshotInstance);
719719
(vnode.__e as SnapshotInstance).__onDestroy = () => {

packages/react/runtime/src/snapshot/event.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
11
// Copyright 2024 The Lynx Authors. All rights reserved.
22
// Licensed under the Apache License Version 2.0 that can be found in the
33
// LICENSE file in the root directory of this source tree.
4+
5+
import { BaseEvent } from '@lynx-js/types';
6+
7+
import { Element } from '../../../worklet-runtime/src/api/element.js';
48
import { SnapshotInstance } from '../snapshot.js';
59

10+
const mtcEvents: Map<number, (e: unknown) => void> = /*#__PURE__*/ new Map();
11+
12+
function registerMTCEvent(id: string, callback: undefined | ((e: BaseEvent) => void)): unknown {
13+
if (!callback) {
14+
mtcEvents.delete(id);
15+
return undefined;
16+
}
17+
mtcEvents.set(id, callback);
18+
return {
19+
type: 'worklet',
20+
value: {
21+
_workletType: 'mtc',
22+
_wkltId: id,
23+
},
24+
};
25+
}
26+
27+
globalThis.runWorklet = (w, e: [BaseEvent]) => {
28+
const event = {
29+
...e[0],
30+
target: new Element(e[0].target.elementRefptr),
31+
currentTarget: new Element(e[0].currentTarget.elementRefptr),
32+
};
33+
console.log('yra runWorklet', w, event);
34+
mtcEvents.get(w._wkltId)(event);
35+
};
36+
637
function updateEvent(
738
snapshot: SnapshotInstance,
839
expIndex: number,
@@ -13,17 +44,19 @@ function updateEvent(
1344
spreadKey: string,
1445
): void {
1546
const value = snapshot.__values![expIndex];
47+
const eventId = `${snapshot.__id}:${expIndex}:${spreadKey}`;
1648
let event;
1749
if (!value) {
1850
event = undefined;
19-
} else if (typeof value === 'string') {
51+
registerMTCEvent(eventId, undefined);
52+
} else if (typeof value === 'string' || (typeof value === 'object' && value.type === 'worklet')) {
2053
event = value;
54+
} else if (typeof value === 'function') {
55+
event = registerMTCEvent(eventId, value);
2156
} else {
22-
event = `${snapshot.__id}:${expIndex}:${spreadKey}`;
57+
event = eventId;
2358
}
2459

25-
// todo: reuseId?
26-
2760
snapshot.__values![expIndex] = event;
2861
if (snapshot.__elements) {
2962
__AddEvent(snapshot.__elements[elementIndex]!, eventType, eventName, event);

packages/react/transform/src/swc_plugin_snapshot/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,11 +1279,7 @@ where
12791279
snapshot_values.push(Some(ExprOrSpread {
12801280
spread: None,
12811281
expr: Box::new(if let AttrName::Event(_, _) = attr_name {
1282-
if target == TransformTarget::LEPUS {
1283-
quote!("1" as Expr)
1284-
} else {
1285-
value
1286-
}
1282+
value
12871283
} else if let AttrName::Ref = attr_name {
12881284
if target == TransformTarget::LEPUS {
12891285
quote!("1" as Expr)

0 commit comments

Comments
 (0)