Skip to content

Commit efb7c0f

Browse files
committed
fix(withCache) bind this context
1 parent c1c1d1d commit efb7c0f

File tree

8 files changed

+80
-5
lines changed

8 files changed

+80
-5
lines changed

packages/core/src/cache/createWithCache/createWithCache.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function createWithCache<T extends AnyFunction>({
4545
argToKeyOptions,
4646
};
4747

48-
const wrapFn = ((...args: Parameters<T>) => {
48+
const wrapFn = function (this: any, ...args: Parameters<T>) {
4949
const storage = getBucket(getPointer());
5050
const cacheKey = args.map(v => argToKey(v, argToKeyOptions)).join('_');
5151

@@ -54,7 +54,7 @@ export function createWithCache<T extends AnyFunction>({
5454
return isAsync ? Promise.resolve(value) : value;
5555
}
5656

57-
const newValue = fn(...args);
57+
const newValue = fn.apply(this, args);
5858

5959
if (isPromise(newValue)) {
6060
// cache only success result
@@ -67,7 +67,7 @@ export function createWithCache<T extends AnyFunction>({
6767
storage.set(cacheKey, newValue);
6868

6969
return newValue;
70-
}) as T;
70+
} as T;
7171

7272
(wrapFn as any).$cache = $cache;
7373

packages/core/src/cache/withCache/withCache.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ describe('withCache', () => {
88
expect(rnd()).toBe(rnd());
99
});
1010

11+
test('should keep this context', () => {
12+
const fn = withCache(function (this: any) {
13+
return this;
14+
});
15+
16+
const context = Symbol();
17+
18+
expect(fn.call(context)).toBe(context);
19+
});
20+
1121
test('object arguments must be handled (strategy: ref)', () => {
1222
const user = { id: 1, name: 'Andrew L.' };
1323

packages/core/src/cache/withCacheBucket/withCacheBucket.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ describe('withCacheBucket', () => {
1010
expect(rnd()).toBe(rnd());
1111
});
1212

13+
test('should keep this context', () => {
14+
const fn = withCacheBucket(
15+
{ sizeMs: 100, capacity: 1 },
16+
function (this: any) {
17+
return this;
18+
},
19+
);
20+
21+
const context = Symbol();
22+
23+
expect(fn.call(context)).toBe(context);
24+
});
25+
1326
test('check size ms', async () => {
1427
let called = 0;
1528

packages/core/src/cache/withCacheBucketBatch/withCacheBucketBatch.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@ import { describe, expect, it } from 'vitest';
22
import { withCacheBucketBatch } from './withCacheBucketBatch';
33

44
describe('withCacheBucketBatch', () => {
5+
it('should keep this context', async () => {
6+
let tracked: any;
7+
const fn = withCacheBucketBatch(
8+
{
9+
key: 'id',
10+
sizeMs: 1000,
11+
capacity: 10,
12+
batchSize: 2,
13+
},
14+
async function (this: any, values: string[]) {
15+
tracked = this;
16+
return [];
17+
},
18+
);
19+
20+
const context = Symbol();
21+
// @ts-expect-error
22+
await fn.call(context, [{ _id: 'test' }]);
23+
expect(tracked).toBe(context);
24+
});
25+
526
it('should cache objects', async () => {
627
const records: Record<string, { id: string; name: string; gen: number }> = {
728
'1': { id: '1', name: 'John', gen: 0 },

packages/core/src/cache/withCacheBucketBatch/withCacheBucketBatch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export function withCacheBucketBatch<T extends object, K extends keyof T>(
9393
return fnCache;
9494
};
9595

96-
const wrapFn = async (values: any) => {
96+
const wrapFn = async function (this: any, values: any) {
9797
const result = new Map();
9898

9999
let fnCache = getBucket();
@@ -103,7 +103,7 @@ export function withCacheBucketBatch<T extends object, K extends keyof T>(
103103
const drainMaybe = async () => {
104104
if (!batchSet.size) return;
105105

106-
const items = await resolver(Array.from(batchSet));
106+
const items = await resolver.call(this, Array.from(batchSet));
107107

108108
for (let idx = 0; idx < items.length; idx++) {
109109
const item = items[idx];

packages/core/src/cache/withCacheFixed/withCacheFixed.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ describe('withCacheFixed', () => {
88
expect(rnd()).toBe(rnd());
99
});
1010

11+
test('should keep this context', () => {
12+
const fn = withCacheFixed({ capacity: 1 }, function (this: any) {
13+
return this;
14+
});
15+
16+
const context = Symbol();
17+
18+
expect(fn.call(context)).toBe(context);
19+
});
20+
1121
test('check capacity', () => {
1222
let called = 0;
1323

packages/core/src/cache/withCacheLRU/withCacheLRU.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ describe('withCacheLRU', () => {
88
expect(rnd()).toBe(rnd());
99
});
1010

11+
test('should keep this context', () => {
12+
const fn = withCacheLRU({ capacity: 1 }, function (this: any) {
13+
return this;
14+
});
15+
16+
const context = Symbol();
17+
18+
expect(fn.call(context)).toBe(context);
19+
});
20+
1121
test('check capacity', () => {
1222
let called = 0;
1323

packages/core/src/cache/withDeepClone/withDeepClone.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,15 @@ describe('withDeepClone', () => {
1212

1313
expect(user.id).lt(user2.id);
1414
});
15+
16+
test('should keep this context', () => {
17+
let tracked: any;
18+
const fn = withDeepClone(function (this: any) {
19+
tracked = this;
20+
});
21+
22+
const context = Symbol();
23+
fn.call(context);
24+
expect(tracked).toBe(context);
25+
});
1526
});

0 commit comments

Comments
 (0)