Skip to main content

Model

Creating a single information unit. Use this method to have synchronized updates for all the subscribers.

Model type is not chaning the behaviour

Even when providing object structure, model will always update all the subscribers. For individual subscription use storm.

createModel

Model value types

Model can accept any JS type.

Creating models with object values

Using an object value will result in updates on each key change. In other words the object's individual keys can’t be consumed by that model.

Model internal structure

Model itself internally keeps track of its lifecycle.

Model has its own list of subscribers. Whenever you are using the subscribe method it will be checked whether it is already registered or not and if not, it will register(exactly push) the provided function to the models internal subscribers list. The subscribe function will return a function. You can call that function to unsubscribe from the model.

Model keeps the options list provided at the creation time. In order to update model's options you can use the setOptions method.

import { createModel } from 'event-storm';

const userModel = createModel({});

Methods

MethodTypeDescription
createModel<T>(value?: T, option?: IModelOptions) => IModel<T>Calling this method will return a model. The first argument is the default state of the model. The second argument is the model default option.

IModel

export interface IModel<T, G extends IModelOption = IModelOption> {
getState: () => T;
setOptions: (options: G) => void;
dispatch: (value: T, options?: IModelOption) => void | Promise<void>;
subscribe: (callback: (nextValue: T) => void, options?: ISubscriptionOptions<T>) => () => void;
}
MethodTypeDescription
getState() => anyThe method returns the fresh state of the model
setOptions(option: IModelOptions) => voidThe model option can be changed at any time
dispatch(value: any, option?: IModelOptions) => void) => void | Promise<void>The method will update the internal state of the model. Depending on the option the state change will/or will not update all the subscribers. Note: its possible to await for the update.
subscribe(callback: (nextValue: any, options?: IModelOptions) => void, option?: ISubscriptionOptions<T>) => () => voidThe method receive a callback. On each model update the receive the callback will be fired with the last updated value. When firing, the callback will be provided with second argument(if exists). The second argument is the same option with triggered the model state change(i.e. dispatch configurations).

IModelOptions

interface IModelConfiguration {
fireDuplicates?: boolean;
[key: string]: any;
}
PropertyTypeRequiredDescription
fireDuplicatesbooleanWhen set to true the model will propagate on duplicate changes. It's NOT recommended to use and rely on this option. Most likely if the code depends on the duplicated event it needs to be refactored. Defaults to false
[key: string]anyYou can provide your own options

ISubscriptionOption

interface ISubscriptionOptions {
needPrevious?: boolean;
}
PropertyTypeRequiredDescription
needPreviousbooleanWhen set to true the model will make the subscription callback run immediately. The subscription callback will receive the last state of the model. Defaults to false