-
Notifications
You must be signed in to change notification settings - Fork 0
/
reduxular.ts
173 lines (146 loc) · 4.81 KB
/
reduxular.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
171
172
173
import {
createStore,
combineReducers,
applyMiddleware,
bindActionCreators,
compose,
Action,
Store as ReduxStore
} from 'redux';
export {
createStore,
combineReducers,
applyMiddleware,
bindActionCreators,
compose
};
export {
ReduxularElement
} from './reduxular-element';
const setInternalPrefix: 'set_internal_' = 'set_internal_';
const setExternalPrefix: 'set_external_' = 'set_external_';
export type Store<T, A extends Action> = {
- readonly [P in keyof T]: T[P];
} & ReduxStore<T, A>;
export type ReduxularListener<T> = (state: T) => any;
type GetterOrSetterAlreadyPresentResult = {
readonly object: Object;
readonly alreadySet: boolean;
};
// Required because the default redux Reducer type allows S to be undefined. We do not.
type ReduxularReducer<S, A> = (
state: S,
action: A
) => S;
export function createObjectStore<StateGeneric extends Object, A extends Action>(
initialState: Required<StateGeneric>,
listener: ReduxularListener<StateGeneric>,
object: Object,
reducer?: ReduxularReducer<StateGeneric, A>
): Store<StateGeneric, A> {
const reduxStore = createStore((state: StateGeneric = initialState, action: A) => {
if (
reducer !== null &&
reducer !== undefined
) {
return setterReducer<StateGeneric, A>(reducer(state, action), action);
}
else {
return setterReducer<StateGeneric, A>(state, action);
}
});
Object.keys(initialState).forEach((key) => {
const getterAlreadyPresentResult: Readonly<GetterOrSetterAlreadyPresentResult> = getGetterOrSetterAlreadyPresent(object, key, 'get');
const setterAlreadyPresentResult: Readonly<GetterOrSetterAlreadyPresentResult> = getGetterOrSetterAlreadyPresent(object, key, 'set');
const objectToOverride: Object = getterAlreadyPresentResult.alreadySet === true ? getterAlreadyPresentResult.object : setterAlreadyPresentResult.alreadySet === true ? setterAlreadyPresentResult.object : object;
Object.defineProperty(objectToOverride, key, {
...(getterAlreadyPresentResult.alreadySet === false ? {
get () {
return reduxStore.getState()[key as keyof StateGeneric];
}
}: {}),
...(setterAlreadyPresentResult.alreadySet === false ? {
set (val) {
(reduxStore.dispatch as any)({
type: `${setExternalPrefix}${key}`,
value: val
});
}
} : {})
});
});
reduxStore.subscribe(() => {
listener(reduxStore.getState());
});
setTimeout(() => {
(reduxStore.dispatch as any)({
type: 'RENDER'
});
});
return new Proxy({
...reduxStore,
...initialState
}, {
get: (obj, prop) => {
if (reduxStore.hasOwnProperty(prop)) {
return reduxStore[prop as keyof typeof reduxStore];
}
const state: Readonly<StateGeneric> = reduxStore.getState();
if (state.hasOwnProperty(prop)) {
return state[prop as keyof typeof state];
}
throw new Error(`Property "${prop.toString()}" not present in store or state.`);
},
set: (obj, prop, value) => {
(reduxStore.dispatch as any)({
type: `${setInternalPrefix}${prop.toString()}`,
value
});
return true;
}
});
}
function setterReducer<T, A extends Action>(state: T, action: A): T {
if (
action.type.startsWith(setInternalPrefix) ||
action.type.startsWith(setExternalPrefix)
) {
const prop = action.type.replace(setInternalPrefix, '').replace(setExternalPrefix, '');
return {
...state,
[prop]: (action as any).value
};
}
return state;
}
function getGetterOrSetterAlreadyPresent(
object: Object,
key: string,
getOrSet: 'get' | 'set'
): Readonly<GetterOrSetterAlreadyPresentResult> {
const ownPropertyDescriptor: Readonly<PropertyDescriptor>|undefined = Object.getOwnPropertyDescriptor(object, key);
if (
ownPropertyDescriptor &&
ownPropertyDescriptor[getOrSet]
) {
return {
object,
alreadySet: true
};
}
else {
const prototype: Object = Object.getPrototypeOf(object);
if (
prototype === null ||
prototype === undefined
) {
return {
object: prototype,
alreadySet: false
};
}
else {
return getGetterOrSetterAlreadyPresent(prototype, key, getOrSet);
}
}
}