Node .on method firing too many times

jla

I have an Electron app that presents a directory listing to the user. When the user clicks a button my interface script, interface.js, clears the container div and sends a message to main.js. On receiving the message, main.js scans the directory into an array of filenames and returns the array to interface.js as a response. Interface.js uses a .on method that fires when the response is received and updates the container div with the contents of the array.

This is my first real attempt at using Node, and as far as interface behaviour went everything worked brilliantly! Wonderful, it's only been a few hours and I'm already loving Node!

However, while debugging/stress testing I printed the returned array within the .on method to the console and noticed some strange behaviour. The first time the user clicks the button, the .on method runs once (verified by one message to the console). The second time the user clicks, the method runs twice (verified by two messages to the console); the third time it runs three times and so on.

The function in main.js that scans the directory only runs once per click, so the issue must be within inteface.js.

My code for main.js and interface.js:

main.js:

const {app, BrowserWindow, ipcMain} = require('electron');
const fs = require('fs');

...

ipcMain.on( 'list-directory', ( event, directory ) => {
    var files = fs.readdirSync( directory );
    event.sender.send( 'list-directory-reply', files );
});

interface.js

var { ipcRenderer, remote } = require( 'electron' );  
var main = remote.require( "./main.js" );

...

button.addEventListener('click', function(){ showDialogue( this ); }, false );

...

showDialogue( select ) {
    // clear the dialogue
    // some other stuff
    ipcRenderer.send( 'list-directory', './files/documents/' );
    ipcRenderer.on( 'list-directory-reply', function( event, contents ) {
        console.log( contents );
        if ( contents.length > 0 ) {
            // add contents to the dialogue
        }
    } );
}

The code is adapted from a tutorial on the Electron website.

Why does ipcRenderer.on run multiple times? Is it possible that it's bound to something every time the button is clicked, and thus runs as many times as past clicks? I put a print statement inside the event listener function, and inside the showDialogue function before the ipcRenderer stuff, but they both only printed once per click so the repeats are definitely only coming from ipcRenderer.on.

planet_hunter

You are subscribing to ipcRenderer.on after every button click which is causing multiple subscriptions. Try to define the ipcRenderer.on event handler outside click event and it should work fine.

Something like this -

button.addEventListener('click', function(){ showDialogue( this ); }, false );


ipcRenderer.on( 'list-directory-reply', function( event, contents ) {
    // ipcRenderer event handler
});

showDialogue(select) {
    ipcRenderer.send( 'list-directory', './files/documents/' );
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Javascript

Scroll event firing too many times. I only want it to fire a maximum of, say, once per second

From Java

JSF preRenderView called too many times

From Java

MigLayout - componentResized() called too many times

From Dev

Local storage being read too many times

From Dev

passport deserialize being called too many times

From Dev

Method returning redirect to itself - localhost redirected too many times

From Dev

Python recursive function called too many times

From Dev

Redirected you too many times - Express/node on Heroku

From Dev

Quartz CronTrigger firing itself many times

From Dev

Assembly loops loop too many times

From Dev

AWS Cloudfront redirects TOO_MANY times

From Dev

onLayoutChange() called too many times

From Dev

Request take too many times

From Dev

Executing a file too many times?

From Dev

Brightness keys adjust brightness too many times

From Dev

android viewpagerindicator calling getPageTitle() too many times

From Dev

Angularjs ng-show method getting called way too many times

From Dev

Knockout computed is triggered too many times

From Dev

Delete method being called too many times. Why?

From Dev

Reactive cocoa, combineLatest firing many times

From Dev

Process evaluated too many times

From Dev

Timer is executing too many times

From Dev

Debugging java for loop that runs too many times

From Dev

Redirected Too Many Times Error [PHP & MySQL]

From Dev

redirect too many times laravel

From Dev

jQuery - on click with * selector trigger too many times

From Dev

Nginx redirecting too many times

From Dev

infinite scroll triggers the event too many times

From Dev

How to prevent LSP onDidChangeContent event from firing too many times

Related Related

HotTag

Archive