-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add plugin store watcher #585
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
Open
jedi-of-the-sea
wants to merge
28
commits into
next
Choose a base branch
from
vue3/add-plugin-store-watcher
base: next
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.
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
b37fde1
feat(core): add plugin store watcher
jedi-of-the-sea 4f33de0
refactor(pins): use pluginStoreWatcher for watching coordinateSource
jedi-of-the-sea 11b075c
refactor(pins): use pluginStoreWatcher for watching coordinateSource
jedi-of-the-sea 7e7aeef
Merge remote-tracking branch 'origin/next' into vue3/add-plugin-store…
jedi-of-the-sea 1fc2774
Merge remote-tracking branch 'origin/next' into vue3/add-plugin-store…
jedi-of-the-sea 18036b0
Merge branch 'next' into vue3/add-plugin-store-watcher
jedi-of-the-sea f528a15
test: accept use of usePluginStoreWatcher in plugins
jedi-of-the-sea be3377b
fix(core): watch coreStore in usePluginWatcher
jedi-of-the-sea c5ed058
test: switch order of plugins for testing plugin store watcher
jedi-of-the-sea 51a48af
Merge branch 'next' into vue3/add-plugin-store-watcher
jedi-of-the-sea 60ab99b
Merge branch 'next' into vue3/add-plugin-store-watcher
jedi-of-the-sea 1bf97fe
Merge branch 'next' into vue3/add-plugin-store-watcher
jedi-of-the-sea 43dc809
Update src/core/composables/usePluginStoreWatcher.ts
jedi-of-the-sea 7c0f3cc
test: restore original test
jedi-of-the-sea a977903
refactor: extract pluginStoreWatcher from core
jedi-of-the-sea 074ec91
fix: delete unnnecessary comments
jedi-of-the-sea bea374c
refactor: access plugin store through coreStore
jedi-of-the-sea 9456b59
refactor: use getPluginStore instead of manually looking through plug…
jedi-of-the-sea e8a36a9
refactor(core): add getter usedPlugins
jedi-of-the-sea 52e24fd
refactor: change pluginIsInstalled to require both plugin ID and load…
jedi-of-the-sea 2aac0a4
refactor: delete unreachable code, leave guard check to prevent type …
jedi-of-the-sea 4a7d352
feat: add warnings when plugin store cannot be found
jedi-of-the-sea ed83242
fix(pins): correct callback parameter types for usePluginStoreWatcher
jedi-of-the-sea f89d83e
fix(reverseGeocoder): correct callback parameter types for usePluginS…
jedi-of-the-sea 9f6d623
test(reverseGeocoder): adjust tests to watcher changes
jedi-of-the-sea fecfba8
refactor(attributions): use pluginStoreWatcher for watching listenToC…
jedi-of-the-sea d5941bb
test: change order of plugins for more testing
jedi-of-the-sea 42571a3
Merge branch 'next' into vue3/add-plugin-store-watcher
jedi-of-the-sea 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,164 @@ | ||
| /* eslint-disable tsdoc/syntax */ | ||
| /** | ||
| * @module \@polar/polar/core/composables/usePluginStoreWatcher | ||
| */ | ||
| /* eslint-enable tsdoc/syntax */ | ||
|
|
||
| import { computed, watch, type ComputedRef, type WatchHandle } from 'vue' | ||
|
|
||
| import type { StoreReference } from '@/core/types' | ||
|
|
||
| import { useCoreStore } from '@/core/stores' | ||
|
|
||
| /** | ||
| * Configuration for a single store reference watcher. | ||
| * @internal | ||
| */ | ||
| interface WatcherConfig { | ||
| callback: (value: unknown) => void | Promise<void> | ||
| handle: WatchHandle | null | ||
| source: StoreReference | ||
| } | ||
|
|
||
| /** | ||
| * Generic composable for watching multiple plugin store references. | ||
| * | ||
| * It watches the list of installed plugins to detect when target plugins | ||
| * are added or removed, and manages the corresponding watchers accordingly. | ||
| * | ||
| * @param sources - Array of plugin store references to watch, or a computed reference to them, or a function returning them | ||
| * @param callback - Function called when any watched plugin store value changes | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * const { setupPlugin, teardownPlugin } = usePluginStoreWatcher( | ||
| * () => configuration.value.coordinateSources || [], | ||
| * (coordinate) => { | ||
| * if (coordinate) { | ||
| * await reverseGeocode(coordinate) | ||
| * } | ||
| * } | ||
| * ) | ||
| * ``` | ||
| * | ||
| * @internal | ||
| */ | ||
| export function usePluginStoreWatcher( | ||
| sources: | ||
| | StoreReference[] | ||
| | ComputedRef<StoreReference[]> | ||
| | (() => StoreReference[]), | ||
| callback: (value: unknown) => void | Promise<void> | ||
| ) { | ||
| const coreStore = useCoreStore() | ||
| const sourcesArray = computed(() => { | ||
| if (typeof sources === 'function') { | ||
| return sources() | ||
| } | ||
| if ('value' in sources) { | ||
| return sources.value | ||
| } | ||
| return sources | ||
| }) | ||
|
|
||
| const watchers: WatcherConfig[] = [] | ||
| let pluginListWatcher: WatchHandle | null = null | ||
| let sourcesWatcher: WatchHandle | null = null | ||
|
|
||
| function setupWatcherForSource(watcherConfig: WatcherConfig) { | ||
| if (watcherConfig.handle !== null) { | ||
| return | ||
| } | ||
|
|
||
| if (!watcherConfig.source.plugin) { | ||
| return | ||
| } | ||
|
|
||
| const store = coreStore.getPluginStore(watcherConfig.source.plugin) | ||
|
|
||
| if (!store) { | ||
| console.warn( | ||
| `usePluginStoreWatcher: "${watcherConfig.source.plugin}" not found. Cannot watch "${watcherConfig.source.key}".` | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| watcherConfig.handle = watch( | ||
| () => store[watcherConfig.source.key], | ||
| watcherConfig.callback | ||
| ) | ||
| } | ||
|
|
||
| function removeWatcherForSource(watcherConfig: WatcherConfig) { | ||
| if (watcherConfig.handle) { | ||
| watcherConfig.handle() | ||
| watcherConfig.handle = null | ||
| } | ||
| } | ||
|
|
||
| function updateWatchersBasedOnInstalledPlugins() { | ||
| const currentSources = sourcesArray.value | ||
|
|
||
| watchers.forEach((watcherConfig, index) => { | ||
| if (!currentSources.some((s) => s === watcherConfig.source)) { | ||
| removeWatcherForSource(watcherConfig) | ||
| watchers.splice(index, 1) | ||
| } | ||
| }) | ||
|
|
||
| currentSources.forEach((source) => { | ||
| let watcherConfig = watchers.find((w) => w.source === source) | ||
|
|
||
| if (!watcherConfig) { | ||
| watcherConfig = { source, callback, handle: null } | ||
| watchers.push(watcherConfig) | ||
| } | ||
|
|
||
| const pluginIsInstalled = | ||
| source.plugin && coreStore.getPluginStore(source.plugin) | ||
|
|
||
| if (pluginIsInstalled && !watcherConfig.handle) { | ||
| setupWatcherForSource(watcherConfig) | ||
| } else if (!pluginIsInstalled && watcherConfig.handle) { | ||
| removeWatcherForSource(watcherConfig) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| function setupPlugin() { | ||
| updateWatchersBasedOnInstalledPlugins() | ||
|
|
||
| sourcesWatcher = watch(sourcesArray, () => { | ||
| updateWatchersBasedOnInstalledPlugins() | ||
| }) | ||
|
|
||
| pluginListWatcher = watch( | ||
| () => coreStore.usedPlugins, | ||
| () => { | ||
| updateWatchersBasedOnInstalledPlugins() | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| function teardownPlugin() { | ||
| watchers.forEach((watcher) => { | ||
| removeWatcherForSource(watcher) | ||
| }) | ||
| watchers.length = 0 | ||
|
|
||
| if (sourcesWatcher) { | ||
| sourcesWatcher() | ||
| sourcesWatcher = null | ||
| } | ||
|
|
||
| if (pluginListWatcher) { | ||
| pluginListWatcher() | ||
| pluginListWatcher = null | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| setupPlugin, | ||
| teardownPlugin, | ||
| } | ||
| } |
This file contains hidden or 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.
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.
There should be some console warnings if something is not working when setting up the watchers.
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.
4a7d352