-
Notifications
You must be signed in to change notification settings - Fork 0
/
MachineSource.ts
53 lines (50 loc) · 1.36 KB
/
MachineSource.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
import { SuperSource } from './Source';
import { MachineSubject } from './Machine';
import { Observable } from 'rxjs';
import { StateMap, MachineSource, Enable, Disable } from './types';
import { EmptyUnion } from '../types';
const machine = Symbol('machine');
/**
* A `MachineSource` implementation as an abstract class.
*/
export abstract class SuperMachineSource<S, T = S, D = EmptyUnion>
extends SuperSource<S, T, D>
implements MachineSource<T> {
private [machine]: MachineSubject;
protected constructor(
state: S,
deps: D,
map: StateMap<S, T>,
enable: Enable,
disable: Disable
) {
super(state, deps, map);
this[machine] = new MachineSubject(enable, disable);
}
public get active(): boolean {
return this[machine].active;
}
public get active$(): Observable<boolean> {
return this[machine].active$;
}
public enable(): void {
return this[machine].enable();
}
public disable(): void {
return this[machine].disable();
}
}
export class MachineSourceSubject<S, T = S> extends SuperMachineSource<S, T>
implements MachineSource<T> {
public constructor(
state: S,
map?: StateMap<S, T>,
enable?: Enable,
disable?: Disable
) {
super(state, null, map as StateMap<S, T>, enable, disable);
}
public next(state: Partial<S>, compare?: boolean): void {
return super.next(state, compare);
}
}