|
17 | 17 | } |
18 | 18 |
|
19 | 19 | let SandBox = window.SandBox = class { |
20 | | - constructor(options, callback) { |
21 | | - this.callbacks = {} |
| 20 | + static create(options) { |
| 21 | + return new Promise(done => { |
| 22 | + let frame = document.createElement("iframe") |
| 23 | + frame.addEventListener("load", loaded) |
| 24 | + frame.src = "empty.html" |
| 25 | + if (options.place) { |
| 26 | + options.place(frame) |
| 27 | + } else { |
| 28 | + frame.style.display = "none" |
| 29 | + document.body.appendChild(frame) |
| 30 | + } |
| 31 | + |
| 32 | + function loaded() { |
| 33 | + frame.removeEventListener("load", loaded) |
| 34 | + let box = new SandBox(options, frame) |
22 | 35 |
|
| 36 | + promiseChain((options.loadFiles || []).map(file => () => { |
| 37 | + let script = box.win.document.createElement("script") |
| 38 | + script.src = file |
| 39 | + box.win.document.body.appendChild(script) |
| 40 | + return new Promise(done => script.addEventListener("load", done)) |
| 41 | + })).then(() => done(box)) |
| 42 | + } |
| 43 | + }) |
| 44 | + } |
| 45 | + |
| 46 | + constructor(options, frame) { |
| 47 | + this.startedAt = null |
| 48 | + this.extraSecs = 2 |
| 49 | + this.output = null |
| 50 | + |
| 51 | + this.callbacks = {} |
23 | 52 | // Used to cancel existing events when new code is loaded |
24 | | - this.timeouts = []; this.intervals = []; this.frames = []; this.framePos = 0 |
| 53 | + this.timeouts = []; this.intervals = []; this.frames = [] |
| 54 | + this.framePos = 0 |
25 | 55 |
|
| 56 | + // Loaded CommonJS modules |
26 | 57 | this.loaded = new Cached(name => resolved.compute(name).then(({name, code}) => this.evalModule(name, code))) |
27 | 58 |
|
28 | | - const loaded = () => { |
29 | | - frame.removeEventListener("load", loaded) |
30 | | - this.win = frame.contentWindow |
31 | | - this.setupEnv() |
| 59 | + this.frame = frame |
| 60 | + this.win = frame.contentWindow |
| 61 | + this.setupEnv() |
32 | 62 |
|
33 | | - const resize = () => { |
34 | | - if (this.frame.style.display != "none") this.resizeFrame() |
35 | | - } |
36 | | - this.frame.addEventListener("load", resize) |
37 | | - let resizeTimeout = null |
38 | | - const scheduleResize = () => { |
39 | | - this.win.clearTimeout(resizeTimeout) |
40 | | - this.win.__setTimeout(resize, 200) |
41 | | - } |
42 | | - this.win.addEventListener("keydown", scheduleResize) |
43 | | - this.win.addEventListener("mousedown", scheduleResize) |
44 | | - |
45 | | - if (options.loadFiles) { |
46 | | - let i = 0 |
47 | | - let loadNext = () => { |
48 | | - if (i == options.loadFiles.length) return callback(this) |
49 | | - let script = this.win.document.createElement("script") |
50 | | - script.src = options.loadFiles[i] |
51 | | - this.win.document.body.appendChild(script) |
52 | | - ++i |
53 | | - script.addEventListener("load", loadNext) |
54 | | - } |
55 | | - loadNext() |
56 | | - } else { |
57 | | - callback(this) |
58 | | - } |
| 63 | + const resize = () => { |
| 64 | + if (this.frame.style.display != "none") this.resizeFrame() |
59 | 65 | } |
60 | | - |
61 | | - let frame = this.frame = document.createElement("iframe") |
62 | | - frame.addEventListener("load", loaded) |
63 | | - frame.src = "empty.html" |
64 | | - if (options.place) { |
65 | | - options.place(frame) |
66 | | - } else { |
67 | | - frame.style.display = "none" |
68 | | - document.body.appendChild(frame) |
| 66 | + this.frame.addEventListener("load", resize) |
| 67 | + let resizeTimeout = null |
| 68 | + const scheduleResize = () => { |
| 69 | + this.win.clearTimeout(resizeTimeout) |
| 70 | + this.win.__setTimeout(resize, 200) |
69 | 71 | } |
70 | | - |
71 | | - this.startedAt = null |
72 | | - this.extraSecs = 2 |
73 | | - this.output = null |
| 72 | + this.win.addEventListener("keydown", scheduleResize) |
| 73 | + this.win.addEventListener("mousedown", scheduleResize) |
74 | 74 | } |
75 | 75 |
|
76 | 76 | run(code, output) { |
|
531 | 531 | wrap.appendChild(document.createTextNode(type == "array" ? "]" : "}")) |
532 | 532 | node.parentNode.replaceChild(wrap, node) |
533 | 533 | } |
| 534 | + |
| 535 | + function promiseChain(thunks) { |
| 536 | + function proceed(i, value) { |
| 537 | + if (i == thunks.length) return Promise.resolve(value) |
| 538 | + else return thunks[i](value).then(value => proceed(i + 1, value)) |
| 539 | + } |
| 540 | + return proceed(0, null) |
| 541 | + } |
534 | 542 | })() |
0 commit comments