From 6abedd4f4a4cff1eb3663a912474f5475a08357f Mon Sep 17 00:00:00 2001 From: Alexander Weidt Date: Wed, 10 Jun 2020 20:26:48 +0200 Subject: [PATCH 1/2] add highly experimental SuspenseStore --- src/entity/DynamicLoadingStore.ts | 8 +++ src/entity/SuspenseStore.ts | 110 ++++++++++++++++++++++++++++++ src/index.ts | 1 + 3 files changed, 119 insertions(+) create mode 100644 src/entity/SuspenseStore.ts diff --git a/src/entity/DynamicLoadingStore.ts b/src/entity/DynamicLoadingStore.ts index 1be583b..588701a 100644 --- a/src/entity/DynamicLoadingStore.ts +++ b/src/entity/DynamicLoadingStore.ts @@ -66,6 +66,14 @@ export class DynamicLoadingStore 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

( value: Readonly | undefined, cachedTimestamp: Date | undefined, diff --git a/src/entity/SuspenseStore.ts b/src/entity/SuspenseStore.ts new file mode 100644 index 0000000..da3d314 --- /dev/null +++ b/src/entity/SuspenseStore.ts @@ -0,0 +1,110 @@ +/* +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 { + status: 'pending' | 'error' | 'success' = 'pending'; + protected result?: entity; + error?: unknown; + protected subscription?: Promise; + + 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 | 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 extends DynamicLoadingStore { + private loadingObservables: Map> = new Map>(); + + // 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 { + let value: entity | undefined | Observable = 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(value); + } + + loadOne( + id: id, + _currTimestamp?: Date, + addPipesFunction?: (observable: Observable) => Observable, + ): Observable { + 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 = DynamicLoadingStoreProperties; + +export function createSuspenseStore_experimental( + props: SuspenseStoreProperties, +): SuspenseStore { + return new SuspenseStore(props); +} diff --git a/src/index.ts b/src/index.ts index 15f8c6f..2f7b8f1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; From 0ac5de6164d08141f13044d0f65a7e8f6c52c2a5 Mon Sep 17 00:00:00 2001 From: Alexander Weidt Date: Wed, 10 Jun 2020 20:31:38 +0200 Subject: [PATCH 2/2] fix linter errors --- src/entity/SuspenseStore.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/entity/SuspenseStore.ts b/src/entity/SuspenseStore.ts index da3d314..6e3b2a4 100644 --- a/src/entity/SuspenseStore.ts +++ b/src/entity/SuspenseStore.ts @@ -54,7 +54,7 @@ class SuspenseWrapper { (error) => { this.error = error; this.status = 'error'; - }, + } ); } else { this.result = result; @@ -88,13 +88,11 @@ export class SuspenseStore extends DynamicLo loadOne( id: id, _currTimestamp?: Date, - addPipesFunction?: (observable: Observable) => Observable, + addPipesFunction?: (observable: Observable) => Observable ): Observable { let result = super.loadOne(id, _currTimestamp, addPipesFunction); this.loadingObservables.set(id, result); - result = result.pipe( - tap(() => this.loadingObservables.delete(id)), - ); + result = result.pipe(tap(() => this.loadingObservables.delete(id))); // todo: could introduce problems, if one is actually subscribing to this observable result.subscribe(); return result; @@ -104,7 +102,7 @@ export class SuspenseStore extends DynamicLo export type SuspenseStoreProperties = DynamicLoadingStoreProperties; export function createSuspenseStore_experimental( - props: SuspenseStoreProperties, + props: SuspenseStoreProperties ): SuspenseStore { return new SuspenseStore(props); }