From 9244e8f58ecb72b0339fed28504eef16583077db Mon Sep 17 00:00:00 2001 From: Roman Date: Sun, 5 Jul 2020 22:45:39 +0300 Subject: [PATCH] user-store: more fixes 2 Signed-off-by: Roman --- src/common/user-store.ts | 43 ++++++++++++------- src/migrations/migration-wrapper.ts | 4 +- src/renderer/_vue/components/WhatsNewPage.vue | 8 +++- src/renderer/_vue/index.js | 28 ++++++------ src/renderer/_vue/router/routeguard/index.js | 22 ++-------- 5 files changed, 53 insertions(+), 52 deletions(-) diff --git a/src/common/user-store.ts b/src/common/user-store.ts index 95df7edf47..09f3b765ea 100644 --- a/src/common/user-store.ts +++ b/src/common/user-store.ts @@ -1,6 +1,6 @@ import path from "path" import { app, remote } from "electron" -import { computed, observable, reaction, toJS } from "mobx"; +import { observable, reaction, toJS } from "mobx"; import Config from "conf" import semver from "semver" import migrations from "../migrations/user-store" @@ -40,31 +40,36 @@ export class UserStore extends Singleton { return path.basename(this.storeConfig.path); } - @computed get hasNewAppVersion() { + get hasNewAppVersion() { return semver.gt(getAppVersion(), this.lastSeenAppVersion); } + get storeModel() { + const storeModel = { ...this.storeConfig.store }; + Reflect.deleteProperty(storeModel, "__internal__"); // fixme: avoid "external-internals" + return storeModel; + } + + saveLastSeenAppVersion() { + this.lastSeenAppVersion = getAppVersion(); + } + private constructor() { super(); this.init(); } - async init() { - /*await*/ this.load(); + protected async init() { + await this.load(); this.bindEvents(); this.isReady = true; } - saveLastSeenAppVersion() { - this.lastSeenAppVersion = getAppVersion(); - } - - // todo: use "conf" as pseudo-async for more future-proof usages protected async load() { this.storeConfig = new Config({ configName: "lens-user-store", migrations: migrations, - cwd: (app || remote.app).getPath("userData"), + cwd: (app || remote.app).getPath("userData"), // todo: move to main process + with ipc.invoke watch: true, // enable onDidChange()-callback }); const data = this.storeConfig.store; @@ -76,16 +81,22 @@ export class UserStore extends Singleton { // refresh from file-system updates this.storeConfig.onDidAnyChange((data, oldValue) => { if (!isEqual(this.toJSON(), data)) { - console.info(`[STORE]: [UPDATE] ${this.name} from file-system`, { data, oldValue }); + console.info(`[STORE]: [UPDATE] from ${this.name}`, { data, oldValue }); this.fromStore(data); } }); // refresh config file from runtime - reaction(() => this.toJSON(), model => { - if (!isEqual(this.storeConfig.store, model)) { - console.info(`[STORE]: [UPDATE] ${this.name} to file-system from runtime model`, model); - this.storeConfig.store = model; // fixme: actual save to fs won't happen + reaction(() => this.toJSON(), (model: UserStoreModel) => { + if (!isEqual(this.storeModel, model)) { + console.info(`[STORE]: [SAVE] ${this.name} from runtime update`, { + data: model, + oldValue: this.storeModel + }); + // fixme: https://github.com/sindresorhus/conf/issues/114 + Object.entries(model).forEach(([key, value]) => { + this.storeConfig.set(key, value); + }); } }); @@ -114,6 +125,8 @@ export class UserStore extends Singleton { lastSeenAppVersion: this.lastSeenAppVersion, seenContexts: Array.from(this.seenContexts), preferences: this.preferences, + }, { + recurseEverything: true, }) } } diff --git a/src/migrations/migration-wrapper.ts b/src/migrations/migration-wrapper.ts index c1527ed586..39015b81fb 100644 --- a/src/migrations/migration-wrapper.ts +++ b/src/migrations/migration-wrapper.ts @@ -1,4 +1,3 @@ -import path from "path"; import Config from "conf"; import { isTestEnv } from "../common/vars"; @@ -15,8 +14,7 @@ function infoLog(...args: any[]) { export function migration({ version, run }: MigrationOpts) { return { [version]: (storeConfig: Config) => { - const storeName = path.dirname(storeConfig.path); - infoLog(`STORE MIGRATION (${storeName}): ${version}`,); + infoLog(`STORE MIGRATION (${storeConfig.path}): ${version}`,); run(storeConfig, infoLog); } }; diff --git a/src/renderer/_vue/components/WhatsNewPage.vue b/src/renderer/_vue/components/WhatsNewPage.vue index 2823c9a25a..5ae3df00a0 100644 --- a/src/renderer/_vue/components/WhatsNewPage.vue +++ b/src/renderer/_vue/components/WhatsNewPage.vue @@ -30,6 +30,8 @@ import marked from 'marked' import {readFileSync} from 'fs' import { getStaticPath } from "../../../common/register-static" +import { userStore } from "../../../common/user-store" +import { tracker } from "../../../common/tracker" export default { name: 'WhatsNewPage', @@ -46,7 +48,11 @@ export default { }, methods: { toLanding: async function() { - await this.$store.dispatch("updateLastSeenAppVersion") + if(userStore.hasNewAppVersion) { + userStore.saveLastSeenAppVersion(); + tracker.event("app", "whats-new-seen") + } + // await this.$store.dispatch("updateLastSeenAppVersion") this.$router.push({ name: "landing-page", }).catch(err => {}) diff --git a/src/renderer/_vue/index.js b/src/renderer/_vue/index.js index d1138d30f7..e5c149961e 100644 --- a/src/renderer/_vue/index.js +++ b/src/renderer/_vue/index.js @@ -9,6 +9,8 @@ import BootstrapVue from 'bootstrap-vue' import App from './App' import router from './router' import store from './store' +import { userStore } from "../../common/user-store" +import { when } from "mobx" const promiseIpc = new PromiseIpc({maxTimeoutMs: 6000}); @@ -27,17 +29,15 @@ Vue.mixin({ }) // any initialization we want to do for app state -setTimeout(() => { - store.dispatch('init', ).catch((error) => { - console.error(error) - }).finally(() => { - /* eslint-disable no-new */ - console.log("start vue") - new Vue({ - components: { App }, - store, - router, - template: '' - }).$mount('#app') - }) -}, 0) +setTimeout(async () => { + await when(() => userStore.isReady); + + await store.dispatch('init') + console.log("start vue") + new Vue({ + components: { App }, + store, + router, + template: '' + }).$mount('#app') +}) diff --git a/src/renderer/_vue/router/routeguard/index.js b/src/renderer/_vue/router/routeguard/index.js index 2c8ba87e55..29fac94417 100644 --- a/src/renderer/_vue/router/routeguard/index.js +++ b/src/renderer/_vue/router/routeguard/index.js @@ -1,23 +1,7 @@ -export function auth ( to, from, store ) { - if(!store.getters.isLoggedIn){ - console.log("router: guard: auth: activated"); - return { - path: '/login' - } - } -} +import { userStore } from "../../../../common/user-store" -export function eula ( to, from, store ) { - if(!store.getters.isEulaAccepted){ - console.log("router: guard: eula: activated"); - return { - path: '/accept-eula' - } - } -} - -export function whatsNew( to, from, store ) { - if(store.getters.showWhatsNew){ +export function whatsNew() { + if(userStore.hasNewAppVersion){ console.log("router: guard: whatsNew: activated"); return { path: '/whats-new'