/
index.ts
125 lines (100 loc) · 3.22 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
import { Observable, Subject, Subscription } from 'rxjs'
export interface ShouldAction<T, IDType> {
/**
* Entity id that action should be applied to
*/
id: IDType
/**
* New value
*/
value: T
}
export interface DidAction<T, IDType> extends ShouldAction<T, IDType> {
/**
* Previous value
*/
previousValue: T
}
interface State<Actions, IDType> {
dids: Map<keyof Actions, Observable<DidAction<any, IDType>>>
shoulds: Map<keyof Actions, Observable<ShouldAction<any, IDType>>>
subscriptions: Map<keyof Actions, Subscription>
}
export type Reducers<Actions, IDType> = Record<
keyof Actions,
(data: ShouldAction<Actions[keyof Actions], IDType>) => Actions[keyof Actions]
>
export abstract class Emitter<Actions, IDType> {
private emitterState: State<Actions, IDType> = {
dids: new Map,
shoulds: new Map,
subscriptions: new Map
}
constructor(reducers: Reducers<Actions, IDType>) {
for (const type in reducers) {
if (reducers.hasOwnProperty(type)) {
this.registerReducer(type, reducers[type])
}
}
}
/**
* Dispatch an action
*/
dispatch<T extends keyof Actions>(type: T, data: ShouldAction<Actions[T], IDType>) {
if (!this.isActionRegistered(type)) {
throw Error(`You must define a reducer for action "${type}" before you call #emit on it.`)
}
this.getShould(type)!.next(data)
return this
}
/**
* Respond to an action (fired after app state has been mutated)
*/
on<T extends keyof Actions>(type: T) {
if (!this.isActionRegistered(type)) {
throw Error(`You must define a reducer for action "${type}" before you call #on on it.`)
}
return this.getDid(type)!
}
destroy() {
this.emitterState.subscriptions.forEach(_ => _.unsubscribe())
}
///////////////////// privates /////////////////////
/**
* Mutate app state in response to an action.
*
* `fn` takes the `ShouldAction`, and returns the previous value.
*/
private registerReducer<T extends keyof Actions>(type: T, fn: (data: ShouldAction<Actions[T], IDType>) => Actions[T]) {
// create observables
if (this.isActionRegistered(type)) {
throw Error(`A reducer is already defined for action "${type}". You cannot define more than 1 reducer per action type.`)
}
this.createDid(type)
this.createShould(type)
this.getShould(type)!.subscribe(data => {
const previousValue = fn(data)
this.getDid(type)!.next({
id: data.id,
previousValue,
value: data.value
})
})
return this
}
private createDid<T extends keyof Actions>(type: T) {
this.emitterState.dids.set(type, new Subject<DidAction<Actions[T], IDType>>())
}
private createShould<T extends keyof Actions>(type: T) {
this.emitterState.shoulds.set(type, new Subject<ShouldAction<Actions[T], IDType>>())
}
private getDid<T extends keyof Actions>(type: T) {
return this.emitterState.dids.get(type) as Subject<DidAction<Actions[T], IDType>>
}
private getShould<T extends keyof Actions>(type: T) {
return this.emitterState.shoulds.get(type) as Subject<ShouldAction<Actions[T], IDType>>
}
private isActionRegistered<T extends keyof Actions>(type: T) {
return this.emitterState.dids.has(type)
}
}