0.1.2 • Published 2 years ago

@capitec/observable-store v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Store

Base class used to create a contextual observable store.

Core concepts and capabilities include:

  • Be default store state is immutable (recommended).
  • Consumers can subscribe to store state changes via exposed RxJS Observables.
  • Store state change history can be captured and accessed (in memory only).
  • Store state changes can be logged to console.
import { Store } from '@capitec/observable-store';
class ClientStore extends Store {
  constructor() {
    super({
      name: 'client-store',
      persistence: `sessionStorage`
    });
  }
}

export default new ClientStore();

Instance Members

stateHistory

Array

stateChanged

Observable

// Subscribe
const clientStoreSub = ClientStore.stateChanged.subscribe(state => {
  this.clients = state && state.clients ? state.clients : null;
});

// Unsubscribe
clientStoreSub.unsubscribe();

stateChangedWithName

Observable

// Subscribe
const clientStoreSub = ClientStore.stateChangedWithName.subscribe(changes => {
  this.clients = changes.state && changes.state.clients ? changes.state.clients : null;
  const stateAction = changes.name;
});

// Unsubscribe
clientStoreSub.unsubscribe();

stateChangedNoPayload

Observable

// Subscribe
const clientStoreSub = ClientStore.stateChangedNoPayload.subscribe(() => {
  console.log(`State Changed`)
});

// Unsubscribe
clientStoreSub.unsubscribe();

stateChangedNoPayloadWithName

Observable

// Subscribe
const clientStoreSub = ClientStore.stateChangedNoPayloadWithName.subscribe(name => {
  console.log(`State Changed: ${name}`);
});

// Unsubscribe
clientStoreSub.unsubscribe();

stateChangedProperties

Observable

// Subscribe
const clientStorePropertiesSub = ClientStore.stateChangedProperties.subscribe(changes => {
  this.clients = changes.state && changes.state.clients ? changes.state.clients : null;
});

// Unsubscribe
clientStorePropertiesSub.unsubscribe();

stateChangedPropertiesWithName

Observable

// Subscribe
const clientStorePropertiesSub = ClientStore.stateChangedPropertiesWithName.subscribe(changes => {
  this.clients = changes.state && changes.state.clients ? changes.state.clients : null;
  const stateAction = changes.name;
});

// Unsubscribe
clientStorePropertiesSub.unsubscribe();

getStateChangedProperties

Instance Functions

getState

deepCloneReturnedState {boolean} - When true, returns a cloned copy of the store state (recommended).

NOTE: When false and the settings had "persistence: 'memory'", a reference to the store state will be returned and it's up to the consumer to ensure the state isn't changed from the outside.

{Object} - Store state to return.

getStateProperty

propertyName {String} - Name of the property to return from the store state.

deepCloneReturnedState {Boolean} - When true, returns a cloned copy of the store state (recommended).

NOTE: When false and the settings had "persistence: 'memory'", a reference to the store state will be returned and it's up to the consumer to ensure the state isn't changed from the outside.

{Object} - Store state to return.

setState

NOTE: State to set MUST be serializable to and from JSON for immutability support and / or supported persistence mechanisms.

state {Object|function} - State to set, can be an object or a function that accepts latest state as input parameter.

action {String} - Descriptive name for state action, e.g. "CLIENT_ADD".

dispatchState {Boolean} - When true, notifies subscribers of state changes.

deepCloneState {Boolean} - When true, clones latest state before performing state update.

{Object} - Latest store state.

clearState

dispatchState {boolean} - When true, notifies subscribers of state changes.

{void} -

resetState

state {Object} - State to set.

dispatchState {boolean} - When true, notifies subscribers of state changes.

{void} -

clearStateHistory

{void} -

-----------------------------------------------------

StoreStateSettings

Settings for Store constructor.

Instance Members

persistence

trackStateHistory

NOTE this will perpetually grow memory use within the current session and usage thereof should be carefully considered, if needed at all.

logStateChanges

-----------------------------------------------------