Skip to content

Commit

Permalink
feat(plugins): introduce plugins architecture
Browse files Browse the repository at this point in the history
each plugin is just a function called immediately and passed with dush instance. They are useful if
you want to make `dush` to work with DOM or whatever you want.

resolves #3
  • Loading branch information
tunnckoCore committed Mar 12, 2017
1 parent a21d6b5 commit e9ac70b
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 6 deletions.
35 changes: 31 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ for few things and that's why `dush` exists.
- [API](#api)
* [dush()](#dush)
* [.all](#all)
* [.use](#use)
* [.on](#on)
* [.once](#once)
* [.off](#off)
Expand Down Expand Up @@ -130,7 +131,33 @@ console.log(emitter.all)
// => { foo: [Function, Function], bar: [Functon] }
```

### [.on](src/index.js#L93)
### [.use](src/index.js#L93)
> Invokes `plugin` function immediately, which is passed with `app` instance. You can use it for adding more methods or properties to the instance. Useful if you want to make dush to work with DOM for example.
**Params**

* `plugin` **{Function}**: A function passed with `(app)` signature
* `returns` **{Object}**: The `dush` instance for chaining

**Example**

```js
const app = dush()

app.on('hi', (str) => {
console.log(str) // => 'Hello World!!'
})

app.use((app) => {
app.foo = 'bar'
app.hello = (place) => app.emit('hi', `Hello ${place}!!`)
})

console.log(app.foo) // => 'bar'
app.hello('World')
```

### [.on](src/index.js#L126)
> Add `handler` for `name` event.
See [JSBin Example](http://jsbin.com/xeketuruto/edit?js,console).
Expand All @@ -157,7 +184,7 @@ emitter
emitter.emit('hi', 'world')
```

### [.once](src/index.js#L132)
### [.once](src/index.js#L165)
> Add `handler` for `name` event that will be called only one time.
See [JSBin Example](http://jsbin.com/teculorima/edit?js,console).
Expand Down Expand Up @@ -187,7 +214,7 @@ emitter
console.log(called) // => 1
```

### [.off](src/index.js#L176)
### [.off](src/index.js#L209)
> Remove `handler` for `name` event. If `handler` not passed will remove **all** listeners for that `name` event.
See [JSBin Example](http://jsbin.com/nujucoquvi/3/edit?js,console).
Expand Down Expand Up @@ -220,7 +247,7 @@ emitter.off('foo')
emitter.emit('foo')
```

### [.emit](src/index.js#L219)
### [.emit](src/index.js#L252)
> Invoke all handlers for given `name` event. If present, `'*'` listeners are invoked too with `(type, ...rest)` signature, where the `type` argument is a string representing the name of the called event; and all of the rest arguments.
See [JSBin Example](http://jsbin.com/muqujavolu/edit?js,console).
Expand Down
33 changes: 33 additions & 0 deletions dist/dush.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,39 @@ function dush () {

all: all,

/**
* > Invokes `plugin` function immediately, which is passed
* with `app` instance. You can use it for adding more methods
* or properties to the instance. Useful if you want to make
* dush to work with DOM for example.
*
* ```js
* const app = dush()
*
* app.on('hi', (str) => {
* console.log(str) // => 'Hello World!!'
* })
*
* app.use((app) => {
* app.foo = 'bar'
* app.hello = (place) => app.emit('hi', `Hello ${place}!!`)
* })
*
* console.log(app.foo) // => 'bar'
* app.hello('World')
* ```
*
* @name .use
* @param {Function} `plugin` A function passed with `(app)` signature
* @return {Object} The `dush` instance for chaining
* @api public
*/

use: function use (plugin) {
plugin(app);
return app
},

/**
* > Add `handler` for `name` event.
*
Expand Down
33 changes: 33 additions & 0 deletions dist/dush.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,39 @@ function dush () {

all: all,

/**
* > Invokes `plugin` function immediately, which is passed
* with `app` instance. You can use it for adding more methods
* or properties to the instance. Useful if you want to make
* dush to work with DOM for example.
*
* ```js
* const app = dush()
*
* app.on('hi', (str) => {
* console.log(str) // => 'Hello World!!'
* })
*
* app.use((app) => {
* app.foo = 'bar'
* app.hello = (place) => app.emit('hi', `Hello ${place}!!`)
* })
*
* console.log(app.foo) // => 'bar'
* app.hello('World')
* ```
*
* @name .use
* @param {Function} `plugin` A function passed with `(app)` signature
* @return {Object} The `dush` instance for chaining
* @api public
*/

use: function use (plugin) {
plugin(app);
return app
},

/**
* > Add `handler` for `name` event.
*
Expand Down
2 changes: 1 addition & 1 deletion dist/dush.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/dush.umd.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/dush.umd.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e9ac70b

Please sign in to comment.