# API

# Event Management

This library maintains it's publish-subscribe implementation for brodcasting events that happen thoughout the Store lifecycle. The API for the PubSub manager is below:

# class PubSub()

Simple publish-subscribe manager for executing events on state changes.

# function subscribe(event, callback)

Subscribe to specific event.

Parameters

  • event (string) - Event name to subscribe to.
  • callback (function) - Function to call on event.

# function publish(event, payload)

Subscribe to specific event.

Parameters

  • event (string) - Event name to broadcast.
  • payload (object) - Arguments to pass to event callbacks.

# Store

This section details classes related to the global Store object and available methods.

# class Store(params)

Simple store helper for managing state in an application.

Parameters

  • params (object) - Object with state, mutations, and actions to manage.

# function register(params)

Register new constructs with the store.

Parameters

  • params (object) - State constructs to register with store.

# function reset(key)

Reset store back to base state.

Parameters

  • key (string) - State param to reset in store.

# function flush(publish)

Flush state changes from stage to store. This method is called after the end of an action or mutation to safely update the store on callable success.

Parameters

  • publish (boolean) - Whether or not to publish a `commit` event after this method is called.

# function rollback(publish)

Rollback staged changes and update stage with state. This method is called after the end of an action or mutation to safely rollback the store on callable success.

Parameters

  • publish (boolean) - Whether or not to publish a `rollback` event after this method is called.

# function subscribe(name, callback)

Proxy for subscribing to specific state changes

Parameters

  • name (string) - State parameter to subscribe to.
  • callback (object) - Callback to execute on state changes.

# function commit(name, payload)

Commit change to store using mutation.

Parameters

  • name (string) - Name of mutation to commit.
  • payload (object) - Arguments for mutation.

# function dispatch(name, payload)

Dispatch method for dispatching new actions managed by the store.

Parameters

  • name (string) - Name of action to dispatch.
  • payload (object) - Arguments for action.

# Status Manager

# class StatusManager(status, callback)

Class for managing state stack, allowing optional callback to be issued when base state is reached.

Parameters

  • status (string) - Base state for manager.
  • callback (function) - Callback to issue once base state is reached.

# member current

Get current status.

# member previous

Get current status.

# function reset()

Reset status manager back to idle.

# function push()

Push new status onto stack.

# function pop()

Pop status off state stack and issue callback if state manager has returned to resting.

# Observable

The observable object is currently undocumented, but provides a useful tool for managing data that needs to broadcast updates for deeply nested state changes. Here is a minimal example showing how the object can be used:

let track = [];
const obj = new Observable({
  foo: 'bar',
  bar: [1, 2],
  baz: { id: 1 },
}, () => { // base callback, execute every time a change is made
  track.push('base');
});
obj.subscribe('foo', () => {
  track.push('foo');
});
obj.bar.subscribe(() => {
  track.push('bar');
});
obj.bar.subscribe(0, () => {
  track.push('bar.0');
});
obj.baz.subscribe('id', () => {
  track.push('baz.id');
});

// test property subscriptions
track = [];
obj.foo = 'test';
//  track -> ['foo', 'base']

track = [];
obj.bar.push(1);
//  track -> ['base', 'bar']

track = [];
obj.bar[0] = 1;
//  track -> ['bar.0', 'base', 'bar']

track = [];
obj.baz.id = 2;
//  track -> ['baz.id', 'base']

# class Observable(target, callback)

Observable class for watching nested data changes and issuing before/after callbacks.

Parameters

  • target (object) - Data to create nested proxy for.
  • callback (function) - Callback to execute after global update/delete events.