-
Notifications
You must be signed in to change notification settings - Fork 25
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
Changes from 4 commits
4773f22
f26af55
4454d31
050bc2a
2c57126
2b4c652
7a25d43
9bbc210
22c34af
ba80560
49b3906
1849fee
564d3c8
6d52779
05ed40c
528c288
ea26043
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ import { expectContext } from "@/utils/expectContext"; | |
import { type RegistryId } from "@/types/registryTypes"; | ||
import { isInnerDefinitionRegistryId } from "@/types/helpers"; | ||
import { memoizeUntilSettled } from "@/utils/promiseUtils"; | ||
import { SimpleEventTarget } from "@/utils/SimpleEventTarget"; | ||
|
||
type Source = | ||
// From the remote brick registry | ||
|
@@ -45,26 +46,10 @@ export class DoesNotExistError extends Error { | |
} | ||
} | ||
|
||
export type RegistryChangeListener = { | ||
onCacheChanged: () => void; | ||
}; | ||
|
||
type DatabaseChangeListener = { | ||
onChanged: () => void; | ||
}; | ||
|
||
/** | ||
* `backgroundRegistry` database change listeners. | ||
*/ | ||
// TODO: Use SimpleEventTarget instead | ||
// eslint-disable-next-line local-rules/persistBackgroundData -- Functions | ||
const databaseChangeListeners: DatabaseChangeListener[] = []; | ||
|
||
function notifyDatabaseListeners() { | ||
for (const listener of databaseChangeListeners) { | ||
listener.onChanged(); | ||
} | ||
} | ||
const databaseChangeListeners = new SimpleEventTarget(); | ||
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. This file has two events and two notifyListeners loops. replaced with:
Feel free to suggest alternative names. 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. These events correspond to:
I will be cleaning up some of the registry architecture next as part of the platform API work. The The likely changes will be:
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.
Renamed ✅
This is used as 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. Perhaps 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. ✅ 05ed40c |
||
|
||
/** | ||
* Replace IDB with remote packages and notify listeners. | ||
|
@@ -73,7 +58,7 @@ export const syncRemotePackages = memoizeUntilSettled(async () => { | |
expectContext("extension"); | ||
|
||
await backgroundRegistry.syncRemote(); | ||
notifyDatabaseListeners(); | ||
databaseChangeListeners.emit(); | ||
}); | ||
|
||
/** | ||
|
@@ -83,7 +68,7 @@ export const clearPackages = async () => { | |
expectContext("extension"); | ||
|
||
await backgroundRegistry.clear(); | ||
notifyDatabaseListeners(); | ||
databaseChangeListeners.emit(); | ||
}; | ||
|
||
/** | ||
|
@@ -134,17 +119,15 @@ class MemoryRegistry< | |
|
||
private deserialize: ((raw: unknown) => Item) | null; | ||
|
||
private listeners: RegistryChangeListener[] = []; | ||
onCacheChanged = new SimpleEventTarget(); | ||
|
||
constructor(kinds: Kind[], deserialize: ((raw: unknown) => Item) | null) { | ||
this.kinds = new Set(kinds); | ||
this.deserialize = deserialize; | ||
|
||
databaseChangeListeners.push({ | ||
onChanged: () => { | ||
// If database changes, clear the cache to force reloading user-defined bricks | ||
this.clear(); | ||
}, | ||
databaseChangeListeners.add(() => { | ||
// If database changes, clear the cache to force reloading user-defined bricks | ||
this.clear(); | ||
}); | ||
} | ||
|
||
|
@@ -160,28 +143,6 @@ class MemoryRegistry< | |
this.deserialize = deserialize; | ||
} | ||
|
||
/** | ||
* Add a change listener | ||
* @param listener the change listener | ||
*/ | ||
addListener(listener: RegistryChangeListener): void { | ||
this.listeners.push(listener); | ||
} | ||
|
||
/** | ||
* Remove a change listener | ||
* @param listener the change listener | ||
*/ | ||
removeListener(listener: RegistryChangeListener): void { | ||
this.listeners = this.listeners.filter((x) => x !== listener); | ||
} | ||
|
||
private notifyAll() { | ||
for (const listener of this.listeners) { | ||
listener.onCacheChanged(); | ||
} | ||
} | ||
|
||
/** | ||
* Return true if the registry contains the given item | ||
* @param id the registry id | ||
|
@@ -304,7 +265,7 @@ class MemoryRegistry< | |
source: "internal", | ||
notify: false, | ||
}); | ||
this.notifyAll(); | ||
this.onCacheChanged.emit(); | ||
|
||
this._cacheInitialized = true; | ||
|
||
|
@@ -343,7 +304,7 @@ class MemoryRegistry< | |
} | ||
|
||
if (changed && notify) { | ||
this.notifyAll(); | ||
this.onCacheChanged.emit(); | ||
} | ||
} | ||
|
||
|
@@ -371,7 +332,7 @@ class MemoryRegistry< | |
// Need to clear the whole thing, including built-ins. Listeners will often can all() to repopulate the cache. | ||
this._cacheInitialized = false; | ||
this._cache.clear(); | ||
this.notifyAll(); | ||
this.onCacheChanged.emit(); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
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. Here I used a regular EventTarget because:
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. Can you leave a comment in the code why you're using EventTarget and not the usual SimpleEventTarget? |
||
|
||
/** | ||
* True if the extension point has been uninstalled | ||
|
@@ -255,7 +255,6 @@ export abstract class MenuItemStarterBrickABC extends StarterBrickABC<MenuItemSt | |
super(platform, metadata); | ||
this.menus = new Map<UUID, HTMLElement>(); | ||
this.removed = new Set<UUID>(); | ||
this.cancelDependencyObservers = new Map<UUID, () => void>(); | ||
} | ||
|
||
inputSchema: Schema = propertiesToSchema( | ||
|
@@ -353,16 +352,13 @@ export abstract class MenuItemStarterBrickABC extends StarterBrickABC<MenuItemSt | |
} | ||
|
||
for (const extension of extensions) { | ||
const clear = this.cancelDependencyObservers.get(extension.id); | ||
if (clear) { | ||
try { | ||
clear(); | ||
} catch { | ||
console.error("Error cancelling dependency observer"); | ||
} | ||
try { | ||
this.cancelDependencyObservers.dispatchEvent( | ||
new CustomEvent(extension.id), | ||
); | ||
} catch { | ||
console.error("Error cancelling dependency observer"); | ||
} | ||
|
||
this.cancelDependencyObservers.delete(extension.id); | ||
} | ||
} | ||
|
||
|
@@ -784,10 +780,7 @@ export abstract class MenuItemStarterBrickABC extends StarterBrickABC<MenuItemSt | |
const { dependencies = [] } = extension.config; | ||
|
||
// Clean up old observers | ||
if (this.cancelDependencyObservers.has(extension.id)) { | ||
this.cancelDependencyObservers.get(extension.id)(); | ||
this.cancelDependencyObservers.delete(extension.id); | ||
} | ||
this.cancelDependencyObservers.dispatchEvent(new CustomEvent(extension.id)); | ||
|
||
if (dependencies.length > 0) { | ||
const rerun = once(() => { | ||
|
@@ -799,8 +792,8 @@ export abstract class MenuItemStarterBrickABC extends StarterBrickABC<MenuItemSt | |
}); | ||
|
||
const observer = new MutationObserver(rerun); | ||
|
||
const abortController = new AbortController(); | ||
onAbort(abortController, observer); | ||
|
||
let elementCount = 0; | ||
for (const dependency of dependencies) { | ||
|
@@ -828,15 +821,13 @@ export abstract class MenuItemStarterBrickABC extends StarterBrickABC<MenuItemSt | |
`Observing ${elementCount} element(s) for extension: ${extension.id}`, | ||
); | ||
|
||
this.cancelDependencyObservers.set(extension.id, () => { | ||
try { | ||
observer.disconnect(); | ||
} catch (error) { | ||
console.error("Error cancelling mutation observer", error); | ||
} | ||
|
||
abortController.abort(); | ||
}); | ||
this.cancelDependencyObservers.addEventListener( | ||
extension.id, | ||
() => { | ||
abortController.abort(); | ||
}, | ||
{ once: true }, | ||
); | ||
} else { | ||
console.debug(`Extension has no dependencies: ${extension.id}`); | ||
} | ||
|
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.
Also here
RepeatableAbortController
was useful rather thanSimpleEventTarget