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

problem with simmessages.send logic #10280

Open
tballmsft opened this issue Nov 20, 2024 · 1 comment
Open

problem with simmessages.send logic #10280

tballmsft opened this issue Nov 20, 2024 · 1 comment
Assignees
Labels

Comments

@tballmsft
Copy link
Contributor

tballmsft commented Nov 20, 2024

This problem applies to both pxt-microbit and pxt-arcade. The following code will cause a second instance of a mbit/arcade simulator to appear:

const data = Buffer.fromUTF8("hello")
control.simmessages.send("foobar", data)

This is because all simmessages sent the above way have
the broadcast bit set, which is used to determine if a
new simulator can be created; if you send a message that doesn't match
to a simulator extension ("jacdac", "robot", etc.)
you will get a second mbit simulator

Repro with arcade: https://makecode.com/_38FRkP531hjj
Repro with mbit: https://makecode.microbit.org/_ETPMjLM6D717

The send function is declared as follows in pxt-common-packages/libs/base/controlmessage.ts:

    //% shim=pxt::sendMessage
    export declare function send(channel: string, message: Buffer, parentOnly?: boolean) : void;

The default value for parentOnly is false; if it is set to true, no new simulators will be created at all.

the implementation is in pxsim.pxtcore as

    // general purpose message sending mechanism
    export function sendMessage(channel: string, message: RefBuffer, parentOnly?: boolean) {
        if (!channel) return;

        Runtime.postMessage({
            type: "messagepacket",
            broadcast: !parentOnly,
            channel: channel,
            data: message && message.data
        } as SimulatorControlMessage)
    }

Here we can see how the broadcast bit is set based on !parentOnly.

@tballmsft
Copy link
Contributor Author

Not sure what the action for this is other than better documentation, but it seems odd that the fall through behavior is to create a new instance of the code simulator (mbit or arcade).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants