| 55 | }); |
| 56 | |
| 57 | function installMockBrowserRuntime() { |
| 58 | let hasPendingMessageEvent = false; |
| 59 | let isFiringMessageEvent = false; |
| 60 | let hasPendingDiscreteEvent = false; |
| 61 | let hasPendingContinuousEvent = false; |
| 62 | |
| 63 | let timerIDCounter = 0; |
| 64 | // let timerIDs = new Map(); |
| 65 | |
| 66 | let eventLog = []; |
| 67 | |
| 68 | let currentTime = 0; |
| 69 | |
| 70 | global.performance = { |
| 71 | now() { |
| 72 | return currentTime; |
| 73 | }, |
| 74 | }; |
| 75 | |
| 76 | // Delete node provide setImmediate so we fall through to MessageChannel. |
| 77 | delete global.setImmediate; |
| 78 | |
| 79 | global.setTimeout = (cb, delay) => { |
| 80 | const id = timerIDCounter++; |
| 81 | log(`Set Timer`); |
| 82 | // TODO |
| 83 | return id; |
| 84 | }; |
| 85 | global.clearTimeout = id => { |
| 86 | // TODO |
| 87 | }; |
| 88 | |
| 89 | const port1 = {}; |
| 90 | const port2 = { |
| 91 | postMessage() { |
| 92 | if (hasPendingMessageEvent) { |
| 93 | throw Error('Message event already scheduled'); |
| 94 | } |
| 95 | log('Post Message'); |
| 96 | hasPendingMessageEvent = true; |
| 97 | }, |
| 98 | }; |
| 99 | global.MessageChannel = function MessageChannel() { |
| 100 | this.port1 = port1; |
| 101 | this.port2 = port2; |
| 102 | }; |
| 103 | |
| 104 | function ensureLogIsEmpty() { |
| 105 | if (eventLog.length !== 0) { |
| 106 | throw Error('Log is not empty. Call assertLog before continuing.'); |
| 107 | } |
| 108 | } |
| 109 | function advanceTime(ms) { |
| 110 | currentTime += ms; |
| 111 | } |
| 112 | function resetTime() { |
| 113 | currentTime = 0; |
| 114 | } |