@_agco/understated

simple state management and caching library

Usage no npm install needed!

<script type="module">
  import AgcoUnderstated from 'https://cdn.skypack.dev/@_agco/understated';
</script>

README

understated.js

A Predictable Promise based data store, caching and state management library for web and native platforms.

Usage

The library exposes simple APIs for data manipulation and management:


get(key: string OR array)

  • This method provides read-access to the state container.
  • Use it to get a snapshot of the whole state object or specific state properties by passing the key as a parameter.
  • The key parameter can either be a string or an array of keys.
  • It returns a promise, and the successful response is a snapshot of the requested data.
// Get a snapshot of the whole state container
state.get().then(state => {
  // statements
});
// Get a snapshot of a specific
// property from the state container
state.get("todos").then(todos => {
  // statements
});
// Get a snapshot of multiple
// properties from the state container
state.get(["todo1", "todo2", "todoN"]).then(todos => {
  // statements
});

update(key: string OR array: required, payload: any)

  • This method provides write-access to the state container.
  • Use it to write data in the state container.
  • The key parameter can either be a string or an array of objects ({ key, payload }) where key is required.
  • If an already existing property is passed as the key, then it gets overwritten with the new value i.e. the payload.
  • It returns a promise, and the successful response is a snapshot of the updated state.
// Add a property to the state container
state.update("amount", 10000).then(newstate => {
  // statements
});
// Add multiple properties to the state container
state
  .update([
    { key: "todo1", payload: "buy grocery" },
    { key: "todo2", payload: "dinner with mom" }
  ])
  .then(newstate => {
    // statements
  });

remove(key: string OR regex: required)

  • This method provides write-access to the state container.
  • Use it to remove one or more properties by passing the key as a parameter.
  • It returns a promise, and the successful response is a snapshot of the updated state.
// Remove a specific property
// from the state conatainer
state.remove("todo").then(newstate => {
  // statements
});

// Remove multiple properties
// from the state conatainer using regex
state.remove(/todo_i/).then(newstate => {
  // statements
});

reset()

  • This method provides write-access to the state container.
  • Use it to empty or reset the state container.
// Empty the state container
state.reset(); // returns state = {}

has(key: string: required)

  • This method provides read-access to the state container.
  • Use it to check whether a property exists in the state container.
  • It returns the value of the property if it exists, otherwise, returns false.
// Check whether a specific property
// exists in the state container
state.has("todo"); // returns {...} or false

subscribe(callback: function: required, shouldExecuteImmediately: bool)

  • This method registers a callback which will be exectued whenever the state is mutated.
  • Use this method to subscribe to state mutations.
  • shouldExecuteImmediately, if passed as true, then the callback will be immediately executed when subscribe is called.
// assign a method which will be
// called everytime the state is mutated.
state.subscribe(render);

history()

  • This method returns the history of all the state mutations since initialization.
// return the history of state mutations
state.history();