Cmd is an asynchronous action that dispatch messages.
Unlike the original TEA, it only handles asynchronous processing. This is because TypeScript allows to occur side effects anywhere, so if the process is synchronous, you can execute it in the update function.
Cmd.none();
Returns Cmd that dispatches nothing.
Cmd.delay(action, timeout);
/**
* where
*
* <Msg>
* action = (dispatch: React.Dispatch<Msg>) => void
* timeout = number
*/
Returns Cmd that dispatches a message after a delay.
Cmd.promise(promiseAction);
/**
* where
*
* <Msg>
* promiseAction = (dispatch: React.Dispatch<Msg>) => Promise<void>
*/
Returns Cmd that dispatches a message asynchronously.
Cmd.perform(msgSupplier, task);
/**
* where
*
* <Msg, Value>
* msgSupplier = (value: Value) => Msg
* task = () => Promise<Value>
*/
Returns Cmd that dispatches a message supplied from msgSupplier
which receives value from task
.
If task
fails, the error will be ignored and nothing will be dispatched.
This is useful to separate side effects as task
.
Cmd.attempt(msgSupplier, task);
/**
* where
*
* <Msg, Value, Err>
* msgSupplier = (valueOrErr: Value | Err) => Msg
* task = () => Promise<Value>
*/
Works like perform
, but this one can handle errors.
Cmd.batch(...cmds);
/**
* where
*
* <Msg>
* cmds = Cmd<Msg>[]
*/
Returns array of Cmd.
Sub is a listener that subscribes external events or component's lifecycle.
It is practically the same as useEffect
.
Sub.none();
Returns Sub that subscribes nothing.
Sub.of(effector);
/**
* where
*
* <Model, Msg, Props>
* effector = (effectorProps) => Parameters<typeof useEffect>
* effectorProps = {
* model: Model;
* dispatch: React.Dispatch<Msg>;
* props: Props;
* }
*/
Returns Sub that subscribes events.
Sub.onMount(callback);
/**
* where
*
* <Model, Msg, Props>
* callback = (props) => void
* props = {
* model: Model;
* dispatch: React.Dispatch<Msg>;
* props: Props;
* }
*/
Returns Sub that subscribes events on mount.
Sub.onUnmount(callback);
/**
* where
*
* <Model, Msg, Props>
* callback = (props) => void
* props = {
* model: Model;
* dispatch: React.Dispatch<Msg>;
* props: Props;
* }
*/
Returns Sub that subscribes events on unmount.
Sub.batch(...subs);
/**
* where
*
* <Model, Msg, Props>
* subs = Sub<Model, Msg, Props>[]
*/
Returns array of Sub.
Tea({ init, update, subscriptions, useHooks, view, displayName });
/**
* where
*
* <Model, Msg, Props, HooksResult>
* init = (initProps) => [Model, Cmd<Msg>]
* initProps = {
* props: Props;
* }
* update = (updateProps) => [Model, Cmd<Msg>]
* updateProps = {
* model: Model;
* msg: Msg;
* props: Props;
* }
* subscriptions = Sub<Model, Msg, Props>
* useHooks? = (useHooksProps) => HooksResult
* useHooksProps = {
* model: Model;
* dispatch: Dispatch<Msg>;
* props: Props;
* }
* view = React.VFC<{
* model: Model;
* dispatch: Dispatch<Msg>;
* props: Props;
* hooksResult?: HooksResult;
* }>
* displayName? = string
*/
Returns React component.
displayName
is used in debugging messages. Default name is TeaComponent
.