Skip to content

Commit a302c22

Browse files
committed
Fix examples
1 parent f84e4da commit a302c22

File tree

1 file changed

+41
-29
lines changed
  • daprdocs/content/en/js-sdk-docs/js-actors

1 file changed

+41
-29
lines changed

daprdocs/content/en/js-sdk-docs/js-actors/_index.md

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ For a more in-depth overview of Dapr actors and supported scenarios, visit the [
1616
- [Latest LTS version of Node or greater](https://nodejs.org/en/) d
1717
- [JavaScript NPM package installed](https://www.npmjs.com/package/dapr-client)
1818

19-
## Code Scenario
20-
The below code examples loosely describe the scenario of a Parking Garage Spot Monitoring System, which can be explained in detail by Mark Russinovich in [this video](https://www.youtube.com/watch?v=eJCu6a-x9uo&t=3785). The parking garage consists of hundreds of parking spots, where each parking spot 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.
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 by Mark Russinovich](https://www.youtube.com/watch?v=eJCu6a-x9uo&t=3785).
2121

22-
To run this example, source code can be found [here](https://github.com/XavierGeerinck/js-sdk/tree/23e1f0ee2bd4c60a4906e38427547c4b3840f89e/examples/http/actor-parking-sensor).
22+
A parking garage consists of hundreds of parking spots, where each parking spot 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 for yourself, the source code can be found in the [JavaScript SDK examples directory](https://github.com/dapr/js-sdk/tree/master/examples/http/actor-parking-sensor).
2325

2426
## Actor Interface
25-
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 sensor:
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:
2628

2729
```javascript
2830
export default interface ParkingSensorInterface {
@@ -32,7 +34,7 @@ export default interface ParkingSensorInterface {
3234
```
3335

3436
## Actor Implementation
35-
An actor implementation defines a class by extending the base type `AbstractActor` and implements the interfaces defined in the actor interface. The example below implements the methods defined in the `ParkingSensorInterface`, but also adds extra helper methods:
37+
An actor implementation defines a class by extending the base type `AbstractActor` and implements the interfaces defined in the actor interface. The following code describes what an actor implmentation consists of by implementing the methods defined in the `ParkingSensorInterface`. It also defines a few extra helper methods:
3638

3739
```javascript
3840
import { AbstractActor } from "dapr-client";
@@ -75,45 +77,48 @@ async function start() {
7577
await server.actor.init(); // Let the server know we need actors
7678
server.actor.registerActor(ParkingSensorImpl); // Register the actor
7779
await server.startServer(); // Start the server
80+
}
7881
```
7982

8083
## Invoking Actors
8184
After Actors are registered, use the DaprClient to invoke methods on an actor. The client will call the actor methods defined in the actor interface.
8285

8386
```javascript
84-
...
85-
// Initialize Dapr client
86-
const client = new DaprClient(daprHost, daprPort);
87+
...
88+
// Initialize Dapr client
89+
const client = new DaprClient(daprHost, daprPort);
8790

88-
await client.actor.invoke("PUT", ParkingSensorImpl.name, `actor-id`, "carEnter");
89-
...
91+
await client.actor.invoke("PUT", ParkingSensorImpl.name, `actor-id`, "carEnter");
92+
...
9093
```
9194

9295
## Saving and Getting State
9396

9497
```javascript
95-
...
96-
await client.actor.stateTransaction("ParkingSensorImpl", `actor-id`, [
97-
{
98-
operation: "upsert",
99-
request: {
100-
key: "parking-sensor-location-lat",
101-
value: "location-x"
102-
}
103-
},
104-
{
105-
operation: "upsert",
106-
request: {
107-
key: "parking-sensor-location-lang",
108-
value: "location-y"
109-
}
98+
...
99+
const client = new DaprClient(`dapr-host`, `dapr-port`);
100+
101+
await client.actor.stateTransaction("ParkingSensorImpl", `actor-id`, [
102+
{
103+
operation: "upsert",
104+
request: {
105+
key: "parking-sensor-location-lat",
106+
value: "location-x"
107+
}
108+
},
109+
{
110+
operation: "upsert",
111+
request: {
112+
key: "parking-sensor-location-lang",
113+
value: "location-y"
110114
}
111-
]);
115+
}
116+
]);
112117

113118

114-
await client.actor.stateGet("ParkingSensorImpl", `actor-id`, `parking-sensor-location-lat`)
115-
await client.actor.stateGet("ParkingSensorImpl", `actor-id`, `parking-sensor-location-lang`)
116-
...
119+
await client.actor.stateGet("ParkingSensorImpl", `actor-id`, `parking-sensor-location-lat`)
120+
await client.actor.stateGet("ParkingSensorImpl", `actor-id`, `parking-sensor-location-lang`)
121+
...
117122
```
118123

119124
## Actor Timers and Reminders
@@ -126,6 +131,8 @@ The scheduling interface of timers and reminders is identical. For an more in-de
126131
### Actor Timers
127132
```javascript
128133
...
134+
const client = new DaprClient(`dapr-host`, `dapr-port`);
135+
129136
// Register a timer
130137
await client.actor.timerCreate(ParkingSensorImpl.name, `actor-id`, `timer-id`, {
131138
callback: "method-to-excute-on-actor",
@@ -135,11 +142,15 @@ await client.actor.timerCreate(ParkingSensorImpl.name, `actor-id`, `timer-id`, {
135142

136143
// Delete the timer
137144
await client.actor.timerDelete(ParkingSensorImpl.name, `actor-id`, `timer-id`);
145+
...
138146
}
139147
```
140148

141149
### Actor Reminders
142150
```javascript
151+
...
152+
const client = new DaprClient(`dapr-host`, `dapr-port`);
153+
143154
// Register a reminder, it has a default callback
144155
await client.actor.reminderCreate(DemoActorImpl.name, `actor-id`, `timer-id`, {
145156
dueTime: Temporal.Duration.from({ seconds: 2 }),
@@ -149,6 +160,7 @@ await client.actor.reminderCreate(DemoActorImpl.name, `actor-id`, `timer-id`, {
149160

150161
// Delete the reminder
151162
await client.actor.reminderDelete(DemoActorImpl.name, `actor-id`, `timer-id`);
163+
...
152164
```
153165

154166
- For a full guide on actors visit [How-To: Use virtual actors in Dapr]({{< ref howto-actors.md >}}).

0 commit comments

Comments
 (0)