Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up types #1016

Merged
merged 8 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { CacheInterface, keyInterface, cacheListener } from './types'
import { Cache as CacheType, Key, CacheListener } from './types'
import hash from './libs/hash'

export default class Cache implements CacheInterface {
export default class Cache implements CacheType {
private __cache: Map<string, any>
private __listeners: cacheListener[]
private __listeners: CacheListener[]

constructor(initialData: any = {}) {
this.__cache = new Map(Object.entries(initialData))
this.__listeners = []
}

get(key: keyInterface): any {
get(key: Key): any {
const [_key] = this.serializeKey(key)
return this.__cache.get(_key)
}

set(key: keyInterface, value: any): any {
set(key: Key, value: any): any {
const [_key] = this.serializeKey(key)
this.__cache.set(_key, value)
this.notify()
Expand All @@ -25,7 +25,7 @@ export default class Cache implements CacheInterface {
return Array.from(this.__cache.keys())
}

has(key: keyInterface) {
has(key: Key) {
const [_key] = this.serializeKey(key)
return this.__cache.has(_key)
}
Expand All @@ -35,14 +35,14 @@ export default class Cache implements CacheInterface {
this.notify()
}

delete(key: keyInterface) {
delete(key: Key) {
const [_key] = this.serializeKey(key)
this.__cache.delete(_key)
this.notify()
}

// TODO: introduce namespace for the cache
serializeKey(key: keyInterface): [string, any, string, string] {
serializeKey(key: Key): [string, any, string, string] {
let args = null
if (typeof key === 'function') {
try {
Expand All @@ -68,7 +68,7 @@ export default class Cache implements CacheInterface {
return [key, args, errorKey, isValidatingKey]
}

subscribe(listener: cacheListener) {
subscribe(listener: CacheListener) {
if (typeof listener !== 'function') {
throw new Error('Expected the listener to be a function.')
}
Expand Down
14 changes: 5 additions & 9 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { dequal } from 'dequal/lite'
import {
ConfigInterface,
RevalidateOptionInterface,
revalidateType
} from './types'
import { SWRConfiguration, RevalidatorOptions, Revalidator } from './types'
import Cache from './cache'
import webPreset from './libs/web-preset'

Expand All @@ -14,9 +10,9 @@ const cache = new Cache()
function onErrorRetry(
_,
__,
config: ConfigInterface,
revalidate: revalidateType,
opts: RevalidateOptionInterface
config: SWRConfiguration,
revalidate: Revalidator,
opts: RevalidatorOptions
): void {
if (!config.isDocumentVisible()) {
// if it's hidden, stop
Expand Down Expand Up @@ -47,7 +43,7 @@ const slowConnection =
['slow-2g', '2g'].indexOf(navigator['connection'].effectiveType) !== -1

// config
const defaultConfig: ConfigInterface = Object.assign(
const defaultConfig: SWRConfiguration = Object.assign(
{
// events
onLoadingSlow: () => {},
Expand Down
28 changes: 21 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
export * from './use-swr'
// `useSWR` and related APIs
import { default as useSWR } from './use-swr'
export {
useSWRInfinite,
SWRInfiniteConfigInterface,
SWRInfiniteResponseInterface
} from './use-swr-infinite'
export default useSWR
export * from './use-swr'

// `useSWRInfinite`
export { useSWRInfinite } from './use-swr-infinite'

// Cache related, to be replaced by the new APIs
export { cache } from './config'

// Types
export {
SWRConfiguration,
SWRInfiniteConfiguration,
SWRInfiniteResponse,
Revalidator,
RevalidatorOptions,
Key,
SWRResponse,
Cache,
// Legacy, for backwards compatibility
ConfigInterface,
SWRInfiniteConfigInterface,
SWRInfiniteResponseInterface,
revalidateType,
RevalidateOptionInterface,
keyInterface,
responseInterface,
CacheInterface
} from './types'
export default useSWR
4 changes: 2 additions & 2 deletions src/swr-config-context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createContext } from 'react'

import { ConfigInterface } from './types'
import { SWRConfiguration } from './types'

const SWRConfigContext = createContext<Partial<ConfigInterface>>({})
const SWRConfigContext = createContext<SWRConfiguration>({})
SWRConfigContext.displayName = 'SWRConfigContext'

export default SWRConfigContext
Loading