@rayondigital/nest-dapr 0.9.46 → 0.9.47
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.
|
@@ -14,13 +14,13 @@ const stateful_actor_1 = require("./stateful.actor");
|
|
|
14
14
|
class StatefulActorOf extends stateful_actor_1.StatefulActor {
|
|
15
15
|
saveStateObject() {
|
|
16
16
|
return __awaiter(this, void 0, void 0, function* () {
|
|
17
|
-
yield this.
|
|
17
|
+
yield this.setStateValue('state', this.state);
|
|
18
18
|
yield this.getStateManager().saveState();
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
21
|
getStateObject() {
|
|
22
22
|
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
-
return yield this.
|
|
23
|
+
return yield this.getStateValue('state');
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
26
|
}
|
|
@@ -1,18 +1,29 @@
|
|
|
1
|
+
import { Logger } from '@nestjs/common';
|
|
2
|
+
import { DaprContextService } from '../dapr-context-service';
|
|
1
3
|
import { AbstractActor } from './abstract-actor';
|
|
4
|
+
import { DaprActorClient } from './dapr-actor-client.service';
|
|
5
|
+
import { ActorId, DaprClient } from '@dapr/dapr';
|
|
2
6
|
export interface IState {
|
|
3
7
|
fromJSON(json: any): this;
|
|
4
8
|
toJSON(): any;
|
|
5
9
|
}
|
|
6
10
|
export declare class StatefulActor extends AbstractActor {
|
|
7
|
-
private
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
private statePropertiesInternal;
|
|
12
|
+
protected readonly client: DaprActorClient;
|
|
13
|
+
protected readonly contextService: DaprContextService;
|
|
14
|
+
protected logger: Logger;
|
|
15
|
+
constructor(daprClient: DaprClient, id: ActorId);
|
|
12
16
|
onActivate(): Promise<void>;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
protected logMessage(message: any, level?: 'log' | 'error' | 'warn' | 'debug'): void;
|
|
18
|
+
protected log(message: any, ...optionalParams: [...any, string?]): void;
|
|
19
|
+
protected verbose(message: any, ...optionalParams: [...any, string?]): void;
|
|
20
|
+
protected warn(message: any, ...optionalParams: [...any, string?]): void;
|
|
21
|
+
protected setStateValue<T>(stateName: string, value: T): Promise<void>;
|
|
22
|
+
protected removeStateValue(stateName: string): Promise<void>;
|
|
23
|
+
protected saveState(): Promise<void>;
|
|
24
|
+
protected clearState(): Promise<void>;
|
|
25
|
+
protected getAllState(): Promise<void>;
|
|
26
|
+
private loadStatePropertyInternal;
|
|
27
|
+
private setAllStateFromPropertiesInternal;
|
|
28
|
+
protected getStateValue<T>(stateName: string, defaultValue?: T): Promise<T | undefined>;
|
|
18
29
|
}
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
2
11
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
12
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
13
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -10,82 +19,125 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
19
|
};
|
|
11
20
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
21
|
exports.StatefulActor = void 0;
|
|
22
|
+
const common_1 = require("@nestjs/common");
|
|
13
23
|
const constants_1 = require("../constants");
|
|
24
|
+
const dapr_context_service_1 = require("../dapr-context-service");
|
|
14
25
|
const abstract_actor_1 = require("./abstract-actor");
|
|
26
|
+
const dapr_actor_client_service_1 = require("./dapr-actor-client.service");
|
|
15
27
|
class StatefulActor extends abstract_actor_1.AbstractActor {
|
|
16
|
-
|
|
28
|
+
constructor(daprClient, id) {
|
|
29
|
+
super(daprClient, id);
|
|
30
|
+
this.logger = new common_1.Logger(this.constructor.name);
|
|
31
|
+
}
|
|
32
|
+
onActivate() {
|
|
33
|
+
const _super = Object.create(null, {
|
|
34
|
+
onActivate: { get: () => super.onActivate }
|
|
35
|
+
});
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
this.statePropertiesInternal =
|
|
38
|
+
Reflect.getMetadata(constants_1.DAPR_ACTOR_STATE_METADATA, this.constructor) || [];
|
|
39
|
+
yield _super.onActivate.call(this);
|
|
40
|
+
yield this.getAllState();
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
logMessage(message, level = 'log') {
|
|
44
|
+
var _a;
|
|
45
|
+
switch (level) {
|
|
46
|
+
case 'log':
|
|
47
|
+
this.logger.log(message, this.constructor.name);
|
|
48
|
+
break;
|
|
49
|
+
case 'error': {
|
|
50
|
+
const stack = (_a = message.stack) !== null && _a !== void 0 ? _a : new Error().stack;
|
|
51
|
+
this.logger.error(message, stack, this.constructor.name);
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
case 'warn':
|
|
55
|
+
this.logger.warn(message, this.constructor.name);
|
|
56
|
+
break;
|
|
57
|
+
case 'debug':
|
|
58
|
+
this.logger.debug(message, this.constructor.name);
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
log(message, ...optionalParams) {
|
|
63
|
+
this.logger.log(message, ...optionalParams);
|
|
64
|
+
}
|
|
65
|
+
verbose(message, ...optionalParams) {
|
|
66
|
+
this.logger.verbose(message, ...optionalParams);
|
|
67
|
+
}
|
|
68
|
+
warn(message, ...optionalParams) {
|
|
69
|
+
this.logger.warn(message, ...optionalParams);
|
|
70
|
+
}
|
|
71
|
+
setStateValue(stateName, value) {
|
|
17
72
|
return __awaiter(this, void 0, void 0, function* () {
|
|
18
73
|
yield this.getStateManager().setState(stateName, value);
|
|
19
74
|
});
|
|
20
75
|
}
|
|
21
|
-
|
|
76
|
+
removeStateValue(stateName) {
|
|
22
77
|
return __awaiter(this, void 0, void 0, function* () {
|
|
23
78
|
yield this.getStateManager().removeState(stateName);
|
|
24
79
|
});
|
|
25
80
|
}
|
|
26
81
|
saveState() {
|
|
27
82
|
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
-
yield this.
|
|
83
|
+
yield this.setAllStateFromPropertiesInternal();
|
|
29
84
|
yield this.getStateManager().saveState();
|
|
30
85
|
});
|
|
31
86
|
}
|
|
32
87
|
clearState() {
|
|
33
88
|
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
-
for (const property of this.
|
|
35
|
-
yield this.
|
|
89
|
+
for (const property of this.statePropertiesInternal) {
|
|
90
|
+
yield this.removeStateValue(property.name);
|
|
36
91
|
}
|
|
37
92
|
});
|
|
38
93
|
}
|
|
39
|
-
|
|
40
|
-
const _super = Object.create(null, {
|
|
41
|
-
onActivate: { get: () => super.onActivate }
|
|
42
|
-
});
|
|
94
|
+
getAllState() {
|
|
43
95
|
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
96
|
+
for (const property of this.statePropertiesInternal) {
|
|
97
|
+
yield this.loadStatePropertyInternal(property);
|
|
98
|
+
}
|
|
47
99
|
});
|
|
48
100
|
}
|
|
49
|
-
|
|
101
|
+
loadStatePropertyInternal(property) {
|
|
50
102
|
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
this[property.key] = property.defaultValue;
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
this[property.key] = this.createInstance(property.type);
|
|
62
|
-
}
|
|
103
|
+
const rawValue = yield this.getStateValue(property.name);
|
|
104
|
+
if (rawValue === undefined) {
|
|
105
|
+
if (typeof property.defaultValue === 'function') {
|
|
106
|
+
this[property.key] = property.defaultValue();
|
|
107
|
+
}
|
|
108
|
+
else if (property.defaultValue !== undefined) {
|
|
109
|
+
this[property.key] = property.defaultValue;
|
|
63
110
|
}
|
|
64
111
|
else {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
112
|
+
this[property.key] = createInstanceOfType(property.type);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
const instance = createInstanceFromProperty(property, rawValue);
|
|
117
|
+
if (instance !== undefined) {
|
|
118
|
+
this[property.key] = instance;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
this[property.key] = rawValue;
|
|
72
122
|
}
|
|
73
123
|
}
|
|
74
124
|
});
|
|
75
125
|
}
|
|
76
|
-
|
|
126
|
+
setAllStateFromPropertiesInternal() {
|
|
77
127
|
return __awaiter(this, void 0, void 0, function* () {
|
|
78
|
-
for (const property of this.
|
|
128
|
+
for (const property of this.statePropertiesInternal) {
|
|
79
129
|
let value = this[property.key];
|
|
80
130
|
if (value !== undefined && property.serializable) {
|
|
81
131
|
const instance = value;
|
|
82
|
-
|
|
132
|
+
if (typeof instance.toJSON === 'function') {
|
|
133
|
+
value = instance === null || instance === void 0 ? void 0 : instance.toJSON();
|
|
134
|
+
}
|
|
83
135
|
}
|
|
84
|
-
yield this.
|
|
136
|
+
yield this.setStateValue(property.name, value);
|
|
85
137
|
}
|
|
86
138
|
});
|
|
87
139
|
}
|
|
88
|
-
|
|
140
|
+
getStateValue(stateName, defaultValue) {
|
|
89
141
|
return __awaiter(this, void 0, void 0, function* () {
|
|
90
142
|
try {
|
|
91
143
|
const value = yield this.getStateManager().getState(stateName);
|
|
@@ -103,54 +155,62 @@ class StatefulActor extends abstract_actor_1.AbstractActor {
|
|
|
103
155
|
}
|
|
104
156
|
});
|
|
105
157
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
instance[key] = rawValue[key];
|
|
124
|
-
}
|
|
125
|
-
}
|
|
158
|
+
}
|
|
159
|
+
__decorate([
|
|
160
|
+
(0, common_1.Inject)(),
|
|
161
|
+
__metadata("design:type", dapr_actor_client_service_1.DaprActorClient)
|
|
162
|
+
], StatefulActor.prototype, "client", void 0);
|
|
163
|
+
__decorate([
|
|
164
|
+
(0, common_1.Inject)(),
|
|
165
|
+
__metadata("design:type", dapr_context_service_1.DaprContextService)
|
|
166
|
+
], StatefulActor.prototype, "contextService", void 0);
|
|
167
|
+
exports.StatefulActor = StatefulActor;
|
|
168
|
+
function createInstanceFromProperty(property, rawValue) {
|
|
169
|
+
var _a;
|
|
170
|
+
try {
|
|
171
|
+
if (property.serializable) {
|
|
172
|
+
let instance;
|
|
173
|
+
if (typeof property.defaultValue === 'function') {
|
|
174
|
+
instance = property.defaultValue();
|
|
126
175
|
}
|
|
127
176
|
else {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
for (const key of Object.keys(rawValue)) {
|
|
137
|
-
instance[key] = rawValue[key];
|
|
138
|
-
}
|
|
139
|
-
return instance;
|
|
177
|
+
instance = createInstanceOfType(property.type);
|
|
178
|
+
}
|
|
179
|
+
if (instance.fromJSON) {
|
|
180
|
+
return (_a = instance.fromJSON(rawValue)) !== null && _a !== void 0 ? _a : instance;
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
for (const key of Object.keys(rawValue)) {
|
|
184
|
+
instance[key] = rawValue[key];
|
|
140
185
|
}
|
|
186
|
+
return instance;
|
|
141
187
|
}
|
|
142
188
|
}
|
|
143
|
-
|
|
144
|
-
|
|
189
|
+
else {
|
|
190
|
+
let instance;
|
|
191
|
+
if (typeof property.defaultValue === 'function') {
|
|
192
|
+
instance = property.defaultValue();
|
|
193
|
+
}
|
|
194
|
+
else if (property.type) {
|
|
195
|
+
instance = createInstanceOfType(property.type);
|
|
196
|
+
}
|
|
197
|
+
if (instance !== undefined) {
|
|
198
|
+
for (const key of Object.keys(rawValue)) {
|
|
199
|
+
instance[key] = rawValue[key];
|
|
200
|
+
}
|
|
201
|
+
return instance;
|
|
202
|
+
}
|
|
145
203
|
}
|
|
146
204
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
205
|
+
catch (e) {
|
|
206
|
+
return undefined;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
function createInstanceOfType(type) {
|
|
210
|
+
try {
|
|
211
|
+
return new type();
|
|
212
|
+
}
|
|
213
|
+
catch (e) {
|
|
214
|
+
return undefined;
|
|
154
215
|
}
|
|
155
216
|
}
|
|
156
|
-
exports.StatefulActor = StatefulActor;
|
package/package.json
CHANGED