-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNgReduxComponent.ts
44 lines (36 loc) · 1.22 KB
/
NgReduxComponent.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
import { Store, Unsubscribe } from 'redux';
import { ChangeDetectorRef, OnDestroy, OnInit } from '@angular/core';
import { InputMapper } from './InputMapper';
export abstract class NgReduxComponent<A, I extends InputMapper<A>, M> implements OnDestroy, OnInit {
static store: Store<any>;
public props: I & M = <any> {};
private unsubscribeStore: Unsubscribe;
constructor(
private inputMapper: new(...args: any[]) => I,
private changeDetectorRef: ChangeDetectorRef
) {
this.init();
}
init() {
this.unsubscribeStore = NgReduxComponent.store.subscribe(() => {
this.onStoreChange(NgReduxComponent.store);
if (this.changeDetectorRef) {
this.changeDetectorRef.markForCheck();
}
});
this.onStoreChange(NgReduxComponent.store);
}
ngOnInit() {
// noop, this ensures angular calls any overrides further down the ctor chain
}
ngOnDestroy() {
this.unsubscribeStore();
}
onStoreChange(store: Store<A>) {
this.props = (Object as any).assign(
{},
new this.inputMapper(store.getState()),
{ state: null }
) as any;
}
}