aboutsummaryrefslogtreecommitdiff
path: root/Timeline/ClientApp/src/app/data/SubscriptionHub.ts
diff options
context:
space:
mode:
Diffstat (limited to 'Timeline/ClientApp/src/app/data/SubscriptionHub.ts')
-rw-r--r--Timeline/ClientApp/src/app/data/SubscriptionHub.ts67
1 files changed, 38 insertions, 29 deletions
diff --git a/Timeline/ClientApp/src/app/data/SubscriptionHub.ts b/Timeline/ClientApp/src/app/data/SubscriptionHub.ts
index 2bc6de56..406d293f 100644
--- a/Timeline/ClientApp/src/app/data/SubscriptionHub.ts
+++ b/Timeline/ClientApp/src/app/data/SubscriptionHub.ts
@@ -3,10 +3,11 @@
// 2. We need a way to finalize the last object. For example, if it has an object url, we need to revoke it.
// 3. Make api easier to use and write less boilerplate codes.
//
+// Currently updator will wait for last update or creation to finish. So the old data passed to it will always be right. We may add feature for just cancel last one but not wait for it.
+//
// There might be some bugs, especially memory leaks and in asynchronization codes.
import * as rxjs from 'rxjs';
-import { filter } from 'rxjs/operators';
export type Subscriber<TData> = (data: TData) => void;
@@ -23,25 +24,26 @@ class SubscriptionToken {
}
class SubscriptionLine<TData> {
- private _lastDataPromise: Promise<void>;
- private _dataSubject = new rxjs.BehaviorSubject<TData | undefined>(undefined);
- private _data$: rxjs.Observable<TData> = this._dataSubject.pipe(
- filter((d) => d !== undefined)
- ) as rxjs.Observable<TData>;
+ private _lastDataPromise: Promise<TData>;
+ private _dataSubject: rxjs.BehaviorSubject<TData>;
private _refCount = 0;
constructor(
- _creator: () => Promise<TData>,
- private _destroyer: (data: TData) => void,
+ defaultValueProvider: () => TData,
+ setup: ((old: TData) => Promise<TData>) | undefined,
+ private _destroyer: ((data: TData) => void) | undefined,
private _onZeroRef: (self: SubscriptionLine<TData>) => void
) {
- this._lastDataPromise = _creator().then((data) => {
- this._dataSubject.next(data);
- });
+ const initValue = defaultValueProvider();
+ this._lastDataPromise = Promise.resolve(initValue);
+ this._dataSubject = new rxjs.BehaviorSubject<TData>(initValue);
+ if (setup != null) {
+ this.next(setup);
+ }
}
subscribe(subscriber: Subscriber<TData>): SubscriptionToken {
- const subscription = this._data$.subscribe(subscriber);
+ const subscription = this._dataSubject.subscribe(subscriber);
this._refCount += 1;
return new SubscriptionToken(subscription);
}
@@ -50,25 +52,26 @@ class SubscriptionLine<TData> {
token._subscription.unsubscribe();
this._refCount -= 1;
if (this._refCount === 0) {
- void this._lastDataPromise.then(() => {
- const last = this._dataSubject.value;
- if (last !== undefined) {
- this._destroyer(last);
- }
- });
+ const { _destroyer: destroyer } = this;
+ if (destroyer != null) {
+ void this._lastDataPromise.then((data) => {
+ destroyer(data);
+ });
+ }
this._onZeroRef(this);
}
}
- next(updator: () => Promise<TData>): void {
+ next(updator: (old: TData) => Promise<TData>): void {
this._lastDataPromise = this._lastDataPromise
- .then(() => updator())
+ .then((old) => updator(old))
.then((data) => {
const last = this._dataSubject.value;
- if (last !== undefined) {
+ if (this._destroyer != null) {
this._destroyer(last);
}
this._dataSubject.next(data);
+ return data;
});
}
}
@@ -79,10 +82,12 @@ export interface ISubscriptionHub<TKey, TData> {
export class SubscriptionHub<TKey, TData>
implements ISubscriptionHub<TKey, TData> {
+ // If setup is set, update is called with setup immediately after setting default value.
constructor(
public keyToString: (key: TKey) => string,
- public creator: (key: TKey) => Promise<TData>,
- public destroyer: (key: TKey, data: TData) => void
+ public defaultValueProvider: (key: TKey) => TData,
+ public setup?: (key: TKey) => Promise<TData>,
+ public destroyer?: (key: TKey, data: TData) => void
) {}
private subscriptionLineMap = new Map<string, SubscriptionLine<TData>>();
@@ -92,11 +97,15 @@ export class SubscriptionHub<TKey, TData>
const line = (() => {
const savedLine = this.subscriptionLineMap.get(keyString);
if (savedLine == null) {
+ const { setup, destroyer } = this;
const newLine = new SubscriptionLine<TData>(
- () => this.creator(key),
- (data) => {
- this.destroyer(key, data);
- },
+ () => this.defaultValueProvider(key),
+ setup != null ? () => setup(key) : undefined,
+ destroyer != null
+ ? (data) => {
+ destroyer(key, data);
+ }
+ : undefined,
() => {
this.subscriptionLineMap.delete(keyString);
}
@@ -115,11 +124,11 @@ export class SubscriptionHub<TKey, TData>
// Old data is destroyed automatically.
// updator is called only if there is subscription.
- update(key: TKey, updator: (key: TKey) => Promise<TData>): void {
+ update(key: TKey, updator: (key: TKey, old: TData) => Promise<TData>): void {
const keyString = this.keyToString(key);
const line = this.subscriptionLineMap.get(keyString);
if (line != null) {
- line.next(() => updator(key));
+ line.next((old) => updator(key, old));
}
}
}