8000 GitHub - dafengzhen/evflow: EvFlow is a simple event management library that supports dependency management, lifecycle hooks, middleware, and event publishing/subscription. It's designed for use cases involving general business flow control.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

EvFlow is a simple event management library that supports dependency management, lifecycle hooks, middleware, and event publishing/subscription. It's designed for use cases involving general business flow control.

License

Notifications You must be signed in to change notification settings

dafengzhen/evflow

Repository files navigation

EvFlow

GitHub License PRs Welcome

EvFlow is a simple event management library that supports dependency management, lifecycle hooks, middleware, and event publishing/subscription. It's designed for use cases involving general business flow control.

简体中文

Installation

npm install evflow

Quick Example

Browser Compatibility

EvFlow provides a legacy build for compatibility with older browsers.

If your project needs to support older versions of browsers, please import it as follows:

import {Dispatcher} from 'evflow/legacy';

Basic Usage

import {Dispatcher} from 'evflow';

const hub = new Dispatcher();

// Register dependencies
hub.add('payment_processed');
hub.add('inventory_check');

// Register main event (dependencies already exist)
hub.add('order_created', ['payment_processed', 'inventory_check']);

// Register handlers for all dependencies
hub.handle('payment_processed', async () => {
  console.log('Processing payment...');
  return {success: true};
});

hub.handle('inventory_check', async () => {
  console.log('Checking inventory...');
  return {stock: 100};
});

hub.handle('order_created', async (_, paymentResult, inventoryResult) => {
  console.log('Order created!', paymentResult, inventoryResult);
});

// Dispatch event
await hub.run('order_created');

// Console logs
/*
Processing payment...
Checking inventory...
Order created! { success: true } { stock: 100 }
*/

Core API

Methods

Method Description
add(event, deps?, tags?) Registers an event.
handle(eventId, handler) Registers an event handler.
run(eventId, options?) Dispatches a single event.
runAll(eventIds?, mode?, options?) Dispatches multiple events.
use(middleware) Adds middleware to the event flow.
subscribe(eventId, callback) Subscribes to state changes for a given event.
unsubscribe(eventId, callback) Unsubscribes from an event's state changes.
onLifecycle(phase, hook) Registers a global lifecycle hook.
onEvent(eventId, phase, hook) Registers a lifecycle hook for a specific event.
clear() Clears all registered events, handlers, and state.

Contributing

Contributions are welcome! Feel free to submit issues or pull requests.

License

MIT

About

EvFlow is a simple event management library that supports dependency management, lifecycle hooks, middleware, and event publishing/subscription. It's designed for use cases involving general business flow control.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0