mirror of
https://github.com/tooot-app/app
synced 2025-02-06 05:13:35 +01:00
Use in-memory global values
As connect needs to be read very often
This commit is contained in:
parent
ebe57be674
commit
5c3e490112
@ -24,6 +24,10 @@ import { enableFreeze } from 'react-native-screens'
|
||||
import i18n from './i18n'
|
||||
import Screens from './screens'
|
||||
|
||||
export const GLOBAL: { connect?: boolean } = {
|
||||
connect: undefined
|
||||
}
|
||||
|
||||
Platform.select({
|
||||
android: LogBox.ignoreLogs(['Setting a timer for a long period of time'])
|
||||
})
|
||||
@ -54,6 +58,7 @@ const App: React.FC = () => {
|
||||
}
|
||||
|
||||
const useConnect = getGlobalStorage.boolean('app.connect')
|
||||
GLOBAL.connect = useConnect
|
||||
log('log', 'App', `connect: ${useConnect}`)
|
||||
if (useConnect) {
|
||||
await connectVerify()
|
||||
|
@ -11,6 +11,7 @@ import * as Localization from 'expo-localization'
|
||||
import React, { useEffect, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { Linking, Platform } from 'react-native'
|
||||
import { GLOBAL } from '../../../../App'
|
||||
import { mapFontsizeToName } from '../SettingsFontsize'
|
||||
|
||||
const SettingsApp: React.FC = () => {
|
||||
@ -34,6 +35,7 @@ const SettingsApp: React.FC = () => {
|
||||
})
|
||||
.catch(() => {
|
||||
if (connect) {
|
||||
GLOBAL.connect = false
|
||||
setConnect(false)
|
||||
} else {
|
||||
setShowConnect(false)
|
||||
@ -173,7 +175,10 @@ const SettingsApp: React.FC = () => {
|
||||
<MenuRow
|
||||
title='使用代理'
|
||||
switchValue={connect || false}
|
||||
switchOnValueChange={() => setConnect(!connect)}
|
||||
switchOnValueChange={() => {
|
||||
GLOBAL.connect = !connect
|
||||
setConnect(!connect)
|
||||
}}
|
||||
/>
|
||||
) : null}
|
||||
</MenuContainer>
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { getGlobalStorage } from '@utils/storage/actions'
|
||||
import axios from 'axios'
|
||||
import { GLOBAL } from '../../App'
|
||||
import { ctx, handleError, PagedResponse, parseHeaderLinks, userAgent } from './helpers'
|
||||
import { CONNECT_DOMAIN } from './helpers/connect'
|
||||
|
||||
@ -34,12 +34,10 @@ const apiGeneral = async <T = unknown>({
|
||||
params ? params : ''
|
||||
)
|
||||
|
||||
const useConnect = getGlobalStorage.boolean('app.connect')
|
||||
|
||||
return axios({
|
||||
timeout: method === 'post' ? 1000 * 60 : 1000 * 15,
|
||||
method,
|
||||
baseURL: `https://${useConnect ? CONNECT_DOMAIN() : domain}`,
|
||||
baseURL: `https://${GLOBAL.connect ? CONNECT_DOMAIN() : domain}`,
|
||||
url,
|
||||
params,
|
||||
headers: {
|
||||
@ -47,7 +45,7 @@ const apiGeneral = async <T = unknown>({
|
||||
...userAgent,
|
||||
...headers,
|
||||
...(body && body instanceof FormData && { 'Content-Type': 'multipart/form-data' }),
|
||||
...(useConnect && { 'x-tooot-domain': domain })
|
||||
...(GLOBAL.connect && { 'x-tooot-domain': domain })
|
||||
},
|
||||
data: body
|
||||
})
|
||||
|
@ -1,8 +1,9 @@
|
||||
import { mapEnvironment } from '@utils/helpers/checkEnvironment'
|
||||
import { getGlobalStorage, setGlobalStorage } from '@utils/storage/actions'
|
||||
import { setGlobalStorage } from '@utils/storage/actions'
|
||||
import axios from 'axios'
|
||||
import parse from 'url-parse'
|
||||
import { userAgent } from '.'
|
||||
import { GLOBAL } from '../../../App'
|
||||
|
||||
const list = [
|
||||
'n61owz4leck',
|
||||
@ -119,8 +120,7 @@ export const connectImage = ({
|
||||
}: {
|
||||
uri?: string
|
||||
}): { uri?: string; headers?: { 'x-tooot-domain': string } } => {
|
||||
const connect = getGlobalStorage.boolean('app.connect')
|
||||
if (connect) {
|
||||
if (GLOBAL.connect) {
|
||||
if (uri) {
|
||||
const host = parse(uri).host
|
||||
return { uri: uri.replace(host, CONNECT_DOMAIN()), headers: { 'x-tooot-domain': host } }
|
||||
@ -139,6 +139,7 @@ export const connectVerify = () =>
|
||||
url: 'verify',
|
||||
headers: { ...userAgent }
|
||||
}).catch(err => {
|
||||
GLOBAL.connect = false
|
||||
setGlobalStorage('app.connect', false)
|
||||
return Promise.reject(err)
|
||||
})
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { getAccountDetails, getGlobalStorage } from '@utils/storage/actions'
|
||||
import { getAccountDetails } from '@utils/storage/actions'
|
||||
import { StorageGlobal } from '@utils/storage/global'
|
||||
import axios, { AxiosRequestConfig } from 'axios'
|
||||
import { GLOBAL } from '../../App'
|
||||
import { ctx, handleError, PagedResponse, parseHeaderLinks, userAgent } from './helpers'
|
||||
import { CONNECT_DOMAIN } from './helpers/connect'
|
||||
|
||||
@ -45,13 +46,11 @@ const apiInstance = async <T = unknown>({
|
||||
params ? params : ''
|
||||
)
|
||||
|
||||
const useConnect = getGlobalStorage.boolean('app.connect')
|
||||
|
||||
return axios({
|
||||
timeout: method === 'post' ? 1000 * 60 : 1000 * 15,
|
||||
method,
|
||||
baseURL: `https://${
|
||||
useConnect ? CONNECT_DOMAIN() : accountDetails['auth.domain']
|
||||
GLOBAL.connect ? CONNECT_DOMAIN() : accountDetails['auth.domain']
|
||||
}/api/${version}`,
|
||||
url,
|
||||
params,
|
||||
@ -61,7 +60,7 @@ const apiInstance = async <T = unknown>({
|
||||
...headers,
|
||||
Authorization: `Bearer ${accountDetails['auth.token']}`,
|
||||
...(body && body instanceof FormData && { 'Content-Type': 'multipart/form-data' }),
|
||||
...(useConnect && { 'x-tooot-domain': accountDetails['auth.domain'] })
|
||||
...(GLOBAL.connect && { 'x-tooot-domain': accountDetails['auth.domain'] })
|
||||
},
|
||||
data: body,
|
||||
...extras
|
||||
|
Loading…
x
Reference in New Issue
Block a user