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

hookify-object

v0.1.2

Published

Wrap an object with ES6 proxy to add hooks capabilities

Downloads

7

Readme

hookify-object

Wrap an object with ES6 proxy to add hooks capabilities through an event emitter.

npm version codecov

Installation

$ npm install --save hookify-object

Usage

Hookify-object provides severals hooks for an object :

  • Before/after call a method;
  • After resolve/reject a promise;
  • Before/after set a object property;
  • Before/after delete a property.

Example

Here is a basic usage to get the execution time of the object's methods :

const hookify = require('hookify-object')  
  
const obj = {  
  process () {  
    /* ... */  
  },  
}  
  
const objWithHooks = hookify(obj)  
  
objWithHooks.hooks.on('beforeCall', (context) => {  
  const { name } = context  
  console.time(name)  
})  
  
objWithHooks.hooks.on('afterCall', (context) => {  
  const { name } = context  
  console.timeEnd(name)  
})  
  
objWithHooks.process() 

API

hookify(target)

Add hook capabilities to a target object.

Parameters

| Name | Type | Default | Description |
| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |
| target | object | - | The object on which we want to add the hook capabilities |

Return value

| Type | Description |
|:---------------:|:---------------:|
| Proxy | The hook wrapper of the target object |

Example

const hookify = require('hookify-object')   
      
const obj = {}    
        
const objWithHooks = hookify(obj)  

proxy.hooks.on(hookName, handler)

Attach a handler for the hook named hookName.

| Name | Type | Default | Description |
| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |
| hookName | string | - | The hook's name |
| handler | Function | - | The handler function for the specified hook (see bellow) |

handler (context)

Check each hook to know the structure of the context object.

| Name | Type | Default | Description |
| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |
| context | object | - | Contains the context of the hook |

Hooks list

beforeCall[:methodName]

Called before calling a method. You can specify a unique method via the methodName option.

Example

objWithHooks.hooks.on('beforeCall', (context) => {  
  /* Call before the call of any method */  
})  
  
objWithHooks.hooks.on('beforeCall:test', (context) => {  
  /* Call before the call of the "test" method */  
})

context object

| Name | Type | Default | Description |
| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |
| self | object | - | The object wrapped by the hook proxy |
| name | string | - | The name of the called method |
| params | Array | - | The parameters of the called method |

afterCall[:methodName]

Called after calling a method. You can specify a unique method via the methodName option.

Example

objWithHooks.hooks.on('afterCall', (context) => {  
  /* Call after the call of any method */  
})  
  
objWithHooks.hooks.on('afterCall:test', (context) => {  
  /* Call after the call of the "test" method */  
}) 

context object

| Name | Type | Default | Description |
| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |
| self | object | - | The object wrapped by the hook proxy |
| name | string | - | The name of the called method |
| params | Array | - | The parameters of the called method |
| result | * | - | The returned value of the called method |

afterResolve[:methodName]

Called when the promise returned by the method has resolved. You can specify a unique method via the methodName option.

Example

objWithHooks.hooks.on('afterResolve', (context) => {  
  /* Call after resolve the promise of any method */  
})  
  
objWithHooks.hooks.on('afterResolve:testAsync', (context) => {  
  /* Call after resolve the promise of the "testAsync" method */  
})

context object

| Name | Type | Default | Description |
| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |
| self | object | - | The object wrapped by the hook proxy |
| name | string | - | The name of the called method |
| params | Array | - | The parameters of the called method |
| result | * | - | The returned value of the promise |

afterReject[:methodName]

Called when the promise returned by the method has rejected. You can specify a unique method via the methodName option.

Example

objWithHooks.hooks.on('afterReject', (context) => {  
  /* Call after reject the promise of any method */  
})  
  
objWithHooks.hooks.on('afterReject:testAsync', (context) => {  
  /* Call after reject the promise of the "testAsync" method */  
})

context object

| Name | Type | Default | Description |
| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |
| self | object | - | The object wrapped by the hook proxy |
| name | string | - | The name of the called method |
| params | Array | - | The parameters of the called method |
| errors | Array | - | The returned errors of the promise |

beforeSet[:propertyName]

Called before setting a property value. You can specify a unique property via the propertyName option.

Example

objWithHooks.hooks.on('beforeSet', (context) => {  
  /* Call before set any property */  
})  
  
objWithHooks.hooks.on('beforeSet:value', (context) => {  
  /* Call before set the property "value" */  
})

context object

| Name | Type | Default | Description |
| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |
| self | object | - | The object wrapped by the hook proxy |
| name | string | - | The name of the property |
| value | * | - | The new value of the property to set |

afterSet:[propertyName]

Called after setting a property value. You can specify a unique property via the propertyName option.

Example

objWithHooks.hooks.on('afterSet', (context) => {  
  /* Call after set any property */  
})  
  
objWithHooks.hooks.on('afterSet:value', (context) => {  
  /* Call after set the property "value" */  
})

context object

| Name | Type | Default | Description |
| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |
| self | object | - | The object wrapped by the hook proxy |
| name | string | - | The name of the property |
| value | * | - | The new value of the property to set |

beforeDelete[:propertyName]

Called before deleting a property via the delete instruction. You can specify a unique property via the propertyName option.

Example

objWithHooks.hooks.on('beforeDelete', (context) => {  
  /* Call before delete any property */  
})  
  
objWithHooks.hooks.on('beforeDelete:value', (context) => {  
  /* Call before delete the property "value" */  
})

context object

| Name | Type | Default | Description |
| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |
| self | object | - | The object wrapped by the hook proxy |
| name | string | - | The name of the property |

afterDelete[:propertyName]

Called after deleting a property via the delete instruction. You can specify a unique property via the propertyName option.

Example

objWithHooks.hooks.on('afterDelete', (context) => {  
  /* Call after delete any property */  
})  
  
objWithHooks.hooks.on('afterDelete:value', (context) => {  
  /* Call after delete the property "value" */  
})

context object

| Name | Type | Default | Description |
| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |
| self | object | - | The object wrapped by the hook proxy |
| name | string | - | The name of the property |