Skip to content

Commit d731ee1

Browse files
Merge pull request #116 from XavierGeerinck/master
Fix issue with Javascript ! checking on state manager
2 parents 04e9102 + 008482e commit d731ee1

File tree

2 files changed

+74
-72
lines changed

2 files changed

+74
-72
lines changed

src/actors/runtime/ActorStateManager.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ export default class ActorStateManager<T> {
7070
return [false, null];
7171
}
7272

73-
return [true, stateMetadata?.getValue() || null];
73+
const val = stateMetadata?.getValue();
74+
return [true, val !== undefined ? val : null];
7475
}
7576

7677
const [hasValue, value] = await this.actor.getStateProvider().tryLoadState(this.actor.getActorType(), this.actor.getId().getId(), stateName);
7778

79+
7880
if (hasValue) {
7981
stateChangeTracker.set(stateName, new StateMetadata(value, StateChangeKind.NONE));
8082
}

src/actors/runtime/StateProvider.ts

Lines changed: 71 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,85 +4,85 @@ import ActorStateChange from "./ActorStateChange";
44
import StateChangeKind from "./StateChangeKind";
55

66
export default class StateProvider {
7-
stateClient: DaprClient;
8-
9-
constructor(daprClient: DaprClient) {
10-
this.stateClient = daprClient;
11-
}
7+
stateClient: DaprClient;
128

13-
async containsState(actorType: string, actorId: string, stateName: string): Promise<boolean> {
14-
const rawStateValue = await this.stateClient.actor.stateGet(actorType, actorId, stateName);
15-
return !!rawStateValue && rawStateValue.length > 0;
16-
}
9+
constructor(daprClient: DaprClient) {
10+
this.stateClient = daprClient;
11+
}
1712

18-
// 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-
const rawStateValue = await this.stateClient.actor.stateGet(actorType, actorId, stateName);
13+
async containsState(actorType: string, actorId: string, stateName: string): Promise<boolean> {
14+
const rawStateValue = await this.stateClient.actor.stateGet(actorType, actorId, stateName);
15+
return !!rawStateValue && rawStateValue.length > 0;
16+
}
2117

22-
if (!rawStateValue || rawStateValue.length === 0) {
23-
return [ false, undefined ];
24-
}
18+
// 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+
const rawStateValue = await this.stateClient.actor.stateGet(actorType, actorId, stateName);
2521

26-
// const result = this.serializer.deserialize(rawStateValue);
27-
28-
return [ true, rawStateValue ];
22+
if (!rawStateValue || rawStateValue.length === 0) {
23+
return [false, undefined];
2924
}
3025

31-
/**
32-
* Create and save the state through a transactional update request body. E.g.
33-
* [
34-
* {
35-
* "operation": "upsert",
36-
* "request": {
37-
* "key": "key1",
38-
* "value": "myData"
39-
* }
40-
* },
41-
* {
42-
* "operation": "delete",
43-
* "request": {
44-
* "key": "key2"
45-
* }
46-
* }
47-
* ]
48-
* @param actorType
49-
* @param actorId
50-
* @param stateChanges
51-
*/
52-
async saveState(actorType: string, actorId: string, stateChanges: ActorStateChange<any>[]): Promise<void> {
53-
const jsonOutput: OperationType[] = [];
26+
// const result = this.serializer.deserialize(rawStateValue);
5427

55-
for (const state of stateChanges) {
56-
let operation;
57-
58-
switch (state.getChangeKind()) {
59-
case StateChangeKind.ADD:
60-
// dapr doesn't know add, we use upsert
61-
// https://github.com/dapr/dapr/blob/master/pkg/actors/transactional_state_request.go
62-
operation = "upsert";
63-
break;
64-
case StateChangeKind.REMOVE:
65-
operation = "remove";
66-
break;
67-
case StateChangeKind.UPDATE:
68-
// dapr doesn't know add, we use upsert
69-
// https://github.com/dapr/dapr/blob/master/pkg/actors/transactional_state_request.go
70-
operation = "upsert";
71-
break;
72-
default:
73-
// skip
74-
continue;
75-
}
28+
return [true, rawStateValue];
29+
}
7630

77-
jsonOutput.push({
78-
operation,
79-
request: {
80-
key: state.getStateName(),
81-
value: state.getValue() as string
82-
}
83-
});
84-
}
31+
/**
32+
* Create and save the state through a transactional update request body. E.g.
33+
* [
34+
* {
35+
* "operation": "upsert",
36+
* "request": {
37+
* "key": "key1",
38+
* "value": "myData"
39+
* }
40+
* },
41+
* {
42+
* "operation": "delete",
43+
* "request": {
44+
* "key": "key2"
45+
* }
46+
* }
47+
* ]
48+
* @param actorType
49+
* @param actorId
50+
* @param stateChanges
51+
*/
52+
async saveState(actorType: string, actorId: string, stateChanges: ActorStateChange<any>[]): Promise<void> {
53+
const jsonOutput: OperationType[] = [];
8554

86-
await this.stateClient.actor.stateTransaction(actorType, actorId, jsonOutput);
55+
for (const state of stateChanges) {
56+
let operation;
57+
58+
switch (state.getChangeKind()) {
59+
case StateChangeKind.ADD:
60+
// dapr doesn't know add, we use upsert
61+
// https://github.com/dapr/dapr/blob/master/pkg/actors/transactional_state_request.go
62+
operation = "upsert";
63+
break;
64+
case StateChangeKind.REMOVE:
65+
operation = "remove";
66+
break;
67+
case StateChangeKind.UPDATE:
68+
// dapr doesn't know add, we use upsert
69+
// https://github.com/dapr/dapr/blob/master/pkg/actors/transactional_state_request.go
70+
operation = "upsert";
71+
break;
72+
default:
73+
// skip
74+
continue;
75+
}
76+
77+
jsonOutput.push({
78+
operation,
79+
request: {
80+
key: state.getStateName(),
81+
value: state.getValue() as string
82+
}
83+
});
8784
}
85+
86+
await this.stateClient.actor.stateTransaction(actorType, actorId, jsonOutput);
87+
}
8888
}

0 commit comments

Comments
 (0)