-
Notifications
You must be signed in to change notification settings - Fork 1
Add a new helper type for SAFE.Client: Optimistic
#12
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
Open
arpxspace
wants to merge
5
commits into
main
Choose a base branch
from
optimistic-updates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aeb607c
[feat]: Add new helper client types for optimistic update to support …
arpxspace b4fd825
[feat]: Updated XML docs; changed Curr property to Value for clarity.
arpxspace d4386c9
[feat]: Added tests and utility functions for Optimistic helper type …
arpxspace 56b5a3b
[fix]: Opted for DU over record type to prevent illegal states.
arpxspace ad9db93
Update src/SAFE.Client/SAFE.fs
arpxspace File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -185,4 +185,53 @@ module RemoteData = | |||||
/// `Loaded x -> Loading x`; | ||||||
/// `NotStarted -> Loading None`; | ||||||
/// `Loading x -> Loading x`; | ||||||
let startLoading (remote: RemoteData<'T>) = remote.StartLoading | ||||||
let startLoading (remote: RemoteData<'T>) = remote.StartLoading | ||||||
|
||||||
///A type which represents optimistic updates. | ||||||
type Optimistic<'T> = | ||||||
| NonExistent | ||||||
| Exists of value:'T * prev:'T option | ||||||
with | ||||||
/// Retrieves the current value | ||||||
member this.Value = | ||||||
match this with | ||||||
| NonExistant -> None | ||||||
| Exists (v, pv) -> Some v | ||||||
|
||||||
/// Updates the current value, shifting the existing current value to previous. | ||||||
member this.Update (value: 'T) = | ||||||
match this with | ||||||
| NonExistant -> NonExistant | ||||||
| Exists (v, pv) -> Exists (value, Some v) | ||||||
|
||||||
/// Rolls back to the previous value, discarding the current one. | ||||||
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.
Suggested change
"Rollback" is a noun, "roll back" is the verb 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 honestly prefer |
||||||
member this.Rollback () = | ||||||
match this with | ||||||
| NonExistant -> NonExistant | ||||||
| Exists (_, Some pv) -> Exists (pv , None) | ||||||
| Exists (_, None) -> NonExistant | ||||||
|
||||||
/// Maps the underlying optimistic value, when it exists, into another shape. | ||||||
member this.Map (f: 'T -> 'U) = | ||||||
match this with | ||||||
| NonExistant -> NonExistant | ||||||
| Exists (v, pv) -> Exists (f v, pv |> Option.map f) | ||||||
|
||||||
/// Module containing functions for working with Optimistic type | ||||||
module Optimistic = | ||||||
/// Creates a new Optimistic value with no history | ||||||
let create value = | ||||||
Exists (value, None) | ||||||
|
||||||
/// Creates an empty Optimistic value | ||||||
let empty = | ||||||
NonExistant | ||||||
|
||||||
/// Updates the current value, shifting existing value to previous | ||||||
let update value (optimistic: Optimistic<'T>) = optimistic.Update value | ||||||
|
||||||
/// Rolls back to the previous value | ||||||
let rollback (optimistic: Optimistic<'T>) = optimistic.Rollback() | ||||||
|
||||||
/// Maps both current and previous values | ||||||
let map f (optimistic: Optimistic<'T>) = optimistic.Map f |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I feel like the case names could perhaps be clearer.
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.
Could it be represented as an option instead? like