microsoft / microsoft/pxt

problem with simmessages.send logic

Open
#10,280 1 comment 0 reactions 1 assignee View on GitHub

@eanders-ms is already working on this.

Since Nov 20, 2024.

bug
Dominant language
TypeScript
Stars
2.3k
Forks
641
Avg merge
12h 4m
Merged PRs (30d)
57

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.