Skip to content

Commit

Permalink
refactor(app/weakCache): more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Dec 7, 2023
1 parent e4e33f4 commit b6f9f57
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/app/actionObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const createActionObserver = (): ActionObserver => {
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getOrCache = createWeakCache<PayloadActionCreator<any>, Observable<any>>()
const getOrCache = createWeakCache<PayloadActionCreator<any>>()

const onAction: OnAction = (actionCreator) =>
getOrCache(actionCreator, () => action$.pipe(filter(matchType(actionCreator)), map(getPayload)))
Expand Down
3 changes: 1 addition & 2 deletions src/app/stateObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export const createStateObserver = <TState>(): StateObserver<TState> => {
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getOrCache = createWeakCache<Selector<TState, unknown>, Observable<any>>()
const getOrCache = createWeakCache<Selector<TState, unknown>>()

const onState: OnState<TState> = (selector) =>
getOrCache(selector, () => distinctState$.pipe(map(selector), distinctUntilChanged()))
Expand Down
7 changes: 4 additions & 3 deletions src/app/weakCache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
type WeakCache<K extends WeakKey, V> = (key: K, createValue: () => V) => V
type WeakCache<K extends WeakKey> = <V>(key: K, createValue: () => V) => V

export const createWeakCache = <K extends WeakKey, V>(): WeakCache<K, V> => {
const cache = new WeakMap<K, V>()
export const createWeakCache = <K extends WeakKey>(): WeakCache<K> => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const cache = new WeakMap<K, any>()

return (key, createValue) => {
let value = cache.get(key)
Expand Down

0 comments on commit b6f9f57

Please sign in to comment.