Skip to content

Conversation

@takaokouji
Copy link

Summary

This PR adds a new Runtime.BEFORE_STEP event to Scratch VM and modifies the Mesh V2 extension to use this event for frame-synchronized broadcasts.

Changes

  • Runtime: Added BEFORE_STEP event constant and emission at the beginning of each _step().
  • Mesh V2:
    • Modified MeshV2Service to listen for BEFORE_STEP event.
    • Replaced setTimeout-based broadcast scheduling with a pendingBroadcasts queue.
    • Implemented processNextBroadcast() which executes one queued broadcast per frame step.
  • Tests: Updated unit and integration tests to verify the new queue-based broadcast logic.

Purpose

In Mesh V2 batch event sending, receiving multiple events in a short interval previously caused multiple broadcasts to fire in the same frame. Due to Scratch VM's behavior, this would restart the same message's scripts, causing processing to be lost. By synchronizing broadcasts with frame steps (one per frame), we ensure every event is correctly processed.

Related Issue: smalruby/smalruby3-gui#475
Related Issue: smalruby/smalruby3-gui#473

takaokouji and others added 4 commits December 30, 2025 09:22
… broadcasts in meshV2

- Add Runtime.BEFORE_STEP event constant
- Emit BEFORE_STEP event at the beginning of each frame step in Runtime._step()
- Modify MeshV2Service to listen for BEFORE_STEP event
- Replace setTimeout-based broadcast scheduling with a queue-based system in meshV2
- Add processNextBroadcast() to handle one broadcast per frame synchronized with runtime
- Update unit tests to match the new frame-synchronized broadcast logic

This ensures that multiple events received in a short time are processed across different frames,
preventing Scratch threads from being restarted and lost.

Issue: smalruby/smalruby3-gui#475
Related: smalruby/smalruby3-gui#473

Co-Authored-By: Gemini <noreply@google.com>
- Fix prefer-const, object-curly-spacing and no-multiple-empty-lines in mesh_service_v2.js
- Fix no-unused-vars in mesh_service_v2_integration.js

🤖 Generated with [Gemini Code](https://gemini.google.com/code)

Co-Authored-By: Gemini <noreply@google.com>
- Remove excessive blank lines between test declarations
- Remove unnecessary blank lines within test bodies
- Improve code readability and consistency

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
…me synchronization

- Update MeshV2Service to store offsetMs for each pending broadcast relative to batch start
- Use Date.now() to accurately determine when each broadcast should fire
- Ensure broadcasts are synchronized with Runtime.BEFORE_STEP (one per frame)
- Update unit and integration tests to verify accurate timing and frame synchronization

🤖 Generated with [Gemini Code](https://gemini.google.com/code)

Co-Authored-By: Gemini <noreply@google.com>
@takaokouji takaokouji merged commit cd466dd into develop Dec 30, 2025
1 check passed
@takaokouji takaokouji deleted the feature/475-before-step-event branch December 30, 2025 02:07
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.

2 participants