Skip to content

Commit 24752b5

Browse files
authored
Merge pull request #10 from oslabs-beta/rydang/timejump
library can now receive message to jump back in time
2 parents e983055 + 564112c commit 24752b5

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

package/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
const snapShot = [];
2+
const mode = { jumping: false };
3+
4+
const linkState = require('./linkState')(snapShot, mode);
5+
const timeJump = require('./timeJump')(snapShot, mode);
6+
7+
const getShot = () => snapShot.map(({ component }) => component.state);
28

39
window.addEventListener('message', ({ data: { action, payload } }) => {
410
if (action === 'jumpToSnap') {
5-
console.log('snap received from chrome', payload);
11+
timeJump(payload);
612
}
713
});
814

9-
const linkState = require('./linkState')(snapShot);
10-
1115
module.exports = {
1216
linkState,
17+
timeJump,
18+
getShot,
1319
};

package/linkState.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,46 @@
11
// links component state to library
22
// changes the setState method to also update our snapshot
33

4-
module.exports = (snapShot) => {
4+
module.exports = (snapShot, mode) => {
5+
// send message to window containing component states
6+
// unless library is currently jumping through time
57
function sendSnapShot() {
6-
const payload = snapShot.map(comp => comp.state);
8+
if (mode.jumping) return;
9+
const payload = snapShot.map(({ component }) => component.state);
710
window.postMessage({
811
action: 'recordSnap',
912
payload,
1013
});
1114
}
1215

1316
return (component) => {
14-
snapShot.push(component);
17+
// make a copy of setState
18+
const oldSetState = component.setState.bind(component);
1519

16-
sendSnapShot();
20+
// convert setState to promise
21+
const setStateAsync = (state) => {
22+
return new Promise(resolve => oldSetState(state, resolve));
23+
};
1724

18-
const oldSetState = component.setState.bind(component);
25+
// add component to snapshot
26+
snapShot.push({ component, setStateAsync });
1927

28+
let first = true;
2029
function newSetState(state, callback = () => { }) {
30+
// if setState is being called for the first time, this conditional sends the initial snapshot
31+
if (first) {
32+
first = false;
33+
sendSnapShot();
34+
}
35+
36+
// continue normal setState functionality, except add sending message middleware
2137
oldSetState(state, () => {
2238
sendSnapShot();
2339
callback();
2440
});
2541
}
2642

43+
// replace component's setState so developer doesn't change syntax
2744
component.setState = newSetState;
2845
};
2946
};

package/timeJump.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = (snapShot, mode) => {
2+
return (newSnapShot) => {
3+
mode.jumping = true;
4+
newSnapShot.forEach(async (state, i) => {
5+
await snapShot[i].setStateAsync(state);
6+
});
7+
mode.jumping = false;
8+
};
9+
};

0 commit comments

Comments
 (0)