Skip to content

Commit

Permalink
refactor(core): return named function for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
ricokahler committed Sep 16, 2024
1 parent 47cd411 commit d94115a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ export function createCallbackResolver<TProperty extends 'hidden' | 'readOnly'>(
const stableTrue = {value: true}
let last: {serializedHash: string; result: StateTree<boolean> | undefined} | null = null

return ({
function callbackResult({
currentUser,
documentValue,
schemaType,
...options
}: ResolveRootCallbackStateOptions<TProperty>) => {
}: ResolveRootCallbackStateOptions<TProperty>) {
const hash = {
currentUser: getId(currentUser),
schemaType: getId(schemaType),
Expand Down Expand Up @@ -192,4 +192,6 @@ export function createCallbackResolver<TProperty extends 'hidden' | 'readOnly'>(

return result
}

return callbackResult
}
21 changes: 6 additions & 15 deletions packages/sanity/src/core/form/store/utils/createMemoizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,21 @@ export function createMemoizer<TFunction extends (...args: never[]) => unknown>(
}: MemoizerOptions<TFunction>): FunctionDecorator<TFunction> {
const cache = new Map<string, {serializedHash: string; result: ReturnType<TFunction>}>()

return (fn) => {
const memoizedFn = ((...args: Parameters<TFunction>) => {
function memoizer(fn: TFunction): TFunction {
function memoizedFn(...args: Parameters<TFunction>) {
const path = toString(getPath(...args))
const hashed = hashInput(...args)
const serializedHash = JSON.stringify(hashed)
const cached = cache.get(path)
if (serializedHash === cached?.serializedHash) return cached.result

// if (cached) {
// console.log({
// name: fn.name,
// path,
// // reason: objectDiff(JSON.parse(cached.serializedHash), hashed).join(', '),
// prev: JSON.parse(cached.serializedHash),
// next: hashed,
// })
// }

const result = fn(...args) as ReturnType<TFunction>
cache.set(path, {serializedHash, result})

return result
}) as TFunction
}

return decorator(memoizedFn)
return decorator(memoizedFn as TFunction)
}

return memoizer
}

0 comments on commit d94115a

Please sign in to comment.