forked from CSFrequency/react-firebase-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseList.ts
167 lines (144 loc) · 4.78 KB
/
useList.ts
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
165
166
167
import { useEffect, useMemo } from 'react';
import { snapshotToData, ValOptions } from './helpers';
import useListReducer from './helpers/useListReducer';
import { ListHook, ListKeysHook, ListValsHook, Val } from './types';
import { useIsEqualRef } from '../util';
import {
DataSnapshot,
Query,
onChildAdded as firebaseOnChildAdded,
onChildChanged as firebaseOnChildChanged,
onChildMoved as firebaseOnChildMoved,
onChildRemoved as firebaseOnChildRemoved,
onValue as firebaseOnValue,
off,
} from 'firebase/database';
export const useList = (query?: Query | null): ListHook => {
const [state, dispatch] = useListReducer();
const queryRef = useIsEqualRef(query, () => dispatch({ type: 'reset' }));
const ref: Query | null | undefined = queryRef.current;
useEffect(() => {
if (!ref) {
dispatch({ type: 'empty' });
return;
}
const onChildAdded = (
snapshot: DataSnapshot | null,
previousKey?: string | null
) => {
dispatch({ type: 'add', previousKey, snapshot });
};
const onChildChanged = (snapshot: DataSnapshot | null) => {
dispatch({ type: 'change', snapshot });
};
const onChildMoved = (
snapshot: DataSnapshot | null,
previousKey?: string | null
) => {
dispatch({ type: 'move', previousKey, snapshot });
};
const onChildRemoved = (snapshot: DataSnapshot | null) => {
dispatch({ type: 'remove', snapshot });
};
const onError = (error: Error) => {
dispatch({ type: 'error', error });
};
const onValue = (snapshots: DataSnapshot[] | null) => {
dispatch({ type: 'value', snapshots });
};
let childAddedHandler: ReturnType<typeof firebaseOnChildAdded> | undefined;
const onInitialLoad = (snapshot: DataSnapshot) => {
const snapshotVal = snapshot.val();
let childrenToProcess = snapshotVal
? Object.keys(snapshot.val()).length
: 0;
// If the list is empty then initialise the hook and use the default `onChildAdded` behaviour
if (childrenToProcess === 0) {
childAddedHandler = firebaseOnChildAdded(ref, onChildAdded, onError);
onValue([]);
} else {
// Otherwise, we load the first batch of children all to reduce re-renders
const children: DataSnapshot[] = [];
const onChildAddedWithoutInitialLoad = (
addedChild: DataSnapshot,
previousKey?: string | null
) => {
if (childrenToProcess > 0) {
childrenToProcess--;
children.push(addedChild);
if (childrenToProcess === 0) {
onValue(children);
}
return;
}
onChildAdded(addedChild, previousKey);
};
childAddedHandler = firebaseOnChildAdded(
ref,
onChildAddedWithoutInitialLoad,
onError
);
}
};
firebaseOnValue(ref, onInitialLoad, onError, { onlyOnce: true });
const childChangedHandler = firebaseOnChildChanged(
ref,
onChildChanged,
onError
);
const childMovedHandler = firebaseOnChildMoved(ref, onChildMoved, onError);
const childRemovedHandler = firebaseOnChildRemoved(
ref,
onChildRemoved,
onError
);
return () => {
off(ref, 'child_added', childAddedHandler);
off(ref, 'child_changed', childChangedHandler);
off(ref, 'child_moved', childMovedHandler);
off(ref, 'child_removed', childRemovedHandler);
};
}, [dispatch, ref]);
const resArray: ListHook = [state.value.values, state.loading, state.error];
return useMemo(() => resArray, resArray);
};
export const useListKeys = (query?: Query | null): ListKeysHook => {
const [snapshots, loading, error] = useList(query);
const values = useMemo(
() =>
snapshots
? snapshots.map((snapshot) => snapshot.key as string)
: undefined,
[snapshots]
);
const resArray: ListKeysHook = [values, loading, error];
return useMemo(() => resArray, resArray);
};
export const useListVals = <
T,
KeyField extends string = '',
RefField extends string = ''
>(
query?: Query | null,
options?: ValOptions<T>
): ListValsHook<T, KeyField, RefField> => {
const keyField = options ? options.keyField : undefined;
const refField = options ? options.refField : undefined;
const transform = options ? options.transform : undefined;
const [snapshots, loading, error] = useList(query);
const values = useMemo(
() =>
(snapshots
? snapshots.map((snapshot) =>
snapshotToData(snapshot, keyField, refField, transform)
)
: undefined) as Val<T, KeyField, RefField>[],
[snapshots, keyField, refField, transform]
);
const resArray: ListValsHook<T, KeyField, RefField> = [
values,
loading,
error,
];
return useMemo(() => resArray, resArray);
};