-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ts-sdk): get rid of typescript-memoize dep
- Loading branch information
Showing
14 changed files
with
168 additions
and
22 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
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
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
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./misc"; | ||
export * from "./memoize-decorator"; |
148 changes: 148 additions & 0 deletions
148
ecosystem/typescript/sdk/src/utils/memoize-decorator.ts
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 |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/** | ||
* Credits to https://github.com/darrylhodgins/typescript-memoize | ||
*/ | ||
|
||
/* eslint-disable no-param-reassign */ | ||
/* eslint-disable no-restricted-syntax */ | ||
|
||
interface MemoizeArgs { | ||
// ttl in milliseconds for cached items. After `ttlMs`, cached items are evicted automatically. If no `ttlMs` | ||
// is provided, cached items won't get auto-evicted. | ||
ttlMs?: number; | ||
// produces the cache key based on `args`. | ||
hashFunction?: boolean | ((...args: any[]) => any); | ||
// cached items can be taged with `tags`. `tags` can be used to evict cached items | ||
tags?: string[]; | ||
} | ||
|
||
export function Memoize(args?: MemoizeArgs | MemoizeArgs["hashFunction"]) { | ||
let hashFunction: MemoizeArgs["hashFunction"]; | ||
let ttlMs: MemoizeArgs["ttlMs"]; | ||
let tags: MemoizeArgs["tags"]; | ||
|
||
if (typeof args === "object") { | ||
hashFunction = args.hashFunction; | ||
ttlMs = args.ttlMs; | ||
tags = args.tags; | ||
} else { | ||
hashFunction = args; | ||
} | ||
|
||
return (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => { | ||
if (descriptor.value != null) { | ||
descriptor.value = getNewFunction(descriptor.value, hashFunction, ttlMs, tags); | ||
} else if (descriptor.get != null) { | ||
descriptor.get = getNewFunction(descriptor.get, hashFunction, ttlMs, tags); | ||
} else { | ||
throw new Error("Only put a Memoize() decorator on a method or get accessor."); | ||
} | ||
}; | ||
} | ||
|
||
export function MemoizeExpiring(ttlMs: number, hashFunction?: MemoizeArgs["hashFunction"]) { | ||
return Memoize({ | ||
ttlMs, | ||
hashFunction, | ||
}); | ||
} | ||
|
||
const clearCacheTagsMap: Map<string, Map<any, any>[]> = new Map(); | ||
|
||
export function clear(tags: string[]): number { | ||
const cleared: Set<Map<any, any>> = new Set(); | ||
for (const tag of tags) { | ||
const maps = clearCacheTagsMap.get(tag); | ||
if (maps) { | ||
for (const mp of maps) { | ||
if (!cleared.has(mp)) { | ||
mp.clear(); | ||
cleared.add(mp); | ||
} | ||
} | ||
} | ||
} | ||
return cleared.size; | ||
} | ||
|
||
function getNewFunction( | ||
originalMethod: () => void, | ||
hashFunction?: MemoizeArgs["hashFunction"], | ||
ttlMs: number = 0, | ||
tags?: MemoizeArgs["tags"], | ||
) { | ||
const propMapName = Symbol("__memoized_map__"); | ||
|
||
// The function returned here gets called instead of originalMethod. | ||
// eslint-disable-next-line func-names | ||
return function (...args: any[]) { | ||
let returnedValue: any; | ||
|
||
// Get or create map | ||
// eslint-disable-next-line no-prototype-builtins | ||
if (!this.hasOwnProperty(propMapName)) { | ||
Object.defineProperty(this, propMapName, { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: new Map<any, any>(), | ||
}); | ||
} | ||
const myMap: Map<any, any> = this[propMapName]; | ||
|
||
if (Array.isArray(tags)) { | ||
for (const tag of tags) { | ||
if (clearCacheTagsMap.has(tag)) { | ||
clearCacheTagsMap.get(tag).push(myMap); | ||
} else { | ||
clearCacheTagsMap.set(tag, [myMap]); | ||
} | ||
} | ||
} | ||
|
||
if (hashFunction || args.length > 0 || ttlMs > 0) { | ||
let hashKey: any; | ||
|
||
// If true is passed as first parameter, will automatically use every argument, passed to string | ||
if (hashFunction === true) { | ||
hashKey = args.map((a) => a.toString()).join("!"); | ||
} else if (hashFunction) { | ||
hashKey = hashFunction.apply(this, args); | ||
} else { | ||
// eslint-disable-next-line prefer-destructuring | ||
hashKey = args[0]; | ||
} | ||
|
||
const timestampKey = `${hashKey}__timestamp`; | ||
let isExpired: boolean = false; | ||
if (ttlMs > 0) { | ||
if (!myMap.has(timestampKey)) { | ||
// "Expired" since it was never called before | ||
isExpired = true; | ||
} else { | ||
const timestamp = myMap.get(timestampKey); | ||
isExpired = Date.now() - timestamp > ttlMs; | ||
} | ||
} | ||
|
||
if (myMap.has(hashKey) && !isExpired) { | ||
returnedValue = myMap.get(hashKey); | ||
} else { | ||
returnedValue = originalMethod.apply(this, args); | ||
myMap.set(hashKey, returnedValue); | ||
if (ttlMs > 0) { | ||
myMap.set(timestampKey, Date.now()); | ||
} | ||
} | ||
} else { | ||
const hashKey = this; | ||
if (myMap.has(hashKey)) { | ||
returnedValue = myMap.get(hashKey); | ||
} else { | ||
returnedValue = originalMethod.apply(this, args); | ||
myMap.set(hashKey, returnedValue); | ||
} | ||
} | ||
|
||
return returnedValue; | ||
}; | ||
} |
12 changes: 1 addition & 11 deletions
12
ecosystem/typescript/sdk/src/util.test.ts → ...tem/typescript/sdk/src/utils/misc.test.ts
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
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const NODE_URL = process.env.APTOS_NODE_URL; | ||
export const FAUCET_URL = process.env.APTOS_FAUCET_URL; | ||
test("noop", () => { | ||
// All TS files are compiled by default into the npm package | ||
// Adding this empty test allows us to: | ||
// 1. Guarantee that this test library won't get compiled | ||
// 2. Prevent jest from exploding when it finds a file with no tests in it | ||
}); |