Skip to content

Commit 21498a6

Browse files
committed
Base refactor
1 parent 83d54cb commit 21498a6

39 files changed

Lines changed: 1887 additions & 3378 deletions

packages/react-native-reanimated/src/ConfigHelper.native.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,10 @@ import { runOnUISync } from 'react-native-worklets';
55
import type { LoggerConfig } from './common';
66
import { getLoggerConfig, IS_JEST, updateLoggerConfig } from './common';
77

8-
/** @deprecated This function is a no-op in Reanimated 4. */
9-
export function addWhitelistedNativeProps(
10-
_props: Record<string, boolean>
11-
): void {
12-
// Do nothing. This is just for backward compatibility.
13-
}
14-
15-
/** @deprecated This function is a no-op in Reanimated 4. */
16-
export function addWhitelistedUIProps(_props: Record<string, boolean>): void {
17-
// Do nothing. This is just for backward compatibility.
18-
}
8+
export {
9+
addWhitelistedNativeProps,
10+
addWhitelistedUIProps,
11+
} from './ConfigHelperBase';
1912

2013
/**
2114
* Updates Reanimated logger config with the user-provided configuration. Will

packages/react-native-reanimated/src/ConfigHelper.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,10 @@
33
import type { LoggerConfig } from './common';
44
import { getLoggerConfig, updateLoggerConfig } from './common';
55

6-
/** @deprecated This function is a no-op in Reanimated 4. */
7-
export function addWhitelistedNativeProps(
8-
_props: Record<string, boolean>
9-
): void {
10-
// Do nothing. This is just for backward compatibility.
11-
}
12-
13-
/** @deprecated This function is a no-op in Reanimated 4. */
14-
export function addWhitelistedUIProps(_props: Record<string, boolean>): void {
15-
// Do nothing. This is just for backward compatibility.
16-
}
6+
export {
7+
addWhitelistedNativeProps,
8+
addWhitelistedUIProps,
9+
} from './ConfigHelperBase';
1710

1811
/**
1912
* Updates Reanimated logger config with the user-provided configuration. Will
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
/** @deprecated This function is a no-op in Reanimated 4. */
4+
export function addWhitelistedNativeProps(
5+
_props: Record<string, boolean>
6+
): void {
7+
// Do nothing. This is just for backward compatibility.
8+
}
9+
10+
/** @deprecated This function is a no-op in Reanimated 4. */
11+
export function addWhitelistedUIProps(_props: Record<string, boolean>): void {
12+
// Do nothing. This is just for backward compatibility.
13+
}
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
'use strict';
2-
import { makeMutable } from './mutables';
2+
import { createReducedMotionManager } from './ReducedMotionBase';
33

44
type localGlobal = typeof global & Record<string, unknown>;
55

66
export function isReducedMotionEnabledInSystem() {
77
return !!(global as localGlobal)._REANIMATED_IS_REDUCED_MOTION;
88
}
99

10-
const IS_REDUCED_MOTION_ENABLED_IN_SYSTEM = isReducedMotionEnabledInSystem();
11-
12-
export const ReducedMotionManager = {
13-
jsValue: IS_REDUCED_MOTION_ENABLED_IN_SYSTEM,
14-
uiValue: makeMutable(IS_REDUCED_MOTION_ENABLED_IN_SYSTEM),
15-
setEnabled(value: boolean) {
16-
ReducedMotionManager.jsValue = value;
17-
ReducedMotionManager.uiValue.value = value;
18-
},
19-
};
10+
export const ReducedMotionManager = createReducedMotionManager(
11+
isReducedMotionEnabledInSystem()
12+
);
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
import { IS_WINDOW_AVAILABLE } from './common';
3-
import { makeMutable } from './mutables';
3+
import { createReducedMotionManager } from './ReducedMotionBase';
44

