aboutsummaryrefslogtreecommitdiffstats
path: root/internal/swextreload.go
diff options
context:
space:
mode:
authorTeddy Wing2023-11-17 00:03:39 +0100
committerTeddy Wing2023-11-17 00:03:39 +0100
commit8d5e2339bc33e03dcf4442057cfc2b3986c81a61 (patch)
tree8744136ca98e4b0ee0b224eb34046339a80d68cd /internal/swextreload.go
parentd1510be20dc362aa94d5e422e93067b8470274ff (diff)
downloadswextreload-8d5e2339bc33e03dcf4442057cfc2b3986c81a61.tar.bz2
swextreload: Try lifting getTargets outside of `reloadExtension`
Call getTargets only once, not each time we want to reload an extension. This hasn't resolved the wonkiness I'm seeing with reloading multiple extensions, unfortunately.
Diffstat (limited to 'internal/swextreload.go')
-rw-r--r--internal/swextreload.go52
1 files changed, 26 insertions, 26 deletions
diff --git a/internal/swextreload.go b/internal/swextreload.go
index e23467e..5f35ee0 100644
--- a/internal/swextreload.go
+++ b/internal/swextreload.go
@@ -26,7 +26,6 @@ func Reload(
extensionIDs []string,
shouldReloadTab bool,
) error {
- var targets []*target.Info
var err error
allocatorContext, cancel := chromedp.NewRemoteAllocator(
@@ -35,9 +34,23 @@ func Reload(
)
defer cancel()
+ ctx, cancel := chromedp.NewContext(allocatorContext)
+ defer cancel()
+
+ // TODO: I think get targets once first, and reload all extensions using those targets. Rather than getting targets for each extension reload.
+ targets, err := chromedp.Targets(ctx)
+ if err != nil {
+ return fmt.Errorf("swextreload: can't get targets: %v", err)
+ }
+
+ if isDebug {
+ log.Printf("Targets: %#v", targets)
+ }
+
for _, extensionID := range extensionIDs {
- targets, err = reloadExtension(
- allocatorContext,
+ err = reloadExtension(
+ ctx,
+ targets,
extensionID,
shouldReloadTab,
)
@@ -82,22 +95,10 @@ func Reload(
// true, also reload the current tab.
func reloadExtension(
ctx context.Context,
+ targets []*target.Info,
extensionID string,
shouldReloadTab bool,
-) ([]*target.Info, error) {
- ctx, cancel := chromedp.NewContext(ctx)
- defer cancel()
-
- // TODO: I think get targets once first, and reload all extensions using those targets. Rather than getting targets for each extension reload.
- targets, err := chromedp.Targets(ctx)
- if err != nil {
- return targets, fmt.Errorf("swextreload: can't get targets: %v", err)
- }
-
- if isDebug {
- log.Printf("Targets: %#v", targets)
- }
-
+) error {
extensionURL := "chrome-extension://" + extensionID + "/"
var targetID target.ID
@@ -112,23 +113,22 @@ func reloadExtension(
}
}
- targetCtx, cancel := chromedp.NewContext(ctx, chromedp.WithTargetID(targetID))
+ targetCtx, _ := chromedp.NewContext(ctx, chromedp.WithTargetID(targetID))
// defer cancel()
log.Printf("Connected to target")
var runtimeResp []byte
- err = chromedp.Run(
+ err := chromedp.Run(
targetCtx,
chromedp.Evaluate(`chrome.runtime.reload();`, &runtimeResp),
)
if err != nil {
- return targets,
- fmt.Errorf(
- "swextreload: error reloading extension '%s': %v",
- extensionID,
- err,
- )
+ return fmt.Errorf(
+ "swextreload: error reloading extension '%s': %v",
+ extensionID,
+ err,
+ )
}
log.Printf("Reloaded extension")
@@ -137,7 +137,7 @@ func reloadExtension(
log.Printf("Runtime: %v", string(runtimeResp))
}
- return targets, nil
+ return nil
}
func reloadTab(