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

Add pxtservices package and iframe driver #9912

Merged
merged 3 commits into from
Mar 14, 2024
Merged

Conversation

riknoll
Copy link
Member

@riknoll riknoll commented Mar 11, 2024

This PR adds a new top-level package called pxtservices to be shared between our many webapps. Currently it just contains a single file that defines a nifty class for driving iframes that embed the makecode editor. Features include:

  • All currently defined requests now have type checked methods (no more calling postMessage directly)
  • An optional host object parameter that lets you implement all of the methods required by the iframe workspace (no need to figure out how that protocol works)
  • All editor events/messages can be accessed via addEventListener() (also type checked)

As part of this, I also fixed three editor events that were being incorrectly fired with the "pxteditor" type instead of "pxthost". The "pxteditor" versions still fire so as not to break existing implementations.

I also updated the makecodeEditorService to use this.

@riknoll riknoll requested a review from a team March 11, 2024 18:12
addEventListener(event: "workspacesync", handler: (ev: pxt.editor.EditorWorkspaceSyncRequest) => void): void;
addEventListener(event: "workspaceloaded", handler: (ev: pxt.editor.EditorWorkspaceSyncRequest) => void): void;
addEventListener(event: "workspacediagnostics", handler: (ev: pxt.editor.EditorWorkspaceDiagnostics) => void): void;
addEventListener(event: "editorcontentloaded", handler: (ev: pxt.editor.EditorContentLoadedRequest) => void): void;
Copy link
Contributor

Choose a reason for hiding this comment

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

duplicate declaration of "editorcontentloaded" handler.

return new Promise((resolve, reject) => {
message.response = true;
message.id = this.nextId++ + "";
this.pendingMessages[message.id] = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Noting a potential leak: All sent messages are wrapped in a promise and added to pendingMessages, which resolve once a reply is received. Is a reply received for every message sent?

Copy link
Member Author

Choose a reason for hiding this comment

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

only requests are wrapped in a promise. all requests with message.response set to true should indeed receive a response from the editor. i guess we could time out requests, but i think it's a minor scenario and i worry it might affect some long running requests (like starting a tutorial on a low end chromebook)

i guess i could implement some sort of heartbeat mechanism where the iframe reports back what messages are still active, but that might be overkill... thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, that seems fine then. If we expect every message sent this way to receive a response, then promises won't leak except when something else is already broken. Adding a heartbeat seems like overkill :)

if (!data || !/^pxt(host|editor|pkgext|sim)$/.test(data.type)) return;

if (data.type === "pxteditor") {
if (data.id && this.pendingMessages[data.id]) {
Copy link
Contributor

@eanders-ms eanders-ms Mar 11, 2024

Choose a reason for hiding this comment

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

Noting a potential leak: The promise associated with sent messages that never receive a their expected reply could hang forever. This driver may want to run a pending message checker in the background (on a timeout), to reject and clear pending messages that haven't received a reply in a certain amount of time.

Copy link
Contributor

@thsparks thsparks left a comment

Choose a reason for hiding this comment

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

Agree with Eric's comments, but otherwise LGTM!

@riknoll riknoll merged commit fb39120 into master Mar 14, 2024
6 checks passed
@riknoll riknoll deleted the dev/riknoll/iframe-driver branch March 14, 2024 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants