Skip to main content

Storm

In general, in event sourcing technology there is no place to have a centralized single information segment. In contrast to having this, the composition and deriving the real information is suggested. The Event Storm library suggests for this purpose usage of virtual models. On the other hand, it's straightforward, that at scale this will result in a lot of boilerplate code. That’s why the library is suggesting a store concept which has the full power of decentralized data store.

What does this mean? The library will organize subscrition and data store updates in such a manner to keep track of individual subscription and individual updates. In other words, The Event Storm claims to update only for the given subscription.

createStorm

The Event Storm library is providing a createStorm method. The function supports a single argument, the default state.

import { createStorm } from 'event-storm';

const store = createStorm({
taxes: 20,
grossSalary: 100_000,
});

console.log(store.getState()) // { taxes: 20, grossSalary: 100_000 }

Methods

MethodTypeDescription
createStorm<T extends AnyObject>(data: T): [IStorm](#istorm<T>Pass the store initial state to the createStorm. The return will be storm.

IStorm

interface IStorm<T> {
getState: () => IStormState<T>;
subscribe: (callback: IStormSubscription<T>) => () => void;
addMiddleware: (middleware: IStormMiddleware) => () => void;
dispatch: (segments: Partial<T> | ((params: IStormState<T>) => Partial<T>), options?: AnyObject) => void;
}
MethodTypeDescription
getState() => IStormState<T>The method will return the actual state of the storm
subscribe(callback: IStormSubscription<T>) => () => voidThe method will receive a subscription function. The return value of this method can be called to unsubcribe
dispatch(segments: Partial<T>((params: IStormState<T>) => Partial<T>), options?: AnyObject) => void
addMiddleware(middleware: IStormMiddleware) => () => voidProvide a middleware to the storm. The middleware will be called before any update to the storm.

IStormSubscription

type IStormSubscription<T, G = any> = (state: IStormState<T>, subscribe: (state: G) => G) => void

To subcribe to a particular section use the second argument of the sucbcription function. E.g. subcribe(state.desired). Pass any part of the IStormState to the second argument and you'll subscribe only for those changes.

IStormMiddleware

type IStormMiddleware<T> = (nextState: IStormState<T>, prevState: IStormState<T>, configs: AnyObject) => void

The middleware will be provided the next state, prev state and the corresponding dispatch configuration.

Note

The middleware will be called before the storm update.

IStormState

type IStormState<Type> = {
[Property in keyof Type]: Type[Property];
};

AnyObject

type AnyObject = Record<string, any>;