-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
370 additions
and
341 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"nostrudel": patch | ||
--- | ||
|
||
Rebuild observable class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
{ | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"printWidth": 120 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import Observable from "zen-observable"; | ||
|
||
export default class ControlledObservable<T> implements Observable<T> { | ||
private observable: Observable<T>; | ||
private subscriptions = new Set<ZenObservable.SubscriptionObserver<T>>(); | ||
private _complete = false; | ||
get closed() { | ||
return this._complete; | ||
} | ||
get used() { | ||
return this.subscriptions.size > 0; | ||
} | ||
|
||
constructor(subscriber?: ZenObservable.Subscriber<T>) { | ||
this.observable = new Observable((observer) => { | ||
this.subscriptions.add(observer); | ||
const cleanup = subscriber && subscriber(observer); | ||
return () => { | ||
this.subscriptions.delete(observer); | ||
if (typeof cleanup === "function") cleanup(); | ||
else if (cleanup?.unsubscribe) cleanup.unsubscribe(); | ||
}; | ||
}); | ||
|
||
this.subscribe = this.observable.subscribe.bind(this.observable); | ||
this.map = this.observable.map.bind(this.observable); | ||
this.flatMap = this.observable.flatMap.bind(this.observable); | ||
this.forEach = this.observable.forEach.bind(this.observable); | ||
this.reduce = this.observable.reduce.bind(this.observable); | ||
this.filter = this.observable.filter.bind(this.observable); | ||
this.concat = this.observable.concat.bind(this.observable); | ||
} | ||
|
||
next(v: T) { | ||
if (this._complete) return; | ||
for (const observer of this.subscriptions) { | ||
observer.next(v); | ||
} | ||
} | ||
error(err: any) { | ||
if (this._complete) return; | ||
for (const observer of this.subscriptions) { | ||
observer.error(err); | ||
} | ||
} | ||
complete() { | ||
if (this._complete) return; | ||
this._complete = true; | ||
for (const observer of this.subscriptions) { | ||
observer.complete(); | ||
} | ||
} | ||
|
||
[Symbol.observable]() { | ||
return this.observable; | ||
} | ||
subscribe: Observable<T>["subscribe"]; | ||
map: Observable<T>["map"]; | ||
flatMap: Observable<T>["flatMap"]; | ||
forEach: Observable<T>["forEach"]; | ||
reduce: Observable<T>["reduce"]; | ||
filter: Observable<T>["filter"]; | ||
concat: Observable<T>["concat"]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.