@@ -4,85 +4,85 @@ import ActorStateChange from "./ActorStateChange";
4
4
import StateChangeKind from "./StateChangeKind" ;
5
5
6
6
export default class StateProvider {
7
- stateClient : DaprClient ;
8
-
9
- constructor ( daprClient : DaprClient ) {
10
- this . stateClient = daprClient ;
11
- }
7
+ stateClient : DaprClient ;
12
8
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
+ }
17
12
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
+ }
21
17
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 ) ;
25
21
26
- // const result = this.serializer.deserialize(rawStateValue);
27
-
28
- return [ true , rawStateValue ] ;
22
+ if ( ! rawStateValue || rawStateValue . length === 0 ) {
23
+ return [ false , undefined ] ;
29
24
}
30
25
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);
54
27
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
+ }
76
30
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 [ ] = [ ] ;
85
54
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
+ } ) ;
87
84
}
85
+
86
+ await this . stateClient . actor . stateTransaction ( actorType , actorId , jsonOutput ) ;
87
+ }
88
88
}
0 commit comments