-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(typed-ipc): main process IpcListener
cleanup functions
#20
Open
psychedelicious
wants to merge
4
commits into
alex8088:master
Choose a base branch
from
psychedelicious:psyche/19
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+717
−13
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b289d15
feat(typed-ipc): main process `IpcListener` cleanup functions
psychedelicious c749133
build: add vitest
psychedelicious 5b3dcec
test(typed-ipc): add tests for main process IpcListener
psychedelicious 7abd49b
refactor(typed-ipc): clearer naming in main process `IpcListener`
psychedelicious File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' | ||
import { IpcEventMap, IpcListener } from './main' | ||
import { ipcMain } from 'electron' | ||
|
||
vi.mock('electron', () => ({ | ||
ipcMain: { | ||
on: vi.fn(), | ||
removeListener: vi.fn(), | ||
handle: vi.fn(), | ||
removeHandler: vi.fn() | ||
} | ||
})) | ||
|
||
describe('IpcListener', () => { | ||
let ipcListener: IpcListener<IpcEventMap> | ||
|
||
beforeEach(() => { | ||
ipcListener = new IpcListener() | ||
}) | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks() | ||
}) | ||
|
||
it('should register a listener and return a cleanup function', () => { | ||
const channel = 'test-channel' | ||
const listener1 = vi.fn() | ||
const listener2 = vi.fn() | ||
|
||
const cleanup1 = ipcListener.on(channel, listener1) | ||
ipcListener.on(channel, listener2) | ||
|
||
cleanup1() | ||
|
||
expect(ipcMain.removeListener).toHaveBeenCalledWith(channel, listener1) | ||
expect(ipcListener['listeners'].get(channel)?.has(listener1)).toBe(false) | ||
expect(ipcListener['listeners'].get(channel)?.has(listener2)).toBe(true) | ||
}) | ||
|
||
it('should delete the channel from the listeners map when all listeners are removed', () => { | ||
const channel = 'test-channel' | ||
const listener = vi.fn() | ||
|
||
const cleanup = ipcListener.on(channel, listener) | ||
|
||
cleanup() | ||
|
||
expect(ipcListener['listeners'].has(channel)).toBe(false) | ||
}) | ||
|
||
it('should register a handler and return a cleanup function', () => { | ||
const channel1 = 'test-channel-1' | ||
const channel2 = 'test-channel-2' | ||
const handler1 = vi.fn() | ||
const handler2 = vi.fn() | ||
|
||
const cleanup1 = ipcListener.handle(channel1, handler1) | ||
const cleanup2 = ipcListener.handle(channel2, handler2) | ||
|
||
cleanup1() | ||
|
||
expect(ipcMain.removeHandler).toHaveBeenCalledWith(channel1) | ||
expect(ipcListener['handlers'].has(channel1)).toBe(false) | ||
expect(ipcListener['handlers'].has(channel2)).toBe(true) | ||
|
||
cleanup2() | ||
|
||
expect(ipcMain.removeHandler).toHaveBeenCalledWith(channel2) | ||
expect(ipcListener['handlers'].has(channel2)).toBe(false) | ||
}) | ||
|
||
it('should dispose all listeners and handlers', () => { | ||
const channel1 = 'test-channel-1' | ||
const channel2 = 'test-channel-2' | ||
const listener1 = vi.fn() | ||
const listener2 = vi.fn() | ||
const handler = vi.fn() | ||
|
||
ipcListener.on(channel1, listener1) | ||
ipcListener.on(channel2, listener2) | ||
ipcListener.handle(channel1, handler) | ||
|
||
ipcListener.dispose() | ||
|
||
expect(ipcMain.removeListener).toHaveBeenCalledWith(channel1, listener1) | ||
expect(ipcMain.removeListener).toHaveBeenCalledWith(channel2, listener2) | ||
expect(ipcMain.removeHandler).toHaveBeenCalledWith(channel1) | ||
expect(ipcListener['listeners'].size).toBe(0) | ||
expect(ipcListener['handlers'].size).toBe(0) | ||
}) | ||
|
||
it('should only dispose listeners and handlers registered by the instance', () => { | ||
const channel1 = 'test-channel-1' | ||
const channel2 = 'test-channel-2' | ||
const listener1 = vi.fn() | ||
const listener2 = vi.fn() | ||
const handler = vi.fn() | ||
|
||
ipcListener.on(channel1, listener1) | ||
ipcListener.on(channel2, listener2) | ||
ipcListener.handle(channel1, handler) | ||
|
||
const otherIpcListener = new IpcListener() | ||
|
||
const otherChannel = 'other-channel' | ||
const otherListener = vi.fn() | ||
const otherHandler = vi.fn() | ||
|
||
otherIpcListener.on(otherChannel, otherListener) | ||
otherIpcListener.handle(otherChannel, otherHandler) | ||
|
||
ipcListener.dispose() | ||
|
||
expect(ipcMain.removeListener).toHaveBeenCalledWith(channel1, listener1) | ||
expect(ipcMain.removeListener).toHaveBeenCalledWith(channel2, listener2) | ||
expect(ipcMain.removeHandler).toHaveBeenCalledWith(channel1) | ||
expect(ipcMain.removeListener).not.toHaveBeenCalledWith(otherChannel, otherListener) | ||
expect(ipcMain.removeHandler).not.toHaveBeenCalledWith(otherChannel) | ||
expect(ipcListener['listeners'].size).toBe(0) | ||
expect(ipcListener['handlers'].size).toBe(0) | ||
}) | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,42 +8,62 @@ export * from './types' | |
* Typed listener for Electron `ipcMain`. | ||
*/ | ||
export class IpcListener<T extends IpcEventMap> { | ||
private listeners: string[] = [] | ||
private handlers: string[] = [] | ||
private listeners: Map<string, Set<(...args: any[]) => void>> = new Map() | ||
private handlers: Set<string> = new Set() | ||
|
||
/** | ||
* Listen to `channel`. | ||
* @returns A function to remove the listener. | ||
*/ | ||
on<E extends keyof ExtractArgs<T>>( | ||
channel: Extract<E, string>, | ||
listener: (e: Electron.IpcMainEvent, ...args: ExtractArgs<T>[E]) => void | Promise<void> | ||
): void { | ||
this.listeners.push(channel) | ||
): () => void { | ||
let channelListeners = this.listeners.get(channel) | ||
if (!channelListeners) { | ||
channelListeners = new Set() | ||
this.listeners.set(channel, channelListeners) | ||
} | ||
channelListeners.add(listener) | ||
ipcMain.on(channel, listener as (e: Electron.IpcMainEvent, ...args: any[]) => void) | ||
return () => { | ||
ipcMain.removeListener(channel, listener) | ||
channelListeners.delete(listener) | ||
if (channelListeners.size === 0) { | ||
this.listeners.delete(channel) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Handle a renderer invoke request. | ||
* @returns A function to remove the handler. | ||
*/ | ||
handle<E extends keyof ExtractHandler<T>>( | ||
channel: Extract<E, string>, | ||
listener: ( | ||
e: Electron.IpcMainInvokeEvent, | ||
...args: Parameters<ExtractHandler<T>[E]> | ||
) => ReturnType<ExtractHandler<T>[E]> | Promise<ReturnType<ExtractHandler<T>[E]>> | ||
): void { | ||
this.handlers.push(channel) | ||
): () => void { | ||
this.handlers.add(channel) | ||
ipcMain.handle(channel, listener as (event: Electron.IpcMainInvokeEvent, ...args: any[]) => any) | ||
return () => { | ||
ipcMain.removeHandler(channel) | ||
this.handlers.delete(channel) | ||
} | ||
} | ||
|
||
/** | ||
* Dispose all listeners and handlers. | ||
* Dispose all registered listeners and handlers. | ||
*/ | ||
dispose(): void { | ||
this.listeners.forEach(c => ipcMain.removeAllListeners(c)) | ||
this.listeners = [] | ||
this.handlers.forEach(c => ipcMain.removeHandler(c)) | ||
this.handlers = [] | ||
this.listeners.forEach((listeners, channel) => { | ||
listeners.forEach(listener => ipcMain.removeListener(channel, listener)) | ||
}) | ||
this.listeners.clear() | ||
this.handlers.forEach(channel => ipcMain.removeHandler(channel)) | ||
this.handlers.clear() | ||
} | ||
} | ||
Comment on lines
57
to
68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think the new behavior is less surprising than the old behavior, but you could argue that this is a breaking change. |
||
|
||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a
Set
fixes a subtle issue. With an array, it's possible that we could end up with duplicates channels in the array.You can only have 1 handler per channel, but the
handle
method will add the channel to the array even if a handler already exists for the channel.When we
dispose
, we may end up attempting to remove listeners for a channel multiple times.I don't think this causes any problems in practice, but using
Set
prevents the situation from happening in the first place.