coderwhy/hy-event-store
حول
An event-based global state management tool for vue, react, mini-program, ect.
تطوير الويب والتطبيقاتما يمكنك فعله
- Zero runtime dependencies
- Event listeners with explicit context, one-time listeners, and cleanup APIs
- Shared state with single-key and multi-key subscriptions
- Synchronous and asynchronous action results returned from dispatch
النمو المقاس للنجوم
- خلال 24 ساعة
- ٠
- خلال 7 أيام
- ٠
- خلال 15 يومًا
- ٠
- خلال شهر
- التحديث متأخر
تغيّر صافي النجوم خلال فترات متداخلة، وليس مجموعًا يوميًا. تبقى القياسات المفقودة فارغة. النجوم تقيس الاهتمام لا جودة البرمجيات.
README.md
الأصلhy-event-store
English · 简体中文
hy-event-store is a small, zero-dependency event bus and shallow
global state store for JavaScript applications. It is framework-agnostic and
works where CommonJS packages are supported, including typical Vue, React, and
Mini Program build workflows.
Highlights
- Zero runtime dependencies
- Event listeners with explicit context, one-time listeners, and cleanup APIs
- Shared state with single-key and multi-key subscriptions
- Synchronous and asynchronous action results returned from
dispatch - Regression tests and CI on Node.js 18, 20, 22, 24, and 26
Installation
npm install hy-event-store
The package exposes a CommonJS entry point and ships first-party TypeScript declarations:
const { HYEventBus, HYEventStore } = require("hy-event-store")
TypeScript users can add event and state types without changing the runtime API:
const bus = new HYEventBus<{ ready: []; signedIn: [user: { name: string }] }>()
bus.on("signedIn", user => console.log(user.name))
const store = new HYEventStore({
state: { count: 0 },
actions: {
increment(state, amount: number) {
state.count += amount
return state.count
}
}
})
Mini Program projects
The package currently uses CommonJS. In WeChat DevTools, enable Use npm modules, run Build npm, and then import the package by its package name:
const { HYEventStore } = require("hy-event-store")
Do not import src/event-store.js or a path such as store/hy-event-store.js.
Those paths refer to source files and are not the built Mini Program module
entry. If you use another bundler, configure it to resolve CommonJS packages
from node_modules and keep the same package-name import.
Quick start
Event bus
const { HYEventBus } = require("hy-event-store")
const bus = new HYEventBus()
function handleSignedIn(user) {
console.log(user.name)
}
bus.on("signed-in", handleSignedIn)
bus.emit("signed-in", { name: "Ada" })
bus.off("signed-in", handleSignedIn)
Use once for an event that should be handled once:
bus.once("ready", () => {
console.log("Runs once")
})
Pass a third argument when a callback needs a component or page as this:
const page = {
title: "Profile",
render(user) {
console.log(this.title, user.name)
}
}
bus.on("user-updated", page.render, page)
bus.emit("user-updated", { name: "Ada" })
bus.off("user-updated", page.render)
Shared state
const { HYEventStore } = require("hy-event-store")
const store = new HYEventStore({
state: {
count: 0,
status: "idle"
},
actions: {
increment(state, amount = 1) {
state.count += amount
return state.count
},
async load(state, request) {
state.status = "loading"
const result = await request()
state.status = "ready"
return result
}
}
})
function renderCount(count) {
console.log("count:", count)
}
store.onState("count", renderCount)
store.setState("count", 1)
store.offState("count", renderCount)
const count = store.dispatch("increment", 2)
async function loadData() {
return store.dispatch("load", () => fetch("/api/data").then(res => res.json()))
}
onState invokes its callback immediately with the current value,
then again when that key changes.
Read one declared value without subscribing to updates:
const status = store.getState("status")
getState does not notify listeners and throws for an undeclared key.
It returns the current value as-is; it is not a deep clone or deep read-only
snapshot.
Subscribe to several keys with onStates:
function renderProfile(change) {
console.log(change)
}
store.onStates(["count", "status"], renderProfile)
// Initial callback: { count: 0, status: "idle" }
// Later callback after setState("count", 1): { count: 1 }
store.offStates(["count", "status"], renderProfile)
API reference
HYEventBus
| Method | Description | Returns |
|---|---|---|
on(eventName, callback, thisArg?) |
Register a listener. | The event bus |
once(eventName, callback, thisArg?) |
Register a listener removed before its first callback runs. | The event bus |
emit(eventName, ...payload) |
Notify every listener for an event. | The event bus |
off(eventName, callback) |
Remove registrations for the callback passed to on or once. It is safe when the event has no listeners. |
The event bus |
clear() |
Remove listeners for every event. | The event bus |
hasEvent(eventName) |
Check whether an event has at least one listener. | Boolean |
Event names must be strings. Listener execution uses a stable snapshot, so a one-time listener removing itself does not skip the next listener.
HYEventStore
Create a store with a required state object and an optional
actions object. Every action must be a function and receives
state as its first argument.
| Method | Description |
|---|---|
onState(key, callback) |
Subscribe to one declared state key and immediately receive its current value. |
offState(key, callback) |
Remove a one-key subscription. |
onStates(keys, callback) |
Subscribe to several declared keys and immediately receive their initial snapshot. Later callbacks contain changed keys only. |
offStates(keys, callback) |
Remove a multi-key subscription. |
setState(key, value) |
Update a declared state key. |
getState(key) |
Read one declared state value without subscribing. |
dispatch(actionName, ...args) |
Run an action and return its value unchanged, including a Promise. |
Usage notes
- Declare every observable key in the initial
stateobject. Updating an unknown key throws instead of creating an unobservable value. - State observation is shallow. Replacing
state.profiletriggers its listener; mutatingstate.profile.namedirectly does not. - Assigning the same value with strict equality does not emit an update.
- Keep a stable callback reference and call
off,offState, oroffStateswhen a page or component is destroyed. This prevents duplicate listeners after component switches. - For Mini Program pages, pass the page as
thisArgwhen registering a method, and unregister the same method during the page cleanup lifecycle.
Compatibility
The distributed package is CommonJS and has no runtime dependencies. CI validates the package on Node.js 18 through 26. Browser and Mini Program projects should use their normal npm-package build or bundling workflow.
Native ESM exports, a read-only state selector, and an opt-in full snapshot
mode for onStates are planned design improvements; see the
roadmap.
Development
npm ci
npm test
npm run test:coverage
npm pack --dry-run
Contributing and security
- Read CONTRIBUTING.md before opening a pull request.
- Report vulnerabilities privately as described in SECURITY.md.
- Browse planned work in ROADMAP.md.
License
MIT © coderwhy
استكشف مشاريع مشابهة ومرتبطة
جارٍ التحميل…