Add data save mode (#4477)

This commit is contained in:
the1812 2023-11-19 19:06:24 +08:00
parent f5162f697c
commit 24b8282c2f
4 changed files with 39 additions and 2 deletions

View File

@ -13,7 +13,8 @@ import {
UpdateCheckItem,
CheckSingleTypeUpdateConfig,
} from './utils'
import { Options } from '.'
import type { Options } from '.'
import { isDataSaveMode } from '@/core/utils'
export const checkUpdate = async (config: CheckUpdateConfig) => {
const {
@ -23,9 +24,12 @@ export const checkUpdate = async (config: CheckUpdateConfig) => {
force = false,
maxCount = Infinity,
} = config
if (isDataSaveMode()) {
return '当前为流量计费网络, 跳过更新检查.'
}
const now = Number(new Date())
const { devMode } = getGeneralSettings()
const { options } = getComponentSettings(name)
const { options } = getComponentSettings<Options>(name)
// Remove uninstalled items
Object.keys(items)
.filter(it => !existPredicate(it))

View File

@ -24,6 +24,10 @@ export const component = defineComponentMetadata({
const { monkey } = await import('@/core/ajax')
const { meta } = await import('@/core/meta')
const { Toast } = await import('@/core/toast')
const { isDataSaveMode } = await import('@/core/utils')
if (isDataSaveMode()) {
return
}
const now = Number(new Date())
const duration = now - options.lastUpdateCheck
if (duration < options.minimumDuration) {

View File

@ -672,3 +672,8 @@ export const todo = (...args: unknown[]): never => {
export const unreachable = (): never => {
throw new Error(`unreachable`)
}
/** 是否为流量计费网络 (不支持的浏览器仍按 false 算) */
export const isDataSaveMode = () => {
return navigator.connection?.saveData ?? false
}

24
src/global.d.ts vendored
View File

@ -9,6 +9,30 @@ declare global {
const lodash: LoDashStatic
const Vue: typeof import('vue/types/umd')
type EnumEventTarget<EventTypes extends string> = EventTarget & {
addEventListener(
type: EventTypes,
callback: EventListenerOrEventListenerObject | null,
options?: AddEventListenerOptions | boolean,
): void
removeEventListener(
type: EventTypes,
callback: EventListenerOrEventListenerObject | null,
options?: EventListenerOptions | boolean,
): void
}
interface NetworkInformation extends EnumEventTarget<'change'> {
downlink: number
downlinkMax: number
effectiveType: string
rtt: number
saveData: boolean
type: string
}
interface Navigator {
connection?: NetworkInformation
}
interface GitInfo {
commitHash: string
branch: string