1.0.5 • Published 2 years ago

@mathias_frost/simplestores v1.0.5

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

Simple Stores

Minimal framework-independent stores

Example:

Creating a store

import {LocalStore, SessionStore, Store} from '@mathias_frost/simplestores';

export interface SomeModel {
  someString: string;
  someNumber: number;
  someBool: boolean;
}

export const store = new Store<SomeModel>({someString: 'string', someNumber: 6, someBool: true});
export const sessionStore = new SessionStore<SomeModel>({someString: 'string', someNumber: 6, someBool: true}, 'some_key');
export const localStore = new LocalStore<SomeModel>({someString: 'string', someNumber: 6, someBool: true}, 'some_key');

Subscribing (React)

import React from 'react';
import {localStore, sessionStore, store} from './stores';
import type {Subscribe} from '@mathias_frost/simplestores';

export class Component extends React.Component<{}, {}> {

  /** Store event as to unsubscrube at unmount (not required) */
  store: Subscribe<SomeModel> | null = null;

  componentDidMount() {
    this.store = store.subscribe((value: SomeModel) => this.setState({store: value}));
  }

  componentWillUnmount() {
    store.unsubscribe(this.store);
  }
}

Unsubscribing is optional but recommended

Set value

import {store} from './stores';

store.value = "New value";

This will update subscribers

Get value

import {store} from './stores';

const value = store.value;

This will fetch the actual value possibly stored in session or local storage

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago