1.0.1 • Published 7 years ago

pea-event v1.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

pea-event · travis-ci Status soverage Status npm version MIT

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

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:

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

PeAEvent

Kind: global class

new PeAEvent(asTools)

recommend extend this class, direct construct will throw an error

ParamTypeDefaultDescription
asToolsbooleanfalseif 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

ParamTypeDescription
eventTypestring"*" means all
listenerfunction
optionsObject
options.oncebooleanIf 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

ParamTypeDescription
eventTypestring"*" means all
listenerfunction
optionsObject

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
Returns: boolean - success or no

ParamTypeDescription
eventTypestring
listenerfunction | 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

ParamTypeDescription
eventTypestring"*" means all
checkerfunctiona 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

ParamType
eventTypestring

PeAEvent.reset()

reset all event listener

Kind: instance method of 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

ParamTypeDescription
typestringevent name to trigger
argsArraypassed 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

ParamTypeDescription
listenerfunctionevent listener
argsArraythe parameters passed in when the event is trigger
eventTypestringthe event name

License

MIT