Skip to content

Commit

Permalink
docs: update doc links
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Aug 3, 2024
1 parent 2dbc96e commit 8fe8355
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
62 changes: 31 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@

[![Build Status](https://secure.travis-ci.org/adaltas/node-plug-and-play.svg)](http://travis-ci.org/adaltas/node-plug-and-play)

# Node.js Plug-And-Play package

Easily create hooks and let users plug their own logic across your code to make it extensible by everyone with new features.

## Main features
## Main features

* Extention points definition
- Extention points definition
Simple to declare new extention points, yet a lot of flexibility to the plugin authors.
* Hook definition
- Hook definition
Plugin writer can intercept calls to a function by placing their own logicl before, after and even switching the default implementation.
* Dependency management
- Dependency management
Plugins can require other plugins as required dependencies as well as choose the order of execution of each hook.
* Promise support
- Promise support
Hook can be synchronous and asynchronous when returning a promise.
* Nested/hierachical
- Nested/hierachical
Instanciate plugin instances with a parent reference and parent hooks will also be available inside the children.

## Learning
Expand All @@ -28,62 +27,63 @@ Here is the documentation:
- [Initialize a new instance](./docs/2.initialization.md)
- [Plugins registration](./docs/3.plugins.md)
- [Hook declaration](./docs/4.hook.md)
- [API usage](./docs/5.api.md)
- [Developers instructions](./docs/6.developers.md)
- [Hook handler definition and usage](./docs/5.handler.md)
- [API usage](./docs/6.api.md)
- [Developers instructions](./docs/7.developers.md)

## Quick example

Library and application authors define hooks, see [`./sample/lib.js`](https://github.com/adaltas/node-plug-and-play/blob/master/sample/lib.js):

```js
const plugandplay = require('plug-and-play')
const plugandplay = require("plug-and-play");

const plugins = plugandplay()
const plugins = plugandplay();

module.exports = {
// Create and export a new Plug and Play instance
plugins: plugins,
// Our core library function
print: function() {
print: function () {
// Wrap-up code
plugins.call({
// Identify this hook with a name
name: 'hooks:print',
name: "hooks:print",
// Expose arguments to plugins authors
args: {
data: { message: 'hello' }
data: { message: "hello" },
},
// Default implementation
handler: ({data}) => {
handler: ({ data }) => {
// Original library
console.log(data.message)
}
})
}
}
console.log(data.message);
},
});
},
};
```

Users and pluging authors can now register their own hooks, see [`./sample/index.js`](https://github.com/adaltas/node-plug-and-play/blob/master/sample/error.js):

```js
const mysuperlibrary = require('./lib')
const mysuperlibrary = require("./lib");

mysuperlibrary.plugins.register({
hooks: {
'hooks:print': ({data}, handler) => {
"hooks:print": ({ data }, handler) => {
// Alter the argument
data.message = 'Hello World'
data.message = "Hello World";
// Print a message before the library code
console.log('>>>>>>>>>>>')
console.log(">>>>>>>>>>>");
// Call the original handler
const result = handler.call(null, {data: data})
const result = handler.call(null, { data: data });
// Print a message after the library code
console.log('<<<<<<<<<<<')
return result
}
}
})
mysuperlibrary.print()
console.log("<<<<<<<<<<<");
return result;
},
},
});
mysuperlibrary.print();
```

While the original `print` function was only printing `Hello` to stdout, the introduction of this new plugin prints:
Expand Down
4 changes: 2 additions & 2 deletions docs/5.handler.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Hook handler definition and pattern usage
# Hook handler definition and usage

The hook handler alter the behavior of original handler passed by `call` or `call_sync`. Its implementation allos multiple execution pattern:
The hook handler alter the behavior of original handler passed by `call` or `call_sync`. Its implementation allos multiple execution patterns:

- Execute code before and after the original handler
- Alter the arguments arguments
Expand Down

0 comments on commit 8fe8355

Please sign in to comment.