| 82 | |
| 83 | // This actually loads in the processor – script files & init code |
| 84 | var loadProcessor = function (ready) { |
| 85 | |
| 86 | var failed = false; |
| 87 | |
| 88 | // Overwritten when the script loads |
| 89 | var callback = function () { |
| 90 | if (window.console) { |
| 91 | window.console.warn('Processor is not ready yet - trying again'); |
| 92 | } |
| 93 | failed = true; |
| 94 | return ''; |
| 95 | }; |
| 96 | |
| 97 | // Script has loaded. |
| 98 | // Run any init code, and swap the callback. If we failed, try again. |
| 99 | var scriptCB = function () { |
| 100 | init(function () { |
| 101 | callback = handler; |
| 102 | if (failed) { |
| 103 | renderLivePreview(); |
| 104 | } |
| 105 | ready(); |
| 106 | }); |
| 107 | }; |
| 108 | |
| 109 | if (url) { |
| 110 | // Load the processor's script |
| 111 | getScript(url, scriptCB); |
| 112 | } else { |
| 113 | // No url, go straight on |
| 114 | init(function () { |
| 115 | callback = handler; |
| 116 | ready(); |
| 117 | }); |
| 118 | } |
| 119 | |
| 120 | // Create a proxy function that holds the handler in scope so that, when |
| 121 | // the callbacks are swapped, rendering still works. |
| 122 | var cache = { |
| 123 | source: '', |
| 124 | result: '' |
| 125 | }; |
| 126 | |
| 127 | if (processorData.target) { |
| 128 | if (!jsbin.state.cache) { |
| 129 | jsbin.state.cache = {}; |
| 130 | } |
| 131 | jsbin.state.cache[processorData.target] = cache; |
| 132 | } |
| 133 | var proxyCallback = function (source) { |
| 134 | return new RSVP.Promise(function (resolve, reject) { |
| 135 | source = source.trim(); |
| 136 | if (source === cache.source) { |
| 137 | resolve(cache.result); |
| 138 | } else { |
| 139 | callback(source, function (result) { |
| 140 | cache.source = source; |
| 141 | cache.result = result; |