-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-ToJSONable-State.ts
196 lines (169 loc) · 5.18 KB
/
01-ToJSONable-State.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { Payload } from "almin";
import * as assert from "assert";
import { MyDomain } from "./domain/MyDomain";
// This is concept model of State
// Simple principle:
interface StatePrinciple<T> {
constructor(state: T): any;
toJSON(): T;
}
// Implementation
// It is based `JSON.stringify` default algorithm
// https://tc39.github.io/ecma262/#sec-serializejsonobject
const ToJSONObject = <T>(object: State<T>): T => {
const json: any = {};
const keys = Object.keys(object);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
json[key] = (object as any)[key];
}
return json as T;
};
abstract class State<T> {
constructor(state: T) {
}
merge(state: Partial<T>): State<T> {
return new (this as any).constructor({
...this as State<T>,
...state as any
});
}
toJSON(): T {
return ToJSONObject(this);
}
}
// Implement State sub class
interface MyStateProps {
value: string;
isLoading: boolean;
}
class MyState extends State<MyStateProps> implements MyStateProps {
value: string;
isLoading: boolean;
constructor(props: MyStateProps) {
super(props);
this.value = props.value;
this.isLoading = props.isLoading;
}
merge(state: Partial<MyState>): MyState {
return new (this as any).constructor({
...this as MyState,
...state
});
}
from(domain: MyDomain): MyState {
return this.merge({
value: domain.value
});
}
reduce(payload: Payload): MyState {
switch (payload.type) {
case "start":
return this.merge({
isLoading: true
});
case "stop":
return this.merge({
isLoading: false
});
default:
return this;
}
}
}
class ExtendState extends State<MyStateProps> implements MyStateProps {
value: string;
isLoading: boolean;
extensionValue: string;
constructor(props: MyStateProps) {
super(props);
this.value = props.value;
this.isLoading = props.isLoading;
this.extensionValue = "extension";
}
get extensionGetter() {
return this.extensionValue + "ext";
}
merge(state: Partial<MyStateProps>): ExtendState {
return new ExtendState({
// TODO: https://github.com/Microsoft/TypeScript/issues/10727
...this as MyStateProps,
...state
});
}
from(domain: MyDomain): ExtendState {
return this.merge({
value: domain.value
});
}
reduce(payload: Payload): ExtendState {
switch (payload.type) {
case "start":
return this.merge({
isLoading: true
});
case "stop":
return this.merge({
isLoading: false
});
default:
return this;
}
}
toJSON() {
return {
value: this.value,
isLoading: this.isLoading,
}; // without extensionValue
}
}
// Pass MyState
testYourStore(MyState);
// Pass Some extension State
testYourStore(ExtendState);
function testYourStore(StoreConstructor: new (..._: Array<any>) => MyState | ExtendState) {
describe(`${(StoreConstructor as any).name}`, () => {
it("Store<T> can unwrap T by Store#toJSON()", () => {
const stateProps = { value: "value", isLoading: false };
const aState = new StoreConstructor(stateProps);
assert.deepStrictEqual(aState.toJSON(), stateProps);
});
it("new Store === new Store", () => {
const stateProps = { value: "value", isLoading: false };
const aState = new StoreConstructor(stateProps);
const bState = new StoreConstructor(stateProps);
assert.deepStrictEqual(aState.toJSON(), bState.toJSON());
});
it("new Store === new Store(new Store())", () => {
const stateProps = { value: "value", isLoading: false };
const aState = new StoreConstructor(stateProps);
const bState = new StoreConstructor(new MyState(stateProps));
assert.deepStrictEqual(aState.toJSON(), bState.toJSON());
});
it("create Store from Payload", () => {
const initialState = new StoreConstructor({
value: "",
isLoading: false
});
const state = initialState.reduce({
type: "start"
});
assert.deepStrictEqual(state.toJSON(), {
value: "",
isLoading: true
});
});
it("create Store from Domain", () => {
const myDomain = new MyDomain("domain value");
const initialState = new StoreConstructor({
value: "",
isLoading: false
});
const state = initialState.from(myDomain);
assert.deepStrictEqual(state.toJSON(), {
value: "domain value",
isLoading: false
});
});
});
}