import { isPlainObject, isArray, isFunction, isEmptyObject, isNil } from '../../internal/base' import { looseCurryN } from '../../functional' import { isAtomLike, Data } from '../atoms' import { replayWithLatest } from '../mediators' import { binaryTweenPipeAtom } from '../helpers' import type { AnyStringRecord, EmptyInterface, ReverseOptional } from '../../@types' import type { AtomLike, Mutation, AtomLikeOfOutput, AtomLikeOfInput } from '../atoms' import type { ReplayDataMediator, ReplayMutationMediator } from '../mediators' /************************************************************************************************ * * Main Types & Functions * ************************************************************************************************/ /** * */ export interface DriverOptions extends EmptyInterface { } export interface DriverLevelContexts extends EmptyInterface { inputs?: AnyStringRecord outputs?: AnyStringRecord } export interface DriverSingletonLevelContexts extends EmptyInterface { inputs?: AnyStringRecord outputs?: AnyStringRecord } export interface DriverInstance extends EmptyInterface { inputs?: AnyStringRecord outputs?: AnyStringRecord } export const DEFAULT_DRIVER_OPTIONS: Required = {} export const DEFAULT_DRIVER_LEVEL_CONTEXTS: Required = { inputs: {}, outputs: {} } export const DEFAULT_SINGLETON_LEVEL_CONTEXTS: Required = { inputs: {}, outputs: {} } type PrepareDriverSingletonLevelContexts< Options extends DriverOptions = DriverOptions, DLC extends DriverLevelContexts = DriverLevelContexts, DSLC extends DriverSingletonLevelContexts = DriverSingletonLevelContexts > = (options: Required, driverLevelContexts: DLC) => DSLC export interface GeneralDriverCreateOptions< Options extends DriverOptions = DriverOptions, DLC extends DriverLevelContexts = DriverLevelContexts, DSLC extends DriverSingletonLevelContexts = DriverSingletonLevelContexts, Instance extends DriverInstance = DriverInstance > { defaultOptions?: ReverseOptional prepareOptions?: (options: Required) => Required prepareDriverLevelContexts?: () => DLC prepareSingletonLevelContexts?: PrepareDriverSingletonLevelContexts prepareInstance?: ( options: Required, driverLevelContexts: DLC, singletonLevelContexts: DSLC ) => Instance } const DEFAULT_GENERAL_DRIVER_CREATE_OPTIONS: Required> = { defaultOptions: DEFAULT_DRIVER_OPTIONS, prepareOptions: (options) => options, prepareDriverLevelContexts: () => DEFAULT_DRIVER_LEVEL_CONTEXTS, prepareSingletonLevelContexts: (options, driverLevelContexts) => DEFAULT_SINGLETON_LEVEL_CONTEXTS, prepareInstance: (options, driverLevelContexts, singletonLevelContexts) => ({ ...singletonLevelContexts }) } export type DriverMaker< Options extends DriverOptions = DriverOptions, Instance extends DriverInstance = DriverInstance > = (options?: Options) => Instance /** * @param createOptions * @return { DriverMaker } DriverFactory :: `(options?: {}) => { inputs: object, outputs: object }` */ export const createGeneralDriver = < Options extends DriverOptions = DriverOptions, DLC extends DriverLevelContexts = DriverLevelContexts, DSLC extends DriverSingletonLevelContexts = DriverSingletonLevelContexts, Instance extends DriverInstance = DriverInstance >( createOptions: GeneralDriverCreateOptions | PrepareDriverSingletonLevelContexts = DEFAULT_GENERAL_DRIVER_CREATE_OPTIONS as any ): DriverMaker => { if (!isPlainObject(createOptions) && !isFunction(createOptions)) { throw (new TypeError('"createOptions" is expected to be type of "PlainObject" | "Function".')) } let preparedCreateOptions: Required> if (isFunction(createOptions)) { preparedCreateOptions = { ...DEFAULT_GENERAL_DRIVER_CREATE_OPTIONS as any, prepareSingletonLevelContexts: createOptions } } else { preparedCreateOptions = { ...DEFAULT_GENERAL_DRIVER_CREATE_OPTIONS as any, ...createOptions } } const { defaultOptions, prepareOptions, prepareDriverLevelContexts, prepareSingletonLevelContexts, prepareInstance } = preparedCreateOptions const _driverLevelContexts = prepareDriverLevelContexts() if (!isPlainObject(_driverLevelContexts)) { throw (new TypeError('The returned value of "prepareDriverLevelContexts" is expected to be type of "PlainObject".')) } const preparedDriverLevelContexts = { ...DEFAULT_DRIVER_LEVEL_CONTEXTS, ..._driverLevelContexts } /** * @param options In order to clarify the role of each configuration item, * the configuration is best to be in object format. * @return { DriverMaker } DriverFactory */ const driverFactory = (options: Options = defaultOptions as any): Instance => { if (!isPlainObject(options)) { throw (new TypeError('"options" is expected to be type of "PlainObject".')) } const preparedOptions = prepareOptions({ ...defaultOptions, ...options } as unknown as Required) if (!isPlainObject(preparedOptions)) { throw (new TypeError('The returned value of "prepareOptions" is expected to be type of "PlainObject".')) } const _singletonLevelContexts = prepareSingletonLevelContexts(preparedOptions, preparedDriverLevelContexts) if (!isPlainObject(_singletonLevelContexts)) { throw (new TypeError('"singletonLevelContexts" is expected to be type of "PlainObject"')) } const preparedSingletonLevelContexts = { ...DEFAULT_SINGLETON_LEVEL_CONTEXTS, ..._singletonLevelContexts } const { inputs, outputs } = preparedSingletonLevelContexts if (!isPlainObject(inputs)) { throw (new TypeError('"inputs" returned as singletonLevelContexts is expected to be type of "PlainObject"')) } if (!isPlainObject(outputs)) { throw (new TypeError('"outputs" returned as singletonLevelContexts is expected to be type of "PlainObject"')) } const driverInterfaces = prepareInstance(preparedOptions, preparedDriverLevelContexts, preparedSingletonLevelContexts) return driverInterfaces } return driverFactory } const formatDriverInterfaces = (driverInterfaces: DriverInstance): DriverInstance => { if (!isPlainObject(driverInterfaces)) { throw (new TypeError('"driverInterfaces" is expected to be type of "PlainObject".')) } const { inputs: { ...inputs } = {}, outputs: { ...outputs } = {} } = driverInterfaces if (!isPlainObject(inputs)) { throw (new TypeError('"inputs" of interfaces is expected to be type of "PlainObject".')) } if (!isPlainObject(outputs)) { throw (new TypeError('"outputs" of interfaces is expected to be type of "PlainObject".')) } Object.entries(driverInterfaces).forEach(([key, value]) => { if (key !== 'inputs' && key !== 'outputs') { inputs[key] = value } }) return { inputs: { ...inputs }, outputs: { ...outputs } } } interface IConnectDriverInterfaces { (up: AtomLike, down: any): void (up: any, down: AtomLike): void (up: Record, down: Record): void (up: any[], down: any[]): void } export const connectDriverInterfaces: IConnectDriverInterfaces = (up: any, down: any): void => { interface INormalize { (value: Data): ReplayDataMediator >>(value: D): D (value: Mutation): ReplayMutationMediator >(value: M): M (value: D): Data } const normalize: INormalize = (value: any): any => isAtomLike(value) ? replayWithLatest(1, value as any) : replayWithLatest(1, Data.of(value)) // The up & down value are expected to be type of Atom, // -> one of the up | down value is required to be type of Atom at least. // -> cause the developer is no way to get the auto-generated down Atom, and it makes no sense. if (isAtomLike(up) && !isAtomLike(down)) { if (isArray(down)) { down.forEach(i => { if (isAtomLike(i)) { binaryTweenPipeAtom(normalize(up), i) } else { // do nothing } }) } else { // do nothing } } else if (isAtomLike(up) && isAtomLike(down)) { // downstream atom do not need to be replayable binaryTweenPipeAtom(normalize(up), down) } else if (!isAtomLike(up) && isAtomLike(down)) { binaryTweenPipeAtom(normalize(up), down) } else if (isPlainObject(up) && isPlainObject(down)) { Object.entries(up).forEach(([key, value]) => { if (!isNil(down[key])) { connectDriverInterfaces(value, down[key]) } }) } else if (isArray(up) && isArray(down)) { up.forEach((value, idx) => { if (!isNil(down[idx])) { connectDriverInterfaces(value, down[idx]) } }) } else { throw (new TypeError( 'The up interfaces & down interfaces are expected to be the type combinations as follows:' + ' (Atom, Any) | (Any, Atom) | (Object | Object) | (Array | Array).' )) } } type ConnectedInputsRecord = { [K in keyof Target]+?: Target[K] extends AtomLikeOfOutput ? V | AtomLikeOfOutput : never } type ConnectedInputs = { [K in keyof Target]+?: Target[K] extends AtomLikeOfOutput ? ( V | AtomLikeOfOutput ) : Target[K] extends AnyStringRecord | undefined ? ( ConnectedInputsRecord> ) : (Target[K] | AtomLikeOfOutput) } type ConnectedOutputs = { [K in keyof Target]+?: Target[K] extends AtomLikeOfOutput ? AtomLikeOfInput : never } type ConnectedInterfacesOf = { [K in keyof Instance]+?: K extends 'inputs' ? ConnectedInputs : K extends 'outputs' ? ConnectedOutputs : any } /** * @return A simple copy of the driver instance, plainobject has `inputs`, `outputs` or others. */ export const useGeneralDriver = ( driver: DriverMaker, driverOptions: Options | undefined, interfaces: ConnectedInterfacesOf ): Instance => { const driverInterfaces = driver(driverOptions) const { inputs: { ...innerInputs } = {}, outputs: { ...innerOutputs } = {}, ...others } = { ...driverInterfaces } // 只有当 interfaces 是对象且不为空的时候,才执行 connect 逻辑 if (isPlainObject(interfaces) && !isEmptyObject(interfaces)) { const { inputs: { ...outerInputs } = {}, outputs: { ...outerOutputs } = {} } = { ...formatDriverInterfaces(interfaces) } // outer inputs as upstream, send data to inner inputs // -> inner inputs as downstream, receive data from outer inputs connectDriverInterfaces(outerInputs, innerInputs) // inner outputs as upstream, send data to outer outputs // -> outer outputs as downstream, receive data from inner outputs connectDriverInterfaces(innerOutputs, outerOutputs) } // return a simple copy of driver instance const returnedDriver = { inputs: innerInputs, outputs: innerOutputs, ...others } as unknown as Instance return returnedDriver } export interface IPartialUseGeneralDriver_< Options extends DriverOptions = DriverOptions, Instance extends DriverInstance = DriverInstance > { (driverOptions: Options | undefined, interfaces: ConnectedInterfacesOf): Instance (driverOptions: Options | undefined): (interfaces: ConnectedInterfacesOf) => Instance } export interface IUseGeneralDriver_ { // pass all three arguments at one time ( driver: DriverMaker, driverOptions: Options | undefined, interfaces: ConnectedInterfacesOf ): Instance // pass first two arguments then rest one, first two at one time (driver: DriverMaker, driverOptions: Options | undefined): (interfaces: ConnectedInterfacesOf) => Instance // pass all three arguments one by one // or pass first argument then rest two, rest two at one time (driver: DriverMaker): IPartialUseGeneralDriver_ } /** * @see {@link useGeneralDriver} */ export const useGeneralDriver_: IUseGeneralDriver_ = looseCurryN(3, useGeneralDriver)