Skip to content

Commit

Permalink
Merge branch 'main' into devtools-hook
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 committed Jun 27, 2022
2 parents c4468b5 + 38180b8 commit 9def180
Show file tree
Hide file tree
Showing 25 changed files with 598 additions and 226 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
- run: pnpm types:check
- run: pnpm lint
- run: pnpm build
- run: pnpm test-typing
- run: pnpm test
env:
CI: true
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ dist
.eslintcache
examples/**/yarn.lock
package-lock.json
tsconfig.tsbuildinfo
*/**/*.tsbuildinfo
coverage
*/**/.turbo
.turbo
.rollup.cache
17 changes: 7 additions & 10 deletions _internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,13 @@ export type MutatorWrapper<Fn> = Fn extends (
export type Mutator<Data = any> = MutatorWrapper<MutatorFn<Data>>

export interface ScopedMutator<Data = any> {
/** This is used for bound mutator */
(
key: Key,
data?: Data | Promise<Data> | MutatorCallback<Data>,
<T = Data>(
matcher: (key?: Arguments) => boolean,
data?: T | Promise<T> | MutatorCallback<T>,
opts?: boolean | MutatorOptions<Data>
): Promise<Data | undefined>
/** This is used for global mutator */
<T = any>(
key: Key,
): Promise<Array<T | undefined>>
<T = Data>(
key: Arguments,
data?: T | Promise<T> | MutatorCallback<T>,
opts?: boolean | MutatorOptions<Data>
): Promise<T | undefined>
Expand All @@ -223,8 +221,6 @@ export type KeyedMutator<Data> = (
opts?: boolean | MutatorOptions<Data>
) => Promise<Data | undefined>

// Public types

export type SWRConfiguration<
Data = any,
Error = any,
Expand Down Expand Up @@ -267,6 +263,7 @@ export type RevalidateCallback = <K extends RevalidateEvent>(
) => RevalidateCallbackReturnType[K]

export interface Cache<Data = any> {
keys(): IterableIterator<string>
get(key: Key): State<Data> | undefined
set(key: Key, value: State<Data>): void
delete(key: Key): void
Expand Down
7 changes: 3 additions & 4 deletions _internal/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const initCache = <Data = any>(
// If there's no global state bound to the provider, create a new one with the
// new mutate function.
const EVENT_REVALIDATORS = {}

const mutate = internalMutate.bind(
UNDEFINED,
provider
Expand All @@ -58,16 +59,14 @@ export const initCache = <Data = any>(
subscriptions[key] = subs

subs.push(callback)
return () => {
subs.splice(subs.indexOf(callback), 1)
}
return () => subs.splice(subs.indexOf(callback), 1)
}
const setter = (key: string, value: any, prev: any) => {
provider.set(key, value)
const subs = subscriptions[key]
if (subs) {
for (let i = subs.length; i--; ) {
subs[i](value, prev)
subs[i](prev, value)
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions _internal/utils/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SWRGlobalState } from './global-state'
import { Key, Cache, State, GlobalState } from '../types'
import type { Key, Cache, State, GlobalState } from '../types'

const EMPTY_CACHE = {}
export const noop = () => {}
Expand All @@ -13,9 +13,12 @@ export const 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 isEmptyCache = (v: any): boolean => v === EMPTY_CACHE
export const mergeObjects = (a: any, b: any) => OBJECT.assign({}, a, b)
export const isFunction = <
T extends (...args: any[]) => any = (...args: any[]) => any
>(
v: unknown
): v is T => typeof v == 'function'
export const mergeObjects = (a: any, b?: any) => OBJECT.assign({}, a, b)

const STR_UNDEFINED = 'undefined'

Expand Down

0 comments on commit 9def180

Please sign in to comment.