-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve gui app disposal handling and close all network connections
- Loading branch information
Showing
14 changed files
with
189 additions
and
76 deletions.
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
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,39 @@ | ||
import type { ObservableV2 } from 'lib0/observable' | ||
|
||
interface Disposable { | ||
dispose(): void | ||
} | ||
|
||
export class AbortScope { | ||
private ctrl: AbortController = new AbortController() | ||
get signal() { | ||
return this.ctrl.signal | ||
} | ||
|
||
dispose(reason?: string) { | ||
this.ctrl.abort(reason) | ||
} | ||
|
||
handleDispose(disposable: Disposable) { | ||
this.signal.throwIfAborted() | ||
this.onAbort(disposable.dispose.bind(disposable)) | ||
} | ||
|
||
onAbort(listener: () => void) { | ||
if (this.signal.aborted) { | ||
setTimeout(listener, 0) | ||
} else { | ||
this.signal.addEventListener('abort', listener, { once: true }) | ||
} | ||
} | ||
|
||
handleObserve< | ||
EVENTS extends { [key in keyof EVENTS]: (...arg0: any[]) => void }, | ||
NAME extends keyof EVENTS & string, | ||
>(observable: ObservableV2<EVENTS>, name: NAME, f: EVENTS[NAME]) { | ||
if (this.signal.aborted) return | ||
observable.on(name, f) | ||
this.onAbort(() => observable.off(name, f)) | ||
return f | ||
} | ||
} |
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
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
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
Oops, something went wrong.