Skip to content

Commit

Permalink
fix(remote-context): enhance state management and add reloading_faile…
Browse files Browse the repository at this point in the history
…d event for handling manual reload
  • Loading branch information
arashagp committed Dec 25, 2024
1 parent 54abf01 commit 785d641
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/remote-context/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServerContextState>;
Expand All @@ -22,6 +24,7 @@ export abstract class AlwatrRemoteContextStateMachineBase<T extends Json = Json>
ExtraEvent
> {
protected context_?: T;
protected isLoadedFromRemote_ = true;

constructor(config: AlwatrRemoteContextStateMachineConfig) {
super(config);
Expand All @@ -36,7 +39,7 @@ export abstract class AlwatrRemoteContextStateMachineBase<T extends Json = Json>
offline_check: {
request_failed: 'failed',
cache_not_found: 'loading',
request_succeeded: 'reloading',
request_succeeded: 'complete',
},
/**
* First loading without any cached context.
Expand Down Expand Up @@ -76,12 +79,14 @@ export abstract class AlwatrRemoteContextStateMachineBase<T extends Json = Json>

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_();
}
Expand Down

0 comments on commit 785d641

Please sign in to comment.