/// import { Environment } from './environment'; import Message, { MessageConstructor } from './message'; /** * A global symbol that allows users to opt into what is currently the default delegate behavior * i.e when a delegate is unspecified, the container is hoisted into it's parent state */ export declare const PARENT: unique symbol; export declare type Empty = false | null | undefined; export declare type MessageOrEmpty = Message | Empty; export declare type GenericObject = { [key: string]: any; }; export declare type UpdateResult = M | ((model: M) => M) | [M, ...MessageOrEmpty[]] | [M, MessageOrEmpty[]]; export declare type Updater = (model: M, message?: GenericObject, relay?: GenericObject) => UpdateResult; export declare type UpdaterDef = (model: M, message: GenericObject, relay: GenericObject) => UpdateResult; export declare type DelegateDef = symbol | string; export declare type UpdateMapDef = [MessageConstructor, UpdaterDef][]; export declare type UpdateMap = Map>; export declare type ContainerDefPartial = { update?: UpdateMapDef; name?: string; }; export declare type ContainerDefMapped = { update: UpdateMap; name: string; }; export declare type ContainerPartial = { delegate?: DelegateDef; init?: (model: M, relay: GenericObject) => UpdateResult; relay?: { [key: string]: (model: M, relay: GenericObject) => M & GenericObject; }; view?: ContainerViewDef; attach?: { store: GenericObject; key?: string; }; subscriptions?: (model: M, relay: GenericObject) => any | any[]; }; export declare type ContainerDef = ContainerDefPartial & ContainerPartial; export declare type Emitter = (msg: MessageConstructor | [MessageConstructor, GenericObject]) => any; export declare type ContainerViewProps = M & { emit: Emitter; relay: GenericObject; }; export declare type ContainerViewDef = (props: ContainerViewProps) => any; export declare type ContainerView = (props?: ContainerViewProps) => any; export declare type Container = ContainerView & ContainerPartial & ContainerDefMapped & { accepts: (m: MessageConstructor) => boolean; identity: () => ContainerDef; }; export declare type IsolatedContainer = Container & { dispatch: any; state: () => M; push: (state: M) => void; }; /** * Creates a container bound to an execution environment * * @param {Object} env The environment * @param {Object} container The container definition * @return {Component} Returns a renderable React component */ export declare const withEnvironment: import("Function/Curry").Curry<((env: Environment, def: ContainerDef) => Container)>; /** * Creates a new container. * * @param {Object} An object with the following functions: * - `init`: Returns an update result, representing the initial value of the container's model. * See "Update Results" below for details. * - `update`: A function that accepts the current model as an object, * and returns a `Map` pairing message types to handler functions. * Each handler function accepts the message's data as a parameter, * returns an update result. See "Update Results" below for details. * - `view`: A pure component (i.e. a function that returns React DOM) — as part of * its props, the component will receive an `emit` function, which can be called * with a user-defined message class. * - `attach`: Optional. If attaching to a Redux store, pass `store` and (optionally) `key`. The * container will then subscribe to the store and propagate changes. Remember to call `reducer()` * on the container and pass the resulting value to `combineReducers()` to complete the loop. * * ### Update Results * * An update result is a value returned from dispatching a message. Update results are used * to update the containers's model, as well as return _command messages_, which represent * side-effects that the container can perform, such as an HTTP request. * * Update results can be one of the following: * * - An object: to update the model without performing any actions — at a minimum, the * current model must always be returned * - An array of `[model, message]`: to update the model _and_ perform an action, return an array * with the model first, and a message object second, * i.e. `[model, new Alert({ message: "Hello world!" })]` * - An array of `[model, [message]]`: to perform multiple actions, simply return an array of * command messages * * @return {Object} returns an object with the following methods: * * - `dispatch`: Accepts a message object to update the container's model * - `state`: Returns the current model * - `view`: A React wrapper component that can be rendered or embedded * in another component * - `reducer`: Returns a Redux-compatible reducer function. Optionally accepts a hash * pairing action types to message types, to enable the container to respond to Redux actions, * which will be mapped to messages. * - `push`: Accepts a new model value to update the container. * - `subscribe`: Accepts a callback which receives a copy of the model when it is updated. * - `identity`: Returns an object containing the original values that created this container. * - `accepts`: Accepts a message class and returns a boolean indicating whether the container * accepts messages of that type. */ export declare const container: (def: ContainerDef) => Container; /** * Returns a copy of a container, disconnected from its effects / command dispatcher. * Calling `dispatch()` on the container will simply return any commands issued. */ export declare const isolate: (ctr: Container, opts?: any) => IsolatedContainer; /** * Helper function for sequencing multiple updaters together using left-to-right composition. * Each subsequent updater will receive the model returned by the preceding updater, and command messages * returned will be aggregated across all updaters. If any updater returns a function, that function * will be treated as an updater. */ export declare function seq(...updaters: Updater[]): (model: M, msg?: GenericObject, relay?: GenericObject) => UpdateResult; export declare type ModelMapperFn = (model: M, message?: GenericObject, relay?: GenericObject) => M; export declare type ModelMapper = ModelMapperFn | { [key: string]: ModelMapperFn; }; /** * Accepts a mapper that transforms a model. The mapper can be an updater, or an object that pairs * keys to updater-signature functions that return a value. The returned values are then paired to the * mapper's keys and merged into the model. */ export declare const mapModel: (mapper: ModelMapper) => (model: M, message?: GenericObject | undefined, relay?: GenericObject | undefined) => UpdateResult; export declare const relay: (fn?: ((r: any) => UpdateResult) | undefined) => () => any; export declare const message: (fn?: ((m: any) => UpdateResult) | undefined) => () => any; export declare const union: (fn?: ((u: { model: M; message?: GenericObject | undefined; relay?: GenericObject | undefined; }) => UpdateResult) | undefined) => (model: M, message?: {}, relay?: {}) => any; /** * Helper function for updaters that only issue commands. Pass in alternating command constructors and * command data, i.e.: * * ``` * [FooMessage, commands(LocalStorage.Write, { key: 'foo' }, LocalStorage.Delete, { key: 'bar' })] * ``` * * Command data arguments can also be functions that return data. These functions have the same type * signature as updaters: * * ``` * [FooMessage, commands(Http.Post, (model, msg) => ({ url: '/foo', data: [model.someData, msg.otherData] }))]] * ``` * * @deprecated */ export declare type CommandParam = MessageConstructor | Empty | GenericObject | UpdateCommandMapper; export declare type UpdateCommandMapper = (model: M, message: GenericObject, relay: GenericObject) => GenericObject; export declare const commands: (...args: CommandParam[]) => Updater;