sw-inject分離したがテストしてない
This commit is contained in:
parent
2288219139
commit
3688859efe
2 changed files with 50 additions and 44 deletions
|
@ -14,15 +14,11 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { defineAsyncComponent, defineComponent, inject } from 'vue';
|
import { defineAsyncComponent, defineComponent } from 'vue';
|
||||||
import { stream, popup, popups, uploads, pendingApiRequestsCount, pageWindow, post } from '@/os';
|
import { stream, popup, popups, uploads, pendingApiRequestsCount } from '@/os';
|
||||||
import * as sound from '@/scripts/sound';
|
import * as sound from '@/scripts/sound';
|
||||||
import { $i, login } from '@/account';
|
import { $i } from '@/account';
|
||||||
import { SwMessage } from '@/sw/types';
|
import { swInject } from './sw-inject';
|
||||||
import { popout } from '@/scripts/popout';
|
|
||||||
import { defaultStore, ColdDeviceStorage } from '@/store';
|
|
||||||
import { getAccountFromId } from '@/scripts/get-account-from-id';
|
|
||||||
import { router } from '@/router';
|
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
components: {
|
components: {
|
||||||
|
@ -53,42 +49,7 @@ export default defineComponent({
|
||||||
|
|
||||||
//#region Listen message from SW
|
//#region Listen message from SW
|
||||||
if ('serviceWorker' in navigator) {
|
if ('serviceWorker' in navigator) {
|
||||||
const navHook = inject('navHook', null);
|
swInject();
|
||||||
const sideViewHook = inject('sideViewHook', null);
|
|
||||||
|
|
||||||
navigator.serviceWorker.addEventListener('message', ev => {
|
|
||||||
if (_DEV_) {
|
|
||||||
console.log('sw msg', ev.data);
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = ev.data as SwMessage;
|
|
||||||
if (data.type !== 'order') return;
|
|
||||||
|
|
||||||
if (data.loginId !== $i?.id) {
|
|
||||||
return getAccountFromId(data.loginId).then(account => {
|
|
||||||
if (!account) return;
|
|
||||||
return login(account.token, data.url);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (data.order) {
|
|
||||||
case 'post':
|
|
||||||
return post(data.options);
|
|
||||||
case 'push':
|
|
||||||
if (router.currentRoute.value.path === data.url) {
|
|
||||||
return window.scroll({ top: 0, behavior: 'smooth' });
|
|
||||||
}
|
|
||||||
if (navHook) {
|
|
||||||
return navHook(data.url);
|
|
||||||
}
|
|
||||||
if (sideViewHook && defaultStore.state.defaultSideView && data.url !== '/') {
|
|
||||||
return sideViewHook(data.url);
|
|
||||||
}
|
|
||||||
return router.push(data.url);
|
|
||||||
default:
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
45
packages/client/src/ui/_common_/sw-inject.ts
Normal file
45
packages/client/src/ui/_common_/sw-inject.ts
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
import { inject } from 'vue';
|
||||||
|
import { post } from '@/os';
|
||||||
|
import { $i, login } from '@/account';
|
||||||
|
import { defaultStore } from '@/store';
|
||||||
|
import { getAccountFromId } from '@/scripts/get-account-from-id';
|
||||||
|
import { router } from '@/router';
|
||||||
|
|
||||||
|
export function swInject() {
|
||||||
|
const navHook = inject('navHook', null);
|
||||||
|
const sideViewHook = inject('sideViewHook', null);
|
||||||
|
|
||||||
|
navigator.serviceWorker.addEventListener('message', ev => {
|
||||||
|
if (_DEV_) {
|
||||||
|
console.log('sw msg', ev.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = ev.data; // as SwMessage
|
||||||
|
if (data.type !== 'order') return;
|
||||||
|
|
||||||
|
if (data.loginId !== $i?.id) {
|
||||||
|
return getAccountFromId(data.loginId).then(account => {
|
||||||
|
if (!account) return;
|
||||||
|
return login(account.token, data.url);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (data.order) {
|
||||||
|
case 'post':
|
||||||
|
return post(data.options);
|
||||||
|
case 'push':
|
||||||
|
if (router.currentRoute.value.path === data.url) {
|
||||||
|
return window.scroll({ top: 0, behavior: 'smooth' });
|
||||||
|
}
|
||||||
|
if (navHook) {
|
||||||
|
return navHook(data.url);
|
||||||
|
}
|
||||||
|
if (sideViewHook && defaultStore.state.defaultSideView && data.url !== '/') {
|
||||||
|
return sideViewHook(data.url);
|
||||||
|
}
|
||||||
|
return router.push(data.url);
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in a new issue