@rayondigital/nest-dapr 0.9.17 → 0.9.18
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.
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import ActorId from '@dapr/dapr/actors/ActorId';
|
|
2
|
+
import ActorStateManager from '@dapr/dapr/actors/runtime/ActorStateManager';
|
|
3
|
+
import StateProvider from '@dapr/dapr/actors/runtime/StateProvider';
|
|
4
|
+
import DaprClient from '@dapr/dapr/implementation/Client/DaprClient';
|
|
5
|
+
import { Temporal } from '@js-temporal/polyfill';
|
|
6
|
+
export declare abstract class AbstractActor {
|
|
7
|
+
private readonly stateManager;
|
|
8
|
+
private readonly id;
|
|
9
|
+
private readonly daprClient;
|
|
10
|
+
private readonly actorClient;
|
|
11
|
+
private readonly daprStateProvider;
|
|
12
|
+
private readonly actorType;
|
|
13
|
+
private readonly daprLogger;
|
|
14
|
+
constructor(daprClient: DaprClient, id: ActorId);
|
|
15
|
+
registerActorReminder<_Type>(reminderName: string, dueTime: Temporal.Duration, period?: Temporal.Duration, ttl?: Temporal.Duration, state?: any): Promise<void>;
|
|
16
|
+
unregisterActorReminder(reminderName: string): Promise<void>;
|
|
17
|
+
registerActorTimer(timerName: string, callback: string, dueTime: Temporal.Duration, period?: Temporal.Duration, ttl?: Temporal.Duration, state?: any): Promise<void>;
|
|
18
|
+
unregisterActorTimer(timerName: string): Promise<void>;
|
|
19
|
+
onActivateInternal(): Promise<void>;
|
|
20
|
+
onDeactivateInternal(): Promise<void>;
|
|
21
|
+
onActorMethodPreInternal(): Promise<void>;
|
|
22
|
+
onActorMethodPostInternal(): Promise<void>;
|
|
23
|
+
resetStateInternal(): Promise<void>;
|
|
24
|
+
saveStateInternal(): Promise<void>;
|
|
25
|
+
onActivate(): Promise<void>;
|
|
26
|
+
onDeactivate(): Promise<void>;
|
|
27
|
+
onActorMethodPre(): Promise<void>;
|
|
28
|
+
onActorMethodPost(): Promise<void>;
|
|
29
|
+
receiveReminder(_data: string): Promise<void>;
|
|
30
|
+
getDaprClient(): DaprClient;
|
|
31
|
+
getStateProvider(): StateProvider;
|
|
32
|
+
getStateManager<T>(): ActorStateManager<T>;
|
|
33
|
+
getActorId(): ActorId;
|
|
34
|
+
getActorType(): any;
|
|
35
|
+
getId(): string;
|
|
36
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.AbstractActor = void 0;
|
|
16
|
+
const ActorStateManager_1 = __importDefault(require("@dapr/dapr/actors/runtime/ActorStateManager"));
|
|
17
|
+
const Logger_1 = require("@dapr/dapr/logger/Logger");
|
|
18
|
+
const polyfill_1 = require("@js-temporal/polyfill");
|
|
19
|
+
const client_cache_1 = require("./client-cache");
|
|
20
|
+
class AbstractActor {
|
|
21
|
+
constructor(daprClient, id) {
|
|
22
|
+
this.daprClient = daprClient;
|
|
23
|
+
this.actorClient = client_cache_1.DaprClientCache.getOrCreateActorClientFromOptions(daprClient.options);
|
|
24
|
+
this.daprLogger = new Logger_1.Logger('Actors', 'AbstractActor', daprClient.options.logger);
|
|
25
|
+
this.id = id;
|
|
26
|
+
this.stateManager = new ActorStateManager_1.default(this);
|
|
27
|
+
this.daprStateProvider = client_cache_1.DaprClientCache.getOrCreateStateProviderFromOptions(daprClient.options);
|
|
28
|
+
this.actorType = this.constructor.name;
|
|
29
|
+
}
|
|
30
|
+
registerActorReminder(reminderName, dueTime, period, ttl, state) {
|
|
31
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
32
|
+
yield this.actorClient.actor.registerActorReminder(this.actorType, this.id, reminderName, {
|
|
33
|
+
period: period !== null && period !== void 0 ? period : polyfill_1.Temporal.Duration.from({ hours: 1 }),
|
|
34
|
+
dueTime,
|
|
35
|
+
ttl,
|
|
36
|
+
data: state,
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
unregisterActorReminder(reminderName) {
|
|
41
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
yield this.actorClient.actor.unregisterActorReminder(this.actorType, this.id, reminderName);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
registerActorTimer(timerName, callback, dueTime, period, ttl, state) {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
return yield this.actorClient.actor.registerActorTimer(this.actorType, this.id, timerName, {
|
|
48
|
+
period: period !== null && period !== void 0 ? period : polyfill_1.Temporal.Duration.from({ hours: 12 }),
|
|
49
|
+
dueTime,
|
|
50
|
+
ttl,
|
|
51
|
+
data: state,
|
|
52
|
+
callback,
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
unregisterActorTimer(timerName) {
|
|
57
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
58
|
+
yield this.actorClient.actor.unregisterActorTimer(this.actorType, this.id, timerName);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
onActivateInternal() {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63
|
+
yield this.resetStateInternal();
|
|
64
|
+
yield this.onActivate();
|
|
65
|
+
yield this.saveStateInternal();
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
onDeactivateInternal() {
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
yield this.resetStateInternal();
|
|
71
|
+
yield this.onDeactivate();
|
|
72
|
+
yield this.saveStateInternal();
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
onActorMethodPreInternal() {
|
|
76
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
77
|
+
yield this.onActorMethodPre();
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
onActorMethodPostInternal() {
|
|
81
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
yield this.onActorMethodPost();
|
|
83
|
+
yield this.saveStateInternal();
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
resetStateInternal() {
|
|
87
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
88
|
+
yield this.stateManager.clearCache();
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
saveStateInternal() {
|
|
92
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
yield this.stateManager.saveState();
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
onActivate() {
|
|
97
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
98
|
+
return;
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
onDeactivate() {
|
|
102
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
+
return;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
onActorMethodPre() {
|
|
107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
return;
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
onActorMethodPost() {
|
|
112
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
113
|
+
return;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
receiveReminder(_data) {
|
|
117
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
118
|
+
this.daprLogger.warn(JSON.stringify({
|
|
119
|
+
error: 'ACTOR_METHOD_NOT_IMPLEMENTED',
|
|
120
|
+
errorMsg: `A reminder was created for the actor with id: ${this.id} but the method 'receiveReminder' was not implemented`,
|
|
121
|
+
}));
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
getDaprClient() {
|
|
125
|
+
return this.daprClient;
|
|
126
|
+
}
|
|
127
|
+
getStateProvider() {
|
|
128
|
+
return this.daprStateProvider;
|
|
129
|
+
}
|
|
130
|
+
getStateManager() {
|
|
131
|
+
return this.stateManager;
|
|
132
|
+
}
|
|
133
|
+
getActorId() {
|
|
134
|
+
return this.id;
|
|
135
|
+
}
|
|
136
|
+
getActorType() {
|
|
137
|
+
return this.actorType;
|
|
138
|
+
}
|
|
139
|
+
getId() {
|
|
140
|
+
return this.getActorId().getId();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
exports.AbstractActor = AbstractActor;
|
|
@@ -5,6 +5,7 @@ export declare class DaprClientCache {
|
|
|
5
5
|
private static clients;
|
|
6
6
|
private static actorClients;
|
|
7
7
|
private static stateProviders;
|
|
8
|
+
static getDaprClient(): DaprClient | undefined;
|
|
8
9
|
static getAllClients(): DaprClient[];
|
|
9
10
|
static getAllActorClients(): ActorClient[];
|
|
10
11
|
static getAllStateProviders(): StateProvider[];
|
|
@@ -15,6 +15,11 @@ const ActorClient_1 = __importDefault(require("@dapr/dapr/actors/client/ActorCli
|
|
|
15
15
|
const StateProvider_1 = __importDefault(require("@dapr/dapr/actors/runtime/StateProvider"));
|
|
16
16
|
const common_1 = require("@nestjs/common");
|
|
17
17
|
let DaprClientCache = class DaprClientCache {
|
|
18
|
+
static getDaprClient() {
|
|
19
|
+
if (this.clients.size === 0)
|
|
20
|
+
return undefined;
|
|
21
|
+
return this.clients.entries().next().value[1];
|
|
22
|
+
}
|
|
18
23
|
static getAllClients() {
|
|
19
24
|
return Array.from(this.clients.values());
|
|
20
25
|
}
|
|
@@ -10,9 +10,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.StatefulActor = void 0;
|
|
13
|
-
const dapr_1 = require("@dapr/dapr");
|
|
14
13
|
const constants_1 = require("../constants");
|
|
15
|
-
|
|
14
|
+
const abstract_actor_1 = require("./abstract-actor");
|
|
15
|
+
class StatefulActor extends abstract_actor_1.AbstractActor {
|
|
16
16
|
setState(stateName, value) {
|
|
17
17
|
return __awaiter(this, void 0, void 0, function* () {
|
|
18
18
|
yield this.getStateManager().setState(stateName, value);
|
package/package.json
CHANGED