Skip to content

Commit 4d9a0bf

Browse files
committed
clean up internal types
1 parent 6a635a9 commit 4d9a0bf

File tree

4 files changed

+46
-57
lines changed

4 files changed

+46
-57
lines changed

src/cache.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Cache as CacheType, Key, cacheListener } from './types'
1+
import { Cache as CacheType, Key, CacheListener } from './types'
22
import hash from './libs/hash'
33

44
export default class Cache implements CacheType {
55
private __cache: Map<string, any>
6-
private __listeners: cacheListener[]
6+
private __listeners: CacheListener[]
77

88
constructor(initialData: any = {}) {
99
this.__cache = new Map(Object.entries(initialData))
@@ -68,7 +68,7 @@ export default class Cache implements CacheType {
6868
return [key, args, errorKey, isValidatingKey]
6969
}
7070

71-
subscribe(listener: cacheListener) {
71+
subscribe(listener: CacheListener) {
7272
if (typeof listener !== 'function') {
7373
throw new Error('Expected the listener to be a function.')
7474
}

src/types.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Internal types
22

3-
export type fetcherFn<Data> = (...args: any) => Data | Promise<Data>
3+
export type Fetcher<Data> = (...args: any) => Data | Promise<Data>
44

55
export type Configuration<
66
Data = any,
77
Error = any,
8-
Fn extends fetcherFn<Data> = fetcherFn<Data>
8+
Fn extends Fetcher<Data> = Fetcher<Data>
99
> = {
1010
errorRetryInterval: number
1111
errorRetryCount?: number
@@ -46,42 +46,40 @@ export type Configuration<
4646
compare: (a: Data | undefined, b: Data | undefined) => boolean
4747
}
4848

49-
export type keyType = string | any[] | null
50-
type keyFunction = () => keyType
49+
export type ValueKey = string | any[] | null
5150

52-
export type updaterInterface<Data = any, Error = any> = (
51+
export type Updater<Data = any, Error = any> = (
5352
shouldRevalidate?: boolean,
5453
data?: Data,
5554
error?: Error,
5655
shouldDedupe?: boolean,
5756
dedupe?: boolean
5857
) => boolean | Promise<boolean>
59-
export type triggerInterface = (
60-
key: Key,
61-
shouldRevalidate?: boolean
62-
) => Promise<any>
63-
export type mutateCallback<Data = any> = (
58+
export type Trigger = (key: Key, shouldRevalidate?: boolean) => Promise<any>
59+
60+
type MutatorCallback<Data = any> = (
6461
currentValue: undefined | Data
6562
) => Promise<undefined | Data> | undefined | Data
66-
export type mutateInterface<Data = any> = (
63+
64+
export type Mutator<Data = any> = (
6765
key: Key,
68-
data?: Data | Promise<Data> | mutateCallback<Data>,
66+
data?: Data | Promise<Data> | MutatorCallback<Data>,
6967
shouldRevalidate?: boolean
7068
) => Promise<Data | undefined>
71-
export type broadcastStateInterface<Data = any, Error = any> = (
69+
export type Broadcaster<Data = any, Error = any> = (
7270
key: string,
7371
data: Data,
7472
error?: Error,
7573
isValidating?: boolean
7674
) => void
7775

78-
export type actionType<Data, Error> = {
76+
export type Action<Data, Error> = {
7977
data?: Data
8078
error?: Error
8179
isValidating?: boolean
8280
}
8381

84-
export type cacheListener = () => void
82+
export type CacheListener = () => void
8583

8684
// Public types
8785

@@ -91,19 +89,19 @@ export type cacheListener = () => void
9189
export type ConfigInterface<
9290
Data = any,
9391
Error = any,
94-
Fn extends fetcherFn<Data> = fetcherFn<Data>
92+
Fn extends Fetcher<Data> = Fetcher<Data>
9593
> = Partial<Configuration<Data, Error, Fn>>
9694
export type SWRConfiguration<
9795
Data = any,
9896
Error = any,
99-
Fn extends fetcherFn<Data> = fetcherFn<Data>
97+
Fn extends Fetcher<Data> = Fetcher<Data>
10098
> = Partial<Configuration<Data, Error, Fn>>
10199

102100
/**
103101
* @deprecated `keyInterface` will be renamed to `Key`.
104102
*/
105-
export type keyInterface = keyFunction | keyType
106-
export type Key = keyFunction | keyType
103+
export type keyInterface = ValueKey | (() => ValueKey)
104+
export type Key = ValueKey | (() => ValueKey)
107105

108106
/**
109107
* @deprecated `responseInterface` will be renamed to `SWRResponse`.
@@ -113,7 +111,7 @@ export type responseInterface<Data, Error> = {
113111
error?: Error
114112
revalidate: () => Promise<boolean>
115113
mutate: (
116-
data?: Data | Promise<Data> | mutateCallback<Data>,
114+
data?: Data | Promise<Data> | MutatorCallback<Data>,
117115
shouldRevalidate?: boolean
118116
) => Promise<Data | undefined>
119117
isValidating: boolean
@@ -123,7 +121,7 @@ export type SWRResponse<Data, Error> = {
123121
error?: Error
124122
revalidate: () => Promise<boolean>
125123
mutate: (
126-
data?: Data | Promise<Data> | mutateCallback<Data>,
124+
data?: Data | Promise<Data> | MutatorCallback<Data>,
127125
shouldRevalidate?: boolean
128126
) => Promise<Data | undefined>
129127
isValidating: boolean
@@ -135,15 +133,15 @@ export type SWRResponse<Data, Error> = {
135133
export type SWRInfiniteConfigInterface<
136134
Data = any,
137135
Error = any
138-
> = SWRConfiguration<Data[], Error, fetcherFn<Data[]>> & {
136+
> = SWRConfiguration<Data[], Error, Fetcher<Data[]>> & {
139137
initialSize?: number
140138
revalidateAll?: boolean
141139
persistSize?: boolean
142140
}
143141
export type SWRInfiniteConfiguration<
144142
Data = any,
145143
Error = any
146-
> = SWRConfiguration<Data[], Error, fetcherFn<Data[]>> & {
144+
> = SWRConfiguration<Data[], Error, Fetcher<Data[]>> & {
147145
initialSize?: number
148146
revalidateAll?: boolean
149147
persistSize?: boolean
@@ -204,7 +202,7 @@ export interface CacheInterface {
204202
delete(key: Key): void
205203
clear(): void
206204
serializeKey(key: Key): [string, any, string, string]
207-
subscribe(listener: cacheListener): () => void
205+
subscribe(listener: CacheListener): () => void
208206
}
209207
export interface Cache {
210208
get(key: Key): any
@@ -214,5 +212,5 @@ export interface Cache {
214212
delete(key: Key): void
215213
clear(): void
216214
serializeKey(key: Key): [string, any, string, string]
217-
subscribe(listener: cacheListener): () => void
215+
subscribe(listener: CacheListener): () => void
218216
}

src/use-swr-infinite.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import SWRConfigContext from './swr-config-context'
55
import useSWR from './use-swr'
66

77
import {
8-
keyType,
9-
fetcherFn,
8+
ValueKey,
9+
Fetcher,
1010
SWRInfiniteConfiguration,
1111
SWRInfiniteResponse
1212
} from './types'
1313

1414
type KeyLoader<Data = any> = (
1515
index: number,
1616
previousPageData: Data | null
17-
) => keyType
17+
) => ValueKey
1818

1919
function useSWRInfinite<Data = any, Error = any>(
2020
getKey: KeyLoader<Data>
@@ -25,14 +25,14 @@ function useSWRInfinite<Data = any, Error = any>(
2525
): SWRInfiniteResponse<Data, Error>
2626
function useSWRInfinite<Data = any, Error = any>(
2727
getKey: KeyLoader<Data>,
28-
fn?: fetcherFn<Data>,
28+
fn?: Fetcher<Data>,
2929
config?: Partial<SWRInfiniteConfiguration<Data, Error>>
3030
): SWRInfiniteResponse<Data, Error>
3131
function useSWRInfinite<Data = any, Error = any>(
3232
getKey: KeyLoader<Data>,
3333
...options: any[]
3434
): SWRInfiniteResponse<Data, Error> {
35-
let _fn: fetcherFn<Data> | undefined,
35+
let _fn: Fetcher<Data> | undefined,
3636
_config: Partial<SWRInfiniteConfiguration<Data, Error>> = {}
3737

3838
if (options.length > 1) {

src/use-swr.ts

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ import {
1212
import defaultConfig, { cache } from './config'
1313
import SWRConfigContext from './swr-config-context'
1414
import {
15-
actionType,
16-
broadcastStateInterface,
15+
Action,
16+
Broadcaster,
1717
Configuration,
1818
SWRConfiguration,
19-
fetcherFn,
19+
Fetcher,
2020
Key,
21-
mutateInterface,
21+
Mutator,
2222
SWRResponse,
2323
RevalidatorOptions,
24-
triggerInterface,
25-
updaterInterface
24+
Trigger,
25+
Updater
2626
} from './types'
2727

2828
const IS_SERVER =
@@ -74,7 +74,7 @@ if (!IS_SERVER) {
7474
}
7575
}
7676

77-
const trigger: triggerInterface = (_key, shouldRevalidate = true) => {
77+
const trigger: Trigger = (_key, shouldRevalidate = true) => {
7878
// we are ignoring the second argument which correspond to the arguments
7979
// the fetcher will receive when key is an array
8080
const [key, , keyErr, keyValidating] = cache.serializeKey(_key)
@@ -104,12 +104,7 @@ const trigger: triggerInterface = (_key, shouldRevalidate = true) => {
104104
return Promise.resolve(cache.get(key))
105105
}
106106

107-
const broadcastState: broadcastStateInterface = (
108-
key,
109-
data,
110-
error,
111-
isValidating
112-
) => {
107+
const broadcastState: Broadcaster = (key, data, error, isValidating) => {
113108
const updaters = CACHE_REVALIDATORS[key]
114109
if (key && updaters) {
115110
for (let i = 0; i < updaters.length; ++i) {
@@ -118,11 +113,7 @@ const broadcastState: broadcastStateInterface = (
118113
}
119114
}
120115

121-
const mutate: mutateInterface = async (
122-
_key,
123-
_data,
124-
shouldRevalidate = true
125-
) => {
116+
const mutate: Mutator = async (_key, _data, shouldRevalidate = true) => {
126117
const [key, , keyErr] = cache.serializeKey(_key)
127118
if (!key) return
128119

@@ -222,14 +213,14 @@ function useSWR<Data = any, Error = any>(
222213
key: Key,
223214
// `null` is used for a hack to manage shared state with SWR
224215
// https://github.com/vercel/swr/pull/918
225-
fn?: fetcherFn<Data> | null,
216+
fn?: Fetcher<Data> | null,
226217
config?: SWRConfiguration<Data, Error>
227218
): SWRResponse<Data, Error>
228219
function useSWR<Data = any, Error = any>(
229220
_key: Key,
230221
...options: any[]
231222
): SWRResponse<Data, Error> {
232-
let _fn: fetcherFn<Data> | undefined,
223+
let _fn: Fetcher<Data> | undefined,
233224
_config: SWRConfiguration<Data, Error> = {}
234225
if (options.length > 1) {
235226
_fn = options[0]
@@ -301,7 +292,7 @@ function useSWR<Data = any, Error = any>(
301292

302293
const [, rerender] = useState(null)
303294
let dispatch = useCallback(
304-
(payload: actionType<Data, Error>) => {
295+
(payload: Action<Data, Error>) => {
305296
let shouldUpdateState = false
306297
for (let k in payload) {
307298
if (stateRef.current[k] === payload[k]) {
@@ -476,7 +467,7 @@ function useSWR<Data = any, Error = any>(
476467
cache.set(keyValidating, false)
477468

478469
// new state for the reducer
479-
const newState: actionType<Data, Error> = {
470+
const newState: Action<Data, Error> = {
480471
isValidating: false
481472
}
482473

@@ -613,15 +604,15 @@ function useSWR<Data = any, Error = any>(
613604
}
614605

615606
// register global cache update listener
616-
const onUpdate: updaterInterface<Data, Error> = (
607+
const onUpdate: Updater<Data, Error> = (
617608
shouldRevalidate = true,
618609
updatedData,
619610
updatedError,
620611
updatedIsValidating,
621612
dedupe = true
622613
) => {
623614
// update hook state
624-
const newState: actionType<Data, Error> = {}
615+
const newState: Action<Data, Error> = {}
625616
let needUpdate = false
626617

627618
if (

0 commit comments

Comments
 (0)