-
Notifications
You must be signed in to change notification settings - Fork 53
Add HasState and default value to GetState<T> #222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The changes here are to make it a bit more robust when it is accessed multiple times with different types. Today the following would throw: operation.State.GetState(typeof(object));
operation.State.GetState(typeof(int));With this change, it will be possible. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,14 +100,16 @@ class StateShim : TaskEntityState | |
|
|
||
| string? value; | ||
| object? cachedValue; | ||
| bool cacheValid; | ||
| string? checkpointValue; | ||
|
|
||
| public StateShim(DataConverter dataConverter) | ||
| { | ||
| this.dataConverter = dataConverter; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override bool HasState => this.value != null; | ||
|
|
||
| public string? CurrentState | ||
| { | ||
| get => this.value; | ||
|
|
@@ -117,7 +119,6 @@ public string? CurrentState | |
| { | ||
| this.value = value; | ||
| this.cachedValue = null; | ||
| this.cacheValid = false; | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -130,29 +131,30 @@ public void Commit() | |
| public void Rollback() | ||
| { | ||
| this.CurrentState = this.checkpointValue; | ||
| this.cachedValue = null; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that clearing the cache explicitly seems like a good idea here. (since the code may have modified the object without calling |
||
| } | ||
|
|
||
| public void Reset() | ||
| { | ||
| this.CurrentState = default; | ||
| this.cachedValue = null; | ||
| } | ||
|
|
||
| public override object? GetState(Type type) | ||
| { | ||
| if (!this.cacheValid) | ||
| if (this.cachedValue?.GetType() is Type t && t.IsAssignableFrom(type)) | ||
| { | ||
| this.cachedValue = this.dataConverter.Deserialize(this.value, type); | ||
| this.cacheValid = true; | ||
| return this.cachedValue; | ||
| } | ||
|
|
||
| this.cachedValue = this.dataConverter.Deserialize(this.value, type); | ||
| return this.cachedValue; | ||
| } | ||
|
|
||
| public override void SetState(object? state) | ||
| { | ||
| this.value = this.dataConverter.Serialize(state); | ||
| this.cachedValue = state; | ||
| this.cacheValid = true; | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shows how to do this before any of the new APIs. If we think this is good enough, can always revert the new API changes here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the new APIs would make more sense here I think.