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

webpremios-app-hook

v0.1.0

Published

The WebPrêmios App Hook Package

Downloads

8

Readme

JSHook

It's a simple package to help create and control injected functionality into an javascript application.

Installation

npm install --save webpremios-app-hook

Usage

Actions

Actions are simple callbacks called in a convenient moment. A complete list of the default actions can be found here

import { Actions } from 'webpremios-app-hook';

// register a 'start' action with 'sample' namespace
// add add the callback function
Actions.start('sample')
    .do(()=>{
        // Do whatever you need to do on startup
    });

Dispatching an action

import { Actions, HookManager } from 'webpremios-app-hook';

// register a 'start' action with 'sample' namespace
// add the callback function
Actions.start('sample')
    .do(()=>{
        // Do whatever you need to do on startup
        console.log('Start hook triggered');
    })

HookManager.trigger('start');
// Console: Start hook triggered

// Or just
Actions.start().dispach();

Filters

Filters are callbacks called when you need to filter the returned argument. A complete list of the default filters can be found here

import { Filters } from 'webpremios-app-hook';
import { SomeComponent } from '/path/to/component';

// register a 'listMenuBar' filter with 'sample' namespace
// add the callback function
Filters.listMenuBar('sample')
    .do( items => {
        let newItem = {
            name: 'Sample',
            icon: 'sample-icon',
            component: <SomeComponent />
        };
        // Filters should always returns the new subject
        return items.concat(newItem);
    })

Filtering

import { Filters, HookManager } from 'webpremios-app-hook';
import { SomeComponent } from '/path/to/component';

// register a 'listMenuBar' filter with 'sample' namespace
// add the callback function
Filters.listMenuBar('sample')
    .do( items => {
        let newItem = {
            name: 'Sample',
            icon: 'sample-icon',
            component: <SomeComponent />
        };
        // Filters should always returns the new subject
        return items.concat(newItem);
    });

let items = [
    {
        name: 'Default Item',
        icon: 'default-icon',
        component: <DefaultComponent />
    }
];

items = HookManager.applyFilter('listMenuBar', items);
// Now items contains the initial value
// plus what was add in the filter hook

// Or just
Filters.listMenuBar().apply(items);

Custom hooks

Actions

Adding your own custom action.

import { Actions, HookManager } from 'webpremios-app-hook';

HookManager.on(
    'custom', 		// register a action with 'custom' GroupName,
    'sample', 		// 'sample' as namespace - Not Required
    (message) => { 	// add the callback function
        console.log(`Executes customAction here - ${message}`);
    } 
)
// Then you can call it as you would normaly do
HookManager.trigger('customAction', 'Hell Yeah!');
// Console: Executes customAction here - Hell Yeah!


// Or just use actions' create method
Actions.create('custom');
// Then use it as a default action
Actions.custom('sample').do( message =>{
    console.log(`Executes customAction here - ${message}`);
});

Filters

Adding your own custom filter.

import { Filters, HookManager } from 'webpremios-app-hook';

HookManager.filter(
    'custom', 		// register a action with 'custom' GroupName,
    'sample', 		// 'sample' as namespace - Not Required
    (message) => {	// add the filter function
        return message + " a Custom Filter";
    }
)


let message = "This string was changed by";

// Then you can call it as you would normaly do
message = HookManager.applyFilter('custom', message);

console.log(message);
// Console: This string was changed by a Custom Filter


// Or just use filters' create method
Filters.create('custom');
// Then use it as a default filter
Filters.custom('sample').do( message => {
    return message + " a Custom Filter";
});

Destroy

Removing actions and filters.

import { Filters, Actions, HookManager } from 'webpremios-app-hook';


// You can destroy it using the constructor helper
Action.start().destroy();
Filters.listMenuBar().destroy();

// Passing a namespace will only destroy those with passed namespace
Action.start().destroy('sample');
Filters.listMenuBar().destroy('sample');

// Using HookManager
HookManager.destroy('start');
HookManager.destroy('listMenuBar');

// You can pass a namespace to filter it too
HookManager.destroy('start', 'sample');
HookManager.destroy('listMenuBar', 'sample');

Important

DO NOT mutate the original variable directly when filtering. You should always return a new variable with the changes you want to apply.