@@ -23,8 +23,8 @@ This is an example class that gets a tweet (type `Status`) from a remote HTTP AP
23
23
import { Failure , fold , Initialized , Pending , RemoteData , Success } from ' @abraham/remotedata' ;
24
24
import { Status } from ' twitter-d' ;
25
25
26
- // Define what the RemoteData Success and Failure types will be.
27
- type State = RemoteData <Status , number >;
26
+ // Define what the RemoteData Failure and Success types will be.
27
+ type State = RemoteData <number , Status >;
28
28
29
29
class Thing {
30
30
// Set the initial state as Initialized.
@@ -54,20 +54,20 @@ class Thing {
54
54
}
55
55
56
56
private get view(state : State ): (state : State ) => string {
57
- // Use `fold` to easily define logic based on the state.
58
- // Fold takes four methods as parameters in the order of Initialized, Pending, Success, Failure
59
- // and return a method method that takes a `RemoteData` instance as a parameter.
60
- // When returned method gets called with a `RemoteData` state, the method matching the current
57
+ // Use `fold` to easily define logic based on the current state.
58
+ // Fold takes four methods as parameters in the order of Initialized, Pending, Failure, and
59
+ // Success and returns a method that takes a `RemoteData` state instance as a parameter. When
60
+ // the returned method gets called with a `RemoteData` state, the method matching the correct
61
61
// state gets called.
62
- return fold <string , Status , number >(
62
+ return fold <string , number , Status >(
63
63
// What to do if the state is Initialized.
64
64
() => ' Initialized' ,
65
65
// What to do if the request is Pending.
66
66
() => ' Loading...' ,
67
+ // What to do if the request fails.
68
+ (error : number ) => ` Error: ${error } ` ,
67
69
// What to do if the request succeeds.
68
70
(status : Status ) => ` Got tweet from ${status .screen_name } ` ,
69
- // What to do if the request fails.
70
- (error : string ) => ` Error: ${error } ` ,
71
71
);
72
72
}
73
73
@@ -86,3 +86,6 @@ References
86
86
- [ How Elm Slays a UI Antipattern] ( http://blog.jenkster.com/2016/06/how-elm-slays-a-ui-antipattern.html )
87
87
- [ Slaying a UI Antipattern with Angular] ( https://medium.com/@joanllenas/slaying-a-ui-antipattern-with-angular-4c7536fafc54 )
88
88
- [ Slaying a UI Antipattern with Flow] ( https://medium.com/@gcanti/slaying-a-ui-antipattern-with-flow-5eed0cfb627b )
89
+ - [ Slaying a UI Antipattern in React] ( https://medium.com/javascript-inside/slaying-a-ui-antipattern-in-react-64a3b98242c )
90
+ - [ Slaying a UI Antipattern in Fantasyland] ( https://medium.com/javascript-inside/slaying-a-ui-antipattern-in-fantasyland-907cbc322d2a )
91
+ - [ Remote Data State as an Enum] ( http://holko.pl/2016/06/09/data-state-as-an-enum/ )
0 commit comments