1.0.4 • Published 6 years ago

handlor v1.0.4

Weekly downloads
1
License
ISC
Repository
github
Last release
6 years ago

Handlor

Sir Handlor will assist you with handling events globally across your app and manage in one convenient place. Eliminate code duplication, make maintaining easier.

N.B. This package has TS types and thus is dependent on tslib.

npm i handlor
npm i --save-dev tslib

or

yarn add handlor
yarn add --dev tslib

Example of usage

import { Handlor } from "handlor"; 
/* Create instance and make it globally */
window.handlor = new Handlor();

Interval/Timeout

handlor.registerHandles({
    /**
     * Use Type enum or simply default listener names
     * e.g. interval, requestAnimationFrame, etc 
     */
    type: Type.Interval, // or Type.Timeout (interval/timeout)
     /**
     * Specify options such as timeout in ms for
     * intervals and timeouts or any other args for
     * the aforementioned listener types
     */
    callback: () => { /* whoosh */},
    options: {
        timeoutInMs: 1000,
    }
})

RequestAnimationFrame

// interval (same as timeout)
handlor.registerHandles({
    /**
     * Use Type enum or simply default listener names
     * e.g. interval, requestAnimationFrame, etc 
     */
    type: Type.RequestAnimationFrame,
     /**
     * Specify options such as timeout in ms for
     * intervals and timeouts or any other args for
     * the aforementioned listener types
     */
    callback: () => { /* whoosh */},
    options: {
        listenerNode: htmlElement, // defaults to window
    }
})

AddEventListener

// interval (same as timeout)
handlor.registerHandles({
    /**
     * Use Type enum or simply default listener names
     * e.g. interval, requestAnimationFrame, etc 
     * 
     * Optional for addEventListener. Defaults to this
     * type of listener
     */
    type: "click", // any event listener name
     /**
     * Specify options such as timeout in ms for
     * intervals and timeouts or any other args for
     * the aforementioned listener types
     */
    callback: () => { /* whoosh */},
    options: {
        listenerNode: htmlElement, // defaults to window
    }
})

Cleaning events

/**
 * addEvent returns id of the event which later can be used for clearing a specific item`
 */
window.handlor.cleanItem(id);

/**
 * Similarly clean several items at once
 */

window.handlor.cleanItems([id1, id2, id3]);

/**
* Clean all items.
* ⚠️ Careful as this will erase all listeners currently active in the app
*/
window.handlor.cleanAll();

Checking registered listeners

window.handlor.listeners // { {}, {} }

API

MethodDescriptionInput
registerHandlesList all new or modified filesSingle entry or an array of objects
cleanAllRemoves all listeners from the instance storageN/A
cleanItemRemoves a specific item from the storageid
cleanItemsRemoves an array of specific item from the storageid1, id2