import { isObject } from '../../internal/base' import { pipe, compose } from '../../functional' import { Vain } from '../vain' import { Meta } from '../metas' import { Particle } from '../particles' import type { Data } from './data.atom' import type { Mutation } from './mutation.atom' import type { DataMediator, MutationMediator } from '../mediators' import type { AnyFunction, AnyStringRecord, CastAny, First, Last } from '../../@types' /****************************************************************************************************** * * Atoms * ******************************************************************************************************/ /** * Number of atoms is finite. Using a enum to group them is reasonable. */ export enum AtomType { Data = '[atom Data]', Mutation = '[atom Mutation]', } /****************************************************************************************************** * * Atom Predicates * ******************************************************************************************************/ /** * */ export interface AtomLike { isAtom: boolean atomType: AtomType particleName: string metaName: string particle: Particle meta: any options: AnyStringRecord subscribe: (consumer: (particle: Particle, atom?: AtomLike) => void, options?: AnyStringRecord) => Subscription getSubscriptionByConsumer: (consumer: (particle: Particle, atom?: AtomLike) => void) => Subscription | undefined getSubscriptionByHostAtom: (hostAtom: AtomLike) => Subscription | undefined getSubscription: (consumerOrHostAtom: ((particle: Particle, atom?: AtomLike) => void) | AtomLike) => Subscription | undefined unsubscribe: (target: ((particle: Particle, atom?: AtomLike) => void) | Subscription | AtomLike) => boolean unsubscribeAll: () => boolean trigger: (particle?: any) => void observe: (atom?: any) => Subscription unobserve: (atom?: any) => boolean unobserveAll: () => boolean beObservedBy: (atom?: any) => Subscription pipe: (...fns: FNS) => FNS extends [] ? [] : ReturnType>> compose: (...fns: AnyFunction[]) => FNS extends [] ? [] : ReturnType>> } export type DataLike = Data | DataMediator export type MutationLike

= Mutation | MutationMediator /** * AtomLike that can take any as input and any as output. */ export type AtomLikeOfAny = Data | DataMediator | Mutation | MutationMediator /** * AtomLike that can take I as input. */ export type AtomLikeOfInput = Data | DataMediator | Mutation | MutationMediator /** * AtomLike that can emit O as output. */ export type AtomLikeOfOutput = Data | DataMediator | Mutation | MutationMediator /** * Target will be considered `AtomLike` when its `isAtom` property is true, etc. Data, Mutation, DataMediator, MutationMediator. */ export const isAtomLike = (target: any): target is AtomLike => isObject(target) && target.isAtom /** * Target will be considered `DataLike` when its `isData` property is true, etc. `Data` or `DataMediator`. */ export function isDataLike (target: DataLike): target is DataLike export function isDataLike (target: unknown): target is DataLike export function isDataLike (target: any): target is DataLike { return isObject(target) && target.isData } /** * Target will be considered `MutationLike` when its `isMutation` property is true, etc. `Mutation` or `MutationMediator`. */ export function isMutationLike

(target: MutationLike): target is MutationLike export function isMutationLike

(target: unknown): target is MutationLike export function isMutationLike

