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