import Base from './base' import Event from './event' import Utils from './utils' import GetSet from './get-set' import { LoaderDone } from './index' import { EventCallback } from './types' import * as Loader from './loader' type StatusLoader = ( self: Status, done: LoaderDone, fail: (error: any) => void, params: any ) => any type StateHandler = () => { [key: string]: any } | Array export type StatusOptions> = { states: T, loaders?: { [key: string]: StatusLoader } } type Channels = { fetch: [{ name: string result: any }] reset: [{ name: string }] } class Status < O extends StatusOptions, T extends O['loaders'] extends undefined ? { [key: string]: any } : O['loaders'] = O['loaders'] extends undefined ? { [key: string]: any } : O['loaders'] > extends Base { readonly _id = Utils.generateId() readonly _name: string readonly _states: any = {} readonly _loaders: Loader.LoaderCase private _options: O private _event = new Event('status') constructor(name: string, options: O) { super('Status') this._name = name this._options = options this._loaders = Loader.create(this, 'status', options.loaders || {}) for (let key in options.states) { let item = options.states[key]() if (item == null || typeof item !== 'object') { throw this.$devError('init', `Alas Status ${key} state must be a object.`) } this._states[key] = item } } get loaders() { return this._loaders._items as { [key in keyof T]: (( params: Parameters[3] extends undefined ? void : Parameters[3] ) => Promise< Parameters[1] extends LoaderDone ? Parameters[1] : any>[0] >) & Loader.default< Parameters[1] extends LoaderDone ? Parameters[1] : any>[0], Parameters[3] extends undefined ? void : Parameters[3] > } } on(channelName: T, callback: EventCallback) { return this._event.on(channelName, callback) } once(channelName: T, callback: EventCallback) { return this._event.once(channelName, callback) } off(channelName: T, id: string) { return this._event.off(channelName, id) } set(name: N, value: ReturnType) { let item = this._options.states[name.toString()] if (item == null) { throw new Error('State not found') } this._event.emit(this, 'set', [{ name, value }]) this._states[name] = value } fetch(name: N): ReturnType { if (this._states[name] == null) { throw this.$devError('fetch', `State ${name as string} not found`) } let result = GetSet(this._states[name], { get: (target, key) => { let output = this._states[name][key] return typeof output === 'function' ? output.bind(target) : output }, set: (target, key, value) => { this._states[name][key] = value return true } }) this._event.emit(this, 'fetch', [ { name, result } ]) return result } reset(name: N) { let item = this._options.states[name.toString()] if (item == null) { throw this.$devError('reset', `State ${name as string} not found`) } this._states[name] = item() this._event.emit(this, 'reset', [{ name }]) } resetAll(params?: { withLoader: boolean }) { let keys = Object.keys(this._options.states) for (let key of keys) { this.reset(key) } if (params?.withLoader) { this._loaders.reset() } } } export default Status