-
-
Save h4ck4life/4097062cde894f57f0c6588c2911ba12 to your computer and use it in GitHub Desktop.
Message Queue - Replay React Native Message Queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import MessageQueue from 'react-native/Libraries/BatchedBridge/MessageQueue.js'; | |
const WHITELIST = ['UIManager']; | |
const NOOP = () => { }; | |
let queue = []; | |
let now = 0; | |
export default { | |
start() { | |
MessageQueue.spy(msg => { | |
if (msg.type === 1) { | |
now = now || new Date().getTime(); | |
queue.push({ | |
...msg, | |
time: new Date().getTime() - now | |
}); | |
now = new Date().getTime(); | |
} | |
}); | |
}, | |
stop() { | |
MessageQueue.spy(false); | |
}, | |
replay: (url = 'http://localhost:3000') => { | |
// Setup replay table | |
let reverseModuleTable = {}; | |
Object.entries(__fbBatchedBridge._remoteModuleTable).forEach(([id, name]) => { | |
reverseModuleTable[name] = parseInt(id, 10); | |
}); | |
const oldGuard = __fbBatchedBridge.__guard; | |
__fbBatchedBridge.__guard = (fn) => { | |
this._inCall++; | |
try { | |
fn(); | |
} catch (error) { | |
console.log(error); | |
} finally { | |
this._inCall--; | |
} | |
} | |
(function run(i) { | |
if (i >= queue.length) { | |
__fbBatchedBridge.__guard = oldGuard; | |
return; | |
} else { | |
let action = queue[i]; | |
let moduleId = reverseModuleTable[action.module] | |
if (moduleId && WHITELIST.indexOf(action.module) !== -1) { | |
if (action.successCbId !== -1) { | |
__fbBatchedBridge._successCallbacks[action.successCbId >>> 1] = NOOP; | |
} | |
__fbBatchedBridge.enqueueNativeCall( | |
moduleId, | |
__fbBatchedBridge._remoteMethodTable[moduleId].indexOf(action.method), | |
action.args | |
); | |
} | |
setTimeout(() => run(i + 1), action.time); | |
} | |
}(0)) | |
}, | |
upload: (url = 'http://localhost:3000') => fetch(url, { | |
headers: { 'Content-Type': 'text/plain' }, | |
method: 'POST', | |
body: `[${queue.map(q => JSON.stringify(q)).join(',\n')}]` | |
}), | |
download: async (url = 'http://localhost:3000') => { | |
let resp = await fetch(url); | |
queue = await resp.json(); | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment