Skip to content

Commit 78f3b16

Browse files
Rename dapr-client to @dapr/dapr
Co-authored-by: @shubham1172 <[email protected]> Signed-off-by: Xavier Geerinck <[email protected]>
1 parent 915260b commit 78f3b16

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+11656
-11302
lines changed

.github/workflows/_index.md

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
---
2+
type: docs
3+
title: "JavaScript SDK for Actors"
4+
linkTitle: "Actors"
5+
weight: 1000
6+
description: How to get up and running with Actors using the Dapr JavaScript SDK
7+
---
8+
9+
The Dapr actors package allows you to interact with Dapr virtual actors from a JavaScript application. The examples below demonstrate how to use the JavaScript SDK for interacting with virtual actors.
10+
11+
For a more in-depth overview of Dapr actors, visit the [actors overview page]({{< ref actors-overview >}}).
12+
13+
## Pre-requisites
14+
- [Dapr CLI]({{< ref install-dapr-cli.md >}}) installed
15+
- Initialized [Dapr environment]({{< ref install-dapr-selfhost.md >}})
16+
- [Latest LTS version of Node or greater](https://nodejs.org/en/)
17+
- [JavaScript NPM package installed](https://www.npmjs.com/package/@dapr/dapr)
18+
19+
## Scenario
20+
The below code examples loosely describe the scenario of a Parking Garage Spot Monitoring System, which can be seen in this [video](https://www.youtube.com/watch?v=eJCu6a-x9uo&t=3785) by Mark Russinovich.
21+
22+
A parking garage consists of hundreds of parking spaces, where each parking space includes a sensor that provides updates to a centralized monitoring system. The parking space sensors (our actors) detect if a parking space is occupied or available.
23+
24+
To jump in and run this example yourself, clone the source code, which can be found in the [JavaScript SDK examples directory](https://github.com/dapr/js-sdk/tree/master/examples/http/actor-parking-sensor).
25+
26+
## Actor Interface
27+
The actor interface defines the contract that is shared between the actor implementation and the clients calling the actor. In the example below, we have created an interace for a parking garage sensor. Each sensor has 2 methods: `carEnter` and `carLeave`, which defines the state of the parking space:
28+
29+
```ts
30+
export default interface ParkingSensorInterface {
31+
carEnter(): Promise<void>;
32+
carLeave(): Promise<void>;
33+
}
34+
```
35+
36+
## Actor Implementation
37+
An actor implementation defines a class by extending the base type `AbstractActor` and implementing the actor interface (`ParkingSensorInterface` in this case).
38+
39+
The following code describes an actor implementation along with a few helper methods.
40+
41+
```ts
42+
import { AbstractActor } from "@dapr/dapr";
43+
import ParkingSensorInterface from "./ParkingSensorInterface";
44+
45+
export default class ParkingSensorImpl extends AbstractActor implements ParkingSensorInterface {
46+
async carEnter(): Promise<void> {
47+
// Implementation that updates state that this parking spaces is occupied.
48+
}
49+
50+
async carLeave(): Promise<void> {
51+
// Implementation that updates state that this parking spaces is available.
52+
}
53+
54+
private async getInfo(): Promise<object> {
55+
// Implementation of requesting an update from the parking space sensor.
56+
}
57+
58+
/**
59+
* @override
60+
*/
61+
async onActivate(): Promise<void> {
62+
// Initialization logic called by AbstractActor.
63+
}
64+
}
65+
```
66+
67+
## Registering Actors
68+
Initialize and register your actors by using the `DaprServer` package:
69+
70+
```javascript
71+
import { DaprServer } from "@dapr/dapr";
72+
import ParkingSensorImpl from "./ParkingSensorImpl";
73+
74+
const daprHost = "127.0.0.1";
75+
const daprPort = "50000";
76+
const serverHost = "127.0.0.1";
77+
const serverPort = "50001";
78+
79+
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
80+
81+
await server.actor.init(); // Let the server know we need actors
82+
server.actor.registerActor(ParkingSensorImpl); // Register the actor
83+
await server.start(); // Start the server
84+
85+
// To get the registered actors, you can invoke `getRegisteredActors`:
86+
const resRegisteredActors = await server.actor.getRegisteredActors();
87+
console.log(`Registered Actors: ${JSON.stringify(resRegisteredActors)}`);
88+
```
89+
90+
## Invoking Actor Methods
91+
After Actors are registered, create a Proxy object that implements `ParkingSensorInterface` using the `ActorProxyBuilder`. You can invoke the actor methods by directly calling methods on the Proxy object. Internally, it translates to making a network call to the Actor API and fetches the result back.
92+
93+
```javascript
94+
import { DaprClient, ActorId } from "@dapr/dapr";
95+
import ParkingSensorImpl from "./ParkingSensorImpl";
96+
import ParkingSensorInterface from "./ParkingSensorInterface";
97+
98+
const daprHost = "127.0.0.1";
99+
const daprPort = "50000";
100+
101+
const client = new DaprClient(daprHost, daprPort);
102+
103+
// Create a new actor builder. It can be used to create multiple actors of a type.
104+
const builder = new ActorProxyBuilder<ParkingSensorInterface>(ParkingSensorImpl, client);
105+
106+
// Create a new actor instance.
107+
const actor = builder.build(new ActorId("my-actor"));
108+
// Or alternatively, use a random ID
109+
// const actor = builder.build(ActorId.createRandomId());
110+
111+
// Invoke the method.
112+
await actor.carEnter();
113+
```
114+
115+
## Using states with Actor
116+
117+
```ts
118+
// ...
119+
120+
const PARKING_SENSOR_PARKED_STATE_NAME = "parking-sensor-parked"
121+
122+
const actor = builder.build(new ActorId("my-actor"))
123+
124+
// SET state
125+
await actor.getStateManager().setState(PARKING_SENSOR_PARKED_STATE_NAME, true);
126+
127+
// GET state
128+
const value = await actor.getStateManager().getState(PARKING_SENSOR_PARKED_STATE_NAME);
129+
if (!value) {
130+
console.log(`Received: ${value}!`);
131+
}
132+
133+
// DELETE state
134+
await actor.removeState(PARKING_SENSOR_PARKED_STATE_NAME);
135+
...
136+
```
137+
138+
## Actor Timers and Reminders
139+
The JS SDK supports actors that can schedule periodic work on themselves by registering either timers or reminders. The main difference between timers and reminders is that the Dapr actor runtime does not retain any information about timers after deactivation, but persists reminders information using the Dapr actor state provider.
140+
141+
This distinction allows users to trade off between light-weight but stateless timers versus more resource-demanding but stateful reminders.
142+
143+
The scheduling interface of timers and reminders is identical. For an more in-depth look at the scheduling configurations see the [actors timers and reminders docs]({{< ref "howto-actors.md#actor-timers-and-reminders" >}}).
144+
145+
### Actor Timers
146+
```javascript
147+
// ...
148+
149+
const actor = builder.build(new ActorId("my-actor"));
150+
151+
// Register a timer
152+
await actor.registerActorTimer(
153+
"timer-id", // Unique name of the timer.
154+
"cb-method", // Callback method to execute when timer is fired.
155+
Temporal.Duration.from({ seconds: 2 }), // DueTime
156+
Temporal.Duration.from({ seconds: 1 }), // Period
157+
Temporal.Duration.from({ seconds: 1 }), // TTL
158+
50 // State to be sent to timer callback.
159+
);
160+
161+
// Delete the timer
162+
await actor.unregisterActorTimer("timer-id");
163+
```
164+
165+
### Actor Reminders
166+
```javascript
167+
// ...
168+
169+
const actor = builder.build(new ActorId("my-actor"));
170+
171+
// Register a reminder, it has a default callback: `receiveReminder`
172+
await actor.registerActorReminder(
173+
"reminder-id", // Unique name of the reminder.
174+
Temporal.Duration.from({ seconds: 2 }), // DueTime
175+
Temporal.Duration.from({ seconds: 1 }), // Period
176+
Temporal.Duration.from({ seconds: 1 }), // TTL
177+
100 // State to be sent to reminder callback.
178+
);
179+
180+
// Delete the reminder
181+
await actor.unregisterActorReminder("reminder-id");
182+
```
183+
184+
To handle the callback, you need to override the default `receiveReminder` implementation in your actor. For example, from our original actor implementation:
185+
```ts
186+
export default class ParkingSensorImpl extends AbstractActor implements ParkingSensorInterface {
187+
// ...
188+
189+
/**
190+
* @override
191+
*/
192+
async receiveReminder(state: any): Promise<void> {
193+
// handle stuff here
194+
}
195+
196+
// ...
197+
}
198+
```
199+
200+
For a full guide on actors, visit [How-To: Use virtual actors in Dapr]({{< ref howto-actors.md >}}).

.github/workflows/build.yml

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212
#
13-
14-
name: Node.js CI
13+
name: Build
1514

1615
on:
1716
push:
@@ -31,32 +30,56 @@ jobs:
3130
env:
3231
NODE_VER: 16.14.0
3332
steps:
34-
- uses: actions/checkout@v2
33+
- uses: actions/checkout@v3
3534

3635
# Setup .npmrc file to publish to npm
37-
# https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
38-
- name: Use Node.js
39-
uses: actions/setup-node@v2
36+
- name: Configure .npmrc for NPM publish to @dapr
37+
uses: actions/setup-node@v3
4038
with:
4139
node-version: ${{ env.NODE_VER }}
4240
registry-url: 'https://registry.npmjs.org'
41+
42+
# - name: Configure to publish to @dapr/dapr
43+
# run: sed -i s#@dapr/dapr"#"@dapr/dapr"# package.json
4344

4445
- name: Build Package
4546
run: ./scripts/build.sh
4647

47-
- name: Run unit tests
48-
id: tests
49-
run: npm run test:unit:all
48+
# @TODO: add a depend on the test-e2e pipeline?
49+
# - name: Run unit tests
50+
# id: tests
51+
# run: npm run test:unit:all
5052

51-
- name: Upload test coverage
52-
uses: codecov/codecov-action@v1
53+
# - name: Upload test coverage
54+
# uses: codecov/codecov-action@v1
5355

54-
- name: Is Release?
55-
if: startswith(github.ref, 'refs/tags/v')
56-
run: echo "DEPLOY_PACKAGE=true" >> $GITHUB_ENV
56+
# - name: Is Release?
57+
# if: startswith(github.ref, 'refs/tags/v')
58+
# run: echo "DEPLOY_PACKAGE=true" >> $GITHUB_ENV
59+
60+
# run: npm set //registry.npmjs.org/:_authToken $NODE_AUTH_TOKEN
61+
62+
# - name: Authorize us to @dapr/dapr
63+
# run: |
64+
# npm set //registry.npmjs.org/:_authToken ${{ secrets.NPM_TOKEN_NEW }}
65+
66+
# - name: Show NPM Config
67+
# run: cat ~/.npmrc
68+
69+
# - name: Show NPM Config
70+
# run: npm whoami
71+
5772

58-
- name: Publish to npm
59-
if: env.DEPLOY_PACKAGE == 'true'
60-
run: npm run build && npm pack && npm publish build/ --access public
73+
# - name: Publish to npm (dapr-client)
74+
# if: env.DEPLOY_PACKAGE == 'true'
75+
# run: npm run build && npm pack && npm publish build/ --access public
76+
# env:
77+
# NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
78+
79+
# Publish to @dapr/dapr
80+
# note: package.json gets updated here for the new package name
81+
- name: Publish to npm (@dapr/dapr)
82+
# if: env.DEPLOY_PACKAGE == 'true'
83+
run: npm publish --access public
6184
env:
62-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
85+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ spec:
4545
**example.ts**
4646
4747
```javascript
48-
import { DaprClient, DaprServer } from "dapr-client";
48+
import { DaprClient, DaprServer } from "@dapr/dapr";
4949

5050
const daprHost = "127.0.0.1"; // Dapr Sidecar Host
5151
const daprPort = "50000"; // Dapr Sidecar Port

0 commit comments

Comments
 (0)