Update backend message handlers

This commit is contained in:
toasted-nutbread 2019-12-08 20:53:42 -05:00
parent f287d68624
commit e2c5c16da6

View File

@ -72,17 +72,20 @@ class Backend {
} }
onMessage({action, params}, sender, callback) { onMessage({action, params}, sender, callback) {
const handlers = Backend.messageHandlers; const handler = Backend._messageHandlers.get(action);
if (hasOwn(handlers, action)) { if (typeof handler !== 'function') { return false; }
const handler = handlers[action];
const promise = handler(params, sender); try {
const promise = handler(this, params, sender);
promise.then( promise.then(
(result) => callback({result}), (result) => callback({result}),
(error) => callback({error: errorToJson(error)}) (error) => callback({error: errorToJson(error)})
); );
return true;
} catch (error) {
callback({error: errorToJson(error)});
return false;
} }
return true;
} }
applyOptions() { applyOptions() {
@ -180,28 +183,102 @@ class Backend {
checkLastError() { checkLastError() {
// NOP // NOP
} }
// Message handlers
_onApiOptionsGet({optionsContext}) {
return apiOptionsGet(optionsContext);
}
_onApiOptionsSet({changedOptions, optionsContext, source}) {
return apiOptionsSet(changedOptions, optionsContext, source);
}
_onApiKanjiFind({text, optionsContext}) {
return apiKanjiFind(text, optionsContext);
}
_onApiTermsFind({text, details, optionsContext}) {
return apiTermsFind(text, details, optionsContext);
}
_onApiTextParse({text, optionsContext}) {
return apiTextParse(text, optionsContext);
}
_onApiTextParseMecab({text, optionsContext}) {
return apiTextParseMecab(text, optionsContext);
}
_onApiDefinitionAdd({definition, mode, context, optionsContext}) {
return apiDefinitionAdd(definition, mode, context, optionsContext);
}
_onApiDefinitionsAddable({definitions, modes, optionsContext}) {
return apiDefinitionsAddable(definitions, modes, optionsContext);
}
_onApiNoteView({noteId}) {
return apiNoteView(noteId);
}
_onApiTemplateRender({template, data, dynamic}) {
return apiTemplateRender(template, data, dynamic);
}
_onApiCommandExec({command, params}) {
return apiCommandExec(command, params);
}
_onApiAudioGetUrl({definition, source, optionsContext}) {
return apiAudioGetUrl(definition, source, optionsContext);
}
_onApiScreenshotGet({options}, sender) {
return apiScreenshotGet(options, sender);
}
_onApiForward({action, params}, sender) {
return apiForward(action, params, sender);
}
_onApiFrameInformationGet(params, sender) {
return apiFrameInformationGet(sender);
}
_onApiInjectStylesheet({css}, sender) {
return apiInjectStylesheet(css, sender);
}
_onApiGetEnvironmentInfo() {
return apiGetEnvironmentInfo();
}
_onApiClipboardGet() {
return apiClipboardGet();
}
} }
Backend.messageHandlers = { Backend._messageHandlers = new Map([
optionsGet: ({optionsContext}) => apiOptionsGet(optionsContext), ['optionsGet', (self, ...args) => self._onApiOptionsGet(...args)],
optionsSet: ({changedOptions, optionsContext, source}) => apiOptionsSet(changedOptions, optionsContext, source), ['optionsSet', (self, ...args) => self._onApiOptionsSet(...args)],
kanjiFind: ({text, optionsContext}) => apiKanjiFind(text, optionsContext), ['kanjiFind', (self, ...args) => self._onApiKanjiFind(...args)],
termsFind: ({text, details, optionsContext}) => apiTermsFind(text, details, optionsContext), ['termsFind', (self, ...args) => self._onApiTermsFind(...args)],
textParse: ({text, optionsContext}) => apiTextParse(text, optionsContext), ['textParse', (self, ...args) => self._onApiTextParse(...args)],
textParseMecab: ({text, optionsContext}) => apiTextParseMecab(text, optionsContext), ['textParseMecab', (self, ...args) => self._onApiTextParseMecab(...args)],
definitionAdd: ({definition, mode, context, optionsContext}) => apiDefinitionAdd(definition, mode, context, optionsContext), ['definitionAdd', (self, ...args) => self._onApiDefinitionAdd(...args)],
definitionsAddable: ({definitions, modes, optionsContext}) => apiDefinitionsAddable(definitions, modes, optionsContext), ['definitionsAddable', (self, ...args) => self._onApiDefinitionsAddable(...args)],
noteView: ({noteId}) => apiNoteView(noteId), ['noteView', (self, ...args) => self._onApiNoteView(...args)],
templateRender: ({template, data, dynamic}) => apiTemplateRender(template, data, dynamic), ['templateRender', (self, ...args) => self._onApiTemplateRender(...args)],
commandExec: ({command, params}) => apiCommandExec(command, params), ['commandExec', (self, ...args) => self._onApiCommandExec(...args)],
audioGetUrl: ({definition, source, optionsContext}) => apiAudioGetUrl(definition, source, optionsContext), ['audioGetUrl', (self, ...args) => self._onApiAudioGetUrl(...args)],
screenshotGet: ({options}, sender) => apiScreenshotGet(options, sender), ['screenshotGet', (self, ...args) => self._onApiScreenshotGet(...args)],
forward: ({action, params}, sender) => apiForward(action, params, sender), ['forward', (self, ...args) => self._onApiForward(...args)],
frameInformationGet: (params, sender) => apiFrameInformationGet(sender), ['frameInformationGet', (self, ...args) => self._onApiFrameInformationGet(...args)],
injectStylesheet: ({css}, sender) => apiInjectStylesheet(css, sender), ['injectStylesheet', (self, ...args) => self._onApiInjectStylesheet(...args)],
getEnvironmentInfo: () => apiGetEnvironmentInfo(), ['getEnvironmentInfo', (self, ...args) => self._onApiGetEnvironmentInfo(...args)],
clipboardGet: () => apiClipboardGet() ['clipboardGet', (self, ...args) => self._onApiClipboardGet(...args)]
}; ]);
window.yomichan_backend = new Backend(); window.yomichan_backend = new Backend();
window.yomichan_backend.prepare(); window.yomichan_backend.prepare();