2024-03-15 22:48:28 +01:00
|
|
|
import { produce } from "immer"
|
|
|
|
|
2024-03-22 23:41:51 +01:00
|
|
|
type Listener<State> = (state: State) => void
|
2024-03-15 22:48:28 +01:00
|
|
|
|
2024-03-22 23:41:51 +01:00
|
|
|
type SetStateAction<State> = (state: State) => void
|
2024-03-15 22:48:28 +01:00
|
|
|
|
2024-03-22 23:41:51 +01:00
|
|
|
export const FETCH_STATES = ["idle", "loading", "error", "success"] as const
|
|
|
|
export type FetchState = (typeof FETCH_STATES)[number]
|
2024-03-15 22:48:28 +01:00
|
|
|
|
2024-03-22 23:41:51 +01:00
|
|
|
export type ErrorGlobal = "unknown" | null
|
|
|
|
|
|
|
|
export abstract class Presenter<State> {
|
|
|
|
private _state: State
|
|
|
|
private readonly _initialState: State
|
|
|
|
private readonly _listeners: Array<Listener<State>>
|
|
|
|
|
|
|
|
public constructor(initialState: State) {
|
2024-03-15 22:48:28 +01:00
|
|
|
this._state = initialState
|
|
|
|
this._initialState = initialState
|
|
|
|
this._listeners = []
|
|
|
|
}
|
|
|
|
|
2024-03-22 23:41:51 +01:00
|
|
|
public get initialState(): State {
|
2024-03-15 22:48:28 +01:00
|
|
|
return this._initialState
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description Set the state of the presenter.
|
|
|
|
* @param action A function that receives the current state and can update it by mutating it.
|
|
|
|
* @returns The new state.
|
|
|
|
*/
|
2024-03-22 23:41:51 +01:00
|
|
|
public setState(action: SetStateAction<State>): State {
|
2024-03-15 22:48:28 +01:00
|
|
|
this._state = produce(this._state, action)
|
|
|
|
this.notifyListeners()
|
|
|
|
return this._state
|
|
|
|
}
|
|
|
|
|
2024-03-22 23:41:51 +01:00
|
|
|
public subscribe(listener: Listener<State>): void {
|
2024-03-15 22:48:28 +01:00
|
|
|
this._listeners.push(listener)
|
|
|
|
}
|
|
|
|
|
2024-03-22 23:41:51 +01:00
|
|
|
public unsubscribe(listener: Listener<State>): void {
|
2024-03-15 22:48:28 +01:00
|
|
|
const listenerIndex = this._listeners.indexOf(listener)
|
|
|
|
const listenerFound = listenerIndex !== -1
|
|
|
|
if (listenerFound) {
|
|
|
|
this._listeners.splice(listenerIndex, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private notifyListeners(): void {
|
|
|
|
for (const listener of this._listeners) {
|
|
|
|
listener(this._state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|