Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hooks-mixin

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hooks-mixin - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

4

index.js

@@ -91,5 +91,5 @@ /**

Class.prototype.removeHook = removeHook;
Class.prototype.__processHooks = processHooks;
Class.prototype.__processHooksAsync = processHooksAsync;
Class.prototype.processHooks = processHooks;
Class.prototype.processHooksAsync = processHooksAsync;
return Class;
};

@@ -14,4 +14,4 @@ /*global test expect jest */

expect(instance).toHaveProperty('removeHook');
expect(instance).toHaveProperty('__processHooks');
expect(instance).toHaveProperty('__processHooksAsync');
expect(instance).toHaveProperty('processHooks');
expect(instance).toHaveProperty('processHooksAsync');
expect(instance).not.toHaveProperty('__hooks');

@@ -29,3 +29,3 @@ instance.hook('test', () => {});

instance.hook('test', mockHook);
instance.__processHooks('test');
instance.processHooks('test');
expect(mockHook).toHaveBeenCalledTimes(2);

@@ -44,3 +44,3 @@ });

});
instance.__processHooks('test', 1, 2, 3);
instance.processHooks('test', 1, 2, 3);
expect(called).toBe(true);

@@ -56,3 +56,3 @@ });

});
instance.__processHooks('test');
instance.processHooks('test');
expect(called).toBe(true);

@@ -92,3 +92,3 @@ });

instance.test = async function() {
await this.__processHooksAsync('test');
await this.processHooksAsync('test');
expect(called).toBe(TIMES);

@@ -95,0 +95,0 @@ done = true;

{
"name": "hooks-mixin",
"version": "1.0.0",
"version": "1.1.0",
"description": "A small mixin for classes. Adds hooks ability to your classes",

@@ -5,0 +5,0 @@ "main": "index.js",

# hooks-mixin
A small mixin for classes. Adds hooks ability to your classes
## Install
### Yarn
```
yarn add hooks-mixin
```
### NPM
```
npm i hooks-mixin
```
## Use
### Add and call
```javascript
const HooksMixin = require('hooks-mixin');
class Class {
async save() {
// sync hook call
this.processHooks('pre-save');
await this.write();
// async hook call
await this.processHooksAsync('saved');
return this;
}
};
HooksMixin(Class);
const instance = new Class();
// add hook
instance.hook('pre-save', (instance) => {
// instance — is an instance this
// things with instance
});
instance.save();
// will call pre-save and saved hooks
```
You can add many hooks:
```javascript
instance.hook('pre-save', () => console.info('One'));
instance.hook('pre-save', () => console.info('Two'));
instance.hook('saved', () => console.info('Three'));
instance.hook('saved', () => console.info('Four'));
instance.save();
// One, Two, Three, Four
```
### Remove hook callback
```javascript
const preSaveHookFunction = () => console.info('=)');
instance.hook('pre-save', preSaveHookFunction);
instance.save();
// =)
instance.removeHook('pre-save', preSaveHookFunction);
instance.save();
// no “=)”
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc