Skip to content

Commit

Permalink
Fix: emit events from events method (#8)
Browse files Browse the repository at this point in the history
* fix: binarypath is properly resolved now

* feat: implemented ws connection

* feat: added test for ws

* fixed test cases

* feat: added check for reinitializing already initialized application

* chore: standardised quotes, better params naming

* chore

* feat: configured class to only run from root of neu app

* chore

* feat: added native API's, implemented queues for extension and native methods

* fix: check if queue exist before processing

* feat: added check before processing Ext queue

* feat: added test for native api's

* Emit event from event object

* fix: Emit event from event object

* feat: added events.dispatch

* feat: added method events.off

* fix merge
  • Loading branch information
viralgupta authored Jul 4, 2024
1 parent 469b486 commit a4ec41b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion spec/app.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('Test NeutralinoApp Class', function () {
reject(new Error('Unable to connect to the Neutralino server'));
}, 3000);

app.on('extClientConnect', (data) => {
app.events.on('extClientConnect', (data) => {
clearTimeout(timeout);
resolve(data);
});
Expand Down
32 changes: 28 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const WS = require("websocket").w3cwebsocket;
const EventEmitter = require("events");
const { v4: uuidv4 } = require("uuid");

class NeutralinoApp extends EventEmitter {
class NeutralinoApp {

url = "";
windowOptions = {};
Expand All @@ -21,9 +21,9 @@ class NeutralinoApp extends EventEmitter {
neuProcess = null;

constructor({ url, windowOptions }) {
super();
this.url = url;
this.windowOptions = windowOptions;
this.eventEmitter = new EventEmitter();
}

init() {
Expand Down Expand Up @@ -112,7 +112,7 @@ class NeutralinoApp extends EventEmitter {
console.log("Connected with the application.");
this._processQueue(this.offlineMessageQueue);
this.getConfig().then((config) => {
if(config.enableExtensions){
if (config.enableExtensions) {
this.extensions.getStats().then((stats) => {
for (const extensionId of stats.connected) {
if (extensionId in this.extensionMessageQueue) {
Expand Down Expand Up @@ -162,7 +162,7 @@ class NeutralinoApp extends EventEmitter {
})
}
}
this.emit(message.event, message.data);
this.eventEmitter.emit(message.event, message.data);
}
}
}
Expand Down Expand Up @@ -355,6 +355,30 @@ class NeutralinoApp extends EventEmitter {
events = {
broadcast: (event, data) => {
return this._sendMessage('events.broadcast', { event, data });
},

on: (event, listener) => {
this.eventEmitter.on(event, listener);
return Promise.resolve({
success: true,
message: 'Event listener added'
});
},

off: (event, listener) => {
this.eventEmitter.off(event, listener);
return Promise.resolve({
success: true,
message: 'Event listener removed'
});
},

dispatch: (event, data) => {
this.eventEmitter.emit(event, data);
return Promise.resolve({
success: true,
message: 'Message dispatched'
});
}
};

Expand Down

0 comments on commit a4ec41b

Please sign in to comment.