@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.setState('state', this.state);
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.getState('state');
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 stateProperties;
8
- setState<T>(stateName: string, value: T): Promise<void>;
9
- removeState(stateName: string): Promise<void>;
10
- saveState(): Promise<void>;
11
- clearState(): Promise<void>;
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
- getAllState(): Promise<void>;
14
- setAllStateFromProperties(): Promise<void>;
15
- getState<T>(stateName: string, defaultValue?: T): Promise<T | undefined>;
16
- private createInstanceFromProperty;
17
- private createInstance;
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
- setState(stateName, value) {
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
- removeState(stateName) {
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.setAllStateFromProperties();
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.stateProperties) {
35
- yield this.removeState(property.name);
89
+ for (const property of this.statePropertiesInternal) {
90
+ yield this.removeStateValue(property.name);
36
91
  }
37
92
  });
38
93
  }
39
- onActivate() {
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
- this.stateProperties = Reflect.getMetadata(constants_1.DAPR_ACTOR_STATE_METADATA, this.constructor) || [];
45
- yield _super.onActivate.call(this);
46
- yield this.getAllState();
96
+ for (const property of this.statePropertiesInternal) {
97
+ yield this.loadStatePropertyInternal(property);
98
+ }
47
99
  });
48
100
  }
49
- getAllState() {
101
+ loadStatePropertyInternal(property) {
50
102
  return __awaiter(this, void 0, void 0, function* () {
51
- for (const property of this.stateProperties) {
52
- const rawValue = yield this.getState(property.name);
53
- if (rawValue === undefined) {
54
- if (typeof property.defaultValue === 'function') {
55
- this[property.key] = property.defaultValue();
56
- }
57
- else if (property.defaultValue !== undefined) {
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
- const instance = this.createInstanceFromProperty(property, rawValue);
66
- if (instance !== undefined) {
67
- this[property.key] = instance;
68
- }
69
- else {
70
- this[property.key] = rawValue;
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
- setAllStateFromProperties() {
126
+ setAllStateFromPropertiesInternal() {
77
127
  return __awaiter(this, void 0, void 0, function* () {
78
- for (const property of this.stateProperties) {
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
- value = instance.toJSON();
132
+ if (typeof instance.toJSON === 'function') {
133
+ value = instance === null || instance === void 0 ? void 0 : instance.toJSON();
134
+ }
83
135
  }
84
- yield this.setState(property.name, value);
136
+ yield this.setStateValue(property.name, value);
85
137
  }
86
138
  });
87
139
  }
88
- getState(stateName, defaultValue = undefined) {
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
- createInstanceFromProperty(property, rawValue) {
107
- var _a;
108
- try {
109
- if (property.serializable) {
110
- let instance;
111
- if (typeof property.defaultValue === 'function') {
112
- instance = property.defaultValue();
113
- }
114
- else {
115
- instance = this.createInstance(property.type);
116
- }
117
- if (instance.fromJSON) {
118
- this[property.key] = (_a = instance.fromJSON(rawValue)) !== null && _a !== void 0 ? _a : instance;
119
- }
120
- else {
121
- this[property.key] = instance;
122
- for (const key of Object.keys(rawValue)) {
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
- let instance;
129
- if (typeof property.defaultValue === 'function') {
130
- instance = property.defaultValue();
131
- }
132
- else if (property.type) {
133
- instance = this.createInstance(property.type);
134
- }
135
- if (instance !== undefined) {
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
- catch (e) {
144
- return undefined;
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
- createInstance(type) {
148
- try {
149
- return new type();
150
- }
151
- catch (e) {
152
- return undefined;
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rayondigital/nest-dapr",
3
- "version": "0.9.46",
3
+ "version": "0.9.47",
4
4
  "description": "Develop NestJs microservices using Dapr pubsub, actors and other bindings",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",