Skip to content

Commit 979d5bd

Browse files
enabled concurrent support
1 parent 4e31247 commit 979d5bd

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

package/astParser.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ module.exports = elementType => {
3030
});
3131
}
3232
});
33+
34+
/* body will look something like:
35+
[ 0: "_useState"
36+
1: "_useState2"
37+
2: "character"
38+
3: "setCharacter" ]
39+
*/
40+
3341
// Iterate array and determine getter/setters based on pattern
3442
statements.forEach((el, i) => {
3543
if (el.match(/_use/)) hookState[el] = statements[i + 2];

package/linkFiber.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-underscore-dangle */
12
/* eslint-disable func-names */
23
/* eslint-disable no-use-before-define */
34
/* eslint-disable no-param-reassign */
@@ -10,6 +11,7 @@ const { saveState } = require('./masterState');
1011
module.exports = (snap, mode) => {
1112
let fiberRoot = null;
1213
let astHooks;
14+
let concurrent = false; // flag to check if we are in concurrent mode
1315

1416
function sendSnapshot() {
1517
// don't send messages while jumping or while paused
@@ -87,7 +89,6 @@ module.exports = (snap, mode) => {
8789
function createTree(currentFiber, tree = new Tree('root')) {
8890
if (!currentFiber) return tree;
8991

90-
// console.log("currentFiber", currentFiber);
9192

9293
const {
9394
sibling,
@@ -128,21 +129,39 @@ module.exports = (snap, mode) => {
128129
}
129130
// runs when page initially loads
130131
// but skips 1st hook click
131-
function updateSnapShotTree() {
132-
const { current } = fiberRoot;
132+
async function updateSnapShotTree() {
133+
let current;
134+
// if concurrent mode, grab current.child'
135+
if (concurrent) {
136+
// we need a way to wait for current child to populate
137+
const promise = new Promise((resolve, reject) => {
138+
setTimeout(() => resolve(fiberRoot.current.child), 400);
139+
});
140+
141+
current = await promise;
142+
143+
current = fiberRoot.current.child;
144+
} else {
145+
current = fiberRoot.current;
146+
}
147+
133148
snap.tree = createTree(current);
134149
}
135150

136-
return container => {
137-
const {
138-
_reactRootContainer: { _internalRoot },
139-
_reactRootContainer,
140-
} = container;
141-
// only assign internal rootp if it actually exists
142-
fiberRoot = _internalRoot || _reactRootContainer;
143-
151+
return async container => {
152+
if (container._internalRoot) {
153+
fiberRoot = container._internalRoot;
154+
concurrent = true;
155+
} else {
156+
const {
157+
_reactRootContainer: { _internalRoot },
158+
_reactRootContainer,
159+
} = container;
160+
// only assign internal rootp if it actually exists
161+
fiberRoot = _internalRoot || _reactRootContainer;
162+
}
144163

145-
updateSnapShotTree();
164+
await updateSnapShotTree();
146165
// send the initial snapshot once the content script has started up
147166
window.addEventListener('message', ({ data: { action } }) => {
148167
if (action === 'contentScriptStarted') sendSnapshot();

src/extension/contentScript.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ let firstMessage = true;
33
// listening for messages from npm package
44
window.addEventListener('message', msg => { // runs automatically every second
55
// window listener picks up the message it sends, so we should filter
6-
// messages sent by contentscript
6+
// messages sent by contentscrip
7+
78
if (msg.data.action !== 'contentScriptStarted' && firstMessage) {
89
// since contentScript is run everytime a page is refreshed
910
// tell the background script that the tab has reloaded
@@ -21,7 +22,6 @@ window.addEventListener('message', msg => { // runs automatically every second
2122

2223
// listening for messages from the UI
2324
chrome.runtime.onMessage.addListener(request => { // seems to never fire
24-
2525
// send the message to npm package
2626
const { action } = request;
2727
switch (action) {

0 commit comments

Comments
 (0)