Add local events support
This commit is contained in:
parent
324764f346
commit
1edbf7ffa4
5
package-lock.json
generated
Normal file
5
package-lock.json
generated
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"name": "rage-eventbus",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"lockfileVersion": 1
|
||||||
|
}
|
@ -15,5 +15,6 @@
|
|||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/micaww/rage-eventbus/issues"
|
"url": "https://github.com/micaww/rage-eventbus/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/micaww/rage-eventbus#readme"
|
"homepage": "https://github.com/micaww/rage-eventbus#readme",
|
||||||
|
"dependencies": {}
|
||||||
}
|
}
|
||||||
|
63
src/index.js
63
src/index.js
@ -0,0 +1,63 @@
|
|||||||
|
const util = require('./util.js');
|
||||||
|
|
||||||
|
//const isClient = !!mp.game.joaat;
|
||||||
|
//const isCEF = !!mp.trigger;
|
||||||
|
|
||||||
|
const listeners = {};
|
||||||
|
|
||||||
|
/*mp.events.add('rbus:process', (data) => {
|
||||||
|
|
||||||
|
});*/
|
||||||
|
|
||||||
|
const rbus = {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register an event listener.
|
||||||
|
* @param {string} eventName - The name of the event.
|
||||||
|
* @param {function} cb - The event's callback. The return value will be sent back to the caller.
|
||||||
|
*/
|
||||||
|
rbus.on = (eventName, cb) => {
|
||||||
|
if(!listeners[eventName]) listeners[eventName] = [];
|
||||||
|
listeners[eventName].push(cb);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unregister an event listener.
|
||||||
|
* @param {string} eventName - The name of the event.
|
||||||
|
* @param {function} cb - The callback that was registered with `on`.
|
||||||
|
*/
|
||||||
|
rbus.off = (eventName, cb) => {
|
||||||
|
if(!listeners[eventName]) return;
|
||||||
|
listeners[eventName] = listeners[eventName].filter(listener => listener !== cb);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calls a local event listener.
|
||||||
|
* @param {string} eventName - The name of the event.
|
||||||
|
* @returns {Promise} - The result from the local event listener.
|
||||||
|
*/
|
||||||
|
rbus.send = (eventName) => {
|
||||||
|
if(!listeners[eventName] || !listeners[eventName].length) return Promise.reject('NO_LISTENERS');
|
||||||
|
return Promise.resolve(listeners[eventName][0]());
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calls a remote event listener residing on the server.
|
||||||
|
* @param {string} eventName - The name of the event.
|
||||||
|
* @returns {Promise} - The result from the remote event listener.
|
||||||
|
*/
|
||||||
|
rbus.sendServer = (eventName) => {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calls a remote event listener residing on the client.
|
||||||
|
* @param player - The player to send to
|
||||||
|
* @param {string} eventName - The name of the event
|
||||||
|
* @returns {Promise} - The result from the remote event listener
|
||||||
|
*/
|
||||||
|
rbus.sendClient = (player, eventName) => {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = rbus;
|
11
src/util.js
Normal file
11
src/util.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
const util = {};
|
||||||
|
|
||||||
|
util.uid = () => {
|
||||||
|
let firstPart = (Math.random() * 46656) | 0;
|
||||||
|
let secondPart = (Math.random() * 46656) | 0;
|
||||||
|
firstPart = ("000" + firstPart.toString(36)).slice(-3);
|
||||||
|
secondPart = ("000" + secondPart.toString(36)).slice(-3);
|
||||||
|
return firstPart + secondPart;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = util;
|
Reference in New Issue
Block a user