pp-events
v1.4.0
Published
Simple manager of events from javascript
Readme
Getting Started
This project uses Bun as its default runtime for development,
building and testing (bun install, bun run build, bun test). The published
package also works from plain Node.js (require or import) and from the
browser through the exports map in package.json.
In the web project include pp-events.js with:
<script src="https://cdn.jsdelivr.net/npm/pp-is/dist/pp-is.min.js" ></script>
<script src="https://cdn.jsdelivr.net/npm/pp-events/dist/pp-events.min.js" ></script>without dependencies
<script src="https://cdn.jsdelivr.net/npm/pp-events/dist/pp-events.nd.min.js" ></script>Or
Install
npm i pp-events --saveInitialize
Nodejs
var ppEvents = require("pp-events")
var Event = ppEvents() // Or new ppEvents()
RequireJS
// in data-main script file
requirejs.config({
baseUrl:"node_modules/",
paths: {
"pp-is": "pp-is/pp-is.min",
"pp-events":"pp-events/pp-events"
}
});
// in your main js file
require(["config"],function(){
require(["pp-events"],function(ppEvents){
const Event = ppEvents(); // or new events();
});
})
Browser Javascript
var Event = ppEvents() // Or new ppEvents()
//say hello function for execute
var sayHello = function( msg ){
console.log(msg)
}
Event.on("sayHello",sayHello)
Event.emit("sayHello","Hi everyone !!!!!")
// remove Events if will be necesary
Event.removeListener("sayHello",sayHello);
QuickJs Engine
// nd = No dependencies, pp-events only uses some functions from pp-is that are already included.
import "./dist/pp-events.nd.min.js"
var Event = ppEvents() // Or new ppEvents()
//say hello function for execute
var sayHello = function( msg ){
console.log(msg)
}
Event.on("sayHello",sayHello)
Event.emit("sayHello","Hi everyone !!!!!")
// remove Events if will be necesary
Event.removeListener("sayHello",sayHello);Initialize with events
The constructor accepts an optional object to register listeners right away,
without calling .on() for each one. Each key becomes an event name; the
value can be a single function or an array of functions:
var Event = ppEvents({
sayHello: function( msg ){ console.log(msg) },
multi: [
function(){ console.log("first listener") },
function(){ console.log("second listener") }
]
})
Event.emit("sayHello","Hi everyone !!!!!")
Event.emit("multi")Methods
on
Event.on("eventName",myFunction);emit
Event.emit("eventName",{
mydata:"myvalue",
otherData:"otherValue"
});checkOn
Event.checkOn("eventName");removeListener
Event.removeListener("eventName",myFunction);once
// myFunction only runs the first time "eventName" is emitted
Event.once("eventName",myFunction);removeAllListeners
// remove every listener for one event
Event.removeAllListeners("eventName");
// remove every listener for every event
Event.removeAllListeners();listenerCount
Event.listenerCount("eventName"); // number of active listenerseventNames
Event.eventNames(); // names of the events that currently have active listeners