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