Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/compiler/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function createDispatcher<sync extends 'sync' | 'async'>(
): Dispatcher<sync> {
return new Dispatcher<sync>(
compilationId,
messageTransformer.outboundMessages$,
messageTransformer.outboundMessagesFor(compilationId),
message => messageTransformer.writeInboundMessage(message),
handlers,
);
Expand Down
6 changes: 1 addition & 5 deletions lib/src/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ export class Dispatcher<sync extends 'sync' | 'async'> {

constructor(
private readonly compilationId: number,
private readonly outboundMessages$: Observable<
[number, proto.OutboundMessage]
>,
private readonly outboundMessages$: Observable<proto.OutboundMessage>,
private readonly writeInboundMessage: (
message: [number, proto.InboundMessage],
) => void,
Expand All @@ -87,8 +85,6 @@ export class Dispatcher<sync extends 'sync' | 'async'> {

this.outboundMessages$
.pipe(
filter(([compilationId]) => compilationId === this.compilationId),
map(([, message]) => message),
mergeMap(message => {
const result = this.handleOutboundMessage(message);
return result instanceof Promise
Expand Down
95 changes: 94 additions & 1 deletion lib/src/message-transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import {Observable, Subject} from 'rxjs';
import {Observable, Subject, Subscription} from 'rxjs';
import * as varint from 'varint';
import {create, toBinary} from '@bufbuild/protobuf';

Expand Down Expand Up @@ -107,4 +107,97 @@ describe('message transformer', () => {
});
});
});

describe('outboundMessagesFor', () => {
let protobufs$: Subject<Uint8Array>;

beforeEach(() => {
protobufs$ = new Subject();
messages = new MessageTransformer(protobufs$, () => {});
});

function sendLogEvent(compilationId: number, text: string): void {
protobufs$.next(
Uint8Array.from([
...varint.encode(compilationId),
...toBinary(
proto.OutboundMessageSchema,
create(proto.OutboundMessageSchema, {
message: {case: 'logEvent', value: {message: text}},
}),
),
]),
);
}

function collect(compilationId: number): {
texts: string[];
subscription: Subscription;
} {
const texts: string[] = [];
const subscription = messages
.outboundMessagesFor(compilationId)
.subscribe(message =>
texts.push(
(message.message.value as proto.OutboundMessage_LogEvent).message,
),
);
return {texts, subscription};
}

it("emits only the compilation's messages", () => {
const first = collect(1);
const second = collect(2);

sendLogEvent(1, 'a');
sendLogEvent(2, 'b');
sendLogEvent(3, 'c');
sendLogEvent(1, 'd');

expect(first.texts).toEqual(['a', 'd']);
expect(second.texts).toEqual(['b']);
});

it('routes a reused compilation ID to its new subscriber', () => {
const previous = collect(1);
sendLogEvent(1, 'a');
previous.subscription.unsubscribe();

const next = collect(1);
sendLogEvent(1, 'b');

expect(previous.texts).toEqual(['a']);
expect(next.texts).toEqual(['b']);
});

it('errors every compilation when a message fails to decode', () => {
const errors: unknown[] = [];
for (const compilationId of [1, 2]) {
messages
.outboundMessagesFor(compilationId)
.subscribe({error: error => errors.push(error)});
}

protobufs$.next(Buffer.from([-1]));
messages
.outboundMessagesFor(3)
.subscribe({error: error => errors.push(error)});

expect(errors).toHaveLength(3);
});

it('completes every compilation when the compiler exits', () => {
let completed = 0;
for (const compilationId of [1, 2]) {
messages
.outboundMessagesFor(compilationId)
.subscribe({complete: () => completed++});
}

protobufs$.complete();
messages.outboundMessagesFor(3).subscribe({complete: () => completed++});

expect(completed).toBe(3);
});
});
});
50 changes: 49 additions & 1 deletion lib/src/message-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import {Observable, Subject} from 'rxjs';
import {Observable, Subject, Subscriber} from 'rxjs';
import {map} from 'rxjs/operators';
import {fromBinary, toBinary} from '@bufbuild/protobuf';
import * as varint from 'varint';
Expand Down Expand Up @@ -32,15 +32,63 @@ export class MessageTransformer {
*/
readonly outboundMessages$ = this.outboundMessagesInternal$.pipe();

// The subscriber to each compilation's OutboundMessages, by compilation ID.
// Routing by ID keeps the cost of each message independent of the number of
// compilations in flight.
private readonly compilationSubscribers = new Map<
number,
Subscriber<OutboundMessage>
>();

// Set once `outboundMessages$` errors or completes, to end subscriptions
// made after that the same way.
private endSubscriber?: (subscriber: Subscriber<OutboundMessage>) => void;

constructor(
private readonly outboundProtobufs$: Observable<Uint8Array>,
private readonly writeInboundProtobuf: (buffer: Uint8Array) => void,
) {
this.outboundMessagesInternal$.subscribe({
next: ([compilationId, message]) =>
this.compilationSubscribers.get(compilationId)?.next(message),
error: error => this.end(subscriber => subscriber.error(error)),
complete: () => this.end(subscriber => subscriber.complete()),
});

this.outboundProtobufs$
.pipe(map(decode))
.subscribe(this.outboundMessagesInternal$);
}

/**
* The OutboundMessages for `compilationId`. Errors or completes when
* `outboundMessages$` does. Each compilation ID supports one subscriber at a
* time.
*/
outboundMessagesFor(compilationId: number): Observable<OutboundMessage> {
return new Observable(subscriber => {
if (this.endSubscriber) {
this.endSubscriber(subscriber);
return;
}
this.compilationSubscribers.set(compilationId, subscriber);
return () => {
if (this.compilationSubscribers.get(compilationId) === subscriber) {
this.compilationSubscribers.delete(compilationId);
}
};
});
}

private end(
endSubscriber: (subscriber: Subscriber<OutboundMessage>) => void,
): void {
this.endSubscriber = endSubscriber;
for (const subscriber of [...this.compilationSubscribers.values()]) {
endSubscriber(subscriber);
}
}

/**
* Converts the inbound `compilationId` and `message` to a protocol buffer.
*/
Expand Down