-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cdk): add
tuiMemo
for decorator less way
- Loading branch information
Showing
3 changed files
with
128 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export function tuiMemo<T extends unknown[], Result>(fn: (...args: T) => Result) { | ||
const cache = new Map<string, Result>(); | ||
|
||
return function (...args: T): Result { | ||
const key = args.map((arg) => `${typeof arg}:${arg}`).join('|'); | ||
let value = cache.get(key); | ||
|
||
if (value === undefined && !cache.has(key)) { | ||
value = fn(...args); | ||
|
||
cache.set(key, value); | ||
} | ||
|
||
return value as Result; | ||
}; | ||
} |
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,111 @@ | ||
import {tuiMemo} from '@taiga-ui/cdk'; | ||
|
||
describe('tuiMemo', () => { | ||
it('calls getter only once and then sets result as a value property on the object', () => { | ||
class TestClass { | ||
private readonly memoized = tuiMemo(() => { | ||
this.count++; | ||
|
||
return 'test'; | ||
}); | ||
|
||
public count = 0; | ||
|
||
public get someGetter(): string { | ||
return this.memoized(); | ||
} | ||
} | ||
|
||
const testObject = new TestClass(); | ||
|
||
expect(testObject.count).toBe(0); | ||
expect(testObject.someGetter).toBe('test'); | ||
expect(testObject.count).toBe(1); | ||
expect(testObject.someGetter).toBe('test'); | ||
expect(testObject.someGetter).toBe('test'); | ||
expect(testObject.count).toBe(1); | ||
}); | ||
|
||
it('memorizes method result', () => { | ||
const name = 'Alex'; | ||
const age = 30; | ||
|
||
class TestClass { | ||
public readonly combine = tuiMemo(this.memoized); | ||
private memoized(name: string, age: number): [string, number] { | ||
return [name, age]; | ||
} | ||
} | ||
|
||
const testObject = new TestClass(); | ||
const result = testObject.combine(name, age); | ||
|
||
expect(testObject.combine(name, age)).toBe(result); | ||
expect(testObject.combine(name, age)).toBe(result); | ||
}); | ||
|
||
it('has access to this', () => { | ||
const name = 'Alex'; | ||
const age = 30; | ||
|
||
class TestClass { | ||
public readonly combine = tuiMemo(this.memoized.bind(this)); | ||
|
||
constructor(public readonly prefix: string) {} | ||
|
||
public memoized(name: string, age: number): [string, string, number] { | ||
return [this.prefix, name, age]; | ||
} | ||
} | ||
|
||
const testObject = new TestClass('awesome'); | ||
const result = testObject.combine(name, age); | ||
|
||
expect(testObject.combine(name, age)).toBe(result); | ||
}); | ||
|
||
it('memoize function without arguments', () => { | ||
class TestClass { | ||
public sideEffect = 0; | ||
public readonly getRange = tuiMemo(this.memoized.bind(this)); | ||
|
||
public get range(): number[] { | ||
return this.getRange(); | ||
} | ||
|
||
public memoized(): number[] { | ||
this.sideEffect++; | ||
|
||
return Array.from({length: 5}, (_item, index: number) => index + 1); | ||
} | ||
} | ||
|
||
const testObject = new TestClass(); | ||
|
||
expect(testObject.range).toEqual([1, 2, 3, 4, 5]); | ||
expect(testObject.range).toEqual([1, 2, 3, 4, 5]); | ||
expect(testObject.range).toEqual([1, 2, 3, 4, 5]); | ||
expect(testObject.sideEffect).toBe(1); | ||
}); | ||
|
||
it('memoize function without arguments and return undefined by default', () => { | ||
class TestClass { | ||
public sideEffect = 0; | ||
public readonly voidFn = tuiMemo(this.memoized.bind(this)); | ||
|
||
public memoized(): void { | ||
this.sideEffect++; | ||
|
||
// necessary for the test | ||
return undefined; | ||
} | ||
} | ||
|
||
const testObject = new TestClass(); | ||
|
||
expect(testObject.voidFn()).toBeUndefined(); | ||
expect(testObject.voidFn()).toBeUndefined(); | ||
expect(testObject.voidFn()).toBeUndefined(); | ||
expect(testObject.sideEffect).toBe(1); | ||
}); | ||
}); |