import { is, Store } from '@zedux/core' import { AtomInstanceTtl, AtomApiGenerics, Prettify, } from '@zedux/atoms/types/index' import { prefix } from '@zedux/atoms/utils/general' export class AtomApi { public static $$typeof = Symbol.for(`${prefix}/AtomApi`) public exports?: G['Exports'] public promise: G['Promise'] public store: G['Store'] public ttl?: AtomInstanceTtl | (() => AtomInstanceTtl) public value: G['State'] | G['Store'] constructor(value: AtomApi | G['Store'] | G['State']) { this.promise = undefined as G['Promise'] this.value = value as G['Store'] | G['State'] this.store = (is(value, Store) ? value : undefined) as G['Store'] if (is(value, AtomApi)) { Object.assign(this, value as AtomApi) } } public addExports>( exports: NewExports ): AtomApi< Prettify< Omit & { Exports: (G['Exports'] extends Record ? unknown : G['Exports']) & NewExports } > > { if (!this.exports) this.exports = exports as any else this.exports = { ...this.exports, ...exports } return this as AtomApi< Omit & { Exports: (G['Exports'] extends Record ? unknown : G['Exports']) & NewExports } > } public setExports>( exports: NewExports ): AtomApi & { Exports: NewExports }>> { ;( this as unknown as AtomApi & { Exports: NewExports }> ).exports = exports return this as unknown as AtomApi< Omit & { Exports: NewExports } > // for chaining } public setPromise(): AtomApi & { Promise: undefined }> public setPromise

| undefined>( promise: P ): AtomApi & { Promise: P }> public setPromise

| undefined>( promise?: P ): AtomApi & { Promise: P }>> { this.promise = promise as unknown as G['Promise'] return this as unknown as AtomApi & { Promise: P }> // for chaining } public setTtl(ttl: AtomInstanceTtl | (() => AtomInstanceTtl)) { this.ttl = ttl return this // for chaining } }