Skip to content

Commit

Permalink
optimizations (#1578)
Browse files Browse the repository at this point in the history
  • Loading branch information
shuding authored Oct 24, 2021
1 parent da33f52 commit f79f646
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defaultConfig } from './utils/config'
import { SWRGlobalState, GlobalState } from './utils/global-state'
import { IS_SERVER, rAF, useIsomorphicLayoutEffect } from './utils/env'
import { serialize } from './utils/serialize'
import { isUndefined, UNDEFINED, mergeObjects } from './utils/helper'
import { isUndefined, UNDEFINED, OBJECT, mergeObjects } from './utils/helper'
import ConfigProvider from './utils/config-context'
import { useStateWithDeps } from './utils/state'
import { withArgs } from './utils/resolve-args'
Expand Down Expand Up @@ -501,7 +501,7 @@ export const useSWRHandler = <Data = any, Error = any>(
} as SWRResponse<Data, Error>
}

export const SWRConfig = Object.defineProperty(ConfigProvider, 'default', {
export const SWRConfig = OBJECT.defineProperty(ConfigProvider, 'default', {
value: defaultConfig
}) as typeof ConfigProvider & {
default: FullConfiguration
Expand Down
8 changes: 3 additions & 5 deletions src/utils/broadcast-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ export const broadcastState: Broadcaster = (
CONCURRENT_PROMISES_TS
] = SWRGlobalState.get(cache) as GlobalState
const revalidators = EVENT_REVALIDATORS[key]
const updaters = STATE_UPDATERS[key]
const updaters = STATE_UPDATERS[key] || []

// Always update states of all hooks.
if (updaters) {
for (let i = 0; i < updaters.length; ++i) {
updaters[i](data, error, isValidating)
}
for (let i = 0; i < updaters.length; ++i) {
updaters[i](data, error, isValidating)
}

// If we also need to revalidate, only do it for the first hook.
Expand Down
11 changes: 7 additions & 4 deletions src/utils/config-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createContext, createElement, useContext, useState, FC } from 'react'
import { cache as defaultCache } from './config'
import { initCache } from './cache'
import { mergeConfigs } from './merge-config'
import { UNDEFINED } from './helper'
import { UNDEFINED, mergeObjects } from './helper'
import { useIsomorphicLayoutEffect } from './env'
import {
SWRConfiguration,
Expand All @@ -18,7 +18,9 @@ const SWRConfig: FC<{
Partial<ProviderConfiguration> & {
provider?: (cache: Readonly<Cache>) => Cache
}
}> = ({ children, value }) => {
}> = props => {
const { value } = props

// Extend parent context values and middleware.
const extendedConfig = mergeConfigs(useContext(SWRConfigContext), value)

Expand Down Expand Up @@ -46,8 +48,9 @@ const SWRConfig: FC<{

return createElement(
SWRConfigContext.Provider,
{ value: extendedConfig },
children
mergeObjects(props, {
value: extendedConfig
})
)
}

Expand Down
8 changes: 4 additions & 4 deletions src/utils/hash.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isUndefined } from './helper'
import { OBJECT, isUndefined } from './helper'

// use WeakMap to store the object->key mapping
// so the objects can be garbage collected.
Expand All @@ -25,7 +25,7 @@ export const stableHash = (arg: any): string => {
let result: any
let index: any

if (Object(arg) === arg && !isDate && constructor != RegExp) {
if (OBJECT(arg) === arg && !isDate && constructor != RegExp) {
// Object/function, not null/date/regexp. Use WeakMap to store the id first.
// If it's already hashed, directly return the result.
result = table.get(arg)
Expand All @@ -45,10 +45,10 @@ export const stableHash = (arg: any): string => {
}
table.set(arg, result)
}
if (constructor == Object) {
if (constructor == OBJECT) {
// Object, sort keys.
result = '#'
const keys = Object.keys(arg).sort()
const keys = OBJECT.keys(arg).sort()
while (!isUndefined((index = keys.pop() as string))) {
if (!isUndefined(arg[index])) {
result += index + ':' + stableHash(arg[index]) + ','
Expand Down
4 changes: 3 additions & 1 deletion src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ export const noop = () => {}
// prettier-ignore
export const UNDEFINED: undefined = (/*#__NOINLINE__*/ noop()) as undefined

export const OBJECT = Object

export const isUndefined = (v: any): v is undefined => v === UNDEFINED
export const isFunction = (v: any): v is Function => typeof v == 'function'
export const mergeObjects = (a: any, b: any) => Object.assign({}, a, b)
export const mergeObjects = (a: any, b: any) => OBJECT.assign({}, a, b)

const STR_UNDEFINED = 'undefined'
export const hasWindow = typeof window != STR_UNDEFINED
Expand Down
15 changes: 4 additions & 11 deletions src/utils/resolve-args.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
import { useContext } from 'react'

import { defaultConfig } from './config'
import { SWRConfigContext } from './config-context'
import { mergeConfigs } from './merge-config'
import { normalize } from './normalize-args'
import { mergeObjects } from './helper'
import { useSWRConfig } from './use-swr-config'

// It's tricky to pass generic types as parameters, so we just directly override
// the types here.
export const withArgs = <SWRType>(hook: any) => {
return (((...args: any) => {
// Get the default and inherited configuration.
const fallbackConfig = useSWRConfig()

// Normalize arguments.
const [key, fn, _config] = normalize<any, any>(args)

// Get the default and inherited configuration.
const fallbackConfig = mergeObjects(
defaultConfig,
useContext(SWRConfigContext)
)

// Merge configurations.
const config = mergeConfigs(fallbackConfig, _config)

Expand Down

0 comments on commit f79f646

Please sign in to comment.