55
export function isReducedMotionEnabledInSystem() {
66
return IS_WINDOW_AVAILABLE
@@ -9,13 +9,6 @@ export function isReducedMotionEnabledInSystem() {
99
: false;
1010
}
1111

12-
const IS_REDUCED_MOTION_ENABLED_IN_SYSTEM = isReducedMotionEnabledInSystem();
13-
14-
export const ReducedMotionManager = {
15-
jsValue: IS_REDUCED_MOTION_ENABLED_IN_SYSTEM,
16-
uiValue: makeMutable(IS_REDUCED_MOTION_ENABLED_IN_SYSTEM),
17-
setEnabled(value: boolean) {
18-
ReducedMotionManager.jsValue = value;
19-
ReducedMotionManager.uiValue.value = value;
20-
},
21-
};
12+
export const ReducedMotionManager = createReducedMotionManager(
13+
isReducedMotionEnabledInSystem()
14+
);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
import { makeMutable } from './mutables';
3+
4+
export function createReducedMotionManager(initialValue: boolean) {
5+
const manager = {
6+
jsValue: initialValue,
7+
uiValue: makeMutable(initialValue),
8+
setEnabled(value: boolean) {
9+
manager.jsValue = value;
10+
manager.uiValue.value = value;
11+
},
12+
};
13+
return manager;
14+
}
Lines changed: 2 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,8 @@
11
'use strict';
2-
import type { NativeSyntheticEvent } from 'react-native';
3-
42
import { IS_JEST } from './common';
53
import { registerEventHandler, unregisterEventHandler } from './core';
6-
import type {
7-
EventPayload,
8-
IWorkletEventHandler,
9-
ReanimatedEvent,
10-
} from './hook/commonTypes';
11-
12-
type JSEvent<Event extends object> = NativeSyntheticEvent<EventPayload<Event>>;
13-
14-
// In JS implementation (e.g. for web) we don't use Reanimated's
15-
// event emitter, therefore we have to handle here
16-
// the event that came from React Native and convert it.
17-
function jsListener<Event extends object>(
18-
eventName: string,
19-
handler: (event: ReanimatedEvent<Event>) => void
20-
) {
21-
return (evt: JSEvent<Event>) => {
22-
handler({ ...evt.nativeEvent, eventName } as ReanimatedEvent<Event>);
23-
};
24-
}
4+
import type { IWorkletEventHandler, ReanimatedEvent } from './hook/commonTypes';
5+
import { WorkletEventHandlerWeb } from './WorkletEventHandlerBase';
256

267
class WorkletEventHandlerNative<
278
Event extends object,
@@ -90,52 +71,6 @@ class WorkletEventHandlerNative<
9071
}
9172
}
9273

93-
class WorkletEventHandlerWeb<
94-
Event extends object,
95-
> implements IWorkletEventHandler<Event> {
96-
eventNames: string[];
97-
listeners:
98-
| Record<string, (event: ReanimatedEvent<ReanimatedEvent<Event>>) => void>
99-
| Record<string, (event: JSEvent<Event>) => void>;
100-
101-
worklet: (event: ReanimatedEvent<Event>) => void;
102-
103-
constructor(
104-
worklet: (event: ReanimatedEvent<Event>) => void,
105-
eventNames: string[] = []
106-
) {
107-
this.worklet = worklet;
108-
this.eventNames = eventNames;
109-
this.listeners = {};
110-
this.setupWebListeners();
111-
}
112-
113-
setupWebListeners() {
114-
this.listeners = {};
115-
this.eventNames.forEach((eventName) => {
116-
this.listeners[eventName] = jsListener(eventName, this.worklet);
117-
});
118-
}
119-
120-
updateEventHandler(
121-
newWorklet: (event: ReanimatedEvent<Event>) => void,
122-
newEvents: string[]
123-
): void {
124-
// Update worklet and event names
125-
this.worklet = newWorklet;
126-
this.eventNames = newEvents;
127-
this.setupWebListeners();
128-
}
129-
130-
registerForEvents(_viewTag: number, _fallbackEventName?: string): void {
131-
// noop
132-
}
133-
134-
unregisterFromEvents(_viewTag: number): void {
135-
// noop
136-
}
137-
}
138-
13974
export const WorkletEventHandler = IS_JEST
14075
? WorkletEventHandlerWeb
14176
: WorkletEventHandlerNative;
Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,4 @@
11
'use strict';
2-
import type { NativeSyntheticEvent } from 'react-native';
3-
4-
import type {
5-
EventPayload,
6-
IWorkletEventHandler,
7-
ReanimatedEvent,
8-
} from './hook/commonTypes';
9-
10-
type JSEvent<Event extends object> = NativeSyntheticEvent<EventPayload<Event>>;
11-
12-
// In JS implementation (e.g. for web) we don't use Reanimated's
13-
// event emitter, therefore we have to handle here
14-
// the event that came from React Native and convert it.
15-
function jsListener<Event extends object>(
16-
eventName: string,
17-
handler: (event: ReanimatedEvent<Event>) => void
18-
) {
19-
return (evt: JSEvent<Event>) => {
20-
handler({ ...evt.nativeEvent, eventName } as ReanimatedEvent<Event>);
21-
};
22-
}
23-
24-
class WorkletEventHandlerWeb<
25-
Event extends object,
26-
> implements IWorkletEventHandler<Event> {
27-
eventNames: string[];
28-
listeners:
29-
| Record<string, (event: ReanimatedEvent<ReanimatedEvent<Event>>) => void>
30-
| Record<string, (event: JSEvent<Event>) => void>;
31-
32-
worklet: (event: ReanimatedEvent<Event>) => void;
33-
34-
constructor(
35-
worklet: (event: ReanimatedEvent<Event>) => void,
36-
eventNames: string[] = []
37-
) {
38-
this.worklet = worklet;
39-
this.eventNames = eventNames;
40-
this.listeners = {};
41-
this.setupWebListeners();
42-
}
43-
44-
setupWebListeners() {
45-
this.listeners = {};
46-
this.eventNames.forEach((eventName) => {
47-
this.listeners[eventName] = jsListener(eventName, this.worklet);
48-
});
49-
}
50-
51-
updateEventHandler(
52-
newWorklet: (event: ReanimatedEvent<Event>) => void,
53-
newEvents: string[]
54-
): void {
55-
// Update worklet and event names
56-
this.worklet = newWorklet;
57-
this.eventNames = newEvents;
58-
this.setupWebListeners();
59-
}
60-
61-
registerForEvents(_viewTag: number, _fallbackEventName?: string): void {
62-
// noop
63-
}
64-
65-
unregisterFromEvents(_viewTag: number): void {
66-
// noop
67-
}
68-
}
2+
import { WorkletEventHandlerWeb } from './WorkletEventHandlerBase';
693

