import { isObject, isEmpty } from '../../internal/base' import { pipe, compose } from '../../functional' import { Vain } from '../vain' import { DEFAULT_ATOM_TRIGGER_REGISTER_OPTIONS, isAtomLike, isDataLike, isMutationLike, DEFAULT_SUBSCRIBE_OPTIONS, GC_DATA, GC_MUTATION } from '../atoms' import { AnyStringRecord } from '../../@types' import type { Particle, Datar, Mutator, MutatorTransformation } from '../particles' import type { AtomType, Trigger, AtomTriggerRegisterOptions, TriggerController, AtomLike, BaseAtom, Data, Mutation, DataLike, MutationLike, DataOptions, MutationOptions, SubscribeOptions, ValueConsumer, DatarConsumer, DataConsumer, DataSubscription, TransformationConsumer, MutatorConsumer, MutationConsumer, MutationSubscription } from '../atoms' /****************************************************************************************************** * * Mediator Predicatess * ******************************************************************************************************/ /** * @param tar anything * @return whether the target is an Mediator instance */ export const isMediator = (tar: any): tar is BaseMediator => isObject(tar) && tar.isMediator /** * @param tar anything * @return whether the target is an Data Mediator instance */ export const isDataMediator = (tar: any): tar is DataMediator => isMediator(tar) && tar.isMediator && tar.isData /** * @param tar anything * @return whether the target is an Mutation Mediator instance */ export const isMutationMediator =

