* Makes kernels easier for mortals (including me) * @param kernel * @returns {function()}
(kernel)
| 6 | * @returns {function()} |
| 7 | */ |
| 8 | function kernelRunShortcut(kernel) { |
| 9 | let run = function() { |
| 10 | kernel.build.apply(kernel, arguments); |
| 11 | run = function() { |
| 12 | let result = kernel.run.apply(kernel, arguments); |
| 13 | if (kernel.switchingKernels) { |
| 14 | const reasons = kernel.resetSwitchingKernels(); |
| 15 | const newKernel = kernel.onRequestSwitchKernel(reasons, arguments, kernel); |
| 16 | shortcut.kernel = kernel = newKernel; |
| 17 | result = newKernel.run.apply(newKernel, arguments); |
| 18 | } |
| 19 | if (kernel.renderKernels) { |
| 20 | return kernel.renderKernels(); |
| 21 | } else if (kernel.renderOutput) { |
| 22 | return kernel.renderOutput(); |
| 23 | } else { |
| 24 | return result; |
| 25 | } |
| 26 | }; |
| 27 | return run.apply(kernel, arguments); |
| 28 | }; |
| 29 | const shortcut = function() { |
| 30 | return run.apply(kernel, arguments); |
| 31 | }; |
| 32 | /** |
| 33 | * Run kernel in async mode |
| 34 | * @returns {Promise<KernelOutput>} |
| 35 | */ |
| 36 | shortcut.exec = function() { |
| 37 | return new Promise((accept, reject) => { |
| 38 | try { |
| 39 | accept(run.apply(this, arguments)); |
| 40 | } catch (e) { |
| 41 | reject(e); |
| 42 | } |
| 43 | }); |
| 44 | }; |
| 45 | shortcut.replaceKernel = function(replacementKernel) { |
| 46 | kernel = replacementKernel; |
| 47 | bindKernelToShortcut(kernel, shortcut); |
| 48 | }; |
| 49 | |
| 50 | bindKernelToShortcut(kernel, shortcut); |
| 51 | return shortcut; |
| 52 | } |
| 53 | |
| 54 | function bindKernelToShortcut(kernel, shortcut) { |
| 55 | if (shortcut.kernel) { |
no test coverage detected
searching dependent graphs…