Skip to content

Commit

Permalink
Update api
Browse files Browse the repository at this point in the history
  • Loading branch information
ziulev committed Dec 7, 2021
1 parent 24948a3 commit fe5d9f2
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 21 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/plugin-manager.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)),
Expand All @@ -59,7 +59,7 @@ export class PluginsManager extends SpotterPlugin {
}

private remove(plugin: string) {
this.removePlugin(plugin);
this.spotter.plugins.remove(plugin);
return true;
}
}
5 changes: 1 addition & 4 deletions src/providers/events.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>,
Expand Down Expand Up @@ -66,9 +66,6 @@ export const EventsProvider: FC<{}> = (props) => {
onQuery(altQuery);
};
}),
// debounceTime(500),
// filter(altQuery => !!altQuery.length),
// tap(() => onSubmit()),
).subscribe(),
)
}, []);
Expand Down
82 changes: 74 additions & 8 deletions src/providers/plugins.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}))
Expand All @@ -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}))
Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit fe5d9f2

Please sign in to comment.