Skip to content

Commit 3bdde0f

Browse files
Make use of ActorId everywhere
Signed-off-by: Xavier Geerinck <[email protected]>
1 parent c648a8e commit 3bdde0f

File tree

7 files changed

+53
-51
lines changed

7 files changed

+53
-51
lines changed

src/actors/ActorId.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ export default class ActorId {
1414
getId() {
1515
return this.id;
1616
}
17+
18+
toString() {
19+
return this.id;
20+
}
1721
}

src/actors/client/ActorClient/ActorClientGRPC.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ActorReminderType } from '../../../types/ActorReminder.type';
77
import { ActorTimerType } from '../../../types/ActorTimer.type';
88
import IClientActor from '../../../interfaces/Client/IClientActor';
99
import { KeyValueType } from '../../../types/KeyValue.type';
10+
import ActorId from "../../ActorId";
1011

1112
// https://docs.dapr.io/reference/api/actors_api/
1213
export default class ActorClientGRPC implements IClientActor {
@@ -16,9 +17,9 @@ export default class ActorClientGRPC implements IClientActor {
1617
this.client = client;
1718
}
1819

19-
async invoke(actorType: string, actorId: string, methodName: string, body?: any): Promise<object> {
20+
async invoke(actorType: string, actorId: ActorId, methodName: string, body?: any): Promise<object> {
2021
const msgService = new InvokeActorRequest();
21-
msgService.setActorId(actorId)
22+
msgService.setActorId(actorId.getId())
2223
msgService.setActorType(actorType);
2324
msgService.setMethod(methodName);
2425

@@ -46,7 +47,7 @@ export default class ActorClientGRPC implements IClientActor {
4647
});
4748
}
4849

49-
async stateTransaction(actorType: string, actorId: string, operations: OperationType[]): Promise<void> {
50+
async stateTransaction(actorType: string, actorId: ActorId, operations: OperationType[]): Promise<void> {
5051
const transactionItems: TransactionalActorStateOperation[] = [];
5152

5253
for (const o of operations) {
@@ -63,7 +64,7 @@ export default class ActorClientGRPC implements IClientActor {
6364

6465
const msgService = new ExecuteActorStateTransactionRequest();
6566
msgService.setActorType(actorType);
66-
msgService.setActorId(actorId);
67+
msgService.setActorId(actorId.getId());
6768
msgService.setOperationsList(transactionItems);
6869

6970
return new Promise((resolve, reject) => {
@@ -79,10 +80,10 @@ export default class ActorClientGRPC implements IClientActor {
7980
});
8081
}
8182

82-
async stateGet(actorType: string, actorId: string, key: string): Promise<KeyValueType | string> {
83+
async stateGet(actorType: string, actorId: ActorId, key: string): Promise<KeyValueType | string> {
8384
const msgService = new GetActorStateRequest();
8485
msgService.setActorType(actorType);
85-
msgService.setActorId(actorId)
86+
msgService.setActorId(actorId.getId())
8687
msgService.setKey(key);
8788

8889
return new Promise((resolve, reject) => {
@@ -105,10 +106,10 @@ export default class ActorClientGRPC implements IClientActor {
105106
});
106107
}
107108

108-
async registerActorReminder(actorType: string, actorId: string, name: string, reminder: ActorReminderType): Promise<void> {
109+
async registerActorReminder(actorType: string, actorId: ActorId, name: string, reminder: ActorReminderType): Promise<void> {
109110
const msgService = new RegisterActorReminderRequest();
110111
msgService.setActorType(actorType);
111-
msgService.setActorId(actorId);
112+
msgService.setActorId(actorId.getId());
112113
msgService.setName(name);
113114

114115
if (reminder.data) {
@@ -136,10 +137,10 @@ export default class ActorClientGRPC implements IClientActor {
136137
});
137138
}
138139

139-
async unregisterActorReminder(actorType: string, actorId: string, name: string): Promise<void> {
140+
async unregisterActorReminder(actorType: string, actorId: ActorId, name: string): Promise<void> {
140141
const msgService = new UnregisterActorReminderRequest();
141142
msgService.setActorType(actorType);
142-
msgService.setActorId(actorId);
143+
msgService.setActorId(actorId.getId());
143144
msgService.setName(name);
144145

145146
return new Promise((resolve, reject) => {
@@ -155,10 +156,10 @@ export default class ActorClientGRPC implements IClientActor {
155156
});
156157
}
157158

158-
async registerActorTimer(actorType: string, actorId: string, name: string, timer: ActorTimerType): Promise<void> {
159+
async registerActorTimer(actorType: string, actorId: ActorId, name: string, timer: ActorTimerType): Promise<void> {
159160
const msgService = new RegisterActorTimerRequest();
160161
msgService.setActorType(actorType);
161-
msgService.setActorId(actorId);
162+
msgService.setActorId(actorId.getId());
162163
msgService.setName(name);
163164

164165
if (timer.callback) {
@@ -190,10 +191,10 @@ export default class ActorClientGRPC implements IClientActor {
190191
});
191192
}
192193

193-
async unregisterActorTimer(actorType: string, actorId: string, name: string): Promise<void> {
194+
async unregisterActorTimer(actorType: string, actorId: ActorId, name: string): Promise<void> {
194195
const msgService = new UnregisterActorTimerRequest();
195196
msgService.setActorType(actorType);
196-
msgService.setActorId(actorId);
197+
msgService.setActorId(actorId.getId());
197198
msgService.setName(name);
198199

199200
return new Promise((resolve, reject) => {

src/actors/client/ActorClient/ActorClientHTTP.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ActorReminderType } from '../../../types/ActorReminder.type';
44
import { ActorTimerType } from '../../../types/ActorTimer.type';
55
import IClientActor from '../../../interfaces/Client/IClientActor';
66
import { KeyValueType } from '../../../types/KeyValue.type';
7+
import ActorId from '../../ActorId';
78

89
// https://docs.dapr.io/reference/api/actors_api/
910
export default class ActorClientHTTP implements IClientActor {
@@ -13,17 +14,17 @@ export default class ActorClientHTTP implements IClientActor {
1314
this.client = client;
1415
}
1516

16-
async invoke(actorType: string, actorId: string, methodName: string, body?: any): Promise<object> {
17-
const result = await this.client.execute(`/actors/${actorType}/${actorId}/method/${methodName}`, {
17+
async invoke(actorType: string, actorId: ActorId, methodName: string, body?: any): Promise<object> {
18+
const result = await this.client.execute(`/actors/${actorType}/${actorId.getId()}/method/${methodName}`, {
1819
method: "POST", // we always use POST calls for Invoking (ref: https://github.com/dapr/js-sdk/pull/137#discussion_r772636068)
1920
body
2021
});
2122

2223
return result as object;
2324
}
2425

25-
async stateTransaction(actorType: string, actorId: string, operations: OperationType[]): Promise<void> {
26-
await this.client.execute(`/actors/${actorType}/${actorId}/state`, {
26+
async stateTransaction(actorType: string, actorId: ActorId, operations: OperationType[]): Promise<void> {
27+
await this.client.execute(`/actors/${actorType}/${actorId.getId()}/state`, {
2728
method: 'POST',
2829
headers: {
2930
"Content-Type": "application/json"
@@ -32,13 +33,13 @@ export default class ActorClientHTTP implements IClientActor {
3233
});
3334
}
3435

35-
async stateGet(actorType: string, actorId: string, key: string): Promise<KeyValueType | string> {
36-
const result = await this.client.execute(`/actors/${actorType}/${actorId}/state/${key}`);
36+
async stateGet(actorType: string, actorId: ActorId, key: string): Promise<KeyValueType | string> {
37+
const result = await this.client.execute(`/actors/${actorType}/${actorId.getId()}/state/${key}`);
3738
return result as any;
3839
}
3940

40-
async registerActorReminder(actorType: string, actorId: string, name: string, reminder: ActorReminderType): Promise<void> {
41-
await this.client.execute(`/actors/${actorType}/${actorId}/reminders/${name}`, {
41+
async registerActorReminder(actorType: string, actorId: ActorId, name: string, reminder: ActorReminderType): Promise<void> {
42+
await this.client.execute(`/actors/${actorType}/${actorId.getId()}/reminders/${name}`, {
4243
method: 'POST',
4344
headers: {
4445
"Content-Type": "application/json"
@@ -51,19 +52,19 @@ export default class ActorClientHTTP implements IClientActor {
5152
});
5253
}
5354

54-
async reminderGet(actorType: string, actorId: string, name: string): Promise<object> {
55-
const result = await this.client.execute(`/actors/${actorType}/${actorId}/reminders/${name}`);
55+
async reminderGet(actorType: string, actorId: ActorId, name: string): Promise<object> {
56+
const result = await this.client.execute(`/actors/${actorType}/${actorId.getId()}/reminders/${name}`);
5657
return result as object;
5758
}
5859

59-
async unregisterActorReminder(actorType: string, actorId: string, name: string): Promise<void> {
60-
await this.client.execute(`/actors/${actorType}/${actorId}/reminders/${name}`, {
60+
async unregisterActorReminder(actorType: string, actorId: ActorId, name: string): Promise<void> {
61+
await this.client.execute(`/actors/${actorType}/${actorId.getId()}/reminders/${name}`, {
6162
method: 'DELETE',
6263
});
6364
}
6465

65-
async registerActorTimer(actorType: string, actorId: string, name: string, timer: ActorTimerType): Promise<void> {
66-
await this.client.execute(`/actors/${actorType}/${actorId}/timers/${name}`, {
66+
async registerActorTimer(actorType: string, actorId: ActorId, name: string, timer: ActorTimerType): Promise<void> {
67+
await this.client.execute(`/actors/${actorType}/${actorId.getId()}/timers/${name}`, {
6768
method: 'POST',
6869
headers: {
6970
"Content-Type": "application/json"
@@ -77,8 +78,8 @@ export default class ActorClientHTTP implements IClientActor {
7778
});
7879
}
7980

80-
async unregisterActorTimer(actorType: string, actorId: string, name: string): Promise<void> {
81-
await this.client.execute(`/actors/${actorType}/${actorId}/timers/${name}`, {
81+
async unregisterActorTimer(actorType: string, actorId: ActorId, name: string): Promise<void> {
82+
await this.client.execute(`/actors/${actorType}/${actorId.getId()}/timers/${name}`, {
8283
method: 'DELETE',
8384
});
8485
}

src/actors/client/ActorProxyBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default class ActorProxyBuilder<T> {
2222
get(target: any, propKey: any, receiver: any) {
2323
return async function (...args: any) {
2424
const body = args.length > 0 ? args : null;
25-
const res = await actorClient.actor.invoke(actorTypeClassName, actorId.getId(), propKey, body);
25+
const res = await actorClient.actor.invoke(actorTypeClassName, actorId, propKey, body);
2626

2727
return res;
2828
};

src/actors/runtime/AbstractActor.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,20 @@ export default abstract class AbstractActor {
7272
* @return Async void response
7373
*/
7474
async registerActorReminder<Type>(reminderName: string, dueTime: Temporal.Duration, period: Temporal.Duration, state?: any) {
75-
await this.actorClient.actor.registerActorReminder(this.actorType, this.id.getId(), reminderName, {
75+
await this.actorClient.actor.registerActorReminder(this.actorType, this.id, reminderName, {
7676
period,
7777
dueTime,
7878
data: state
7979
});
8080
}
8181

8282
async unregisterActorReminder(reminderName: string) {
83-
await this.actorClient.actor.unregisterActorReminder(this.actorType, this.id.getId(), reminderName);
83+
await this.actorClient.actor.unregisterActorReminder(this.actorType, this.id, reminderName);
8484
}
8585

8686
async registerActorTimer(timerName: string, callback: string, dueTime: Temporal.Duration, period: Temporal.Duration, state?: any) {
8787
// Register the timer in the sidecar
88-
// console.log(`actorType: ${this.actorType}, actorId: ${this.id.getId()}, timerName: ${timerName}, callback: ${callback}, dueTime: ${dueTime.toString()}, period: ${period.toString()}`);
89-
return await this.actorClient.actor.registerActorTimer(this.actorType, this.id.getId(), timerName, {
88+
return await this.actorClient.actor.registerActorTimer(this.actorType, this.id, timerName, {
9089
period,
9190
dueTime,
9291
data: state,
@@ -95,7 +94,7 @@ export default abstract class AbstractActor {
9594
}
9695

9796
async unregisterActorTimer(timerName: string) {
98-
await this.actorClient.actor.unregisterActorTimer(this.actorType, this.id.getId(), timerName);
97+
await this.actorClient.actor.unregisterActorTimer(this.actorType, this.id, timerName);
9998
}
10099

101100
/**
@@ -188,7 +187,7 @@ export default abstract class AbstractActor {
188187
async receiveReminder(data: string): Promise<void> {
189188
console.warn(JSON.stringify({
190189
error: "ACTOR_METHOD_NOT_IMPLEMENTED",
191-
errorMsg: `A reminder was created for the actor with id: ${this.id.getId()} but the method 'receiveReminder' was not implemented`,
190+
errorMsg: `A reminder was created for the actor with id: ${this.id} but the method 'receiveReminder' was not implemented`,
192191
}));
193192
}
194193

@@ -204,10 +203,6 @@ export default abstract class AbstractActor {
204203
return this.stateManager;
205204
}
206205

207-
getId(): string {
208-
return this.id.getId();
209-
}
210-
211206
getActorId(): ActorId {
212207
return this.id;
213208
}

src/actors/runtime/ActorStateManager.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default class ActorStateManager<T> {
4040
return false;
4141
}
4242

43-
const didExist = await this.actor.getStateProvider().containsState(this.actor.getActorType(), this.actor.getId(), stateName);
43+
const didExist = await this.actor.getStateProvider().containsState(this.actor.getActorType(), this.actor.getActorId(), stateName);
4444

4545
if (!didExist) {
4646
return false;
@@ -74,7 +74,7 @@ export default class ActorStateManager<T> {
7474
return [true, val !== undefined ? val : null];
7575
}
7676

77-
const [hasValue, value] = await this.actor.getStateProvider().tryLoadState(this.actor.getActorType(), this.actor.getId(), stateName);
77+
const [hasValue, value] = await this.actor.getStateProvider().tryLoadState(this.actor.getActorType(), this.actor.getActorId(), stateName);
7878

7979

8080
if (hasValue) {
@@ -106,7 +106,7 @@ export default class ActorStateManager<T> {
106106
return;
107107
}
108108

109-
const didExist = await this.actor.getStateProvider().containsState(this.actor.getActorType(), this.actor.getId(), stateName);
109+
const didExist = await this.actor.getStateProvider().containsState(this.actor.getActorType(), this.actor.getActorId(), stateName);
110110

111111
if (didExist) {
112112
stateChangeTracker.set(stateName, new StateMetadata(value, StateChangeKind.UPDATE));
@@ -141,7 +141,7 @@ export default class ActorStateManager<T> {
141141
return true;
142142
}
143143

144-
const didExist = await this.actor.getStateProvider().containsState(this.actor.getActorType(), this.actor.getId(), stateName);
144+
const didExist = await this.actor.getStateProvider().containsState(this.actor.getActorType(), this.actor.getActorId(), stateName);
145145

146146
if (didExist) {
147147
stateChangeTracker.set(stateName, new StateMetadata(null as any, StateChangeKind.REMOVE));
@@ -159,7 +159,7 @@ export default class ActorStateManager<T> {
159159
return stateMetadata?.getChangeKind() !== StateChangeKind.REMOVE;
160160
}
161161

162-
const doesContainState = await this.actor.getStateProvider().containsState(this.actor.getActorType(), this.actor.getId(), stateName);
162+
const doesContainState = await this.actor.getStateProvider().containsState(this.actor.getActorType(), this.actor.getActorId(), stateName);
163163
return doesContainState;
164164
}
165165

@@ -215,7 +215,7 @@ export default class ActorStateManager<T> {
215215
return newValue;
216216
}
217217

218-
const [hasValue, val] = await this.actor.getStateProvider().tryLoadState(this.actor.getActorType(), this.actor.getId(), stateName);
218+
const [hasValue, val] = await this.actor.getStateProvider().tryLoadState(this.actor.getActorType(), this.actor.getActorId(), stateName);
219219

220220
if (hasValue) {
221221
const newValue = updateValueFactory(stateName, val);
@@ -273,7 +273,7 @@ export default class ActorStateManager<T> {
273273
});
274274

275275
if (stateChanges.length > 0) {
276-
await this.actor.getStateProvider().saveState(this.actor.getActorType(), this.actor.getId(), stateChanges);
276+
await this.actor.getStateProvider().saveState(this.actor.getActorType(), this.actor.getActorId(), stateChanges);
277277
}
278278

279279
for (const stateName of statesToRemove) {

src/actors/runtime/StateProvider.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { OperationType } from "../../types/Operation.type";
2+
import ActorId from "../ActorId";
23
import ActorClient from "../client/ActorClient/ActorClient";
34
import ActorStateChange from "./ActorStateChange";
45
import StateChangeKind from "./StateChangeKind";
@@ -10,13 +11,13 @@ export default class StateProvider {
1011
this.actorClient = actorClient;
1112
}
1213

13-
async containsState(actorType: string, actorId: string, stateName: string): Promise<boolean> {
14+
async containsState(actorType: string, actorId: ActorId, stateName: string): Promise<boolean> {
1415
const rawStateValue = await this.actorClient.actor.stateGet(actorType, actorId, stateName);
1516
return !!rawStateValue && rawStateValue.length > 0;
1617
}
1718

1819
// SEE https://github.com/dapr/python-sdk/blob/0f0b6f6a1cf45d2ac0c519b48fc868898d81124e/dapr/actor/runtime/_state_provider.py#L24
19-
async tryLoadState(actorType: string, actorId: string, stateName: string): Promise<[boolean, any]> {
20+
async tryLoadState(actorType: string, actorId: ActorId, stateName: string): Promise<[boolean, any]> {
2021
const rawStateValue = await this.actorClient.actor.stateGet(actorType, actorId, stateName);
2122

2223
if (!rawStateValue || rawStateValue.length === 0) {
@@ -49,7 +50,7 @@ export default class StateProvider {
4950
* @param actorId
5051
* @param stateChanges
5152
*/
52-
async saveState(actorType: string, actorId: string, stateChanges: ActorStateChange<any>[]): Promise<void> {
53+
async saveState(actorType: string, actorId: ActorId, stateChanges: ActorStateChange<any>[]): Promise<void> {
5354
const jsonOutput: OperationType[] = [];
5455

5556
for (const state of stateChanges) {

0 commit comments

Comments
 (0)