Skip to content

Commit 2cb8957

Browse files
Merge pull request #165 from XavierGeerinck/actor_add_abstraction
feat: Actor add abstraction for calling through DaprClient
2 parents 6beae03 + 7f8eb0f commit 2cb8957

File tree

11 files changed

+80
-364
lines changed

11 files changed

+80
-364
lines changed

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@
22

33
## 2.x release
44

5+
### v2.1.0
6+
7+
#### Actors
8+
9+
To make development easier, Actors can now be called in 2 ways:
10+
11+
1. By creating an actor through a helper method in the `DaprClient` class, removing the need of needing to know how a builder works.
12+
13+
```javascript
14+
const actor = await client.actor.create<DemoActorSayImpl>(DemoActorSayImpl);
15+
const res = await actor.sayMulti(123, "123", { hello: "world 123" }, [1, 2, 3]);
16+
```
17+
18+
2. By utilizing the Actor builder, where we create a Proxy object that does this for us.
19+
20+
```javascript
21+
const builder = new ActorProxyBuilder<DemoActorSayImpl>(DemoActorSayImpl, client);
22+
const actor = builder.build(ActorId.createRandomId());
23+
const res = await actor.sayMulti(123, "123", { hello: "world 123" }, [1, 2, 3]);
24+
```
25+
26+
Behind the hoods, method #1 will utilize method #2
27+
528
### v2.0.0
629

730
Version 2.0.0 brings a lot of changes to the Dapr JS SDK that were long due. Below an overview of the major contributions can be found, with a more detailed overview of the **Breaking Changes** under it.

examples/http/actor/src/actor/DemoActorCounterImpl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default class DemoActorCounterImpl extends AbstractActor implements DemoA
88
this.counter++;
99
}
1010

11-
async countBy(amount: number, multiplier: number = 1): Promise<void> {
11+
async countBy(amount: number, multiplier = 1): Promise<void> {
1212
this.counter += (amount * multiplier);
1313
}
1414
}

examples/http/actor/src/index.ts

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ const serverHost = "127.0.0.1"; // App Host of this Example Server
1010
const serverPort = "50001"; // App Port of this Example Server
1111
const daprAppId = "example-hello-world";
1212

