@telazer/event-helper
v1.0.6
Published
A library for managing custom events
Readme
Telazer - EventHelper
For more helpers and utilities, check out the Telazer NPM Page
A TypeScript utility library for creating and managing custom event handlers.
Installation
npm install @telazer/event-helperKey Features
- Create and manage custom events with type-safe data passing.
- Flexible event handling with callback, ID, and group-based subscriptions.
- Singleton pattern implementation for global event management using a static class.
- Efficient app-wide communication and state management.
- Granular control over event listener removal by callback, name, ID, or group.
- Precise subscriber targeting with
observekeys viatrigger. - Backward compatible with
dispatchwhile you migrate.
Usage
Import EventHelper:
import EventHelper from "@telazer/event-helper";Targeted Triggering with observe
// subscribe to only "gold" changes
EventHelper.on({
eventName: "ITEM_CHANGE",
observe: ["gold"],
callback: (data) => {
console.log("gold changed:", data);
},
});
// subscribe to only "diamond" changes
EventHelper.on({
eventName: "ITEM_CHANGE",
observe: ["diamond"],
callback: (data) => {
console.log("diamond changed:", data);
},
});
// emit only for "gold" observers
EventHelper.trigger({
eventName: "ITEM_CHANGE",
observe: ["gold"],
data: { amount: 5 },
});Notes:
- If a subscriber does not provide
observe, it will be called bytriggerfor compatibility. - If
triggeris called withoutobserve, it behaves likedispatchand calls all subscribers of that event.
Start Listening (ON)
EventHelper.on({
eventName: "state_change",
callback: () => {
console.log("state_change triggered");
},
});Passing Data with Types
interface IEventData {
foo: string;
}
EventHelper.trigger<IEventData>({
eventName: "state_change",
data: { foo: "bar" },
});
EventHelper.on<IEventData>({
eventName: "state_change",
callback: ({ foo }) => {
console.log("state_change triggered", foo);
},
});Using Callback as Matcher
const callbackFc = () => {
console.log("state_change from listener1");
};
EventHelper.on({
eventName: "state_change",
callback: callbackFc,
});
EventHelper.on({
eventName: "state_change",
callback: () => {
console.log("state_change from listener2");
},
});
// remove listener 1 by its callback reference
EventHelper.off({ eventName: "state_change", callback: callbackFc });
// listener 2 is still activeUsing ID as Matcher
Important:
- Using
idwill replace the previous listener with the sameeventNameandid.- When calling
off()withid, you must also provideeventName. If you calloff({ id: "someId" })withouteventName, all events will be removed.
EventHelper.on({
id: "listener1",
eventName: "state_change",
callback: () => console.log("from listener1"),
});
EventHelper.on({
id: "listener2",
eventName: "state_change",
callback: () => console.log("from listener2"),
});
// remove only listener1
EventHelper.off({ eventName: "state_change", id: "listener1" });Grouping
EventHelper.on({
group: "group1",
eventName: "state_change",
callback: () => console.log("group1 a"),
});
EventHelper.on({
group: "group1",
eventName: "state_change",
callback: () => console.log("group1 b"),
});
EventHelper.on({
group: "group2",
eventName: "state_change",
callback: () => console.log("group2 a"),
});
// stop all listeners in group1
EventHelper.off({ eventName: "state_change", group: "group1" });Stop All Listeners
// stop ALL listeners for ALL events
EventHelper.off();Deprecation Notice
dispatch(eventName, data?)is deprecated. Usetrigger({ eventName, data, observe? }).- The static event registry remains for backward compatibility. New code should use
triggerandobservefor precise targeting.
Migration Guide
Replace:
EventHelper.dispatch("ITEM_CHANGE", payload);With:
EventHelper.trigger({ eventName: "ITEM_CHANGE", data: payload });If you only want certain subscribers:
EventHelper.trigger({
eventName: "ITEM_CHANGE",
observe: ["gold", "diamond"],
data: payload,
});Subscribers can opt in:
EventHelper.on({
eventName: "ITEM_CHANGE",
observe: ["gold"],
callback: onGoldChange,
});If a subscriber omits observe, it will still be called by trigger for compatibility. To fully opt in to targeted calls, always provide observe.
Development
# Clone the repo
git clone https://github.com/Telazer/event-helper
# Install dependencies
npm install
# Start the watcher for development
npm run watch
# Build the library
npm run buildLicense
MIT License
Copyright (c) 2025 Telazer LLC.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
