Skip to content

Instantly share code, notes, and snippets.

@axemclion
Last active February 8, 2023 06:25
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save axemclion/2f47ba4a5e2190eaf32eeb9e80cefcd5 to your computer and use it in GitHub Desktop.
Save axemclion/2f47ba4a5e2190eaf32eeb9e80cefcd5 to your computer and use it in GitHub Desktop.
Message Queue - Replay React Native Message Queue
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