Skip to content

Commit

Permalink
fix: fix an issue that cannot display Elenctron app
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton committed Jan 21, 2023
1 parent c1acf88 commit e84e170
Show file tree
Hide file tree
Showing 9 changed files with 314 additions and 159 deletions.
2 changes: 1 addition & 1 deletion src/app/containers/Settings/Workspace/ImportSettings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ImportSettings extends PureComponent {
btnStyle="danger"
onClick={() => {
// Persist data locally
store.persist(data);
store.persist(data); // async

// Refresh
window.location.reload();
Expand Down
2 changes: 1 addition & 1 deletion src/app/containers/Settings/Workspace/RestoreDefaults.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class RestoreDefaults extends PureComponent {
store.state = defaultState;

// Persist data locally
store.persist();
store.persist(); // async

// Refresh
window.location.reload();
Expand Down
4 changes: 2 additions & 2 deletions src/app/containers/Settings/Workspace/Workspace.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ class Workspace extends PureComponent {
this.fileInput.click();
};

handleExport = (event) => {
handleExport = async (event) => {
// https://github.com/mholt/PapaParse/issues/175#issuecomment-201308792
const text = store.getConfig();
const text = await store.getConfig(); // async
const data = new Blob([text], {
type: 'text/plain;charset=utf-8;'
});
Expand Down
4 changes: 2 additions & 2 deletions src/app/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ series([
}

if (settings.error.corruptedWorkspaceSettings) {
const text = store.getConfig();
const text = await store.getConfig(); // async
const url = 'data:text/plain;charset=utf-8,' + encodeURIComponent(text);
const filename = `${settings.name}-${settings.version}.json`;

Expand Down Expand Up @@ -193,7 +193,7 @@ series([
store.state = defaultState;

// Persist data locally
store.persist();
store.persist(); // async
},
onClose
)}
Expand Down
2 changes: 1 addition & 1 deletion src/app/lib/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const signin = ({ token, name, password }) => new Promise((resolve, rejec
config.set('session.name', name);

// Persist data after successful login to prevent debounced update
config.persist();
config.persist(); // async

_authenticated = true;
resolve({ authenticated: true, token: token });
Expand Down
80 changes: 34 additions & 46 deletions src/app/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,33 @@ import ImmutableStore from '../lib/immutable-store';
import log from '../lib/log';
import defaultState from './defaultState';

const store = new ImmutableStore(defaultState);
const cnc = {
version: settings.version,
state: {}
};

let userData = null;
const store = new ImmutableStore(defaultState);

// Check whether the code is running in Electron renderer process
if (isElectron()) {
const electron = window.require('electron');
const path = window.require('path'); // Require the path module within Electron
const app = electron.remote.app;
userData = {
path: path.join(app.getPath('userData'), 'cnc.json')
};
}
// Debouncing enforces that a function not be called again until a certain amount of time (e.g. 100ms) has passed without it being called.
store.on('change', debounce(async (state) => {
await persist({ state: state });
}, 100));

const getConfig = () => {
const getConfig = async () => {
let content = '';

// Check whether the code is running in Electron renderer process
if (isElectron()) {
const fs = window.require('fs'); // Require the fs module within Electron
if (fs.existsSync(userData.path)) {
content = fs.readFileSync(userData.path, 'utf8') || '{}';
}
const electron = window.require('electron');
content = await electron.ipcRenderer.invoke('read-user-data');
} else {
content = localStorage.getItem('cnc') || '{}';
}

return content;
};

const persist = (data) => {
const persist = async (data) => {
const { version, state } = { ...data };

data = {
Expand All @@ -58,8 +54,8 @@ const persist = (data) => {

// Check whether the code is running in Electron renderer process
if (isElectron()) {
const fs = window.require('fs'); // Use window.require to require fs module in Electron
fs.writeFileSync(userData.path, value);
const electron = window.require('electron');
await electron.ipcRenderer.invoke('write-user-data', value);
} else {
localStorage.setItem('cnc', value);
}
Expand Down Expand Up @@ -117,28 +113,6 @@ const normalizeState = (state) => {
return state;
};

const cnc = {
version: settings.version,
state: {}
};

try {
const text = getConfig();
const data = JSON.parse(text);
cnc.version = get(data, 'version', settings.version);
cnc.state = get(data, 'state', {});
} catch (e) {
set(settings, 'error.corruptedWorkspaceSettings', true);
log.error(e);
}

store.state = normalizeState(merge({}, defaultState, cnc.state || {}));

// Debouncing enforces that a function not be called again until a certain amount of time (e.g. 100ms) has passed without it being called.
store.on('change', debounce((state) => {
persist({ state: state });
}, 100));

//
// Migration
//
Expand Down Expand Up @@ -194,11 +168,25 @@ const migrateStore = () => {
}
};

try {
migrateStore();
} catch (err) {
log.error(err);
}
(async () => {
try {
const text = await getConfig();
const data = JSON.parse(text);
cnc.version = get(data, 'version', settings.version);
cnc.state = get(data, 'state', {});
} catch (e) {
set(settings, 'error.corruptedWorkspaceSettings', true);
log.error(e);
}

store.state = normalizeState(merge({}, defaultState, cnc.state || {}));

try {
migrateStore();
} catch (err) {
log.error(err);
}
})();

store.getConfig = getConfig;
store.persist = persist;
Expand Down
Loading

0 comments on commit e84e170

Please sign in to comment.