refactor: plugin.tsの型を修正する (#10027)
* refactor:plugin内のすべての関数にreturn typeを設定 * fix:pluginContextsから取得できない場合は早期リターンする * fix:valueがstring以外の時は早期リターンする * fix:valueが取得できる時以外は早期リターンする * fix:pluginに型をつける * fix:絞り込みをassertStringに変更 * fix:修正漏れ * fix:valToJsからvalueを取得するように変更
This commit is contained in:
parent
c34e7e6e08
commit
830fabef12
|
@ -1,12 +1,12 @@
|
||||||
import { Interpreter, Parser, utils, values } from '@syuilo/aiscript';
|
import { Interpreter, Parser, utils, values } from '@syuilo/aiscript';
|
||||||
import { createAiScriptEnv } from '@/scripts/aiscript/api';
|
import { createAiScriptEnv } from '@/scripts/aiscript/api';
|
||||||
import { inputText } from '@/os';
|
import { inputText } from '@/os';
|
||||||
import { noteActions, notePostInterruptors, noteViewInterruptors, postFormActions, userActions } from '@/store';
|
import { Plugin, noteActions, notePostInterruptors, noteViewInterruptors, postFormActions, userActions } from '@/store';
|
||||||
|
|
||||||
const parser = new Parser();
|
const parser = new Parser();
|
||||||
const pluginContexts = new Map<string, Interpreter>();
|
const pluginContexts = new Map<string, Interpreter>();
|
||||||
|
|
||||||
export function install(plugin) {
|
export function install(plugin: Plugin): void {
|
||||||
// 後方互換性のため
|
// 後方互換性のため
|
||||||
if (plugin.src == null) return;
|
if (plugin.src == null) return;
|
||||||
console.info('Plugin installed:', plugin.name, 'v' + plugin.version);
|
console.info('Plugin installed:', plugin.name, 'v' + plugin.version);
|
||||||
|
@ -15,7 +15,7 @@ export function install(plugin) {
|
||||||
plugin: plugin,
|
plugin: plugin,
|
||||||
storageKey: 'plugins:' + plugin.id,
|
storageKey: 'plugins:' + plugin.id,
|
||||||
}), {
|
}), {
|
||||||
in: (q) => {
|
in: (q): Promise<string> => {
|
||||||
return new Promise(ok => {
|
return new Promise(ok => {
|
||||||
inputText({
|
inputText({
|
||||||
title: q,
|
title: q,
|
||||||
|
@ -28,10 +28,10 @@ export function install(plugin) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
out: (value) => {
|
out: (value): void => {
|
||||||
console.log(value);
|
console.log(value);
|
||||||
},
|
},
|
||||||
log: (type, params) => {
|
log: (): void => {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -40,9 +40,9 @@ export function install(plugin) {
|
||||||
aiscript.exec(parser.parse(plugin.src));
|
aiscript.exec(parser.parse(plugin.src));
|
||||||
}
|
}
|
||||||
|
|
||||||
function createPluginEnv(opts) {
|
function createPluginEnv(opts: { plugin: Plugin; storageKey: string }): Record<string, values.Value> {
|
||||||
const config = new Map();
|
const config = new Map<string, values.Value>();
|
||||||
for (const [k, v] of Object.entries(opts.plugin.config || {})) {
|
for (const [k, v] of Object.entries(opts.plugin.config ?? {})) {
|
||||||
config.set(k, utils.jsToVal(typeof opts.plugin.configData[k] !== 'undefined' ? opts.plugin.configData[k] : v.default));
|
config.set(k, utils.jsToVal(typeof opts.plugin.configData[k] !== 'undefined' ? opts.plugin.configData[k] : v.default));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,22 +50,28 @@ function createPluginEnv(opts) {
|
||||||
...createAiScriptEnv({ ...opts, token: opts.plugin.token }),
|
...createAiScriptEnv({ ...opts, token: opts.plugin.token }),
|
||||||
//#region Deprecated
|
//#region Deprecated
|
||||||
'Mk:register_post_form_action': values.FN_NATIVE(([title, handler]) => {
|
'Mk:register_post_form_action': values.FN_NATIVE(([title, handler]) => {
|
||||||
|
utils.assertString(title);
|
||||||
registerPostFormAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
registerPostFormAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
||||||
}),
|
}),
|
||||||
'Mk:register_user_action': values.FN_NATIVE(([title, handler]) => {
|
'Mk:register_user_action': values.FN_NATIVE(([title, handler]) => {
|
||||||
|
utils.assertString(title);
|
||||||
registerUserAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
registerUserAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
||||||
}),
|
}),
|
||||||
'Mk:register_note_action': values.FN_NATIVE(([title, handler]) => {
|
'Mk:register_note_action': values.FN_NATIVE(([title, handler]) => {
|
||||||
|
utils.assertString(title);
|
||||||
registerNoteAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
registerNoteAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
||||||
}),
|
}),
|
||||||
//#endregion
|
//#endregion
|
||||||
'Plugin:register_post_form_action': values.FN_NATIVE(([title, handler]) => {
|
'Plugin:register_post_form_action': values.FN_NATIVE(([title, handler]) => {
|
||||||
|
utils.assertString(title);
|
||||||
registerPostFormAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
registerPostFormAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
||||||
}),
|
}),
|
||||||
'Plugin:register_user_action': values.FN_NATIVE(([title, handler]) => {
|
'Plugin:register_user_action': values.FN_NATIVE(([title, handler]) => {
|
||||||
|
utils.assertString(title);
|
||||||
registerUserAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
registerUserAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
||||||
}),
|
}),
|
||||||
'Plugin:register_note_action': values.FN_NATIVE(([title, handler]) => {
|
'Plugin:register_note_action': values.FN_NATIVE(([title, handler]) => {
|
||||||
|
utils.assertString(title);
|
||||||
registerNoteAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
registerNoteAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
||||||
}),
|
}),
|
||||||
'Plugin:register_note_view_interruptor': values.FN_NATIVE(([handler]) => {
|
'Plugin:register_note_view_interruptor': values.FN_NATIVE(([handler]) => {
|
||||||
|
@ -75,54 +81,78 @@ function createPluginEnv(opts) {
|
||||||
registerNotePostInterruptor({ pluginId: opts.plugin.id, handler });
|
registerNotePostInterruptor({ pluginId: opts.plugin.id, handler });
|
||||||
}),
|
}),
|
||||||
'Plugin:open_url': values.FN_NATIVE(([url]) => {
|
'Plugin:open_url': values.FN_NATIVE(([url]) => {
|
||||||
|
utils.assertString(url);
|
||||||
window.open(url.value, '_blank');
|
window.open(url.value, '_blank');
|
||||||
}),
|
}),
|
||||||
'Plugin:config': values.OBJ(config),
|
'Plugin:config': values.OBJ(config),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function initPlugin({ plugin, aiscript }) {
|
function initPlugin({ plugin, aiscript }): void {
|
||||||
pluginContexts.set(plugin.id, aiscript);
|
pluginContexts.set(plugin.id, aiscript);
|
||||||
}
|
}
|
||||||
|
|
||||||
function registerPostFormAction({ pluginId, title, handler }) {
|
function registerPostFormAction({ pluginId, title, handler }): void {
|
||||||
postFormActions.push({
|
postFormActions.push({
|
||||||
title, handler: (form, update) => {
|
title, handler: (form, update) => {
|
||||||
pluginContexts.get(pluginId).execFn(handler, [utils.jsToVal(form), values.FN_NATIVE(([key, value]) => {
|
const pluginContext = pluginContexts.get(pluginId);
|
||||||
update(key.value, value.value);
|
if (!pluginContext) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pluginContext.execFn(handler, [utils.jsToVal(form), values.FN_NATIVE(([key, value]) => {
|
||||||
|
if (!key || !value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
update(utils.valToJs(key), utils.valToJs(value));
|
||||||
})]);
|
})]);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function registerUserAction({ pluginId, title, handler }) {
|
function registerUserAction({ pluginId, title, handler }): void {
|
||||||
userActions.push({
|
userActions.push({
|
||||||
title, handler: (user) => {
|
title, handler: (user) => {
|
||||||
pluginContexts.get(pluginId).execFn(handler, [utils.jsToVal(user)]);
|
const pluginContext = pluginContexts.get(pluginId);
|
||||||
|
if (!pluginContext) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pluginContext.execFn(handler, [utils.jsToVal(user)]);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function registerNoteAction({ pluginId, title, handler }) {
|
function registerNoteAction({ pluginId, title, handler }): void {
|
||||||
noteActions.push({
|
noteActions.push({
|
||||||
title, handler: (note) => {
|
title, handler: (note) => {
|
||||||
pluginContexts.get(pluginId).execFn(handler, [utils.jsToVal(note)]);
|
const pluginContext = pluginContexts.get(pluginId);
|
||||||
|
if (!pluginContext) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pluginContext.execFn(handler, [utils.jsToVal(note)]);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function registerNoteViewInterruptor({ pluginId, handler }) {
|
function registerNoteViewInterruptor({ pluginId, handler }): void {
|
||||||
noteViewInterruptors.push({
|
noteViewInterruptors.push({
|
||||||
handler: async (note) => {
|
handler: async (note) => {
|
||||||
return utils.valToJs(await pluginContexts.get(pluginId).execFn(handler, [utils.jsToVal(note)]));
|
const pluginContext = pluginContexts.get(pluginId);
|
||||||
|
if (!pluginContext) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return utils.valToJs(await pluginContext.execFn(handler, [utils.jsToVal(note)]));
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function registerNotePostInterruptor({ pluginId, handler }) {
|
function registerNotePostInterruptor({ pluginId, handler }): void {
|
||||||
notePostInterruptors.push({
|
notePostInterruptors.push({
|
||||||
handler: async (note) => {
|
handler: async (note) => {
|
||||||
return utils.valToJs(await pluginContexts.get(pluginId).execFn(handler, [utils.jsToVal(note)]));
|
const pluginContext = pluginContexts.get(pluginId);
|
||||||
|
if (!pluginContext) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return utils.valToJs(await pluginContext.execFn(handler, [utils.jsToVal(note)]));
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -287,12 +287,15 @@ export const defaultStore = markRaw(new Storage('base', {
|
||||||
|
|
||||||
const PREFIX = 'miux:' as const;
|
const PREFIX = 'miux:' as const;
|
||||||
|
|
||||||
type Plugin = {
|
export type Plugin = {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
active: boolean;
|
active: boolean;
|
||||||
|
config?: Record<string, { default: any }>;
|
||||||
configData: Record<string, any>;
|
configData: Record<string, any>;
|
||||||
token: string;
|
token: string;
|
||||||
|
src: string | null;
|
||||||
|
version: string;
|
||||||
ast: any[];
|
ast: any[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue