Skip to content

Commit 8735268

Browse files
committed
fixed linkFiber.ts
1 parent ba5445f commit 8735268

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/backend/linkFiber.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,60 @@ function createTree(
336336
}
337337
return tree;
338338
}
339+
340+
/**
341+
* @method linkFiber
342+
* @param snap The current snapshot
343+
* @param mode The current mode (i.e. jumping, time-traveling, or paused)
344+
* @return a function to be invoked by index.js that initiates snapshot monitoring
345+
* linkFiber contains core module functionality, exported as an anonymous function.
346+
*/
347+
export default (snap: Snapshot, mode: Mode): (() => void) => {
348+
// checks for visiblity of document
349+
function onVisibilityChange(): void {
350+
// hidden property = background tab/minimized window
351+
doWork = !document.hidden;
352+
}
353+
return () => {
354+
// react devtools global hook is a global object that was injected by the React Devtools content script, allows access to fiber nodes and react version
355+
const devTools = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
356+
// check if reactDev Tools is installed
357+
if (!devTools) { return; }
358+
window.postMessage({
359+
action: 'devToolsInstalled',
360+
payload: 'devToolsInstalled'
361+
}, '*');
362+
// reactInstance returns an object of the react, 1st element in map
363+
const reactInstance = devTools.renderers.get(1);
364+
// if no React Instance found then target is not a compatible app
365+
if (!reactInstance) { return; }
366+
window.postMessage({
367+
action: 'aReactApp',
368+
payload: 'aReactApp'
369+
}, '*');
370+
371+
const throttledUpdateSnapshot = throttle(() => { updateSnapShotTree(snap, mode); }, 70);
372+
document.addEventListener('visibilitychange', onVisibilityChange);
373+
374+
if (reactInstance && reactInstance.version) {
375+
fiberRoot = devTools.getFiberRoots(1).values().next().value;
376+
// React has inherent methods that are called with react fiber
377+
// we attach new functionality without compromising the original work that onCommitFiberRoot does
378+
const addOneMoreStep = function (original) {
379+
return function (...args) {
380+
// eslint-disable-next-line prefer-destructuring
381+
fiberRoot = args[1];
382+
// this is the additional functionality we added
383+
if (doWork) {
384+
throttledUpdateSnapshot();
385+
}
386+
// after our added work is completed we invoke the original function
387+
return original(...args);
388+
};
389+
};
390+
devTools.onCommitFiberRoot = addOneMoreStep(devTools.onCommitFiberRoot);
391+
392+
throttledUpdateSnapshot(); // only runs on start up
393+
}
394+
};
395+
};

0 commit comments

Comments
 (0)