New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

sute-store

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sute-store

一个基于事件总线的全局状态管理工具.

latest
npmnpm
Version
1.0.1
Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

一个基于事件总线的全局状态管理工具.

如何使用?

1、npm 安装依赖

npm install sute-store

2、事件总线(EventBus)

const { EventBus } = require("sute-store");

const eventBus = new EventBus();

const foo = (...args) => {
  console.log("执行了该函数", ...args);
};

eventBus.on("test", foo);
eventBus.emit("test", 1, 2, 3); // 执行了该函数 1 2 3

setTimeout(() => {
  eventBus.on("test", foo);
  eventBus.off("test", foo);
  eventBus.emit("test", 1, 2, 3); // 无输出
}, 2000);

eventBus.once(true, foo); // 执行了该函数 1 2 3
eventBus.emit("test", 1, 2, 3); // 无输出

3、数据共享(EventStore)

const { EventStore } = require("sute-store");

const eventStores = new EventStore({
  state: {
    name: "cxw",
    age: 25,
    habby: () => {
      console.log("123");
    },
  },
  actions: {
    setNameFn: (ctx) => {
      setTimeout(() => {
        ctx.name = "demo";
      }, 2000);
    },
  },
});

const foo = (value) => {
  console.log("value", value);
};

eventStores.onState("name", foo); // value cxw
eventStores.setState("name", 123); // value 123

eventStores.onStates(["name", "age"], foo); // value { name: 'cxw', age: 25 }
eventStores.setState("name", "123"); // value { name: '123'}
eventStores.setState("age", "321"); // value { age: '321' }

eventStores.onState("name", foo); // value cxw
eventStores.offState("name", foo);
eventStores.setState("name", "321"); // 无输出.

eventStores.onStates(["name", "age"], foo); // value { name: 'cxw', age: 25 }
eventStores.offState(["name", "age"], foo); // 无输出
eventStores.setState("name", "123"); // throw new Error("the state does not contain your key");
eventStores.setState("age", "321"); // throw new Error("the state does not contain your key");

eventStores.onState("name", foo); // value cxw
eventStores.disPatch("setNameFn"); // value demo

eventStores.disPatch("setNameFn");

参考来源: https://github.com/coderwhy/hy-event-store,

Keywords

typescript

FAQs

Package last updated on 01 Aug 2023

Did you know?

Socket

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