This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
methods.ts
118 lines (90 loc) · 3.31 KB
/
methods.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
import { Reducers, State } from '../default';
import Callback from '../types/callback';
import Dispatchers from '../types/dispatchers';
import NewGlobalState from '../types/new-global-state';
import { ReactNComponent, ReactNPureComponent } from './components';
import GlobalStateManager from './global-state-manager';
import setGlobal from './set-global';
import getGlobalStateManager from './utils/get-global-state-manager';
// Accurately define React components as having an updater member variable.
interface TrueComponent extends ReactNComponent {
updater: {
enqueueForceUpdate: (
component: ReactNComponent,
_: null,
action: 'forceUpdate',
) => void;
};
}
// static getDerivedStateFromProps
/*
export function createReactNGetDerivedStateFromProps<P>(
Component: ReactNComponent<P> | ReactNPureComponent<P>
) {
return function ReactNGetDerivedStateFromProps(props: P, ...args) {
const globalState = getGlobalStateManager();
const newGlobalState = Component.getDerivedGlobalFromProps(props, globalState.state, ...args);
globalState.setAny(newGlobalState);
// getDerivedStateFromProps
if (Object.prototype.hasOwnProperty.call(Component, 'getDerivedStateFromProps')) {
return Component.getDerivedStateFromProps(props, ...args);
}
return null;
};
}
*/
// this.componentWillUnmount
export function ReactNComponentWillUnmount<G extends {} = State>(
that: ReactNComponent<any, any, G> | ReactNPureComponent<any, any, G>,
): void {
// No longer re-render this component on global state change.
getGlobalStateManager<G>().removePropertyListener(that._globalCallback);
}
// this.componentWillUpdate
export function ReactNComponentWillUpdate<G extends {} = State>(
that: ReactNComponent<any, any, G> | ReactNPureComponent<any, any, G>,
): void {
// No longer re-render this component on global state change.
getGlobalStateManager<G>().removePropertyListener(that._globalCallback);
}
// this.shouldComponentUpdate
export function ReactNShouldComponentUpdate<G extends {} = State>(
that: ReactNComponent<any, any, G> | ReactNPureComponent<any, any, G>,
): void {
// No longer re-render this component on global state change.
getGlobalStateManager<G>().removePropertyListener(that._globalCallback);
}
// this.dispatch
export function ReactNDispatch<
G extends {} = State,
R extends {} = Reducers,
>(): Dispatchers<G, R> {
return getGlobalStateManager<G, R>().dispatchers;
}
// this._globalCallback
export function ReactNGlobalCallback(
that: ReactNComponent | ReactNPureComponent,
): void {
(that as TrueComponent).updater.enqueueForceUpdate(
that as TrueComponent,
null,
'forceUpdate',
);
}
// this.global
// Provider.withGlobal passes its own global state instance.
export function ReactNGlobal<G extends {} = State>(
that: ReactNComponent<any, any, G>,
globalStateManager: GlobalStateManager<G> = getGlobalStateManager<G>(),
): Readonly<G> {
return globalStateManager.spyState(that._globalCallback);
}
// this.setGlobal
export function ReactNSetGlobal<G extends {} = State, R extends {} = Reducers>(
newGlobalState: NewGlobalState<G>,
callback: Callback<G, R> | null,
_sync: boolean,
globalStateManager: GlobalStateManager<G, R> = getGlobalStateManager<G, R>(),
): Promise<G> {
return setGlobal(globalStateManager, newGlobalState, callback);
}