704
export const WorkletEventHandler = WorkletEventHandlerWeb;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use strict';
2+
import type { NativeSyntheticEvent } from 'react-native';
3+
4+
import type {
5+
EventPayload,
6+
IWorkletEventHandler,
7+
ReanimatedEvent,
8+
} from './hook/commonTypes';
9+
10+
type JSEvent<Event extends object> = NativeSyntheticEvent<EventPayload<Event>>;
11+
12+
// In JS implementation (e.g. for web) we don't use Reanimated's
13+
// event emitter, therefore we have to handle here
14+
// the event that came from React Native and convert it.
15+
export function jsListener<Event extends object>(
16+
eventName: string,
17+
handler: (event: ReanimatedEvent<Event>) => void
18+
) {
19+
return (evt: JSEvent<Event>) => {
20+
handler({ ...evt.nativeEvent, eventName } as ReanimatedEvent<Event>);
21+
};
22+
}
23+
24+
export class WorkletEventHandlerWeb<
25+
Event extends object,
26+
> implements IWorkletEventHandler<Event> {
27+
eventNames: string[];
28+
listeners:
29+
| Record<string, (event: ReanimatedEvent<ReanimatedEvent<Event>>) => void>
30+
| Record<string, (event: JSEvent<Event>) => void>;
31+
32+
worklet: (event: ReanimatedEvent<Event>) => void;
33+
34+
constructor(
35+
worklet: (event: ReanimatedEvent<Event>) => void,
36+
eventNames: string[] = []
37+
) {
38+
this.worklet = worklet;
39+
this.eventNames = eventNames;
40+
this.listeners = {};
41+
this.setupWebListeners();
42+
}
43+
44+
setupWebListeners() {
45+
this.listeners = {};
46+
this.eventNames.forEach((eventName) => {
47+
this.listeners[eventName] = jsListener(eventName, this.worklet);
48+
});
49+
}
50+
51+
updateEventHandler(
52+
newWorklet: (event: ReanimatedEvent<Event>) => void,
53+
newEvents: string[]
54+
): void {
55+
// Update worklet and event names
56+
this.worklet = newWorklet;
57+
this.eventNames = newEvents;
58+
this.setupWebListeners();
59+
}
60+
61+
registerForEvents(_viewTag: number, _fallbackEventName?: string): void {
62+
// noop
63+
}
64+
65+
unregisterFromEvents(_viewTag: number): void {
66+
// noop
67+
}
68+
}

0 commit comments

Comments
 (0)