Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Facilitate running custom javascript on the main JS process #382

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions astilectron.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package astilectron

import (
"encoding/json"
"fmt"
"net"
"os/exec"
Expand Down Expand Up @@ -28,15 +29,17 @@ var (

// App event names
const (
EventNameAppClose = "app.close"
EventNameAppCmdQuit = "app.cmd.quit" // Sends an event to Electron to properly quit the app
EventNameAppCmdStop = "app.cmd.stop" // Cancel the context which results in exiting abruptly Electron's app
EventNameAppCrash = "app.crash"
EventNameAppErrorAccept = "app.error.accept"
EventNameAppEventReady = "app.event.ready"
EventNameAppEventSecondInstance = "app.event.second.instance"
EventNameAppNoAccept = "app.no.accept"
EventNameAppTooManyAccept = "app.too.many.accept"
EventNameAppClose = "app.close"
EventNameAppCmdQuit = "app.cmd.quit" // Sends an event to Electron to properly quit the app
EventNameAppCmdStop = "app.cmd.stop" // Cancel the context which results in exiting abruptly Electron's app
EventNameAppCrash = "app.crash"
EventNameAppErrorAccept = "app.error.accept"
EventNameAppEventReady = "app.event.ready"
EventNameAppEventSecondInstance = "app.event.second.instance"
EventNameAppExecuteJavaScript = "app.execute.javascript"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rename those based on what I've suggested in the JS PR ?

EventNameAppExecuteJavaScriptCallback = "app.execute.javascript.callback"
EventNameAppNoAccept = "app.no.accept"
EventNameAppTooManyAccept = "app.too.many.accept"
)

// Astilectron represents an object capable of interacting with Astilectron
Expand Down Expand Up @@ -444,3 +447,35 @@ func (a *Astilectron) NewTray(o *TrayOptions) *Tray {
func (a *Astilectron) NewNotification(o *NotificationOptions) *Notification {
return newNotification(a.worker.Context(), o, a.supported != nil && a.supported.Notification != nil && *a.supported.Notification, a.dispatcher, a.identifier, a.writer)
}

type eventProxy struct {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need this new struct, *Astilectron already has an On() method

id string
dispatcher *dispatcher
logger astikit.SeverityLogger
}

func (g *eventProxy) On(eventName string, l Listener) {
g.dispatcher.addListener(g.id, eventName, l)
}

func (a *Astilectron) ExecuteJavascript(code string, response interface{}) error {
ep := &eventProxy{
id: a.identifier.new(),
dispatcher: a.dispatcher,
logger: a.l,
}
event, err := synchronousEvent(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use this instead:

synchronousEvent(a.worker.Context(), a, a.writer, Event{Name: EventNameAppCmdExecuteJavaScript, TargetID: targetIDApp, Code: code}, EventNameAppEventExecutedJavaScript)

a.worker.Context(), ep, a.writer,
Event{Name: EventNameAppExecuteJavaScript, TargetID: ep.id, Code: code},
EventNameAppExecuteJavaScriptCallback)
if err != nil {
return fmt.Errorf("sending ExecuteJavascript event failed: %w", err)
}
if event.Error != nil && *event.Error != "" {
return fmt.Errorf("ExecuteJavascript failed: %s", *event.Error)
}
if err := json.Unmarshal([]byte(event.Reply), response); err != nil {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Could you wrap this in if response != nil && len(event.Success) > 0 because some developers may not need a response here
  2. If you use the Success field as I mentioned, you can use json.Unmarshal(event.Success, response) instead

return fmt.Errorf("unmarshalling ExecuteJavascript response failed: %w", err)
}
return nil
}
1 change: 1 addition & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Event struct {
Code string `json:"code,omitempty"`
Displays *EventDisplays `json:"displays,omitempty"`
Enable *bool `json:"enable,omitempty"`
Error *string `json:"error,omitempty"`
FilePath string `json:"filePath,omitempty"`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather you create this new field instead of using Reply:

Success json.RawMessage `json:"success"`

ID *int `json:"id,omitempty"`
Image string `json:"image,omitempty"`
Expand Down