Last active
February 8, 2023 06:25
-
-
Save axemclion/2f47ba4a5e2190eaf32eeb9e80cefcd5 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