Skip to content

Commit ddf9194

Browse files
authored
Merge pull request #21 from oslabs-beta/rydang/snapshotrefactor
middleware for componentWillUnmount
2 parents 57ed117 + 0736a0c commit ddf9194

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

package/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const mode = { jumping: false };
33

44
const linkState = require('./linkState')(snapShot, mode);
55
const timeJump = require('./timeJump')(snapShot, mode);
6+
const unlinkState = require('./unlinkState')(snapShot);
67

78
const getShot = () => snapShot.map(({ component }) => component.state);
89

@@ -16,6 +17,7 @@ window.addEventListener('message', ({ data: { action, payload } }) => {
1617

1718
module.exports = {
1819
linkState,
20+
unlinkState,
1921
timeJump,
2022
getShot,
2123
};

package/linkState.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// changes the setState method to also update our snapshot
33

44
module.exports = (snapShot, mode) => {
5+
const unlinkState = require('./unlinkState')(snapShot);
6+
57
// send message to window containing component states
68
// unless library is currently jumping through time
79
function sendSnapShot() {
@@ -45,15 +47,13 @@ module.exports = (snapShot, mode) => {
4547
}
4648

4749
function changeComponentWillUnmount(component) {
50+
let oldComponentWillUnmount = () => { };
51+
// if componentWillUnmount is defined, then create copy by value
52+
if (typeof component.componentWillUnmount === 'function') oldComponentWillUnmount = component.componentWillUnmount.bind(component);
53+
// replace componentWillUnmount
4854
component.componentWillUnmount = () => {
49-
let snapShotIndex = snapShot.length;
50-
for (let i = 0; i < snapShot.length; i += 1) {
51-
const { component: comp } = snapShot[i];
52-
if (component === comp) {
53-
snapShotIndex = i;
54-
}
55-
}
56-
snapShot.splice(snapShotIndex, 1);
55+
oldComponentWillUnmount();
56+
unlinkState(component);
5757
};
5858
}
5959

package/unlinkState.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = (snapShot) => {
2+
return (component) => {
3+
let snapShotIndex = snapShot.length;
4+
for (let i = 0; i < snapShot.length; i += 1) {
5+
const { component: comp } = snapShot[i];
6+
if (component === comp) {
7+
snapShotIndex = i;
8+
}
9+
}
10+
snapShot.splice(snapShotIndex, 1);
11+
};
12+
};

0 commit comments

Comments
 (0)