-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcontainers.ts
More file actions
164 lines (134 loc) · 3.87 KB
/
containers.ts
File metadata and controls
164 lines (134 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { useSyncExternalStore } from "react";
import { findNodeHandle, type ReactNativeElement } from "react-native";
import type { ReactNativeShadowNode } from "./types";
import { getFabricUIManager } from "./fabric";
export type GrabSelectionOwnerKind = "root" | "screen";
export type GrabSelectionOwner = {
id: string;
kind: GrabSelectionOwnerKind;
shadowNode: ReactNativeShadowNode;
registrationOrder: number;
};
type SelectionOwnersStoreSnapshot = {
owners: Map<string, GrabSelectionOwner>;
focusedScreenOwnerId: string | null;
};
let ownerIdCounter = 0;
let registrationOrder = 0;
let focusedScreenOwnerId: string | null = null;
const owners = new Map<string, GrabSelectionOwner>();
const listeners = new Set<() => void>();
const notify = () => {
for (const listener of listeners) {
listener();
}
};
const subscribe = (listener: () => void) => {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
};
const getSnapshot = (): SelectionOwnersStoreSnapshot => ({
owners: new Map(owners),
focusedScreenOwnerId,
});
const getOwnerShadowNode = (ref: ReactNativeElement, errorMessage: string) => {
// @ts-expect-error - findNodeHandle is not typed correctly
const nativeTag = findNodeHandle(ref);
if (!nativeTag) {
throw new Error(errorMessage);
}
return getFabricUIManager().findShadowNodeByTag_DEPRECATED(nativeTag);
};
const getFallbackRootOwner = () => {
const rootOwners = Array.from(owners.values()).filter((owner) => owner.kind === "root");
rootOwners.sort((left, right) => right.registrationOrder - left.registrationOrder);
return rootOwners[0] ?? null;
};
export const createGrabSelectionOwnerId = (kind: GrabSelectionOwnerKind) => {
ownerIdCounter += 1;
return `react-native-grab-${kind}-${ownerIdCounter}`;
};
export const registerGrabSelectionOwner = (
id: string,
kind: GrabSelectionOwnerKind,
ref: ReactNativeElement,
) => {
const shadowNode = getOwnerShadowNode(
ref,
kind === "root"
? "Failed to find native tag for app root"
: "Failed to find native tag for screen",
);
registrationOrder += 1;
owners.set(id, {
id,
kind,
shadowNode,
registrationOrder,
});
notify();
};
export const unregisterGrabSelectionOwner = (id: string) => {
const removedOwner = owners.get(id);
if (!removedOwner) {
return;
}
owners.delete(id);
if (focusedScreenOwnerId === id) {
focusedScreenOwnerId = null;
}
notify();
};
export const setGrabSelectionOwnerFocused = (id: string, isFocused: boolean) => {
const owner = owners.get(id);
if (!owner || owner.kind !== "screen") {
return;
}
if (isFocused) {
focusedScreenOwnerId = id;
} else if (focusedScreenOwnerId === id) {
focusedScreenOwnerId = null;
}
notify();
};
export const clearGrabSelectionOwnerFocus = (id: string) => {
if (focusedScreenOwnerId !== id) {
return;
}
focusedScreenOwnerId = null;
notify();
};
export const getGrabSelectionOwner = (id: string): GrabSelectionOwner | null => {
return owners.get(id) ?? null;
};
export const getResolvedGrabSelectionOwner = (): GrabSelectionOwner | null => {
if (focusedScreenOwnerId) {
const focusedOwner = owners.get(focusedScreenOwnerId);
if (focusedOwner) {
return focusedOwner;
}
}
return getFallbackRootOwner();
};
export const getResolvedGrabSelectionOwnerId = (): string | null => {
return getResolvedGrabSelectionOwner()?.id ?? null;
};
export const useResolvedGrabSelectionOwnerId = () => {
return useSyncExternalStore(
subscribe,
() => getResolvedGrabSelectionOwnerId(),
() => null,
);
};
export const useIsResolvedGrabSelectionOwner = (id: string) => {
return useSyncExternalStore(
subscribe,
() => getResolvedGrabSelectionOwnerId() === id,
() => false,
);
};
export const useSelectionOwnersStore = () => {
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
};