From fe5d9f2379d46857964919e9a3bcb339a3056d13 Mon Sep 17 00:00:00 2001 From: Denis Z Date: Tue, 7 Dec 2021 11:47:37 +0100 Subject: [PATCH] Update api --- package.json | 4 +- src/plugins/plugin-manager.plugin.ts | 6 +- src/providers/events.provider.tsx | 5 +- src/providers/plugins.provider.tsx | 82 +++++++++++++++++++++++++--- yarn.lock | 8 +-- 5 files changed, 84 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 79ecfdfd..1bd093b0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "spotter", - "version": "2.0.0.beta.8", + "version": "2.0.0.beta.9", "description": "🔎 macOS productivity tool to launch everything", "keywords": [ "Spotter", @@ -25,7 +25,7 @@ "@babel/core": "^7.16.0", "@babel/runtime": "^7.16.3", "@react-native-community/eslint-config": "^3.0.1", - "@spotter-app/core": "1.2.54", + "@spotter-app/core": "2.0.0-beta.0", "@types/jest": "^27.0.3", "@types/react": "^17.0.37", "@types/react-native": "^0.66.6", diff --git a/src/plugins/plugin-manager.plugin.ts b/src/plugins/plugin-manager.plugin.ts index 08410eff..b14389a8 100644 --- a/src/plugins/plugin-manager.plugin.ts +++ b/src/plugins/plugin-manager.plugin.ts @@ -31,7 +31,7 @@ const icon = RNFS.MainBundlePath; export class PluginsManager extends SpotterPlugin { async onInit() { - this.registerOptions([{ + this.spotter.registries.options.set([{ icon, title: 'Plugins', tabAction: this.pluginsMenu, @@ -40,7 +40,7 @@ export class PluginsManager extends SpotterPlugin { private async pluginsMenu() { // TODO: display dev plugins as well - const plugins = await this.getPlugins(); + const plugins = await this.spotter.plugins.get(); return plugins.map(p => ({ icon, title: toTitleCase(shortPath(p.path)), @@ -59,7 +59,7 @@ export class PluginsManager extends SpotterPlugin { } private remove(plugin: string) { - this.removePlugin(plugin); + this.spotter.plugins.remove(plugin); return true; } } diff --git a/src/providers/events.provider.tsx b/src/providers/events.provider.tsx index 4e12a2f4..1ba4feef 100644 --- a/src/providers/events.provider.tsx +++ b/src/providers/events.provider.tsx @@ -8,7 +8,7 @@ import { hideOptions, getHistoryPath, sortOptions } from '../helpers'; import { useHistory } from './history.provider'; import { useSpotterState } from './state.provider'; import { usePlugins } from './plugins.provider'; -import { debounceTime, filter, Subscription, tap } from 'rxjs'; +import { Subscription, tap } from 'rxjs'; type Context = { onQuery: (query: string) => Promise, @@ -66,9 +66,6 @@ export const EventsProvider: FC<{}> = (props) => { onQuery(altQuery); }; }), - // debounceTime(500), - // filter(altQuery => !!altQuery.length), - // tap(() => onSubmit()), ).subscribe(), ) }, []); diff --git a/src/providers/plugins.provider.tsx b/src/providers/plugins.provider.tsx index df430592..e51c5145 100644 --- a/src/providers/plugins.provider.tsx +++ b/src/providers/plugins.provider.tsx @@ -146,8 +146,36 @@ export const PluginsProvider: FC<{}> = (props) => { ); } - const registerOptionsCommand = ( - command: (PluginCommand & {type: CommandType.registerOptions}) + const setRegisteredOptionsCommand = ( + command: (PluginCommand & {type: CommandType.setRegisteredOptions}) + ) => { + const nextRegisteredOptions = command.value + .map(o => ({...o, pluginName: command.pluginName})) + .reduce( + (acc: PluginOption[], curr: PluginOption) => { + const needsToBeReplaced = acc.find((o: PluginOption) => + o.title === curr.title && o.pluginName === curr.pluginName, + ); + + if (needsToBeReplaced) { + return acc.map(o => { + if (o.title === curr.title && o.pluginName === curr.pluginName) { + return curr; + } + + return o; + }); + } + + return [...acc, curr]; + }, + registeredOptions$.value.filter(o => o.pluginName !== command.pluginName), + ); + registeredOptions$.next(nextRegisteredOptions); + } + + const patchRegisteredOptionsCommand = ( + command: (PluginCommand & {type: CommandType.patchRegisteredOptions}) ) => { const nextRegisteredOptions = command.value .map(o => ({...o, pluginName: command.pluginName})) @@ -174,8 +202,36 @@ export const PluginsProvider: FC<{}> = (props) => { registeredOptions$.next(nextRegisteredOptions); } - const registerPrefixesCommand = ( - command: (PluginCommand & {type: CommandType.registerPrefixes}) + const setRegisteredPrefixesCommand = ( + command: (PluginCommand & {type: CommandType.setRegisteredPrefixes}) + ) => { + const nextRegisteredPrefixes = command.value + .map(p => ({...p, pluginName: command.pluginName})) + .reduce( + (acc: PluginPrefix[], curr: PluginPrefix) => { + const needsToBeReplaced = acc.find((p: PluginPrefix) => + p.prefix === curr.prefix && p.pluginName === curr.pluginName, + ); + + if (needsToBeReplaced) { + return acc.map(p => { + if (p.prefix === curr.prefix && p.pluginName === curr.pluginName) { + return curr; + } + + return p; + }); + } + + return [...acc, curr]; + }, + registeredPrefixes$.value.filter(p => p.pluginName !== command.pluginName), + ); + registeredPrefixes$.next(nextRegisteredPrefixes); + } + + const patchRegisteredPrefixesCommand = ( + command: (PluginCommand & {type: CommandType.patchRegisteredPrefixes}) ) => { const nextRegisteredPrefixes = command.value .map(p => ({...p, pluginName: command.pluginName})) @@ -318,13 +374,23 @@ export const PluginsProvider: FC<{}> = (props) => { } const handleCommand = async (command: PluginCommand) => { - if (command.type === CommandType.registerOptions) { - registerOptionsCommand(command); + if (command.type === CommandType.setRegisteredOptions) { + setRegisteredOptionsCommand(command); + return; + } + + if (command.type === CommandType.patchRegisteredOptions) { + patchRegisteredOptionsCommand(command); + return; + } + + if (command.type === CommandType.setRegisteredPrefixes) { + setRegisteredPrefixesCommand(command); return; } - if (command.type === CommandType.registerPrefixes) { - registerPrefixesCommand(command); + if (command.type === CommandType.patchRegisteredPrefixes) { + patchRegisteredPrefixesCommand(command); return; } diff --git a/yarn.lock b/yarn.lock index 8107cb36..3f81e43a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1369,10 +1369,10 @@ dependencies: "@sinonjs/commons" "^1.7.0" -"@spotter-app/core@1.2.54": - version "1.2.54" - resolved "https://registry.yarnpkg.com/@spotter-app/core/-/core-1.2.54.tgz#0018001a86e17edfcf305d9d0c3c500073b15808" - integrity sha512-hq9buZqtO8yRkmnP9aoDncuY4c0w6u+XzbOEj9gfU66khu3TmUOs1RZ/vzvLufHTMuthjPZHsmG55pUi6BZ4Hg== +"@spotter-app/core@2.0.0-beta.0": + version "2.0.0-beta.0" + resolved "https://registry.yarnpkg.com/@spotter-app/core/-/core-2.0.0-beta.0.tgz#76692834b93069e6c5d5117d5fe33cf0e1eee1c7" + integrity sha512-0OdhqK1oi9N5H6r9zIabriURoDmq/v7E533zDeuCTeVk1cb9+Wj5rw7N85BDR8kMiG6O6OCSABLUuaMqkC+HXA== "@tootallnate/once@1": version "1.1.2"