Skip to content

Commit

Permalink
feat(server-context): dynamic extra state/event type
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Jun 21, 2023
1 parent 32a46c1 commit 1cb6db1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
18 changes: 8 additions & 10 deletions core/server-context/src/api-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import {AlwatrServerRequestBase} from './server-request.js';
import type {ServerRequestState} from './server-request.js';
import type {FetchOptions} from '@alwatr/fetch/type.js';
import type {ListenerCallback, SubscribeOptions, SubscribeResult} from '@alwatr/signal2';
import type {AlwatrServiceResponse, Stringifyable, StringifyableRecord} from '@alwatr/type';
import type {AlwatrServiceResponse} from '@alwatr/type';

export abstract class AlwatrApiRequestBase<
TData extends Stringifyable = Stringifyable,
TMeta extends StringifyableRecord = StringifyableRecord
> extends AlwatrServerRequestBase {
protected _responseJson?: AlwatrServiceResponse<TData, TMeta>;
T extends AlwatrServiceResponse = AlwatrServiceResponse,
ExtraState extends string = never,
ExtraEvent extends string = never
> extends AlwatrServerRequestBase<ExtraState, ExtraEvent> {
protected _responseJson?: T;

protected override async _$fetch(options: FetchOptions): Promise<void> {
if (!NODE_MODE) {
Expand Down Expand Up @@ -60,18 +61,15 @@ export abstract class AlwatrApiRequestBase<
}
}

export class AlwatrApiRequest<
TData extends Stringifyable = Stringifyable,
TMeta extends StringifyableRecord = StringifyableRecord
> extends AlwatrApiRequestBase<TData, TMeta> {
export class AlwatrApiRequest<T extends AlwatrServiceResponse = AlwatrServiceResponse> extends AlwatrApiRequestBase<T> {
/**
* Current state.
*/
get state(): ServerRequestState {
return this._state;
}

get response(): AlwatrServiceResponse<TData, TMeta> | undefined {
get response(): T | undefined {
return this._responseJson;
}

Expand Down
52 changes: 27 additions & 25 deletions core/server-context/src/server-request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {fetch} from '@alwatr/fetch';
import {FiniteStateMachineBase} from '@alwatr/fsm2';
import {ActionRecord, FiniteStateMachineBase, StateRecord} from '@alwatr/fsm2';

import type {FetchOptions} from '@alwatr/fetch/type.js';
import type {ListenerCallback, SubscribeOptions, SubscribeResult} from '@alwatr/signal2';
Expand All @@ -9,35 +9,37 @@ export interface ServerRequestConfig extends Partial<FetchOptions> {
}

export type ServerRequestState = 'initial' | 'loading' | 'failed' | 'complete';

export type ServerRequestEvent = 'request' | 'requestFailed' | 'requestSuccess';

export abstract class AlwatrServerRequestBase extends FiniteStateMachineBase<ServerRequestState, ServerRequestEvent> {
export abstract class AlwatrServerRequestBase<
ExtraState extends string = never,
ExtraEvent extends string = never
> extends FiniteStateMachineBase<ServerRequestState | ExtraState, ServerRequestEvent | ExtraEvent> {
protected _$fetchOptions?: FetchOptions;
protected _response?: Response;

protected override _stateRecord = <StateRecord<ServerRequestState | ExtraState, ServerRequestEvent | ExtraEvent>>{
initial: {
request: 'loading',
},
loading: {
requestFailed: 'failed',
requestSuccess: 'complete',
},
failed: {
request: 'loading',
},
complete: {
request: 'loading',
},
};

protected override _actionRecord = <ActionRecord<ServerRequestState | ExtraState, ServerRequestEvent | ExtraEvent>>{
_on_loading_enter: this._requestAction,
};

constructor(protected _config: ServerRequestConfig) {
super({name: _config.name, initialState: 'initial'});

this._stateRecord = {
initial: {
request: 'loading',
},
loading: {
requestFailed: 'failed',
requestSuccess: 'complete',
},
failed: {
request: 'loading',
},
complete: {
request: 'loading',
},
};

this._actionRecord = {
_on_loading_enter: this._requestAction,
};
}

protected _request(options?: Partial<FetchOptions>): void {
Expand Down Expand Up @@ -122,8 +124,8 @@ export class AlwatrServerRequest extends AlwatrServerRequestBase {
}

/**
* Unsubscribe from changes.
*/
* Unsubscribe from changes.
*/
unsubscribe(listenerCallback: ListenerCallback<this, ServerRequestState>): void {
return this._unsubscribe(listenerCallback);
}
Expand Down

0 comments on commit 1cb6db1

Please sign in to comment.