Skip to content

Instantly share code, notes, and snippets.

@axemclion
Last active February 8, 2023 06:25

Revisions

  1. axemclion revised this gist Sep 10, 2017. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions MessageQueueSpy.js
    Original file line number Diff line number Diff line change
    @@ -28,8 +28,22 @@ export default {
    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];
  2. axemclion revised this gist Sep 7, 2017. 1 changed file with 15 additions and 16 deletions.
    31 changes: 15 additions & 16 deletions MessageQueueSpy.js
    Original file line number Diff line number Diff line change
    @@ -10,38 +10,29 @@ export default {
    MessageQueue.spy(msg => {
    if (msg.type === 1) {
    now = now || new Date().getTime();
    queue.push(JSON.stringify({
    queue.push({
    ...msg,
    time: new Date().getTime() - now
    }));
    });
    now = new Date().getTime();
    }
    });
    },
    stop() {
    MessageQueue.spy(false);
    },
    upload: (url = 'http://localhost:3000') => fetch(url, {
    headers: { 'Content-Type': 'text/plain' },
    method: 'POST',
    body: `[${queue.join(',\n')}]`
    }),
    replay: async (url = 'http://localhost:3000') => {
    let resp = await fetch(url);
    let actions = await resp.json();

    replay: (url = 'http://localhost:3000') => {
    // Setup replay table
    let reverseModuleTable = {};
    Object.entries(__fbBatchedBridge._remoteModuleTable).forEach(([id, name]) => {
    reverseModuleTable[name] = parseInt(id, 10);
    });


    (function run(i) {
    if (i >= actions.length) {
    if (i >= queue.length) {
    return;
    } else {
    let action = actions[i];
    let action = queue[i];
    let moduleId = reverseModuleTable[action.module]
    if (moduleId && WHITELIST.indexOf(action.module) !== -1) {

    @@ -58,6 +49,14 @@ export default {
    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();
    },
    }
  3. axemclion created this gist Sep 7, 2017.
    63 changes: 63 additions & 0 deletions MessageQueueSpy.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    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(JSON.stringify({
    ...msg,
    time: new Date().getTime() - now
    }));
    now = new Date().getTime();
    }
    });
    },
    stop() {
    MessageQueue.spy(false);
    },
    upload: (url = 'http://localhost:3000') => fetch(url, {
    headers: { 'Content-Type': 'text/plain' },
    method: 'POST',
    body: `[${queue.join(',\n')}]`
    }),
    replay: async (url = 'http://localhost:3000') => {
    let resp = await fetch(url);
    let actions = await resp.json();

    // Setup replay table
    let reverseModuleTable = {};
    Object.entries(__fbBatchedBridge._remoteModuleTable).forEach(([id, name]) => {
    reverseModuleTable[name] = parseInt(id, 10);
    });


    (function run(i) {
    if (i >= actions.length) {
    return;
    } else {
    let action = actions[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))

    }
    }