(tar: any): tar is MutationMediator => isMediator(tar) && tar.isMediator && !tar.isMutation /****************************************************************************************************** * * Mediator Classes * ******************************************************************************************************/ /** * */ type Unary = (input: I) => O /** * */ interface _MediatorTypeSymbol /** private */ { _type: 'MediatorType' } export type MediatorTypeMaker = T & _MediatorTypeSymbol export type MediatorType = string & _MediatorTypeSymbol /** * */ export abstract class BaseMediator extends Vain { _atom: A constructor (atom: A) { super() if (new.target === BaseMediator) { throw new Error('Mediator class can not be instantiated!') } this._atom = atom } /****************************************************************************************************** * Mediator's propertys and methods ******************************************************************************************************/ /** * */ get isMediator (): true { return true } abstract get mediatorType (): MediatorType /****************************************************************************************************** * Atom's propertys and methods ******************************************************************************************************/ /** * */ get atom (): A { return this._atom } get atomType (): AtomType { return this._atom.atomType } get particleName (): string { return this._atom.particleName } get particle (): Particle { return this._atom.particle } get metaName (): string { return this._atom.metaName } get meta (): any { return this._atom.meta } get options (): AnyStringRecord { return this._atom.options } get isAtom (): boolean { return isAtomLike(this._atom) } get isData (): boolean { return isDataLike(this._atom) } get isMutation (): boolean { return isMutationLike(this._atom) } get isEmpty (): boolean { return isEmpty(this._atom) } 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 { // ! do not use: // ! return this._atom.pipe(...args) // @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 { // ! do not use: // ! return this._atom.compose(...args) // @ts-expect-error pass args through return compose(...args)(this) } release (): void { if (isDataLike(this._atom)) { this._atom = GC_DATA as unknown as A } else if (isMutationLike(this._atom)) { this._atom = GC_MUTATION as unknown as A } else { throw (new TypeError('Invalid atom type!')) } } } /****************************************************************************************************** * * DataMediator * ******************************************************************************************************/ /** * */ export abstract class DataMediator extends BaseMediator> implements AtomLike { setOptions(key: K, value: Required[K]): void { this._atom.setOptions(key, value) } get datar (): Datar { return this._atom.datar } get value (): V { return this._atom.value } subscribe (consumer: DatarConsumer): DataSubscription subscribe (consumer: DatarConsumer, options: SubscribeOptions & { isExtracted: false }): DataSubscription subscribe (consumer: ValueConsumer, options: SubscribeOptions & { isExtracted: true }): DataSubscription // NOTE: catch all overload subscribe (consumer: DataConsumer, options?: SubscribeOptions): DataSubscription subscribe ( consumer: DataConsumer, options: SubscribeOptions = DEFAULT_SUBSCRIBE_OPTIONS ): DataSubscription { return this._atom.subscribe(consumer, options) } subscribeValue ( consumer: ValueConsumer, options: SubscribeOptions = DEFAULT_SUBSCRIBE_OPTIONS ): DataSubscription { return this._atom.subscribeValue(consumer, options) } getSubscriptionByConsumer (consumer: DataConsumer): DataSubscription | undefined { return this._atom.getSubscriptionByConsumer(consumer) } getSubscriptionByHostAtom (hostAtom: AtomLike): DataSubscription | undefined { return this._atom.getSubscriptionByHostAtom(hostAtom) } getSubscription (consumerOrHostAtom: DataConsumer | AtomLike): DataSubscription | undefined { return this._atom.getSubscription(consumerOrHostAtom) } unsubscribe (target: DataConsumer | DataSubscription | AtomLike): boolean { return this._atom.unsubscribe(target) } unsubscribeAll (): boolean { return this._atom.unsubscribeAll() } trigger (datar?: Datar): void { return this._atom.trigger(datar) } triggerValue (value?: Datar['value']): void { return this._atom.triggerValue(value) } observe (mutation: MutationLike): MutationSubscription { return this._atom.observe(mutation) } unobserve (mutation: MutationLike): boolean { return this._atom.unobserve(mutation) } unobserveAll (): boolean { return this._atom.unobserveAll() } beObservedBy (mutation: MutationLike): DataSubscription { return this._atom.beObservedBy(mutation) } mutate

( mutator: MutationLike | Mutator | MutatorTransformation | ((...args: any[]) => V), mutation?: MutationLike ): this { this._atom.mutate(mutator, mutation) return this } registerTrigger ( trigger: Trigger>, options: AtomTriggerRegisterOptions = DEFAULT_ATOM_TRIGGER_REGISTER_OPTIONS ): TriggerController { return this._atom.registerTrigger(trigger, options) } release (): void { this._atom = GC_DATA as Data } } /****************************************************************************************************** * * MutationMediator * ******************************************************************************************************/ /** * */ export abstract class MutationMediator

extends BaseMediator> implements AtomLike { setOptions(key: K, value: Required[K]): void { this._atom.setOptions(key, value) } get mutator (): Mutator { return this._atom.mutator } get transformation (): MutatorTransformation { return this._atom.transformation } subscribe (consumer: MutatorConsumer): MutationSubscription subscribe (consumer: MutatorConsumer, options: SubscribeOptions & { isExtracted: false }): MutationSubscription subscribe (consumer: TransformationConsumer, options: SubscribeOptions & { isExtracted: true }): MutationSubscription // NOTE: catch all overload subscribe (consumer: MutationConsumer, options?: SubscribeOptions): MutationSubscription subscribe ( consumer: MutationConsumer, options: SubscribeOptions = DEFAULT_SUBSCRIBE_OPTIONS ): MutationSubscription { return this._atom.subscribe(consumer, options) } subscribeTransformation ( consumer: TransformationConsumer, options: SubscribeOptions = DEFAULT_SUBSCRIBE_OPTIONS ): MutationSubscription { return this._atom.subscribeTransformation(consumer, options) } trigger (mutator?: Mutator): void { return this._atom.trigger(mutator) } triggerTransformation (transformation?: Mutator['transformation']): void { return this._atom.triggerTransformation(transformation) } getSubscriptionByConsumer (consumer: MutationConsumer): MutationSubscription | undefined { return this._atom.getSubscriptionByConsumer(consumer) } getSubscriptionByHostAtom (hostAtom: AtomLike): MutationSubscription | undefined { return this._atom.getSubscriptionByHostAtom(hostAtom) } getSubscription (consumerOrHostAtom: MutationConsumer | AtomLike): MutationSubscription | undefined { return this._atom.getSubscription(consumerOrHostAtom) } unsubscribe (target: MutationConsumer | MutationSubscription | AtomLike): boolean { return this._atom.unsubscribe(target) } unsubscribeAll (): boolean { return this._atom.unsubscribeAll() } observe (data: DataLike

): DataSubscription

{ return this._atom.observe(data) } unobserve (data: DataLike

): boolean { return this._atom.unobserve(data) } unobserveAll (): boolean { return this._atom.unobserveAll() } beObservedBy (data: DataLike): MutationSubscription { // return this._atom.beObservedBy(...args) as ReturnType // NOTE: for type support return this._atom.beObservedBy(data) } mutate (datar: DataLike

| Datar

| P, data?: DataLike

): this { this._atom.mutate(datar, data) return this } registerTrigger ( trigger: Trigger | ((...args: any[]) => C) | C>, options: AtomTriggerRegisterOptions = DEFAULT_ATOM_TRIGGER_REGISTER_OPTIONS ): TriggerController { return this._atom.registerTrigger(trigger, options) } release (): void { this._atom = GC_MUTATION as Mutation } }