Skip to content

Commit ac72421

Browse files
committed
Update docs
Signed-off-by: Shubham Sharma <[email protected]>
1 parent 03408fd commit ac72421

File tree

1 file changed

+83
-82
lines changed
  • daprdocs/content/en/js-sdk-docs/js-actors

1 file changed

+83
-82
lines changed

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

+83-82
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ To jump in and run this example yourself, clone the source code, which can be fo
2626
## Actor Interface
2727
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:
2828

29-
```javascript
29+
```ts
3030
export default interface ParkingSensorInterface {
3131
carEnter(): Promise<void>;
3232
carLeave(): Promise<void>;
@@ -38,7 +38,7 @@ An actor implementation defines a class by extending the base type `AbstractActo
3838

3939
The following code describes an actor implementation along with a few helper methods.
4040

41-
```javascript
41+
```ts
4242
import { AbstractActor } from "dapr-client";
4343
import ParkingSensorInterface from "./ParkingSensorInterface";
4444

@@ -51,10 +51,13 @@ export default class ParkingSensorImpl extends AbstractActor implements ParkingS
5151
// Implementation that updates state that this parking spaces is available.
5252
}
5353

54-
async getInfo(): Promise<object> {
54+
private async getInfo(): Promise<object> {
5555
// Implementation of requesting an update from the parking space sensor.
5656
}
5757

58+
/**
59+
* @override
60+
*/
5861
async onActivate(): Promise<void> {
5962
// Initialization logic called by AbstractActor.
6063
}
@@ -78,121 +81,119 @@ const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
7881
await server.actor.init(); // Let the server know we need actors
7982
server.actor.registerActor(ParkingSensorImpl); // Register the actor
8083
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)}`);
8188
```
8289

83-
## Invoking Actors
90+
## Invoking Actor Methods
8491
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.
8592

8693
```javascript
87-
import { DaprClient } from "dapr-client";
94+
import { DaprClient, ActorId } from "dapr-client";
8895
import ParkingSensorImpl from "./ParkingSensorImpl";
8996

9097
const daprHost = "127.0.0.1";
9198
const daprPort = "50000";
9299

93100
const client = new DaprClient(daprHost, daprPort);
94101

95-
await client.actor.invoke("PUT", ParkingSensorImpl.name, `actor-id`, "carEnter"); // Invoke the ParkingSensor Actor by calling the carEnter function
96-
}
102+
// Create a new actor builder. It can be used to create multiple actors of a type.
103+
const builder = new ActorProxyBuilder<ActorSayInterface>(ActorSayImpl, client);
104+
105+
// Create a new actor instance.
106+
const actor = builder.build(new ActorId("my-actor"));
107+
// Or alternatively, use a random ID
108+
// const actor = builder.build(ActorId.createRandomId());
109+
110+
// Invoke the method.
111+
await actor.carEnter();
97112
```
98113

99-
## Saving and Getting State
114+
## Using states with Actor
100115

101-
```javascript
102-
import { DaprClient, DaprServer } from "dapr-client";
103-
import ParkingSensorImpl from "./ParkingSensorImpl";
116+
```ts
117+
// ...
118+
119+
const PARKING_SENSOR_PARKED_STATE_NAME = "parking-sensor-parked"
104120

105-
async function start() {
106-
const server = new DaprServer(`server-host`, `server-port`, `dapr-host`, `dapr-port`);
107-
const client = new DaprClient(`dapr-host`, `dapr-port`);
108-
109-
await server.actor.init();
110-
server.actor.registerActor(ParkingSensorImpl);
111-
await server.start();
112-
113-
// Perform state transaction
114-
await client.actor.stateTransaction("ParkingSensorImpl", `actor-id`, [
115-
{
116-
operation: "upsert",
117-
request: {
118-
key: "parking-sensor-location-lat",
119-
value: "location-x"
120-
}
121-
},
122-
{
123-
operation: "upsert",
124-
request: {
125-
key: "parking-sensor-location-lang",
126-
value: "location-y"
127-
}
128-
}
129-
]);
130-
131-
// GET state from an actor
132-
await client.actor.stateGet("ParkingSensorImpl", `actor-id`, `parking-sensor-location-lat`)
133-
await client.actor.stateGet("ParkingSensorImpl", `actor-id`, `parking-sensor-location-lang`)
121+
const actor = builder.build(new ActorId("my-actor"))
122+
123+
// SET state
124+
await actor.getStateManager().setState(PARKING_SENSOR_PARKED_STATE_NAME, true);
125+
126+
// GET state
127+
const value = await actor.getStateManager().getState(PARKING_SENSOR_PARKED_STATE_NAME);
128+
if (value !== null) {
129+
console.log(`Received: ${value}!`);
134130
}
131+
132+
// DELETE state
133+
await actor.removeState(PARKING_SENSOR_PARKED_STATE_NAME);
135134
...
136135
```
137136