13-
async function sleep(ms: number): Promise<void> {
14-
return new Promise(resolve => setTimeout(resolve, ms));
15-
}
16-
1713
async function start() {
1814
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
1915
const client = new DaprClient(daprHost, daprPort);
@@ -39,43 +35,13 @@ async function start() {
3935
console.log("===============================================================");
4036
const actorId = "MyActorId1";
4137

42-
4338
const resRegisteredActors = await server.actor.getRegisteredActors();
4439
console.log(`Registered Actor Types: ${JSON.stringify(resRegisteredActors)}`);
4540

4641
console.log("[Dapr-JS][Example][Actors] Trying to invoke method 'say' with msg 'hello world'");
47-
const resActorInvokeSay = await client.actor.invoke("PUT", DemoActorSayImpl.name, actorId, "sayString", "Hello World");
42+
const actor = client.actor.create<DemoActorSayImpl>(DemoActorSayImpl);
43+
const resActorInvokeSay = await actor.sayString("Hello World");
4844
console.log(`[Dapr-JS][Example][Actors] Invoked Method and got data: ${resActorInvokeSay}`);
49-
50-
let resActorStateGet = await client.actor.stateGet(DemoActorSayImpl.name, actorId, "PropertyA");
51-
await client.actor.stateTransaction(DemoActorSayImpl.name, actorId, [
52-
{
53-
operation: "upsert",
54-
request: {
55-
key: "key-1",
56-
value: "my-new-data-1"
57-
}
58-
},
59-
{
60-
operation: "upsert",
61-
request: {
62-
key: "key-to-delete",
63-
value: "my-new-data-1"
64-
}
65-
},
66-
{
67-
operation: "delete",
68-
request: {
69-
key: "key-to-delete"
70-
}
71-
}
72-
]);
73-
74-
resActorStateGet = await client.actor.stateGet(DemoActorSayImpl.name, actorId, "key-to-delete");
75-
console.log(`[Dapr-JS][Example][Actors] Get State (should be empty): ${JSON.stringify(resActorStateGet)}`);
76-
77-
const resActorStateGet2 = await client.actor.stateGet(DemoActorSayImpl.name, actorId, "key-1");
78-
console.log(`[Dapr-JS][Example][Actors] Get State (should be my-new-data-1): ${JSON.stringify(resActorStateGet2)}`);
7945
}
8046

8147
start().catch((e) => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dapr-client",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "The official Dapr (https://dapr.io) SDK for Node.js",
55
"types": "http/index.d.ts",
66
"scripts": {

src/actors/client/ActorProxyBuilder.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1-
import { DaprClient } from "../..";
1+
import { CommunicationProtocolEnum, DaprClient } from "../..";
22
import Class from "../../types/Class";
33
import ActorClient from "./ActorClient/ActorClient";
44
import ActorId from "../ActorId";
5+
import { DaprClientOptions } from "../../types/DaprClientOptions";
56

67
export default class ActorProxyBuilder<T> {
78
actorClient: ActorClient;
8-
daprClient: DaprClient;
99
actorTypeClass: Class<T>;
1010

11-
constructor(actorTypeClass: Class<T>, daprClient: DaprClient) {
11+
constructor(actorTypeClass: Class<T>, daprClient: DaprClient);
12+
constructor(actorTypeClass: Class<T>, host: string, port: string, communicationProtocol: CommunicationProtocolEnum, clientOptions: DaprClientOptions);
13+
constructor(actorTypeClass: Class<T>, ...args: any[]) {
1214
this.actorTypeClass = actorTypeClass;
13-
this.daprClient = daprClient;
14-
this.actorClient = new ActorClient(daprClient.getDaprHost(), daprClient.getDaprPort(), daprClient.getCommunicationProtocol(), daprClient.getOptions());
15+
16+
if (args.length == 1) {
17+
const [daprClient] = args;
18+
this.actorClient = new ActorClient(daprClient.getDaprHost(), daprClient.getDaprPort(), daprClient.getCommunicationProtocol(), daprClient.getOptions());
19+
} else {
20+
const [host, port, communicationProtocol, clientOptions] = args;
21+
this.actorClient = new ActorClient(host, port, communicationProtocol, clientOptions);
22+
}
1523
}
1624

1725
build(actorId: ActorId): T {

src/implementation/Client/DaprClient.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import IClientSecret from '../../interfaces/Client/IClientSecret';
66
import IClientHealth from '../../interfaces/Client/IClientHealth';
77
import IClientMetadata from '../../interfaces/Client/IClientMetadata';
88
import IClientSidecar from '../../interfaces/Client/IClientSidecar';
9+
import IClientActorBuilder from '../../interfaces/Client/IClientActorBuilder';
910
import IClient from '../../interfaces/Client/IClient';
1011

1112
import GRPCClientBinding from './GRPCClient/binding';
@@ -16,6 +17,7 @@ import GRPCClientSecret from './GRPCClient/secret';
1617
import GRPCClientHealth from './GRPCClient/health';
1718
import GRPCClientMetadata from './GRPCClient/metadata';
1819
import GRPCClientSidecar from './GRPCClient/sidecar';
20+
import GRPCClientActor from './GRPCClient/actor';
1921
import GRPCClient from './GRPCClient/GRPCClient';
2022

2123
import HTTPClientBinding from './HTTPClient/binding';
@@ -26,6 +28,7 @@ import HTTPClientSecret from './HTTPClient/secret';
2628
import HTTPClientHealth from './HTTPClient/health';
2729
import HTTPClientMetadata from './HTTPClient/metadata';
2830
import HTTPClientSidecar from './HTTPClient/sidecar';
31+
import HTTPClientActor from './HTTPClient/actor';
2932
import HTTPClient from './HTTPClient/HTTPClient';
3033

3134
import CommunicationProtocolEnum from '../../enum/CommunicationProtocol.enum';
@@ -46,6 +49,7 @@ export default class DaprClient {
4649
readonly health: IClientHealth;
4750
readonly metadata: IClientMetadata;
4851
readonly sidecar: IClientSidecar;
52+
readonly actor: IClientActorBuilder;
4953

5054
constructor(
5155
daprHost: string
@@ -79,6 +83,7 @@ export default class DaprClient {
7983
this.health = new GRPCClientHealth(client);
8084
this.metadata = new GRPCClientMetadata(client);
8185
this.sidecar = new GRPCClientSidecar(client);
86+
this.actor = new GRPCClientActor(client); // we use a abstractor here since we interface through a builder with the Actor Runtime
8287
break;
8388
}
8489
case CommunicationProtocolEnum.HTTP:
@@ -94,6 +99,7 @@ export default class DaprClient {
9499
this.health = new HTTPClientHealth(client);
95100
this.metadata = new HTTPClientMetadata(client);
96101
this.sidecar = new HTTPClientSidecar(client);
102+
this.actor = new HTTPClientActor(client); // we use a abstractor here since we interface through a builder with the Actor Runtime
97103
break;
98104
}
99105
}

0 commit comments

Comments
 (0)