@rayondigital/nest-dapr 0.9.11 → 0.9.13
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.
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { AbstractActor } from '@dapr/dapr';
|
|
2
|
+
export interface IState {
|
|
3
|
+
fromJSON(json: any): this;
|
|
4
|
+
toJSON(): any;
|
|
5
|
+
}
|
|
2
6
|
export declare class StatefulActor extends AbstractActor {
|
|
3
7
|
private stateProperties;
|
|
4
8
|
setState<T>(stateName: string, value: T): Promise<void>;
|
|
@@ -9,4 +13,5 @@ export declare class StatefulActor extends AbstractActor {
|
|
|
9
13
|
getAllState(): Promise<void>;
|
|
10
14
|
setAllStateFromProperties(): Promise<void>;
|
|
11
15
|
getState<T>(stateName: string, defaultValue?: T): Promise<T | undefined>;
|
|
16
|
+
private createInstance;
|
|
12
17
|
}
|
|
@@ -41,26 +41,41 @@ class StatefulActor extends dapr_1.AbstractActor {
|
|
|
41
41
|
onActivate: { get: () => super.onActivate }
|
|
42
42
|
});
|
|
43
43
|
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
-
this.stateProperties =
|
|
45
|
-
Reflect.getMetadata(constants_1.DAPR_ACTOR_STATE_METADATA, this.constructor) || [];
|
|
44
|
+
this.stateProperties = Reflect.getMetadata(constants_1.DAPR_ACTOR_STATE_METADATA, this.constructor) || [];
|
|
46
45
|
yield _super.onActivate.call(this);
|
|
47
46
|
yield this.getAllState();
|
|
48
47
|
});
|
|
49
48
|
}
|
|
50
49
|
getAllState() {
|
|
50
|
+
var _a;
|
|
51
51
|
return __awaiter(this, void 0, void 0, function* () {
|
|
52
52
|
for (const property of this.stateProperties) {
|
|
53
|
-
const
|
|
54
|
-
if (
|
|
53
|
+
const rawValue = yield this.getState(property.name);
|
|
54
|
+
if (rawValue === undefined) {
|
|
55
55
|
if (typeof property.defaultValue === 'function') {
|
|
56
56
|
this[property.key] = property.defaultValue();
|
|
57
57
|
}
|
|
58
|
-
else {
|
|
58
|
+
else if (property.defaultValue !== undefined) {
|
|
59
59
|
this[property.key] = property.defaultValue;
|
|
60
60
|
}
|
|
61
|
+
else {
|
|
62
|
+
this[property.key] = this.createInstance(property.type);
|
|
63
|
+
}
|
|
61
64
|
}
|
|
62
65
|
else {
|
|
63
|
-
|
|
66
|
+
if (property.serializable) {
|
|
67
|
+
let instance;
|
|
68
|
+
if (typeof property.defaultValue === 'function') {
|
|
69
|
+
instance = property.defaultValue();
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
instance = this.createInstance(property.type);
|
|
73
|
+
}
|
|
74
|
+
this[property.key] = (_a = instance.fromJSON(rawValue)) !== null && _a !== void 0 ? _a : instance;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
this[property.key] = rawValue;
|
|
78
|
+
}
|
|
64
79
|
}
|
|
65
80
|
}
|
|
66
81
|
});
|
|
@@ -68,7 +83,12 @@ class StatefulActor extends dapr_1.AbstractActor {
|
|
|
68
83
|
setAllStateFromProperties() {
|
|
69
84
|
return __awaiter(this, void 0, void 0, function* () {
|
|
70
85
|
for (const property of this.stateProperties) {
|
|
71
|
-
|
|
86
|
+
let value = this[property.key];
|
|
87
|
+
if (property.serializable) {
|
|
88
|
+
const instance = value;
|
|
89
|
+
value = instance.toJSON();
|
|
90
|
+
}
|
|
91
|
+
yield this.setState(property.name, value);
|
|
72
92
|
}
|
|
73
93
|
});
|
|
74
94
|
}
|
|
@@ -90,5 +110,13 @@ class StatefulActor extends dapr_1.AbstractActor {
|
|
|
90
110
|
}
|
|
91
111
|
});
|
|
92
112
|
}
|
|
113
|
+
createInstance(type) {
|
|
114
|
+
try {
|
|
115
|
+
return new type();
|
|
116
|
+
}
|
|
117
|
+
catch (e) {
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
93
121
|
}
|
|
94
122
|
exports.StatefulActor = StatefulActor;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { Type } from '@nestjs/common';
|
|
1
2
|
export interface StateProperty {
|
|
2
3
|
key: string | symbol;
|
|
3
4
|
name: string;
|
|
4
|
-
type: Function
|
|
5
|
+
type: Function | Type<any>;
|
|
5
6
|
defaultValue?: any;
|
|
7
|
+
serializable?: boolean;
|
|
6
8
|
}
|
|
7
9
|
export declare function State(options?: {
|
|
8
10
|
defaultValue?: any;
|
|
@@ -7,11 +7,13 @@ function State(options) {
|
|
|
7
7
|
var _a;
|
|
8
8
|
const properties = Reflect.getMetadata(constants_1.DAPR_ACTOR_STATE_METADATA, target.constructor) || [];
|
|
9
9
|
const type = Reflect.getMetadata('design:type', target, propertyKey);
|
|
10
|
+
const serializable = typeof type.prototype['fromJSON'] === 'function' && typeof type.prototype['toJSON'] === 'function';
|
|
10
11
|
properties.push({
|
|
11
12
|
key: propertyKey,
|
|
12
13
|
name: (_a = options === null || options === void 0 ? void 0 : options.name) !== null && _a !== void 0 ? _a : propertyKey.toString(),
|
|
13
14
|
type,
|
|
14
15
|
defaultValue: options === null || options === void 0 ? void 0 : options.defaultValue,
|
|
16
|
+
serializable,
|
|
15
17
|
});
|
|
16
18
|
Reflect.defineMetadata(constants_1.DAPR_ACTOR_STATE_METADATA, properties, target.constructor);
|
|
17
19
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DaprActorClient } from './actors/dapr-actor-client.service';
|
|
2
2
|
import { StatefulActorOf } from './actors/stateful-actor-of';
|
|
3
|
-
import { StatefulActor } from './actors/stateful.actor';
|
|
3
|
+
import { IState, StatefulActor } from './actors/stateful.actor';
|
|
4
4
|
import { DAPR_BINDING_METADATA, DAPR_PUBSUB_METADATA, DAPR_ACTOR_METADATA, DAPR_ACTOR_STATE_METADATA } from './constants';
|
|
5
5
|
import { State } from './dapr-actor-state.decorator';
|
|
6
6
|
import { DaprActor, DaprActorMetadata } from './dapr-actor.decorator';
|
|
@@ -10,4 +10,4 @@ import { DaprMetadataAccessor } from './dapr-metadata.accessor';
|
|
|
10
10
|
import { DaprPubSub, DaprPubSubMetadata } from './dapr-pubsub.decorator';
|
|
11
11
|
import { DaprLoader } from './dapr.loader';
|
|
12
12
|
import { DaprModule } from './dapr.module';
|
|
13
|
-
export { DAPR_BINDING_METADATA, DAPR_PUBSUB_METADATA, DAPR_ACTOR_METADATA, DAPR_ACTOR_STATE_METADATA, DaprMetadataAccessor, DaprBindingMetadata, DaprBinding, DaprPubSubMetadata, DaprPubSub, DaprActorMetadata, State, DaprActor, DaprLoader, DaprModule, DaprActorClient, DaprContextService, StatefulActor, StatefulActorOf, };
|
|
13
|
+
export { DAPR_BINDING_METADATA, DAPR_PUBSUB_METADATA, DAPR_ACTOR_METADATA, DAPR_ACTOR_STATE_METADATA, DaprMetadataAccessor, DaprBindingMetadata, DaprBinding, DaprPubSubMetadata, DaprPubSub, DaprActorMetadata, State, DaprActor, DaprLoader, DaprModule, DaprActorClient, DaprContextService, StatefulActor, StatefulActorOf, IState, };
|
package/package.json
CHANGED