Skip to content

#1070 Can check if the current window is the first one. #1282

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

Merged
merged 1 commit into from
Aug 4, 2022
Merged

Conversation

kittaakos
Copy link
Contributor

Motivation

This is a dev thing. From now on, devs can call a service and check if the current window is the first one or not.

Change description

New service for checking the state of the current window. See #1070 why this is needed.

How to verify?

  • Check out the code,
  • Modify any contributions to log whether or not the current window is the first one. Example:
diff --git a/arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx b/arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx
index f2b4af00..ac77421a 100644
--- a/arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx
+++ b/arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx
@@ -47,6 +47,7 @@ import { MonitorViewContribution } from './serial/monitor/monitor-view-contribut
 import { ArduinoToolbar } from './toolbar/arduino-toolbar';
 import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state';
 import { SerialPlotterContribution } from './serial/plotter/plotter-frontend-contribution';
+import { WindowServiceExt } from './theia/core/window-service-ext';
 
 @injectable()
 export class ArduinoFrontendContribution
@@ -78,6 +79,9 @@ export class ArduinoFrontendContribution
   @inject(FrontendApplicationStateService)
   private readonly appStateService: FrontendApplicationStateService;
 
+  @inject(WindowServiceExt)
+  private readonly windowServiceExt: WindowServiceExt;
+
   @postConstruct()
   protected async init(): Promise<void> {
     if (!window.navigator.onLine) {
@@ -89,6 +93,9 @@ export class ArduinoFrontendContribution
         )
       );
     }
+    this.windowServiceExt
+      .isFirstWindow()
+      .then((firstWindow) => console.log('is first window?', firstWindow));
   }
 
   async onStart(app: FrontendApplication): Promise<void> {
  • Start the IDE2, make sure you have at least two sketches opened,
  • Quit the IDE2,
  • Start the IDE2,
  • Once all windows are loaded, open the dev tools of the top most one, and it should show true,
  • Other windows should show false.

In action:

is-first-window--final.mp4

Other information

Closes #1070

Reviewer checklist

  • PR addresses a single concern.
  • The PR has no duplicates (please search among the Pull Requests before creating one)
  • PR title and description are properly filled.
  • Docs have been added / updated (for bug fixes / features)

Closes #1070

Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
@per1234 per1234 added type: enhancement Proposed improvement topic: code Related to content of the project itself labels Aug 3, 2022
Copy link
Contributor

@AlbyIanna AlbyIanna left a comment

Choose a reason for hiding this comment

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

The code's looking great! Thanks, @kittaakos

I would just add one more thing to this PR:
Here below we are asking the electron backend to check for updates (it is done by every renderer process)

.then(() => this.updater.checkForUpdates(true))

and the electron main process is smart enough to return early if the request was already been made by another renderer process so that in the end we only check for updates once.
if (initialCheck) {
if (this.isAlreadyChecked) return Promise.resolve();
this.isAlreadyChecked = true;
}

But with your improvement, now we could avoid sending a request to electron every time a new renderer is spawn.

What do you think?

@kittaakos
Copy link
Contributor Author

Thank you for the review!

But with your improvement, now we could avoid sending a request to electron every time a new renderer is spawn.

What do you think?

Exactly, excellent remark! This is my long-term plan, but I did not want to change the service APIs in this PR. I'd prefer handling them in a separate PR, removing the backend part of the survey service, and applying the IDE updater changes you have mentioned.

Eventually, I can make the changes here if you think it makes sense. What do you think?

@AlbyIanna
Copy link
Contributor

but I did not want to change the service APIs in this PR. I'd prefer handling them in a separate PR

this sounds like a good plan, let's go for it 😉

Copy link
Contributor

@francescospissu francescospissu left a comment

Choose a reason for hiding this comment

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

Great work, thanks @kittaakos!

I have tried it and it works as expected. Thanks to this generalized behavior we can avoid creating a custom service implementation just to check if the current window is the first as I did here:

async isFirstInstance(): Promise<boolean> {
if (this.surveyDidShow) {
return false;
}
this.surveyDidShow = true;
return this.surveyDidShow;
}

I will modify this implementation accordingly to your improvements.

@kittaakos
Copy link
Contributor Author

Thank you for the review @AlbyIanna, @francescospissu 🙏

I will modify this implementation accordingly to your improvements.

Sounds great. Let's tackle the API updates afterward.

@kittaakos kittaakos merged commit 36ac47b into main Aug 4, 2022
@kittaakos kittaakos deleted the #1070 branch August 4, 2022 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: enhancement Proposed improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[dev] As a dev I want to distinguish between first and other browser windows
4 participants