-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse-subject.ts
108 lines (93 loc) · 2.96 KB
/
use-subject.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
import { type Context, createContext, useCallback, useContext, useEffect, useState } from 'react';
type SubjectLike<TValue> = {
next?(nextValue: TValue): void;
subscribe(listener: (value: TValue) => void): { unsubscribe(): void };
readonly value: TValue;
};
type SubjectHookResult<TValue> = readonly [
value: TValue,
setValue: (dispatch: TValue | ((current: TValue) => TValue)) => boolean,
];
const isDispatchFunction = <TValue>(
value: TValue | ((current: TValue) => TValue),
): value is (current: TValue) => TValue => {
return typeof value === 'function';
};
/**
* Subscribe to a subject for the lifetime of a React component.
*
* @param subject Subject-like object (`value`, `subscribe`, and optionally `next` properties).
*/
const useSubject = <TValue>(subject: SubjectLike<TValue>): SubjectHookResult<TValue> => {
const [value, setValueInternal] = useState(subject.value);
const setValue = useCallback(
(dispatch: TValue | ((current: TValue) => TValue)): boolean => {
if (typeof subject.next !== 'function') {
return false;
}
subject.next(isDispatchFunction(dispatch) ? dispatch(subject.value) : dispatch);
return true;
},
[subject],
);
useEffect(() => {
/**
* Ignore the first callback if the subject calls the listener immediately
* (eg. RxJS BehaviorSubject).
*/
let set: (newValue: TValue) => void = () => {
return;
};
const subscription = subject.subscribe((newValue) => {
set(newValue);
});
set = setValueInternal;
set(subject.value);
return () => {
subscription.unsubscribe();
};
}, [subject]);
return [value, setValue];
};
/**
* Create a React context and hook for a subject type.
*/
type createSubjectContext = {
/**
* Create a React context and hook for a subject type.
*
* Returns undefined if no subject is provided by the current React context.
*/
<TValue = undefined>(): readonly [
hook: () => SubjectHookResult<TValue | undefined>,
context: Context<SubjectLike<TValue | undefined>>,
];
/**
* Create a React context and hook for a subject type.
*
* Returns the `defaultValue` if no subject is provided by the current React
* context.
*/
<TValue = undefined>(
defaultValue?: TValue,
): readonly [hook: () => SubjectHookResult<TValue>, context: Context<SubjectLike<TValue>>];
};
const createSubjectContext: createSubjectContext = <TValue = undefined>(
defaultValue?: TValue,
): [hook: () => SubjectHookResult<TValue>, context: Context<SubjectLike<TValue>>] => {
const context = createContext<SubjectLike<TValue>>({
subscribe: () => {
return {
unsubscribe: () => {
return;
},
};
},
value: defaultValue as unknown as TValue,
});
const useSubjectContext = (): SubjectHookResult<TValue> => {
return useSubject(useContext(context));
};
return [useSubjectContext, context];
};
export { createSubjectContext, type SubjectLike, useSubject };