https://github.com/pea3nut/pea-event
An event model, extended in your js app
https://github.com/pea3nut/pea-event
class event event-model extend
Last synced: 10 months ago
JSON representation
An event model, extended in your js app
- Host: GitHub
- URL: https://github.com/pea3nut/pea-event
- Owner: pea3nut
- License: mit
- Created: 2017-09-05T09:48:06.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-10-20T07:00:02.000Z (over 8 years ago)
- Last Synced: 2025-08-09T13:12:25.492Z (11 months ago)
- Topics: class, event, event-model, extend
- Language: JavaScript
- Homepage:
- Size: 13.7 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pea-event · [](https://www.travis-ci.org/pea3nut/pea-event) [](https://coveralls.io/github/pea3nut/pea-event?branch=master) [](https://www.npmjs.com/package/pea-event) [](LICENSE)
An event model, extended in your js app
# What is this?
pea-event is a class of js that helps you manage event/listener model
```js
class App extends PeAEvent{};
var app =new App();
app.on('click',function(date){
console.log('app be clicked',date);
});
app.execEventAll('click',[new Date]);
```
Is nothing to be surprised at?
How about this:
```js
class App extends PeAEvent{};
var app =new App();
setTimeout(function(){
app.execEventAll('load');
},1000);
(async function(){
await app.wait('load');
console.log('app loaded');
}());
```
It's very semantic right?
pea-event can do more than this, maybe you should look at the [APIs](#apis)
# APIs
## PeAEvent
**Kind**: global class
* [PeAEvent](#PeAEvent)
* [new PeAEvent([asTools])](#new_PeAEvent_new)
* [.on(eventType, listener, [options])](#PeAEvent+on)
* [.one(eventType, listener, [options])](#PeAEvent+one)
* [.off(eventType, listener)](#PeAEvent+off) ⇒ boolean
* [.wait(eventType, [checker])](#PeAEvent+wait) ⇒ Promise
* [.has(eventType)](#PeAEvent+has) ⇒ Number
* [.reset()](#PeAEvent+reset)
* *[.execEventAll(type, args)](#PeAEvent+execEventAll) ⇒ Promise*
* *[.execListener(listener, args, eventType)](#PeAEvent+execListener)*
### new PeAEvent([asTools])
recommend extend this class, direct construct will throw an error
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [asTools] | boolean | false | if you really want just construct it, call with true |
### PeAEvent.on(eventType, listener, [options])
listen a event, add a listener for event
**Kind**: instance method of [PeAEvent](#PeAEvent)
| Param | Type | Description |
| --- | --- | --- |
| eventType | string | "*" means all |
| listener | function | |
| [options] | Object | |
| options.once | boolean | If true, the listener would be automatically removed when invoked. |
### PeAEvent.one(eventType, listener, [options])
listen a event but once.
it's alias for PeAEvent#on(eventType,listener,{once:true})
**Kind**: instance method of [PeAEvent](#PeAEvent)
| Param | Type | Description |
| --- | --- | --- |
| eventType | string | "*" means all |
| listener | function | |
| [options] | Object | |
### PeAEvent.off(eventType, listener) ⇒ boolean
remove a event listener.
> note: if you use * in listener, waiter will be remove too
**Kind**: instance method of [PeAEvent](#PeAEvent)
**Returns**: boolean - success or no
| Param | Type | Description |
| --- | --- | --- |
| eventType | string | |
| listener | function \| string | "*" means all |
### PeAEvent.wait(eventType, [checker]) ⇒ Promise
wait a event.
you can add a checker, return a boolean to specify whether to wait
if return a true, promise will be resolve
**Kind**: instance method of [PeAEvent](#PeAEvent)
| Param | Type | Description |
| --- | --- | --- |
| eventType | string | "*" means all |
| [checker] | function | a checker function, it will call by event dispatch and received dispatch's argument, return a boolean for resolve or keep waiting |
### PeAEvent.has(eventType) ⇒ Number
return the number of listeners for a given event type
**Kind**: instance method of [PeAEvent](#PeAEvent)
| Param | Type |
| --- | --- |
| eventType | string |
### PeAEvent.reset()
reset all event listener
**Kind**: instance method of [PeAEvent](#PeAEvent)
### *PeAEvent.execEventAll(type, args) ⇒ Promise*
trigger a event, exec this event's listener all.
you can overwrite this method to changed you want,
but overwrite method should use PeAEvent#execListener(listener ,arguments ,eventType) to exec a listener.
**Kind**: instance abstract method of [PeAEvent](#PeAEvent)
| Param | Type | Description |
| --- | --- | --- |
| type | string | event name to trigger |
| args | Array | passed argument |
### *PeAEvent.execListener(listener, args, eventType)*
how call a event listener.
you can overwrite this method to changed you want.
**Kind**: instance abstract method of [PeAEvent](#PeAEvent)
| Param | Type | Description |
| --- | --- | --- |
| listener | function | event listener |
| args | Array | the parameters passed in when the event is trigger |
| eventType | string | the event name |
# License
MIT