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
87 lines (68 loc) · 2.21 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
import { BehaviorSubject, distinctUntilChanged, shareReplay } from 'rxjs';
import type { Subscription } from 'rxjs';
import { PublicSubject } from '../common/types';
export class Subject<T> {
public state: T;
private firstState: T;
private subject: BehaviorSubject<T>;
private subscriptions: Map<string | this, Subscription[]> = new Map();
constructor(state: T, subject: BehaviorSubject<T>) {
this.state = state;
this.firstState = state;
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);
if (this.subscriptions.has(subscriptionId)) {
this.subscriptions.get(subscriptionId).push(subscription);
return;
}
this.subscriptions.set(subscriptionId, [subscription]);
};
public unsubscribe(subscriptionId: string) {
this.subscriptions.get(subscriptionId)?.forEach((subscription) => subscription.unsubscribe());
this.subscriptions.delete(subscriptionId);
}
public destroy() {
this.subscriptions.clear();
this.subject.complete();
this.restart();
}
private restart() {
this.state = this.firstState;
this.subject = new BehaviorSubject<T>(this.firstState).pipe(
distinctUntilChanged(),
shareReplay({ bufferSize: 1, refCount: true }),
) as BehaviorSubject<T>;
}
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): Subject<T> {
const subject = new BehaviorSubject<T>(initialState);
return new Subject<T>(initialState, subject);
}