These are chat archives for rosshinkley/nightmare

5th
Dec 2016
johnferro
@johnferro
Dec 05 2016 15:17
@Siilwyn Not 100% sure but I think within the respondTo callback, instead of just returning the position, you would want to call done with the position as the second argument (the first argument being null or an error if appropriate). Here's an example of it being used within nightmare for responding to javascript: https://github.com/segmentio/nightmare/blob/eab9161e61c78246563a94345a2c4162806e1bc3/lib/runner.js#L318
Selwyn
@Siilwyn
Dec 05 2016 19:42
@johnferro alright thanks going to try that right now!
Selwyn
@Siilwyn
Dec 05 2016 19:48
Woo thanks!
It works! :sparkles:
Nightmare.action(
    'getWindowPosition',
    function (name, options, parent, win, renderer, done) {
        parent.respondTo('getWindowPosition', function (done) {
            parent.emit('log', `Window position: ${win.getPosition()}`);
            done(null, win.getPosition());
        });
        done();
    },
    function (done) {
        this.child.call('getWindowPosition', done);
    }
);
Anybody knows a conciser way to write this? The second done call seems a bit weird to me`.
johnferro
@johnferro
Dec 05 2016 21:15
By the second done you mean the one outside of the parent.respondTo scope? Here might be a good place to get started looking at what nightmare is doing under the hood to register the action: https://github.com/segmentio/nightmare/blob/eab9161e61c78246563a94345a2c4162806e1bc3/lib/nightmare.js#L519. From my understanding (take with a grain of salt) since nightmare and electron are running in separate processes, nightmare needs to queue the registering of the action itself with the electron process. The second done is what let's nightmare know that this registration is complete. The first done (within the parent.respondTo) on the other hand is electron triggering a callback to send its response (in this case to a 'getWindowPosition' command) back to the node/nightmare process. So that first done callback is actually being called every time a 'getWindowPosition' command while the second done is only being called when the action first gets registered.
Selwyn
@Siilwyn
Dec 05 2016 21:25
oh that makes sense, thank you again so much @johnferro
appreciate it