# flex-event-store

> An event-based global state management tool for vue, react, mini-program, ect.

Latest version **1.1.6** (published 2022-02-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install flex-event-store
pnpm add flex-event-store
yarn add flex-event-store
bun add flex-event-store
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.6 |
| Published | 2022-02-12 |
| First published | 2022-01-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Shuanglin Deng |
| Maintainers | shuanglindeng |
| Keywords | state management, mini-program, vue, react |

## Links

- npm: https://www.npmjs.com/package/flex-event-store
- Repository: https://github.com/DengShuanglin/flex-event-store
- Homepage: https://github.com/DengShuanglin/flex-event-store#readme
- Issues: https://github.com/DengShuanglin/flex-event-store/issues
- npm.io page: https://npm.io/package/flex-event-store

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.6 (latest) — 2022-02-12
- 1.1.5 — 2022-02-12
- 1.1.4 — 2022-02-12
- 1.1.3 — 2022-02-10
- 1.1.2 — 2022-02-10
- 1.1.1 — 2022-02-10
- 1.1.0 — 2022-02-10
- 1.0.6 — 2022-01-05
- 1.0.5 — 2022-01-05
- 1.0.4 — 2022-01-05
- 1.0.3 — 2022-01-04
- 1.0.2 — 2022-01-04
- 1.0.1 — 2022-01-04
- 1.0.0 — 2022-01-03

## README

# flex-event-store

<!-- 感谢coderwhy老师的细致讲解 -->

An event-based global state management tool for vue, react, mini-program, etc.

一个基于事件的全局状态管理工具，可以在Vue、React、小程序等任何地方使用。





# 设计灵感

在项目中找到一个更加方便快捷的数据共享方案：

* 后续会完善文档和增加更多好用功能；
* 欢迎star、issue、pull requests，会进行更多改变；



# 如何使用呢？

## 1、npm安装依赖

```shell
npm install flex-event-store
```



## 2、事件总线（event-bus）

```js
const { FlexEventBus } = require('flex-event-store')

const eventBus = new FlexEventBus();

const eventCallback1 = (...payload) => {
  console.log("eventCallback1:", payload);
};

const eventCallback2 = (...payload) => {
  console.log("eventCallback2:", payload);
};

const sampleCallback1 = (...payload) => {
  console.log("sampleCallback1:", payload);
};

eventBus.on("event", eventCallback1);
eventBus.on("event", eventCallback2);
eventBus.on("sample", sampleCallback1);
eventBus.once("event", (...payload) => {
  console.log("event once:", payload);
});

setTimeout(() => {
  eventBus.emit("event", "abc", "cba", "nba");
  eventBus.emit("sample", "abc", "cba", "nba");
}, 1000);

setTimeout(() => {
  eventBus.off("event", eventCallback1);
  eventBus.off("sample", sampleCallback1);
}, 2000);

setTimeout(() => {
  eventBus.emit("event");
  eventBus.emit("sample");
}, 3000);

```





## 3、数据共享（event-store）

```js
const { FlexEventStore } = require("flex-event-store")
const axios = require('axios')

const eventStore = new FlexEventStore({
  state: {
    name: "test",
    friends: ["abc", "cba", "nba"],
    banners: [],
    recommends: [],
  },
  actions: {
    getData(ctx) {
      console.log("ctx", ctx);
      // test code
      // axios.get("url").then((res) => {
      //   const banner = res.data.banner;
      //   const recommend = res.data.recommend;
      //
      //   ctx.banners = banner;
      //   ctx.recommends = recommend;
      // });
    },
  },
});

// 数据监听
eventStore.onState("name", (value) => {
  console.log("监听name:", value);
});

eventStore.onState("friends", (value) => {
  console.log("监听friends:", value);
});

eventStore.onState("banners", (value) => {
  console.log("监听banners:", value);
});

eventStore.onState("recommends", (value) => {
  console.log("监听recommends", value);
});

// 数据变化
setTimeout(() => {
  eventStore.setState("name", "temp");
  eventStore.setState("friends", ["kobe", "james"]);
}, 1000);

eventStore.dispatch("getData");
```

---
_Source: https://npm.io/package/flex-event-store · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
