Skip to content

Should RpcTarget be used with ReadableStream? #211

Description

@codehz
  it("can send an RpcTarget as a stream chunk and call methods on it", async () => {
    // The sender enqueues a pass-by-reference RpcTarget into a ReadableStream. The receiver
    // reads it back as a stub and can call methods on it, which route back to the sender.
    class ChunkTarget extends RpcTarget {
      square(i: number) { return i * i; }
      get label() { return "chunk-target"; }
    }

    let stream = new ReadableStream({
      start(controller) {
        controller.enqueue(new ChunkTarget());
        controller.close();
      }
    });

    class StreamReceiver extends RpcTarget {
      async receiveStream(stream: ReadableStream) {
        let reader = stream.getReader();
        let { value } = await reader.read();
        using chunk = value as any;
        let result = {
          squared: await chunk.square(5),
          label: await chunk.label,
        };
        // Drain the closing `done` chunk so the underlying pipe is released.
        await reader.read();
        return result;
      }
    }

    await using harness = new TestHarness(new StreamReceiver());
    let result: any = await harness.stub.receiveStream(stream);

    expect(result.squared).toBe(25);
    expect(result.label).toBe("chunk-target");
  });

This code is failed right now (RpcImportHook was already disposed), I think it should be allowed?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions