diff --git a/src/client/init.ts b/src/client/init.ts index 1fcd97190..73e02145d 100644 --- a/src/client/init.ts +++ b/src/client/init.ts @@ -20,6 +20,7 @@ import Menu from './components/menu.vue'; import { router } from './router'; import { applyTheme, lightTheme, builtinThemes } from './theme'; import { isDeviceDarkmode } from './scripts/is-device-darkmode'; +import createStore from './store'; Vue.use(Vuex); Vue.use(VueHotkey); @@ -134,36 +135,38 @@ document.body.setAttribute('ontouchstart', ''); // アプリ基底要素マウント document.body.innerHTML = '
'; -const os = new MiOS(); +const store = createStore(); + +const os = new MiOS(store); os.init(async () => { window.addEventListener('storage', e => { if (e.key === 'vuex') { - os.store.replaceState(JSON.parse(localStorage['vuex'])); + store.replaceState(JSON.parse(localStorage['vuex'])); } else if (e.key === 'i') { location.reload(); } }, false) - os.store.watch(state => state.device.darkMode, darkMode => { + store.watch(state => state.device.darkMode, darkMode => { // TODO: このファイルでbuiltinThemesを参照するとcode splittingが効かず、初回読み込み時に全てのテーマコードを読み込むことになってしまい無駄なので何とかする - const themes = builtinThemes.concat(os.store.state.device.themes); - applyTheme(themes.find(x => x.id === (darkMode ? os.store.state.device.darkTheme : os.store.state.device.lightTheme))); + const themes = builtinThemes.concat(store.state.device.themes); + applyTheme(themes.find(x => x.id === (darkMode ? store.state.device.darkTheme : store.state.device.lightTheme))); }); //#region Sync dark mode - if (os.store.state.device.syncDeviceDarkMode) { - os.store.commit('device/set', { key: 'darkMode', value: isDeviceDarkmode() }); + if (store.state.device.syncDeviceDarkMode) { + store.commit('device/set', { key: 'darkMode', value: isDeviceDarkmode() }); } window.matchMedia('(prefers-color-scheme: dark)').addListener(mql => { - if (os.store.state.device.syncDeviceDarkMode) { - os.store.commit('device/set', { key: 'darkMode', value: mql.matches }); + if (store.state.device.syncDeviceDarkMode) { + store.commit('device/set', { key: 'darkMode', value: mql.matches }); } }); //#endregion - if ('Notification' in window && os.store.getters.isSignedIn) { + if ('Notification' in window && store.getters.isSignedIn) { // 許可を得ていなかったらリクエスト if (Notification.permission === 'default') { Notification.requestPermission(); @@ -171,7 +174,7 @@ os.init(async () => { } const app = new Vue({ - store: os.store, + store: store, metaInfo: { title: null, titleTemplate: title => title ? `${title} | ${(instanceName || 'Misskey')}` : (instanceName || 'Misskey') @@ -183,7 +186,7 @@ os.init(async () => { }; }, methods: { - api: os.api, + api: (endpoint: string, data: { [x: string]: any } = {}, token?) => store.dispatch('api', { endpoint, data, token }), signout: os.signout, new(vm, props) { const x = new vm({ @@ -234,58 +237,58 @@ os.init(async () => { // マウント app.$mount('#app'); - if (app.$store.getters.isSignedIn) { + if (store.getters.isSignedIn) { const main = os.stream.useSharedConnection('main'); // 自分の情報が更新されたとき main.on('meUpdated', i => { - app.$store.dispatch('mergeMe', i); + store.dispatch('mergeMe', i); }); main.on('readAllNotifications', () => { - app.$store.dispatch('mergeMe', { + store.dispatch('mergeMe', { hasUnreadNotification: false }); }); main.on('unreadNotification', () => { - app.$store.dispatch('mergeMe', { + store.dispatch('mergeMe', { hasUnreadNotification: true }); }); main.on('unreadMention', () => { - app.$store.dispatch('mergeMe', { + store.dispatch('mergeMe', { hasUnreadMentions: true }); }); main.on('readAllUnreadMentions', () => { - app.$store.dispatch('mergeMe', { + store.dispatch('mergeMe', { hasUnreadMentions: false }); }); main.on('unreadSpecifiedNote', () => { - app.$store.dispatch('mergeMe', { + store.dispatch('mergeMe', { hasUnreadSpecifiedNotes: true }); }); main.on('readAllUnreadSpecifiedNotes', () => { - app.$store.dispatch('mergeMe', { + store.dispatch('mergeMe', { hasUnreadSpecifiedNotes: false }); }); main.on('readAllMessagingMessages', () => { - app.$store.dispatch('mergeMe', { + store.dispatch('mergeMe', { hasUnreadMessagingMessage: false }); }); main.on('unreadMessagingMessage', () => { - app.$store.dispatch('mergeMe', { + store.dispatch('mergeMe', { hasUnreadMessagingMessage: true }); @@ -293,13 +296,13 @@ os.init(async () => { }); main.on('readAllAntennas', () => { - app.$store.dispatch('mergeMe', { + store.dispatch('mergeMe', { hasUnreadAntenna: false }); }); main.on('unreadAntenna', () => { - app.$store.dispatch('mergeMe', { + store.dispatch('mergeMe', { hasUnreadAntenna: true }); @@ -307,13 +310,13 @@ os.init(async () => { }); main.on('readAllAnnouncements', () => { - app.$store.dispatch('mergeMe', { + store.dispatch('mergeMe', { hasUnreadAnnouncement: false }); }); main.on('clientSettingUpdated', x => { - app.$store.commit('settings/set', { + store.commit('settings/set', { key: x.key, value: x.value }); diff --git a/src/client/mios.ts b/src/client/mios.ts index aa2b202ab..63d9f7d3a 100644 --- a/src/client/mios.ts +++ b/src/client/mios.ts @@ -2,16 +2,11 @@ import autobind from 'autobind-decorator'; import Vue from 'vue'; import { EventEmitter } from 'eventemitter3'; -import initStore from './store'; import { apiUrl, version } from './config'; import Progress from './scripts/loading'; import Stream from './scripts/stream'; - -//#region api requests -let spinner = null; -let pending = 0; -//#endregion +import store from './store'; /** * Misskey Operating System @@ -19,7 +14,7 @@ let pending = 0; export default class MiOS extends EventEmitter { public app: Vue; - public store: ReturnType