Notifications Center
A notification center can manage all events off a composant in nodefony .
A notification center is a wrapper of node.js Events .
This wrapper add some particularities and assumes backward compatibility with old version of events manager.
Create Notifications Center :
#!/usr/bin/env node
const nodefony = require("nodefony");
// create([settings], [context], [nbListener]) {
const notificationsCenter = nodefony.notificationsCenter.create();
// or with class ([settings], [context], [nbListener]) {
const notificationsCenter = new nodefony.notificationsCenter.notification();
console.log(notificationsCenter);
/*Notification {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: 20
}*/
Listen an event of Notification Center :
wrapper of addListener
//listen an event of notification center : (eventName, callback)
notificationsCenter.on( "ready", (myArg) => {
console.log(myArg);
});
// The next time eventName is triggered, this listener is removed and then invoked.
notificationsCenter.once( "ready", (myArg) => {
console.log(myArg);
});
// change context (autobinding in context) (context, eventName, callback)
notificationsCenter.listen( context, "ready", function(myArg) {
console.log(this);
});
Fire an event of Notification Center :
Wrapper of emitter.emit
//Fire an event of notification center : (eventName, argument ...)
notificationsCenter.emit("ready", myArg);
// or
notificationsCenter.fire("ready", myArg);
Automatically add events in Notification Center :
Is useful to Automatically add events when you you have somme settings (configurations ). settingsToListen find all EventsName who begin by "on" on the setting object
#!/usr/bin/env node
const nodefony = require("nodefony");
const notificationsCenter = nodefony.notificationsCenter.create();
//Add events with settings in notification center : (localSettings, context)
notificationsCenter.settingsToListen({
display: true,
onBoot
: function() {
// here context is mycontext
console.log("EVENT : onBoot");
},
onInitialize
: function() {
// here context is mycontext
console.log("EVENT : onInitialize");
}
}, mycontext);
notificationsCenter.emit("onBoot", {});
notificationsCenter.emit("onInitialize", {});
/*
EVENT : onBoot
EVENT : onInitialize
*/
// All in One
const notificationsCenter = nodefony.notificationsCenter.create({
display: true,
onBoot
: function() {
// here context this is mycontext
console.log("EVENT : onBoot");
},
onInitialize
: function() {
// here context this is mycontext
console.log("EVENT : onInitialize");
}, mycontext, 50);
/**
* Notification {
* _events:
* [Object: null prototype] {
* onBoot: [Function: bound onBoot],
* onInitialize: [Function: bound onInitialize] },
* _eventsCount: 2,
* _maxListeners: 50
*}
*/
removeListener event of Notification Center :
#!/usr/bin/env node
const nodefony = require("nodefony");
const notificationsCenter = nodefony.notificationsCenter.create();
const callback = function(myArg1, myArg2) {
console.log(`my callback ${myArg1} : ${myArg2}`);
};
// listen
notificationsCenter.on("ready", callback);
// listen
notificationsCenter.on("ready", (myArg1, myArg2) => {
console.log(`my callback2 ${myArg1} : ${myArg2}`);
});
console.log(notificationsCenter);
// emit
notificationsCenter.emit("ready", "arg1", "arg1");
/*
my callback arg1 : arg1
my callback2 arg1 : arg1
Notification {
_events:
[Object: null prototype] { ready: [ [Function: callback], [Function] ] },
_eventsCount: 1,
_maxListeners: 20
}
*/
//removeListener ready callback of notification center : (eventName, listener)
notificationsCenter.removeListener("ready", callback);
console.log(notificationsCenter);
// emit
notificationsCenter.emit("ready", "arg1", "arg1");
/*
my callback2 arg1 : arg1
Notification {
_events: [Object: null prototype] { ready: [Function] },
_eventsCount: 1,
_maxListeners: 20
}
*/
remove All Listeners of an event of Notification Center :
//Unlisten all listeners of an event Name notification center : ([eventName])
notificationsCenter.removeAllListeners("ready");