138137
## 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 is not retaining any information about timers after deactivation, while persisting the information about reminders using the Dapr actor state provider.
138+
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.
140139

141-
This distintcion allows users to trade off between light-weight but stateless timers vs. more resource-demanding but stateful reminders.
140+
This distinction allows users to trade off between light-weight but stateless timers versus more resource-demanding but stateful reminders.
142141

143142
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" >}}).
144143

145144
### Actor Timers
146145
```javascript
147-
import { DaprClient, DaprServer } from "dapr-client";
148-
import ParkingSensorImpl from "./ParkingSensorImpl";
149-
150-
async function start()
151-
const server = new DaprServer(`server-host`, `server-port`, `dapr-host`, `dapr-port`);
152-
const client = new DaprClient(`dapr-host`, `dapr-port`);
153-
154-
await server.actor.init();
155-
server.actor.registerActor(ParkingSensorImpl);
156-
await server.start();
157-
158-
// Register a timer
159-
await client.actor.timerCreate(ParkingSensorImpl.name, `actor-id`, `timer-id`, {
160-
callback: "method-to-excute-on-actor",
161-
dueTime: Temporal.Duration.from({ seconds: 2 }),
162-
period: Temporal.Duration.from({ seconds: 1 }),
163-
ttl: Temporal.Duration.from({ seconds: 1 }),
164-
});
165-
166-
// Delete the timer
167-
await client.actor.timerDelete(ParkingSensorImpl.name, `actor-id`, `timer-id`);
168-
}
146+
// ...
147+
148+
const actor = builder.build(new ActorId("my-actor"));
149+
150+
// Register a timer
151+
await actor.registerActorTimer(
152+
"timer-id", // Unique name of the timer.
153+
"cb-method", // Callback method to execute when timer is fired.
154+
Temporal.Duration.from({ seconds: 2 }), // DueTime
155+
Temporal.Duration.from({ seconds: 1 }), // Period
156+
Temporal.Duration.from({ seconds: 1 }), // TTL
157+
50 // State to be sent to timer callback.
158+
);
159+
160+
// Delete the timer
161+
await actor.unregisterActorTimer("timer-id");
169162
```
170163

171164
### Actor Reminders
172165
```javascript
173-
import { DaprClient, DaprServer } from "dapr-client";
174-
import ParkingSensorImpl from "./ParkingSensorImpl";
166+
// ...
175167

176-
async function start()
177-
const server = new DaprServer(`server-host`, `server-port`, `dapr-host`, `dapr-port`);
178-
const client = new DaprClient(`dapr-host`, `dapr-port`);
168+
const actor = builder.build(new ActorId("my-actor"));
179169

180-
await server.actor.init();
181-
server.actor.registerActor(ParkingSensorImpl);
182-
await server.start();
170+
// Register a reminder, it has a default callback: `receiveReminder`
171+
await actor.registerActorReminder(
172+
"reminder-id", // Unique name of the reminder.
173+
Temporal.Duration.from({ seconds: 2 }), // DueTime
174+
Temporal.Duration.from({ seconds: 1 }), // Period
175+
Temporal.Duration.from({ seconds: 1 }), // TTL
176+
100 // State to be sent to reminder callback.
177+
);
183178

179+
// Delete the reminder
180+
await actor.unregisterActorReminder("reminder-id");
181+
```
184182

185-
// Register a reminder, it has a default callback
186-
await client.actor.reminderCreate(DemoActorImpl.name, `actor-id`, `timer-id`, {
187-
dueTime: Temporal.Duration.from({ seconds: 2 }),
188-
period: Temporal.Duration.from({ seconds: 1 }),
189-
ttl: Temporal.Duration.from({ seconds: 1 }),
190-
data: 100
191-
});
183+
To handle the callback, you need to override the default `receiveReminder` implementation in your actor. For example, from our original actor implementation:
184+
```ts
185+
export default class ParkingSensorImpl extends AbstractActor implements ParkingSensorInterface {
186+
// ...
187+
188+
/**
189+
* @override
190+
*/
191+
async receiveReminder(state: any): Promise<void> {
192+
// handle stuff here
193+
}
192194

193-
// Delete the reminder
194-
await client.actor.reminderDelete(DemoActorImpl.name, `actor-id`, `timer-id`);
195+
// ...
195196
}
196197
```
197198

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

0 commit comments

Comments
 (0)