Skip to content
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

#7675: Reuse SimpleEventTarget wherever possible #7765

Merged
merged 17 commits into from
Feb 29, 2024
Merged

Conversation

fregante
Copy link
Contributor

@fregante fregante commented Feb 29, 2024

What does this PR do?

This removes the last custom "event listener" implementations

Screenshot 2

Checklist

  • Double-check the behavior by running the extension
  • Designate a primary reviewer: up for grabs for now

Copy link

codecov bot commented Feb 29, 2024

Codecov Report

Attention: Patch coverage is 83.33333% with 6 lines in your changes are missing coverage. Please review.

Project coverage is 72.20%. Comparing base (061ea92) to head (ea26043).

Files Patch % Lines
...fields/schemaFields/widgets/varPopup/useTreeRow.ts 44.44% 5 Missing ⚠️
src/registry/memoryRegistry.ts 90.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7765      +/-   ##
==========================================
- Coverage   72.22%   72.20%   -0.02%     
==========================================
  Files        1270     1270              
  Lines       39735    39681      -54     
  Branches     7369     7365       -4     
==========================================
- Hits        28697    28652      -45     
+ Misses      11038    11029       -9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

*/
// eslint-disable-next-line local-rules/persistBackgroundData -- Functions
const _navigationListeners = new Set<() => void>();
const navigationListeners = new RepeatableAbortController();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also here RepeatableAbortController was useful rather than SimpleEventTarget

listener.onChanged();
}
}
const databaseChangeListeners = new SimpleEventTarget();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This file has two events and two notifyListeners loops. replaced with:

  • databaseChangeListeners
  • MemoryRegistry#onCacheChanged

Feel free to suggest alternative names.

Copy link
Contributor

@twschiller twschiller Feb 29, 2024

Choose a reason for hiding this comment

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

These events correspond to:

  • databaseChangeListeners -> packageRegistryChange (the background package registry in IDB has changed)
  • MemoryRegistry -> change (the memory registry's content may have changed, e.g., because the background package registry in IDB has changed)

I will be cleaning up some of the registry architecture next as part of the platform API work. The MemoryRegistry class is currently tightly coupled to the background API and also the assumption it can enumerate all packages

The likely changes will be:

  • Having RegistryProtocol and EnumerableRegistryProtocol
  • Splitting out MemoryRegistry from its backing data source (either IDB, or lazily fetching)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • databaseChangeListeners -> packageRegistryChange

Renamed ✅

  • MemoryRegistry -> change

This is used as brickRegistry.onCacheChanged.add(callback) right now so it might not be super readable as brickRegistry.change.add(). I'm open to changing it anyway.

Copy link
Contributor

@twschiller twschiller Feb 29, 2024

Choose a reason for hiding this comment

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

Perhaps onChange or changeEvent to make it clear it's an event from the name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

05ed40c

@@ -206,7 +206,7 @@ export abstract class MenuItemStarterBrickABC extends StarterBrickABC<MenuItemSt
* @see MenuItemStarterBrickConfig.dependencies
* @private
*/
private readonly cancelDependencyObservers: Map<UUID, () => void>;
private readonly cancelDependencyObservers = new EventTarget();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here I used a regular EventTarget because:

  • one observer must be fired only for a specific extension.id
  • the listener is removed via once: true
    • it looked like every call was "call and remove from list", so once should match that

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you leave a comment in the code why you're using EventTarget and not the usual SimpleEventTarget?

// Highlight the exact item being hovered, but
// don't highlight it when its children are hovered
li:hover:not(:has(li:hover)) {
background-color: $color-hover;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tested. Drops all the mouseenter/mouseover listeners from useTreeRow.ts

no scrollbar

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By the way, the up/down keys don't work :(

isHMR &&
new ReactRefreshWebpackPlugin({
overlay: false,
}),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unrelated, I'll be tweaking HMR over time. I'm seeing some serious issues (reload loops on most websites)

@fregante fregante marked this pull request as ready for review February 29, 2024 17:19
@twschiller twschiller merged commit cd2e37a into main Feb 29, 2024
17 checks passed
@twschiller twschiller deleted the F/dev/listeners branch February 29, 2024 19:57
@grahamlangford grahamlangford added this to the 1.8.10 milestone Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants