Skip to content

Deinit native event listeners in features using AbortSignal - #5523

Merged
cheap-glitch merged 7 commits into
mainfrom
light-the-bat-signal
May 6, 2022
Merged

cheap-glitch merged 7 commits into
mainfrom
light-the-bat-signal

Conversation

@cheap-glitch

@cheap-glitch cheap-glitch commented Mar 18, 2022 •

Copy link
Copy Markdown
Contributor

Follow-up of #5422

Let's do fregante/delegate-it#26 while we're at it 🔥

@cheap-glitch cheap-glitch added the meta Related to Refined GitHub itself label Mar 18, 2022
...onPrMergePanelOpen(updateCommitTitle),
function init(signal: AbortSignal): Deinit {
subscriptions.push(
onPrMergePanelOpen(updateCommitTitle, signal),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't work because the signal is managed by the global manager but this array is local and we must be able to deinit it here.

@fregante fregante May 3, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have 2 quick solutions here:

  • drop the changes made to onPrMergePanelOpen and to this file
  • create a local abortcontroller so that onPrMergePanelOpen can keep its improvement

For the latter, probably:

function init() {
	// Local controller needed because the feature can be deinited locally
	const controller = new AbortControler()
	
	subscriptions.push(
		onPrMergePanelOpen(updateCommitTitle, signal),
		() => controller.abort(),
		etc

Other, more involved solutions, that should be done separately:

  • (in this feature) only use a local controller and have deinit call that. init would return controller.abort
  • add an API that allows a feature to deinit itself on demand, like:
     function deinit() {
     	features.deinit(import.meta.url)
     }
    This would free us from having to deal with the local listeners/subscriptions arrays

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't work because the signal is managed by the global manager but this array is local and we must be able to deinit it here.

This was actually rendered moot by #5549 😅
The native listener was dropped, so now it just returns a delegate subscription.

add an API that allows a feature to deinit itself on demand

I like that idea, as long as we reserve it for special or tricky cases.

@cheap-glitch cheap-glitch changed the title Deinit event listeners in features using AbortSignal Deinit native event listeners in features using AbortSignal May 4, 2022
@cheap-glitch
cheap-glitch marked this pull request as ready for review May 4, 2022 15:26
@cheap-glitch

Copy link
Copy Markdown
Contributor Author

Let's merge this when it passes review, we can include fregante/delegate-it#26 later when it's done.

@cheap-glitch
cheap-glitch merged commit 976426b into main May 6, 2022
@cheap-glitch
cheap-glitch deleted the light-the-bat-signal branch May 6, 2022 10:29
Comment thread source/features/index.tsx
}

function setupDeinit(deinit: Deinit): void {
document.addEventListener('pjax:start', getDeinitHandler(deinit), {once: true});

@fregante fregante Jun 24, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setupPageLoad already attaches an ajaxed event listener, so we should change this function to something like:

 function onPageUnload(signal, deinit: Deinit): void {
   	signal.addEventListener('abort', getDeinitHandler(deinit));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

meta Related to Refined GitHub itself

Development

Successfully merging this pull request may close these issues.

2 participants