-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-State-Update-method.ts
56 lines (50 loc) · 1.38 KB
/
02-State-Update-method.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
import { MyDomain } from "./domain/MyDomain";
import { Payload } from "almin";
// static method
interface StateStatics<Props> {
// create new state from domain
create(domain: MyDomain): State<Props>;
}
// state
interface State<Props> {
// create new state
constructor(props: Props): State<Props>;
// update and merge state
merge(props: Partial<Props>): State<Props>;
// update state with domain
from(domain: MyDomain): State<Props>;
// update state with payload
reduce(payload: Payload): State<Props>;
}
const DefaultProps = {
value: "default value"
};
type Copy<T> = {
[P in keyof T]: T[P];
}
type Props = Copy<typeof DefaultProps>;
class BasicState implements State<Props> {
value: string;
static create(domain: MyDomain): BasicState {
const initialState = new this();
return initialState.from(domain);
}
constructor(props: typeof DefaultProps = DefaultProps) {
this.a = props.a;
this.b = props.b;
}
merge(props: Partial<Props>): BasicState {
return new (this as any).constrctor({
...this as BasicState,
...props as any as Object
})
}
from(domain: MyDomain): State<Props> {
return this.merge({
a: domain.value
});
}
reduce(payload: Payload): State<Props> {
throw new Error("Method not implemented.");
}
}