1
0
Fork 0
mirror of https://github.com/anyproto/anytype-ts.git synced 2025-06-08 05:57:02 +09:00

JS-6181: Storage refactoring

This commit is contained in:
Andrew Simachev 2025-01-19 12:54:49 +01:00
parent df1b2c29db
commit 0ef8536304
No known key found for this signature in database
GPG key ID: 1DFE44B21443F0EF
7 changed files with 40 additions and 43 deletions

View file

@ -10,6 +10,8 @@ const remote = require('@electron/remote/main');
const { installNativeMessagingHost } = require('./electron/js/lib/installNativeMessagingHost.js');
const binPath = fixPathForAsarUnpack(path.join(__dirname, 'dist', `anytypeHelper${is.windows ? '.exe' : ''}`));
const Store = require('electron-store');
const suffix = app.isPackaged ? '' : 'dev';
const store = new Store({ name: [ 'localStorage', suffix ].join('-') });
// Fix notifications app name
if (is.windows) {
@ -17,7 +19,7 @@ if (is.windows) {
};
storage.setDataPath(app.getPath('userData'));
Store.initRenderer();
//Store.initRenderer();
const Api = require('./electron/js/api.js');
const ConfigManager = require('./electron/js/config.js');
@ -53,6 +55,16 @@ powerMonitor.on('resume', () => {
Util.log('info', '[PowerMonitor] resume');
});
ipcMain.on('storeGet', (e, key) => {
e.returnValue = store.get(key);
});
ipcMain.on('storeSet', (e, key, value) => {
e.returnValue = store.set(key, value);
});
ipcMain.on('storeDelete', (e, key) => {
e.returnValue = store.delete(key);
});
let deeplinkingUrl = '';
let waitLibraryPromise = null;
let mainWindow = null;

View file

@ -5,9 +5,6 @@ const os = require('os');
const path = require('path');
const mime = require('mime-types');
const tmpPath = () => app.getPath('temp');
const Store = require('electron-store');
const suffix = app.isPackaged ? '' : 'dev';
const store = new Store({ name: [ 'localStorage', suffix ].join('-') });
contextBridge.exposeInMainWorld('Electron', {
version: {
@ -19,9 +16,9 @@ contextBridge.exposeInMainWorld('Electron', {
platform: os.platform(),
arch: process.arch,
storeSet: (key, value) => store.set(key, value),
storeGet: key => store.get(key),
storeDelete: key => store.delete(key),
storeGet: key => ipcRenderer.sendSync('storeGet', key),
storeSet: (key, value) => ipcRenderer.sendSync('storeSet', key, value),
storeDelete: key => ipcRenderer.sendSync('storeDelete', key),
isPackaged: app.isPackaged,
userPath: () => app.getPath('userData'),

View file

@ -82,7 +82,7 @@ const PopupSpaceCreate = observer(forwardRef<{}, I.Popup>(({ param = {}, close }
const ids = [ message.objectId ].concat(U.Menu.getVaultItems().map(it => it.id));
Storage.set('spaceOrder', ids, true);
Storage.set('spaceOrder', ids);
U.Router.switchSpace(message.objectId, '', true, {
onRouteChange: () => {

View file

@ -61,12 +61,13 @@ const Progress: FC = observer(() => {
};
const onDragMove = (e: any) => {
const obj = Storage.get('progress') || {};
const win = $(window);
const x = e.pageX - dx.current - win.scrollLeft();
const y = e.pageY - dy.current - win.scrollTop();
setStyle(x, y);
Storage.set('progress', { x, y }, true);
Storage.set('progress', { ...obj, x, y });
};
const onDragEnd = (e: any) => {

View file

@ -310,7 +310,7 @@ const Vault = observer(class Vault extends React.Component {
let ids = U.Menu.getVaultItems().map(it => it.id);
ids = arrayMove(ids, oldIndex, newIndex);
Storage.set('spaceOrder', ids, true);
Storage.set('spaceOrder', ids);
keyboard.disableSelection(false);
keyboard.setDragging(false);

View file

@ -25,7 +25,7 @@ const Api = {
if (electron.storeGet) {
return electron.storeGet(key);
} else {
localStorage.getItem(key);
return localStorage.getItem(key);
};
},
@ -50,14 +50,14 @@ class Storage {
get (key: string): any {
let o = Api.get(key);
if (!o) {
if (undefined === o) {
o = this.parse(String(localStorage.getItem(key) || ''));
};
if (this.isSpaceKey(key)) {
if (o) {
localStorage.removeItem(key);
this.set(key, o, true);
this.set(key, o);
};
return this.getSpaceKey(key);
@ -65,7 +65,7 @@ class Storage {
if (this.isAccountKey(key)) {
if (o) {
localStorage.removeItem(key);
this.set(key, o, true);
this.set(key, o);
};
return this.getAccountKey(key);
@ -74,7 +74,7 @@ class Storage {
};
};
set (key: string, obj: any, del?: boolean): void {
set (key: string, obj: any): void {
obj = U.Common.objectCopy(obj);
if (!key) {
@ -82,26 +82,13 @@ class Storage {
return;
};
if (del) {
this.delete(key);
};
let o = this.get(key);
if ((typeof o === 'object') && (o !== null)) {
for (const i in obj) {
o[i] = obj[i];
};
} else {
o = obj;
};
if (this.isSpaceKey(key)) {
this.setSpaceKey(key, o);
this.setSpaceKey(key, obj);
} else
if (this.isAccountKey(key)) {
this.setAccountKey(key, o);
this.setAccountKey(key, obj);
} else {
Api.set(key, o);
Api.set(key, obj);
//localStorage.removeItem(key);
};
};
@ -162,7 +149,7 @@ class Storage {
};
setSpace (obj: any) {
this.set('space', obj, true);
this.set('space', obj);
};
deleteSpace (id: string) {
@ -216,7 +203,7 @@ class Storage {
};
setAccount (obj: any) {
this.set('account', obj, true);
this.set('account', obj);
};
deleteAccount (id: string) {
@ -248,7 +235,7 @@ class Storage {
const obj = this.get('lastOpenedObject') || {};
obj[windowId] = Object.assign(obj[windowId] || {}, param);
this.set('lastOpenedObject', obj, true);
this.set('lastOpenedObject', obj);
};
deleteLastOpenedByObjectId (objectIds: string[]) {
@ -276,7 +263,7 @@ class Storage {
const obj = this.get('lastOpenedObject') || {};
windowIds.forEach(windowId => delete(obj[windowId]));
this.set('lastOpenedObject', obj, true);
this.set('lastOpenedObject', obj);
};
getLastOpened (windowId: string) {
@ -299,7 +286,7 @@ class Storage {
list = [ ...new Set(list) ];
obj[rootId] = list;
this.set('toggle', obj, true);
this.set('toggle', obj);
return obj;
};
@ -316,7 +303,7 @@ class Storage {
const obj = this.get('toggle') || {};
delete(obj[rootId]);
this.set('toggle', obj, true);
this.set('toggle', obj);
};
setScroll (key: string, rootId: string, scroll: number, isPopup: boolean) {
@ -327,7 +314,7 @@ class Storage {
obj[key] = obj[key] || {};
obj[key][rootId] = Number(scroll) || 0;
this.set('scroll', obj, true);
this.set('scroll', obj);
} catch (e) { /**/ };
return obj;
};
@ -347,7 +334,7 @@ class Storage {
const obj = this.get('focus') || {};
obj[rootId] = state;
this.set('focus', obj, true);
this.set('focus', obj);
return obj;
};
@ -363,7 +350,7 @@ class Storage {
keys.push(key);
};
this.set('onboarding', keys, true);
this.set('onboarding', keys);
return keys;
};
@ -393,7 +380,7 @@ class Storage {
setSurvey (type: I.SurveyType, param: any) {
const obj = this.get('survey') || {};
obj[type] = Object.assign(obj[type] || {}, param);
this.set('survey', obj, true);
this.set('survey', obj);
};
initPinnedTypes () {
@ -445,7 +432,7 @@ class Storage {
setPinnedTypes (list: string[]) {
list = list.slice(0, 50);
this.set('pinnedTypes', this.checkArray([ ...new Set(list) ]), true);
this.set('pinnedTypes', this.checkArray([ ...new Set(list) ]));
};
getPinnedTypes () {

View file

@ -67,7 +67,7 @@ class AuthStore {
};
networkConfigSet (obj: NetworkConfig) {
Storage.set('networkConfig', obj, true);
Storage.set('networkConfig', obj);
};
appKeySet (v: string) {