Skip to content

Commit

Permalink
Fixes #169 - emit ready ack for race condition (#170)
Browse files Browse the repository at this point in the history
* fixes #169 - Add ack event when ready event is received, get rid of superfluous event listeners #169
* bumped version to 1.1.8 #169
  • Loading branch information
codecounselor authored Feb 14, 2017
1 parent 0470229 commit 70cac7d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ In your application, at the point which the view is ready for rendering
document.body.dispatchEvent(new Event('view-ready'))
```

**Warning:** It is possible that your application will be ready and emit the event before the main electron process has had a chance execute the javascript in the renderer process which listens for this event.

If you are finding that the [event is not effective](https://github.com/fraserxu/electron-pdf/issues/169) and your page waits until the full timeout has occurred, then you should use `setInterval` to emit the event until it is acknowledged like so:

```javascript
var eventEmitInterval = setInterval(function () {
document.dispatchEvent(new Event('ready'))
}, 25)

document.body.addEventListener('view-ready-ack', function(){
clearInterval(eventEmitInterval)
})
```

When the main process first receives your ready event it will emit a single acknowlegement on `document.body` with whatever event name you are using suffixed with `-ack`. So the default would be `view-ready-ack`

#### Observing your own event

If the page you are rending is under your control, and you wish to modify the behavior
Expand Down
10 changes: 6 additions & 4 deletions lib/exportJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ class ExportJob extends EventEmitter {
this._initializeWindowForResource()
const targetFile = this._getTargetFile(i)
const generateFunction = this._generateOutput.bind(this, win, targetFile, pageDone)
const waitFunction = this._waitForPage.bind(this, win, generateFunction, this.args.outputWait)
// Need a new listener to generate the resource
win.webContents.removeAllListeners('did-finish-load')
win.webContents.on('did-finish-load', waitFunction)
this._waitForPage(win, generateFunction, this.args.outputWait)
this._loadURL(win, uriPath)
})
})
Expand Down Expand Up @@ -428,6 +425,9 @@ class ExportJob extends EventEmitter {

/**
* Injects a wait if defined before calling the generateFunction
* Electron will apply the javascript we provide after the page is loaded,
* we don't have to wire up event listeners
* (https://github.com/electron/electron/pull/5319)
*
* @param window used for JavaScript injection to emit event back through IPC
* @param generateFunction called when view is ready
Expand Down Expand Up @@ -468,6 +468,8 @@ class ExportJob extends EventEmitter {
document.body.addEventListener('${eventName}',
function(event) {
ipcRenderer.send('${IPC_MAIN_CHANNEL_RENDER}', '${this.jobId}', event.detail)
// #169 - allows clients to send event until we acknowledge receipt
document.dispatchEvent(new Event('${eventName}-ack'))
}
)`

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "electron-pdf",
"version": "1.1.7",
"version": "1.1.8",
"description": "A command line tool to generate PDF from URL, HTML or Markdown files",
"main": "lib/index.js",
"scripts": {
Expand Down
14 changes: 11 additions & 3 deletions test/event-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@ <h1>Used to test responding to a ready event emitted on document</h1>
table.style.transformOrigin = 'top left'
table.style.transform='scale(.8)'

setTimeout(function () {
document.body.dispatchEvent(new CustomEvent('view-ready', {}))

/* */
var eventEmitInterval = setInterval(function () {
// document.dispatchEvent(new Event('ready'))
document.body.dispatchEvent(new CustomEvent('ready', {}))
}, 500)
document.body.dispatchEvent(new CustomEvent('view-ready', {}))
}, 25)

document.body.addEventListener('view-ready-ack', function(){
clearInterval(eventEmitInterval)
})


</script>
</body>
Expand Down

0 comments on commit 70cac7d

Please sign in to comment.