generated from iadvize/hello-world-monorepo-javascript-library
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
170 lines (124 loc) · 3.51 KB
/
index.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
168
169
170
export type Reducer<State> = (state: State) => State;
export type Listener<State> = (state: State) => void;
export type Unsubscribe = () => void;
export type Store<State> = {
/**
* Read the store current state
*
* @example
import * as Store from 'store';
const store = Store.create(() =\> 2)();
store.read() // 2
*
*/
readonly read: () => State;
/**
* Apply a reducer on the state and update the store current state with the
* result
*
* @example
import * as Store from 'store';
const store = Store.create(() =\> 2)();
store.apply(state =\> state + 1)();
store.read() // 3
*
*/
readonly apply: (reducer: Reducer<State>) => () => void;
/**
* Subscribe a listener to state changes
*
* The subscription returns a function to call to unsubscribe.
*
* @example
import * as Store from 'store';
const store = Store.create(() =\> 2)();
const subscribe = store.subscribe(() =\> console.log('hello world'));
const unsubscribe = subscribe();
store.apply(state =\> state + 1)();
// hello world
unsubscribe();
store.apply(state =\> state + 1)();
*
*/
readonly subscribe: (listener: Listener<State>) => () => Unsubscribe;
};
/**
* Store creation with a initialState
*
* @example
import * as Store from 'store';
const createNumberStore = Store.create(() =\> 2);
const store = createNumberStore();
*
*/
export function create<State>(
setupInitialState: () => State,
): () => Store<State> {
return () => {
let state = setupInitialState();
const listeners: Listener<State>[] = [];
function read(): State {
return state;
}
function apply(reducer: Reducer<State>) {
return (): void => {
state = reducer(state);
// copying listeners list here to deals with unsubscribe nested in
// subscription
const localListeners = listeners.slice();
localListeners.forEach((listener) => {
listener(state);
});
};
}
function subscribe(listener: Listener<State>) {
if (typeof listener !== 'function') {
throw new Error('Expected the listener to be a function.');
}
return (): Unsubscribe => {
listeners.push(listener);
let removed = false;
return (): void => {
// don't remove `listener` again if unsubscribe function has already
// been called once
if (removed) {
return;
}
removed = true;
const index = listeners.indexOf(listener);
if (index > -1) {
listeners.splice(index, 1);
}
};
};
}
return {
read,
apply,
subscribe,
};
};
}
/**
* Lift and apply a reducer factory, returning a function that, given some
* arguments, will apply the reducer on the state and update the store current
* state with the result
*
* @example
import * as Store from 'store';
const store = Store.create(() =\> 2)();
// instead of:
const add = (n: number) =\> store.apply(state =\> state + n);
// write:
const add = Store.liftAndApply((n: number) =\> state =\> state + n, store);
add(2)();
store.read() // 4
*
*/
export function liftAndApply<
State,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Fn extends (...args: any[]) => Reducer<State>
>(fn: Fn, store: Store<State>): (...args: Parameters<Fn>) => () => void {
return (...args: Parameters<Fn>) => store.apply(fn(...args));
}