Skip to content

Commit b35cada

Browse files
authored
Merge pull request #6 from oslabs-beta/rydang/state-record
state is now recorded into snapshot whenever this.setState is called. also snapshot is sent to window
2 parents 1b71b24 + 1d8e7e9 commit b35cada

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ node_modules
22
.DS_Store
33
dist/
44
extension/bundle.js
5+
package/react-time-travel-1.0.1.tgz
6+
testing

package/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const snapShot = [];
2+
3+
window.addEventListener('message', ({ data: { action, payload } }) => {
4+
if (action === 'jumpToSnap') {
5+
console.log('snap received from chrome', payload);
6+
}
7+
});
8+
9+
const linkState = require('./linkState')(snapShot);
10+
11+
module.exports = {
12+
linkState,
13+
};

package/linkState.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// links component state to library
2+
// changes the setState method to also update our snapshot
3+
4+
module.exports = (snapShot) => {
5+
function sendSnapShot() {
6+
const payload = snapShot.map(comp => comp.state);
7+
window.postMessage({
8+
action: 'recordSnap',
9+
payload,
10+
});
11+
}
12+
13+
return (component) => {
14+
snapShot.push(component);
15+
16+
sendSnapShot();
17+
18+
const oldSetState = component.setState.bind(component);
19+
20+
function newSetState(state, callback = () => { }) {
21+
oldSetState(state, () => {
22+
sendSnapShot();
23+
callback();
24+
});
25+
}
26+
27+
component.setState = newSetState;
28+
};
29+
};

package/package-lock.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

package/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-time-travel",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "A library that helps debug React by memorizing the state of components with every render.",
55
"main": "index.js",
66
"repository": {

0 commit comments

Comments
 (0)