feat: Add optional parameter to unwire event when terminated (#12)#39
Open
remdalm wants to merge 1 commit intosilvermine:masterfrom
Open
feat: Add optional parameter to unwire event when terminated (#12)#39remdalm wants to merge 1 commit intosilvermine:masterfrom
remdalm wants to merge 1 commit intosilvermine:masterfrom
Conversation
481ad16 to
bd731a4
Compare
velocitysystems
requested changes
Mar 31, 2026
guest-js/actions.ts
Outdated
| const actions = { | ||
| listen(listener: (download: DownloadWithAnyStatus) => void): Promise<UnlistenFn> { | ||
| return DownloadEventManager.shared.addListener(this.path, listener); | ||
| listen( |
Collaborator
There was a problem hiding this comment.
Rather than refactoring DownloadEventManager we can simplify this by relocating the wrapping logic from DownloadEventManager.addListener to the listen(..) method:
async listen(
listener: (download: DownloadWithAnyStatus) => void,
options?: ListenOptions,
): Promise<UnlistenFn> {
if (!options?.autoUnlisten) {
return DownloadEventManager.shared.addListener(this.path, listener);
}
let unlisten: UnlistenFn;
const wrappedListener = (download: DownloadWithAnyStatus): void => {
try {
listener(download);
} finally {
const isTerminal = download.status === DownloadStatus.Completed
|| download.status === DownloadStatus.Canceled;
if (isTerminal) {
unlisten();
}
}
};
unlisten = await DownloadEventManager.shared.addListener(this.path, wrappedListener);
return unlisten;
},However this still leaves the issue of "how do we test?" without exposing public methods/utilities for resetting state. We can solve this by extracting into a small, pure, exported function:
// actions.ts
export function wrapListenerWithAutoUnlisten(
listener: (download: DownloadWithAnyStatus) => void,
unlisten: () => void,
): (download: DownloadWithAnyStatus) => void {
return (download: DownloadWithAnyStatus): void => {
try {
listener(download);
} finally {
const isTerminal = download.status === DownloadStatus.Completed
|| download.status === DownloadStatus.Canceled;
if (isTerminal) {
unlisten();
}
}
};
}Then in actions.listen:
async listen(
listener: (download: DownloadWithAnyStatus) => void,
options?: ListenOptions,
): Promise<UnlistenFn> {
if (!options?.autoUnlisten) {
return DownloadEventManager.shared.addListener(this.path, listener);
}
let unlisten: UnlistenFn;
const wrapped = wrapListenerWithAutoUnlisten(listener, () => unlisten());
unlisten = await DownloadEventManager.shared.addListener(this.path, wrapped);
return unlisten;
},Tests then become trivial — just call the wrapper directly:
it('calls unlisten on Completed', () => {
const listener = vi.fn();
const unlisten = vi.fn();
const wrapped = wrapListenerWithAutoUnlisten(listener, unlisten);
wrapped(attachDownload({ ...IDLE_STATE, status: DownloadStatus.Completed }));
expect(listener).toHaveBeenCalledTimes(1);
expect(unlisten).toHaveBeenCalledTimes(1);
});
velocitysystems
requested changes
Mar 31, 2026
yokuze
reviewed
Mar 31, 2026
a36a0b5 to
134954d
Compare
134954d to
24f6f7c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.