I would probably structure this so differently that it's hard to even say
where the changes should be. I don't know the Electron API, so the
following is almost certainly wrong, but it should demonstrate the basic
ideas:
```
const promisify = require('some-promisify-function');
const BrowserWindow = require('electron').BrowserWindow;
const options = { frame: false, height: 768, width: 1024, x: 0, y: 0 };
const delay = time => value => new Promise(resolve =>
setTimeout(() => resolve(value), time)
)
const loadElectron = () => {
console.log('wait for electron to init');
return promisify(require('electron').app.once)('ready');
}
const open = () => {
const url = process.argv[2];
console.log(`open url ${url}`);
window = new BrowserWindow(options);
window.loadURL(url);
return promisify(window.webContents.once)('did-stop-loading')
.then(() => window);
}
const capture = window => {
console.log('screenshot webpage');
return promisify(window.capturePage)(options);
}
const save = screenshot => {
console.log('save file electron.screenshot.png');
return promisify(require('fs').writeFile)('electron.screenshot.png',
data.toPng())
}
const exit = () => {
console.log('exit electron');
process.exit(0);
}
loadElectron()
.then(open)
.then(delay(5000))
.then(capture)
.then(save)
.then(exit)
```
And with one of the pipeline proposals, the final bit would look something
like this:
```
await loadElectron()
|> await open
|> await delay(5000)
|> await capture
|> await save
|> exit
```
I have no ideas if we could find one `promisify` function that could handle
the various parts of the Electron API, but we could certainly manage this
with individual wrappers if necessary.
I personally find this style *much* more readable.
I consider myself a senior-level developer, and I use Ramda's `pipe` and
`compose` [1] all the time. While I don't have particularly strong
feelings on the pipeline proposals, suggesting that they are only for toy
problems seems absurd to me.
[1]: http://ramdajs.com/docs/?pipe, http://ramdajs.com/docs/?compose
-- Scott
> On Mar 15, 2018, at 10:49 PM, Michael J. Ryan <[email protected]> wrote:
>
> And only 50x the amount of code too.
>
>
> fair enough. but lets move from small-picture toy-cases to bigger-picture
> integration-level ones, where non-blocking code is common.
>
> here's a working, simple but useful 40-sloc real-world electron-script [1]
> which employs a single recursive-callback (function onNext()) to
> step-by-step screen-capture websites/demos to png. how would you
> re-express the linear-steps in the example into something significantly
> more readable with function-composition or pipeline-operators?
>
> ```
> /*
> * screen-capture.js
> *
> * this electron-script will screen-capture the website with the given url
> in the commandline
> *
> * exsmple usage:
> * $ electron screen-capture.js https://www.pinterest.com/
> *
> * output:
> * case 1: wait for electron to init
> * case 2: open url https://www.pinterest.com/
> * case 3: wait 5000ms for webpage to render
> * case 4: screenshot webpage
> * case 5: save file electron.screenshot.png
> * case 6: exit electron
> */
>
> /*jslint
> bitwise: true,
> browser: true,
> maxerr: 8,
> maxlen: 96,
> node: true,
> nomen: true,
> regexp: true,
> stupid: true
> */
> (function () {
> 'use strict';
> var options, modeNext, onNext;
> modeNext = 0;
> onNext = function (data) {
> modeNext += 1;
> switch (modeNext) {
> case 1:
> console.log('case ' + modeNext + ': wait for electron to
> init');
> // wait for electron to init
> require('electron').app.once('ready', onNext);
> break;
> case 2:
> console.log('case ' + modeNext + ': open url ' +
> process.argv[2]);
> // init options
> options = { frame: false, height: 768, width: 1024, x: 0, y: 0
> };
> // init browserWindow;
> options.BrowserWindow = require('electron').BrowserWindow;
> options.browserWindow = new options.BrowserWindow(options);
> // goto next step when webpage is loaded
> options.browserWindow.webContents.once('did-stop-loading',
> onNext);
> // open url
> options.browserWindow.loadURL(process.argv[2]);
> break;
> case 3:
> console.log('case ' + modeNext + ': wait 5000ms for webpage to
> render’);
> // wait 5000ms for webpage to render
> setTimeout(onNext, 5000);
> break;
> case 4:
> console.log('case ' + modeNext + ': screenshot webpage');
> // screenshot webpage
> options.browserWindow.capturePage(options, onNext);
> break;
> case 5:
> console.log('case ' + modeNext + ': save file
> electron.screenshot.png');
> // save screenshot
> require('fs').writeFile('electron.screenshot.png',
> data.toPng(), onNext);
> break;
> case 6:
> console.log('case ' + modeNext + ': exit electron');
> // exit
> process.exit(0);
> break;
> }
> };
> onNext();
> }());
> ```
>
> [1] https://github.com/kaizhu256/node-electron-lite#
> quickstart-screenshot-example
>
>
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss