Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/session #53

Merged
merged 2 commits into from
Jan 3, 2025
Merged

Feature/session #53

merged 2 commits into from
Jan 3, 2025

Conversation

jmercouris
Copy link
Member

This expands cl-electron to support Session objects and DownloadItem objects.

@jmercouris
Copy link
Member Author

jmercouris commented Dec 31, 2024

@aadcg This design is not without compromise. I tried to create a listener within a listener dynamically to fulfill the example shown here:

const win = new BrowserWindow()
win.webContents.session.on('will-download', (event, item, webContents) => {
  // Set the save path, making Electron not to prompt a save dialog.
  item.setSavePath('/tmp/save.pdf')

  item.on('updated', (event, state) => {
    if (state === 'interrupted') {
      console.log('Download is interrupted but can be resumed')
    } else if (state === 'progressing') {
      if (item.isPaused()) {
        console.log('Download is paused')
      } else {
        console.log(`Received bytes: ${item.getReceivedBytes()}`)
      }
    }
  })
...

https://www.electronjs.org/docs/latest/api/download-item#class-downloaditem

This proved to be very complicated, and prone to strange deadlocks. Having nested electron:add-listener, was quite confusing, and involved another eval. As such, I compromised and created a specialization of add-listener for event :download-item-updated. This is a fictional event type. It does not exist within Electron. It is however where all of the magic happens.

Within this method, we'll establish listeners for will-download and then for the child item's updated and done signals. Then we'll listen to all three signals and do different things depending on each.

Additionally, we'll wrap the callback lambda to automatically update the session download-items slot, and keep the download-items up to date.

I hope that all makes sense! Please let me know if any of this is unclear! I am sure as we add more listeners we'll discover more compromises in our design, and things we must change. I resisted the urge to change many things already with this PR.

@jmercouris
Copy link
Member Author

P.S. I suggest reading through this PR by going through the commits iteratively.

@jmercouris jmercouris marked this pull request as ready for review December 31, 2024 06:36
@aadcg
Copy link
Member

aadcg commented Jan 2, 2025

I understand the implementation and I'd like to build upon it.

  1. session should be a slot of web-contents, just like the latter is a slot of window or view. This allows re-using references from the JS process.
  2. There seems to be no need to create a fictional event type. add-listener will specialize against will-download.
  3. In the example, I'd like to show how to cancel the download once it has been started (by the user, from the REPL).

I'll finish this PR, thanks!

@jmercouris
Copy link
Member Author

I understand the implementation and I'd like to build upon it.

1. `session` should be a slot of `web-contents`, just like the latter is a slot of `window` or `view`. This allows re-using references from the JS process.

Agreed!

2. There seems to be no need to create a fictional event type. `add-listener` will specialize against `will-download`.

I didn't want to shadow will-download because technically I am only interested in the download item updated event! However, you are right, we will probably not use will-download by itself ever, and thus it is OK to shadow it.

3. In the example, I'd like to show how to cancel the download once it has been started (by the user, from the REPL).

That is how it already works in the example!

I'll finish this PR, thanks!

thank you!

This function is used when the origin of a new object has to be in
JavaScript. For example, when a new object is created from within a listener,
but we wish to maintain a reference to it.
@aadcg
Copy link
Member

aadcg commented Jan 3, 2025

I didn't want to shadow will-download because technically I am only interested in the download item updated event! However, you are right, we will probably not use will-download by itself ever, and thus it is OK to shadow it.

This is a good point. Let's keep keep download-item-updated then.

@aadcg aadcg merged commit afae720 into master Jan 3, 2025
@aadcg aadcg deleted the feature/session branch January 3, 2025 20:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants