@travetto/cache 6.0.0 → 6.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -5
- package/src/decorator.ts +12 -0
- package/support/transformer.cache.ts +0 -68
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/cache",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.1",
|
|
4
4
|
"description": "Caching functionality with decorators for declarative use.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -25,12 +25,12 @@
|
|
|
25
25
|
"directory": "module/cache"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@travetto/di": "^6.0.
|
|
29
|
-
"@travetto/model": "^6.0.
|
|
28
|
+
"@travetto/di": "^6.0.1",
|
|
29
|
+
"@travetto/model": "^6.0.1"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@travetto/test": "^6.0.
|
|
33
|
-
"@travetto/transformer": "^6.0.
|
|
32
|
+
"@travetto/test": "^6.0.2",
|
|
33
|
+
"@travetto/transformer": "^6.0.1"
|
|
34
34
|
},
|
|
35
35
|
"peerDependenciesMeta": {
|
|
36
36
|
"@travetto/transformer": {
|
package/src/decorator.ts
CHANGED
|
@@ -24,6 +24,12 @@ export function Cache<F extends string, U extends Record<F, CacheService>>(
|
|
|
24
24
|
const dec = function <R extends Promise<unknown>>(target: U & CacheAware, propertyKey: string, _descriptor: MethodDescriptor<R>): void {
|
|
25
25
|
config.keySpace ??= `${target.constructor.name}.${propertyKey}`;
|
|
26
26
|
(target[CacheConfigSymbol] ??= {})[propertyKey] = config;
|
|
27
|
+
const handler = _descriptor.value!.bind(castTo(target));
|
|
28
|
+
// Allows for DI to run, as the service will not be bound until after the decorator is run
|
|
29
|
+
_descriptor.value = castTo(function (this: typeof target) {
|
|
30
|
+
return this[field].cache(this, propertyKey, handler, [...arguments]);
|
|
31
|
+
});
|
|
32
|
+
Object.defineProperty(_descriptor.value, 'name', { value: propertyKey, writable: false });
|
|
27
33
|
};
|
|
28
34
|
return castTo(dec);
|
|
29
35
|
}
|
|
@@ -39,5 +45,11 @@ export function EvictCache<F extends string, U extends Record<F, CacheService>>(
|
|
|
39
45
|
return function <R extends Promise<unknown>>(target: U & CacheAware, propertyKey: string, _descriptor: MethodDescriptor<R>): void {
|
|
40
46
|
config.keySpace ??= `${target.constructor.name}.${propertyKey}`;
|
|
41
47
|
(target[EvictConfigSymbol] ??= {})[propertyKey] = config;
|
|
48
|
+
const handler = _descriptor.value!.bind(castTo(target));
|
|
49
|
+
// Allows for DI to run, as the service will not be bound until after the decorator is run
|
|
50
|
+
_descriptor.value = castTo(function (this: typeof target) {
|
|
51
|
+
return this[field].evict(this, propertyKey, handler, [...arguments]);
|
|
52
|
+
});
|
|
53
|
+
Object.defineProperty(_descriptor.value, 'name', { value: propertyKey, writable: false });
|
|
42
54
|
};
|
|
43
55
|
}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import ts from 'typescript';
|
|
2
|
-
|
|
3
|
-
import { TransformerState, DecoratorMeta, OnMethod } from '@travetto/transformer';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Transform the cache headers
|
|
7
|
-
*/
|
|
8
|
-
export class CacheTransformer {
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* When `@Cache` and `@Evict` are present
|
|
12
|
-
*/
|
|
13
|
-
@OnMethod('Cache', 'Evict')
|
|
14
|
-
static instrumentCache(state: TransformerState, node: ts.MethodDeclaration, dm?: DecoratorMeta): ts.MethodDeclaration {
|
|
15
|
-
|
|
16
|
-
const isCache = !!state.findDecorator(this, node, 'Cache');
|
|
17
|
-
const dec = dm?.dec;
|
|
18
|
-
|
|
19
|
-
// If valid function
|
|
20
|
-
if (dec && ts.isCallExpression(dec.expression)) {
|
|
21
|
-
const params = dec.expression.arguments;
|
|
22
|
-
const mainExpression = params[0];
|
|
23
|
-
|
|
24
|
-
const op = isCache ? 'cache' : 'evict';
|
|
25
|
-
|
|
26
|
-
// Create an arrow function to retain the `this` value.
|
|
27
|
-
const fn = state.factory.createArrowFunction(
|
|
28
|
-
[state.factory.createModifier(ts.SyntaxKind.AsyncKeyword)],
|
|
29
|
-
undefined,
|
|
30
|
-
[],
|
|
31
|
-
undefined,
|
|
32
|
-
undefined,
|
|
33
|
-
node.body!
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
// Return new method calling evict or cache depending on decorator.
|
|
37
|
-
return state.factory.updateMethodDeclaration(
|
|
38
|
-
node,
|
|
39
|
-
node.modifiers,
|
|
40
|
-
node.asteriskToken,
|
|
41
|
-
node.name,
|
|
42
|
-
node.questionToken,
|
|
43
|
-
node.typeParameters,
|
|
44
|
-
node.parameters,
|
|
45
|
-
node.type,
|
|
46
|
-
state.factory.createBlock([
|
|
47
|
-
state.factory.createReturnStatement(
|
|
48
|
-
state.factory.createCallExpression(
|
|
49
|
-
state.factory.createPropertyAccessExpression(
|
|
50
|
-
state.factory.createElementAccessExpression(state.factory.createThis(), mainExpression), op
|
|
51
|
-
),
|
|
52
|
-
undefined,
|
|
53
|
-
[
|
|
54
|
-
state.factory.createThis(),
|
|
55
|
-
state.factory.createStringLiteral(node.name.getText()),
|
|
56
|
-
fn,
|
|
57
|
-
state.factory.createArrayLiteralExpression([
|
|
58
|
-
state.factory.createSpreadElement(state.createIdentifier('arguments'))
|
|
59
|
-
])
|
|
60
|
-
]
|
|
61
|
-
)
|
|
62
|
-
)
|
|
63
|
-
])
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
return node;
|
|
67
|
-
}
|
|
68
|
-
}
|