-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add memoize config to add custom cache type
- Loading branch information
1 parent
0ce91dc
commit 735eb58
Showing
5 changed files
with
74 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,33 @@ | ||
import { isFunction, isObject } from 'lodash'; | ||
|
||
import { Applicator, ApplicateOptions } from './Applicator'; | ||
import { resolveFunction } from '../utils'; | ||
|
||
export class MemoizeApplicator extends Applicator { | ||
apply({ value, instance, config: { execute }, args, target }: ApplicateOptions): any { | ||
if (!instance) { | ||
return execute(value, ...args); | ||
let resolver = resolveFunction( | ||
isFunction(args[0]) ? args[0] : isObject(args[0]) ? args[0].resolver : args[0], | ||
instance, | ||
target, | ||
false | ||
); | ||
|
||
if (resolver && instance) { | ||
resolver = resolver.bind(instance); | ||
} | ||
|
||
let resolver = resolveFunction(args[0], instance, target, false); | ||
const memoized = resolver ? execute(value, resolver) : execute(value); | ||
|
||
if (resolver) { | ||
resolver = resolver.bind(instance); | ||
if (isObject(args[0])) { | ||
const { cache, type } = args[0]; | ||
|
||
if (cache) { | ||
memoized.cache = cache; | ||
} else if (isFunction(type)) { | ||
memoized.cache = new type(); | ||
} | ||
} | ||
|
||
return resolver ? execute(value, resolver) : execute(value); | ||
return memoized; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters