Skip to content

Commit

Permalink
refactor(daemon): move makeIteratorRef from mail.followMessages to gu…
Browse files Browse the repository at this point in the history
…est and host
  • Loading branch information
kumavis committed Mar 2, 2024
1 parent 70e5eac commit bd762c3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/daemon/src/guest.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export const makeGuestMaker = ({
const external = Far('EndoGuest', {
...guest,
followChanges: () => makeIteratorRef(guest.followChanges()),
followMessages: () => makeIteratorRef(guest.followMessages()),
});
const internal = harden({
receive,
Expand Down
1 change: 1 addition & 0 deletions packages/daemon/src/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ export const makeHostMaker = ({
const external = Far('EndoHost', {
...host,
followChanges: () => makeIteratorRef(host.followChanges()),
followMessages: () => makeIteratorRef(host.followMessages()),
});
const internal = harden({ receive, respond, petStore });

Expand Down
35 changes: 15 additions & 20 deletions packages/daemon/src/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { makePromiseKit } from '@endo/promise-kit';
import { makeChangeTopic } from './pubsub.js';
import { makeIteratorRef } from './reader-ref.js';
import { assertPetName } from './pet-name.js';

const { quote: q } = assert;
Expand Down Expand Up @@ -88,24 +87,21 @@ export const makeMailboxMaker = ({
const listMessages = async () => harden(Array.from(dubAndFilterMessages()));

/** @type {import('./types.js').Mail['followMessages']} */
const followMessages = async () =>
makeIteratorRef(
(async function* currentAndSubsequentMessages() {
const subsequentRequests = messagesTopic.subscribe();
for (const message of messages.values()) {
const dubbedMessage = dubMessage(message);
if (dubbedMessage !== undefined) {
yield dubbedMessage;
}
}
for await (const message of subsequentRequests) {
const dubbedMessage = dubMessage(message);
if (dubbedMessage !== undefined) {
yield dubbedMessage;
}
}
})(),
);
const followMessages = async function* currentAndSubsequentMessages() {
const subsequentRequests = messagesTopic.subscribe();
for (const message of messages.values()) {
const dubbedMessage = dubMessage(message);
if (dubbedMessage !== undefined) {
yield dubbedMessage;
}
}
for await (const message of subsequentRequests) {
const dubbedMessage = dubMessage(message);
if (dubbedMessage !== undefined) {
yield dubbedMessage;
}
}
};

/**
* @param {object} partialMessage
Expand Down Expand Up @@ -444,7 +440,6 @@ export const makeMailboxMaker = ({

return harden({
petStore: mailStore,
// Mail
listMessages,
followMessages,
request,
Expand Down
2 changes: 1 addition & 1 deletion packages/daemon/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ export interface Mail {
petStore: PetStore;
// Mail operations:
listMessages(): Promise<Array<Message>>;
followMessages(): Promise<FarRef<Reader<Message>>>;
followMessages(): AsyncGenerator<Message, undefined, undefined>;
resolve(messageNumber: number, resolutionName: string): Promise<void>;
reject(messageNumber: number, message?: string): Promise<void>;
adopt(
Expand Down

0 comments on commit bd762c3

Please sign in to comment.