This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
68 lines (56 loc) · 1.87 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
import { BehaviorSubject, distinctUntilChanged, shareReplay } from 'rxjs';
import type { Subscription } from 'rxjs';
import { PublicSubject } from '../common/types';
export class Subject<T> {
public state: T;
private subject: BehaviorSubject<T>;
private subscriptions: Map<string | this, Subscription> = new Map();
private showLog: boolean;
constructor(state: T, subject: BehaviorSubject<T>, showLog?: boolean) {
this.state = state;
this.showLog = !!showLog;
this.subject = subject.pipe(
distinctUntilChanged(),
shareReplay({ bufferSize: 1, refCount: true }),
) as BehaviorSubject<T>;
}
private getValue(): T {
return this.state;
}
private setValue = (newValue: T): void => {
this.state = newValue;
this.subject.next(this.state);
};
public subscribe = (subscriptionId: string | this, callback: (value: T) => void) => {
const subscription = this.subject.subscribe(callback);
this.subscriptions.set(subscriptionId, subscription);
};
public unsubscribe(subscriptionId: string) {
this.subscriptions.get(subscriptionId)?.unsubscribe();
this.subscriptions.delete(subscriptionId);
}
public destroy() {
this.subscriptions.forEach((subscription) => subscription.unsubscribe());
this.subscriptions.clear();
}
public expose(): PublicSubject<T> {
const subscribe = this.subscribe.bind(this);
const unsubscribe = this.unsubscribe.bind(this);
const getter = this.getValue.bind(this);
const setter = this.setValue.bind(this);
return {
get value() {
return getter();
},
set value(newValue: T) {
setter(newValue);
},
subscribe,
unsubscribe,
};
}
}
export default function subject<T>(initialState: T, showLog?: boolean): Subject<T> {
const subject = new BehaviorSubject<T>(initialState);
return new Subject<T>(initialState, subject, showLog);
}