Common Crate Patterns

Thera are some common crate patterns you can use:

State

const State = Allotize.Crate({
    route: "myState",
    state: {
      count: 0,
    },

    onChange: (oldState, newState) => {
      console.log(newState.count);
    }
});

Stream

A stream crate will not persist any data, this means that you can use this crate as a simple message channel that holds the last transmitted message.

const Stream = Allotize.Crate({
    route: "myStream",
    persist: false,
    state: {
      count: 0,
    },

    onChange: (oldState, newState) => {
      console.log(newState.count);
    }
});

Generator

Oftentimes you want to be able to dynamically create new routes:

let counter = (name) => Allotize.Crate({
    route: `counters/${name}`,

    state: {
        count: 0,
    },
})