From 785d6417fb9b9c73223dc4bc406135e78e976a5f Mon Sep 17 00:00:00 2001 From: arashagp Date: Wed, 25 Dec 2024 10:04:22 +0330 Subject: [PATCH] fix(remote-context): enhance state management and add reloading_failed event for handling manual reload --- packages/remote-context/src/base.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/remote-context/src/base.ts b/packages/remote-context/src/base.ts index bfb5ff3..7f36f45 100644 --- a/packages/remote-context/src/base.ts +++ b/packages/remote-context/src/base.ts @@ -6,12 +6,14 @@ import { } from '@alwatr/fetch-state-machine'; import {packageTracer} from '@alwatr/nanolib'; +import type {Json} from '@alwatr/type-helper'; + __dev_mode__: packageTracer.add(__package_name__, __package_version__); type ExtraState = 'offline_check' | 'reloading' | 'reloading_failed'; export type ServerContextState = ServerRequestState | ExtraState; -type ExtraEvent = 'cache_not_found'; +type ExtraEvent = 'cache_not_found' | 'reloading_failed'; export type ServerContextEvent = ServerRequestEvent | ExtraEvent; export type AlwatrRemoteContextStateMachineConfig = AlwatrFetchStateMachineConfig; @@ -22,6 +24,7 @@ export abstract class AlwatrRemoteContextStateMachineBase ExtraEvent > { protected context_?: T; + protected isLoadedFromRemote_ = true; constructor(config: AlwatrRemoteContextStateMachineConfig) { super(config); @@ -36,7 +39,7 @@ export abstract class AlwatrRemoteContextStateMachineBase offline_check: { request_failed: 'failed', cache_not_found: 'loading', - request_succeeded: 'reloading', + request_succeeded: 'complete', }, /** * First loading without any cached context. @@ -76,12 +79,14 @@ export abstract class AlwatrRemoteContextStateMachineBase protected offlineRequestAction_(): void { this.logger_.logMethod?.('offlineRequestAction_'); + this.isLoadedFromRemote_ = false; this.currentFetchOptions_!.cacheStrategy = 'cache_only'; this.requestAction_(); } protected onlineRequestAction_(): void { this.logger_.logMethod?.('onlineRequestAction_'); + this.isLoadedFromRemote_ = true; this.currentFetchOptions_!.cacheStrategy = 'update_cache'; this.requestAction_(); }