Socket
Socket
Sign inDemoInstall

@zenstack/zen-store

Package Overview
Dependencies
0
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @zenstack/zen-store

A simple reactive state store


Version published
Maintainers
1
Install size
14.3 kB
Created

Readme

Source

Zen Store ☮️

A simple reactive state store

installation

yarn add @zenstack/zen-store

or

npm install @zenstack/zen-store

Usage

Create A State Store

import { createStateStore } from "@zenstack/zen-store";
const initialState = 0;
const stateStore = createStateStore(initialState);

//you can also use a class if you are object oriented
import { StateStore } from "@zenstack/zen-bus";
const initialState = 0;
const stateStore = new StateStore(initialState);

Retrieve State

The store offers an API, .getState() to retrieve the current state

const currentState = store.getState();

Update State

The store offers an API, .setState(:newState) to update the state

const newState = 5;
store.setState(newState);

Subscribe To State

The store offers an API, .subscribe(:handler) to subscribe to updates in the store. The .subscribe(:handler) returns an unsubscribe function that can be executed to detach the handler from the event.

const logState = (state) => console.log(state);

// .subscribe() returns an unsubscribe method
const unsubscribe = store.subscribe(logState);

// logState will be triggered
store.setState('some new state');

// logState will not be triggered after unsubscribing
unsubscribe();
store.setState('some other new state');

Keywords

FAQs

Last updated on 13 Feb 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc