Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

2.0 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file    MessagePortTransport.ts
 * @brief   NetTransport over a MessagePort — replication between same-machine
 *          realms (editor multiplayer preview across play iframes, web
 *          workers) with no socket. Structured clone carries both wire planes
 *          (JSON strings and ArrayBuffer frames) natively, and a port queues
 *          messages until the other end attaches, so wiring order is free.
 */
import { Emitter } from '../ecs/emitter';
import type { ReliableOrderedTransport } from './NetChannel';

/** The minimal MessagePort surface (structural — works for window and worker
 *  ports). A real DOM MessagePort types its `onmessage` against the full
 *  MessageEvent, which mutable-property variance rejects against this shape,
 *  so the constructor also accepts MessagePort directly. */
export interface MessagePortLike {
    postMessage(data: unknown): void;
    onmessage: ((event: { data: unknown }) => void) | null;
    close?(): void;
}

export class MessagePortTransport implements ReliableOrderedTransport {
    /** A MessagePort is a FIFO between two realms on one machine. */
    readonly delivery = 'reliable-ordered' as const;

    private events_ = new Emitter<{ message: [data: string | ArrayBuffer] }>();
    private readonly port_: MessagePortLike;

    constructor(port: MessagePortLike | MessagePort) {
        this.port_ = port as MessagePortLike;
        // Assigning onmessage implicitly starts a DOM MessagePort.
        this.port_.onmessage = (e) => {
            const d = e.data;
            if (typeof d === 'string' || d instanceof ArrayBuffer) {
                this.events_.emit('message', d);
            }
        };
    }

    on(event: 'message', handler: (data: string | ArrayBuffer) => void): () => void {
        return this.events_.on(event, handler);
    }

    send(data: string | ArrayBuffer): void {
        this.port_.postMessage(data);
    }

    close(): void {
        this.port_.close?.();
    }
}