Skip to content

Commit

Permalink
add highly experimental SuspenseStore (#3)
Browse files Browse the repository at this point in the history
add highly experimental SuspenseStore
  • Loading branch information
BiggA94 authored Jun 10, 2020
1 parent 96fc61a commit ba77d86
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/entity/DynamicLoadingStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ export class DynamicLoadingStore<entity, id extends idType = number> extends Sel
return value;
}

protected getLastLoadedTime(id: id): Date | undefined {
return this.lastLoadedAt.get(id);
}

protected isCurrentlyLoading(id: id): boolean {
return this.currentlyLoading.has(id);
}

protected cacheIsInvalid<P>(
value: Readonly<entity> | undefined,
cachedTimestamp: Date | undefined,
Expand Down
108 changes: 108 additions & 0 deletions src/entity/SuspenseStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
MIT License
Copyright (c) 2020 Alexander Weidt (BiggA94)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

// CAREFUL! Highly experimental!
import {DynamicLoadingStore, DynamicLoadingStoreProperties} from './DynamicLoadingStore';
import {idType} from './EntityHandler';
import {Observable} from 'rxjs';
import {tap} from 'rxjs/operators';

class SuspenseWrapper<entity> {
status: 'pending' | 'error' | 'success' = 'pending';
protected result?: entity;
error?: unknown;
protected subscription?: Promise<void>;

public read(): entity | undefined {
if (this.status === 'pending') {
throw this.subscription;
} else if (this.status === 'error') {
throw this.error;
} else if (this.status === 'success') {
return this.result;
}
}

constructor(result: Observable<entity> | entity | undefined) {
if (result instanceof Observable) {
this.subscription = result.toPromise().then(
(result) => {
this.result = result;
this.status = 'success';
},
(error) => {
this.error = error;
this.status = 'error';
}
);
} else {
this.result = result;
this.status = 'success';
}
}
}

export class SuspenseStore<entity, id extends idType = number> extends DynamicLoadingStore<entity, id> {
private loadingObservables: Map<id, Observable<entity>> = new Map<id, Observable<entity>>();

// ignore this for now, decide wether to use same method, or other method for suspense api...
// eslint-disable-next-line
// @ts-ignore
getOne(id: id): SuspenseWrapper<entity> {
let value: entity | undefined | Observable<entity> = super.getOne(id);
const currentTimestamp = new Date(Date.now());
const cachedTimestamp = this.getLastLoadedTime(id);
if (this.cacheIsInvalid(value as entity | undefined, cachedTimestamp, currentTimestamp, id)) {
value = this.loadOne(id, currentTimestamp);
} else {
// if already loading
const loadingObservable = this.loadingObservables.get(id);
if (loadingObservable) {
value = loadingObservable;
}
}
return new SuspenseWrapper<entity>(value);
}

loadOne(
id: id,
_currTimestamp?: Date,
addPipesFunction?: (observable: Observable<entity>) => Observable<entity>
): Observable<entity> {
let result = super.loadOne(id, _currTimestamp, addPipesFunction);
this.loadingObservables.set(id, result);
result = result.pipe(tap(() => this.loadingObservables.delete(id)));
// todo: could introduce problems, if one is actually subscribing to this observable
result.subscribe();
return result;
}
}

export type SuspenseStoreProperties<entity, id extends idType = number> = DynamicLoadingStoreProperties<entity, id>;

export function createSuspenseStore_experimental<entity, id extends idType = number>(
props: SuspenseStoreProperties<entity, id>
): SuspenseStore<entity, id> {
return new SuspenseStore<entity, id>(props);
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from './entity/EntityStore';
export * from './entity/SelectEntityStore';
export * from './entity/DynamicLoadingStore';
export * from './entity/PersistentEntityStore';
export * from './entity/SuspenseStore';
export * from './entity/util';

0 comments on commit ba77d86

Please sign in to comment.