(target: any): target is MutationLike { return isObject(target) && target.isMutation } /** * @param target anything * @return { boolean } whether the target is an Atom instance */ export const isAtom = (target: any): target is BaseAtom => isObject(target) && target.isAtom /****************************************************************************************************** * * Atom Classes * ******************************************************************************************************/ /** * */ type Unary = (input: I) => O /** * */ export interface BaseAtomOptions { isAsync?: boolean } export const DEFAULT_BASEATOM_OPTIONS: Required = { isAsync: false } /** * @important please consider BaseMediator when add property or method to Atom * !! */ export abstract class BaseAtom extends Vain { constructor () { super() if (new.target === BaseAtom) { throw new Error('BaseAtom class can not be instantiated!') } } abstract get atomType (): AtomType abstract get particleName (): string abstract get metaName (): string abstract get particle (): Particle abstract get meta (): any get isAtom (): boolean { return true } abstract get options (): AnyStringRecord abstract setOptions (key: keyof BaseAtomOptions, value: BaseAtomOptions[Key]): void pipe (): this pipe (fn1: Unary): A pipe (fn1: Unary, fn2: Unary): B pipe (fn1: Unary, fn2: Unary, fn3: Unary): C pipe (fn1: Unary, fn2: Unary, fn3: Unary, fn4: Unary): D // eslint-disable-next-line max-len pipe (fn1: Unary, fn2: Unary, fn3: Unary, fn4: Unary, fn5: Unary): E // eslint-disable-next-line max-len pipe (fn1: Unary, fn2: Unary, fn3: Unary, fn4: Unary, fn5: Unary, fn6: Unary): F // eslint-disable-next-line max-len pipe (fn1: Unary, fn2: Unary, fn3: Unary, fn4: Unary, fn5: Unary, fn6: Unary, fn7: Unary): G // eslint-disable-next-line max-len pipe (fn1: Unary, fn2: Unary, fn3: Unary, fn4: Unary, fn5: Unary, fn6: Unary, fn7: Unary, fn8: Unary): H // eslint-disable-next-line max-len pipe (fn1: Unary, fn2: Unary, fn3: Unary, fn4: Unary, fn5: Unary, fn6: Unary, fn7: Unary, fn8: Unary, fn9: Unary): I // eslint-disable-next-line max-len pipe (fn1: Unary, fn2: Unary, fn3: Unary, fn4: Unary, fn5: Unary, fn6: Unary, fn7: Unary, fn8: Unary, fn9: Unary, fn10: Unary): J // eslint-disable-next-line max-len pipe (fn1: Unary, fn2: Unary, fn3: Unary, fn4: Unary, fn5: Unary, fn6: Unary, fn7: Unary, fn8: Unary, fn9: Unary, fn10: Unary, ...fns: Array>): any pipe (...args: any[]): any { // @ts-expect-error pass args through return pipe(...args)(this) } compose (): this compose (fn1: Unary): A compose (fn2: Unary, fn1: Unary): B compose (fn3: Unary, fn2: Unary, fn1: Unary): C compose (fn4: Unary, fn3: Unary, fn2: Unary, fn1: Unary): D // eslint-disable-next-line max-len compose (fn5: Unary, fn4: Unary, fn3: Unary, fn2: Unary, fn1: Unary): E // eslint-disable-next-line max-len compose (fn6: Unary, fn5: Unary, fn4: Unary, fn3: Unary, fn2: Unary, fn1: Unary): F // eslint-disable-next-line max-len compose (fn7: Unary, fn6: Unary, fn5: Unary, fn4: Unary, fn3: Unary, fn2: Unary, fn1: Unary): G // eslint-disable-next-line max-len compose (fn8: Unary, fn7: Unary, fn6: Unary, fn5: Unary, fn4: Unary, fn3: Unary, fn2: Unary, fn1: Unary): H // eslint-disable-next-line max-len compose (fn9: Unary, fn8: Unary, fn7: Unary, fn6: Unary, fn5: Unary, fn4: Unary, fn3: Unary, fn2: Unary, fn1: Unary): I // eslint-disable-next-line max-len compose (fn10: Unary, fn9: Unary, fn8: Unary, fn7: Unary, fn6: Unary, fn5: Unary, fn4: Unary, fn3: Unary, fn2: Unary, fn1: Unary): J compose (...args: any[]): any { // @ts-expect-error pass args through return compose(...args)(this) } } /****************************************************************************************************** * * Atom Related Types * ******************************************************************************************************/ /** *—————————————————————————————————————————————— Subscribe Related ———————————————————————————————————————————————————— */ /** * */ export interface SubscribeOptions { hostAtom?: AtomLike | null isExtracted?: boolean } export const DEFAULT_SUBSCRIBE_OPTIONS: Required = { hostAtom: null, isExtracted: false } export interface Subscription { hostAtom?: AtomLike subscribeOptions: SubscribeOptions originalConsumer: AnyFunction proxyConsumer: (particle: any, atom?: any) => void unsubscribe: () => boolean } /** *—————————————————————————————————————————————— Trigger Related ———————————————————————————————————————————————————— */ /** * */ export interface AtomTriggerRegisterOptions { forceWrap?: boolean } export const DEFAULT_ATOM_TRIGGER_REGISTER_OPTIONS: Required = { forceWrap: false } export interface TriggerController { start?: () => void pause?: () => void cancel?: () => void } export type InternalTrigger = (accepted: Accepted, ...args: any[]) => void export type Trigger = (internalTrigger: InternalTrigger) => TriggerController