Skip to content

Commit 8e6e451

Browse files
committed
Abstract project hydration logic into a client
1 parent d4f7d56 commit 8e6e451

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

src/clients/projectStorage.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const PROJECT_STORAGE_KEY = 'last-closed-session-project-state';
2+
3+
export function dehydrateProject(project) {
4+
localStorage.setItem(PROJECT_STORAGE_KEY, JSON.stringify({
5+
dehydratedAt: Date.now(),
6+
project,
7+
}));
8+
}
9+
10+
export function rehydrateProject() {
11+
const dehydrated = localStorage.getItem(PROJECT_STORAGE_KEY);
12+
localStorage.removeItem(PROJECT_STORAGE_KEY);
13+
if (dehydrated) {
14+
const rehydrated = JSON.parse(dehydrated);
15+
if (Date.now() - rehydrated.dehydratedAt <= 5 * 60 * 1000) {
16+
return rehydrated.project;
17+
}
18+
}
19+
return null;
20+
}

src/components/Workspace.jsx

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
onSignedOut,
1717
startSessionHeartbeat,
1818
} from '../clients/firebase';
19+
import {dehydrateProject, rehydrateProject} from '../clients/projectStorage';
1920

2021
import {
2122
updateProjectSource,
@@ -83,7 +84,6 @@ class Workspace extends React.Component {
8384
let gistId = null;
8485
let snapshotKey = null;
8586
let isExperimental = false;
86-
let rehydratedProject = null;
8787
if (location.search) {
8888
const query = qs.parse(location.search.slice(1));
8989
if (query.gist) {
@@ -94,14 +94,7 @@ class Workspace extends React.Component {
9494
}
9595
isExperimental = Object.keys(query).includes('experimental');
9696
}
97-
const dehydrated = localStorage.getItem('popcode-project-state');
98-
if (dehydrated) {
99-
const rehydrated = JSON.parse(dehydrated);
100-
if (Date.now() - rehydrated.dehydratedAt <= 5 * 60 * 1000) {
101-
rehydratedProject = rehydrated.project;
102-
}
103-
}
104-
localStorage.removeItem('popcode-project-state');
97+
const rehydratedProject = rehydrateProject();
10598
history.replaceState({}, '', location.pathname);
10699
this.props.dispatch(applicationLoaded({
107100
snapshotKey,
@@ -124,11 +117,7 @@ class Workspace extends React.Component {
124117
_handleUnload() {
125118
const {currentProject} = this.props;
126119
if (!isNull(currentProject) && !isPristineProject(currentProject)) {
127-
const dehydrated = JSON.stringify({
128-
dehydratedAt: Date.now(),
129-
project: currentProject,
130-
});
131-
localStorage.setItem('popcode-project-state', dehydrated);
120+
dehydrateProject(currentProject);
132121
}
133122
}
134123

0 commit comments

Comments
 (0)