forked from OctoWoW/OctoLauncher
Initial commit
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import { observable } from '@trpc/server/observable';
|
||||
|
||||
type Func<T> = (arg: T) => void;
|
||||
|
||||
abstract class Observable<T> {
|
||||
private _listeners: Func<T>[] = [];
|
||||
|
||||
protected abstract _value: T;
|
||||
|
||||
protected _notifyObservers(v = this._value) {
|
||||
this._listeners = this._listeners.filter(l => {
|
||||
try {
|
||||
l(v);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
observe() {
|
||||
return observable<T>(e => {
|
||||
e.next(this._value);
|
||||
this._listeners.push(e.next);
|
||||
return () => {
|
||||
this._listeners = this._listeners.filter(v => v !== e.next);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
clearObservers() {
|
||||
this._listeners = [];
|
||||
}
|
||||
}
|
||||
|
||||
export default Observable;
|
||||
Reference in New Issue
Block a user