npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kshook

v1.0.17

Published

Ksike Hook library for easy implementation of Event Driven Design

Downloads

41

Readme

KsHook: Dynamic Event Handling Library

Ksike Hook library for easy implementation of Event Driven Design. The Hooks pattern is a design pattern that facilitates event-driven programming within a software architecture. It allows you to define events and configure them with subscribers and notifiers. Subscribers listen to specific events and define which notifier should handle them when the event is triggered. This pattern enhances modularity and extensibility in event-driven systems.

This library belong to the Ksike ecosystem:

  • KsMf - Microframework (WEB, REST API, CLI, Proxy, etc)
  • Ksdp - Design Patterns Library (GoF, GRASP, IoC, DI, etc)
  • KsCryp - Cryptographic Library (RSA, JWT, x509, HEX, Base64, Hash, etc)
  • KsHook - Event Driven Library
  • KsEval - Expression Evaluator Library
  • KsWC - Web API deployment Library

For further information, check the following topics:

Quick overview

The library comprises four key components: Hooks, Subscriber, Notifier, and Processor. These components work in tandem to provide a modular and customizable solution for managing events and executing actions based on event triggers.

Hooks

  • Represents the core interface responsible for dynamically loading instances of controller classes for Subscribers, Notifiers, and Processors.
  • Implements patterns such as service locator, inversion of control, strategy, dependency injection, and observers.

Subscriber

  • Manages all data associated with events, defining the type of database to use and the required fields for each subscriber record.
  • Interacts with different data storage sources (memory, databases, files, MySQL, MongoDB, Redis, etc.).
  • Defines the data type and handles interactions with various data storage solutions.

Notifier:

  • Implements actions based on parameters received, including the event name, subscriber content, and specified parameters at the time of event triggering.
  • Executes actions based on event data and subscriber information.

Processor:

  • Optional class defining how a logical expression is evaluated to determine whether the specified Notifier should be executed.
  • Evaluates logical expressions and determines whether to proceed with Notifier execution.
  • Provides flexibility to conditionally execute actions based on configurable logic.

Library Workflow:

  • Initialize an instance of the KsHook, configuring the supported Subscriber, Notifier, and Processor implementations.
  • Configure the list of observers for a specific event, managed by the Subscriber. Each observer record includes the names of the Notifier and Processor to use.
  • Trigger an event through the trigger function, specifying the event name, parameterized data, and the Subscriber or list of Subscribers from which the KsHook obtains the list of subscribers for the triggered event.

Usage Example:

Install the library:

npm install kshook

Load your custom notifier, subscriber, and processors:

const locator = require('./notifier.locator');
const { models, driver, manager } = require('./db');

Register subscribers for each event in the data model

--------------------------------------------------------------------
| id | event  | notifier | value    | processor | expression       |
--------------------------------------------------------------------
| 1  | logout | locator  | newrelic | null      | null             |
| 2  | login  | locator  | alert    | native    | failure EQUAL 11 |
--------------------------------------------------------------------

Configure and run

const KsHook = require('kshook');

// Initialize KsHook instance
const hook = KsHook.get();

// Configure supported components
hook.notifier.set({
    name: "locator",
    target: locator
});
hook.subscriber.get("Model").configure({
    models,
    driver,
    manager
});

// Trigger the 'login' event
const res = hook.trigger({
    subscriber: ["Model"],
    event: "login",
    data: { failure: 10, flow: "6868465468415", step: "STEP-1" }
});

Explore the potential of dynamic event handling with KsHook, a library designed to empower developers with flexibility, extensibility, and seamless event-driven architecture.