|
1 | | -# star.mo |
2 | | -A library for dealing with async* state. |
| 1 | +# Star Motoko Library - star.mo |
| 2 | + |
| 3 | +A Motoko library for handling asynchronous and trappable behavior with the async* functions. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +`Star` is a custom type with three variants: `#trappable`, `#awaited`, and `#err`. These represent different states of success or failure. `#trappable` and `#awaited` represent success, while `#err` represents error. The difference between `#trappable` and `#awaited` is that `#awaited` is produced with an awaited call, while `#trappable` is produced without one. |
| 8 | + |
| 9 | +This distinction is important because a value returned from an async* function carries no state information about whether the called function made a state commitment or not. You will not know if the logic before your call has been committed to the state tree or not. |
| 10 | + |
| 11 | +The suggested pattern is to never use async* without returning a Star and handling the four possible states: |
| 12 | + |
| 13 | +- an await occured and you have a return value #awaited(X) |
| 14 | +- an await did not occur and you have a return value #trappable(X) |
| 15 | +- an error occured but state was committed. #err(#awaited(E)) |
| 16 | +- an error occured but state was not committed. #err(#trappable(E)) |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +To use this library in your project, you will need to import it first. |
| 21 | + |
| 22 | +mops install star |
| 23 | + |
| 24 | +```motoko |
| 25 | +import Star "mo:star/star"; |
| 26 | +``` |
| 27 | + |
| 28 | +Requires at least moc 0.8.3. |
| 29 | + |
| 30 | +Then, you can use the provided functions to work with Star types: |
| 31 | + |
| 32 | +``` |
| 33 | +equal |
| 34 | +compare |
| 35 | +flatten |
| 36 | +mapOk |
| 37 | +mapErr |
| 38 | +fromOption |
| 39 | +toOption |
| 40 | +toResult |
| 41 | +fromResult |
| 42 | +iterate |
| 43 | +isOk |
| 44 | +isAwaited |
| 45 | +isTrappable |
| 46 | +isErr |
| 47 | +assertOk |
| 48 | +assertErr |
| 49 | +assertTrappable |
| 50 | +assertAwaited |
| 51 | +``` |
| 52 | + |
| 53 | +Example |
| 54 | +Here's an example of using a Star type with a function createUser(user : User) : Star<Id, String>: |
| 55 | + |
| 56 | +``` |
| 57 | +switch(await* createUser(myUser)) { |
| 58 | + case (#awaited(id)) { Debug.print("Created new user with id and state committed: " # id) }; |
| 59 | + case (#trappable(id)) { Debug.print("Created new user with id and state has not been committed: " # id) }; |
| 60 | + case (#err(#awaited(msg))) { Debug.print("Failed to create user with the error but state was committed: " # msg) }; |
| 61 | + case (#err(#trappable(msg))) { Debug.print("Failed to create user with the error but state was not committed: " # msg) }; |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +## License |
| 66 | + |
| 67 | +This library is provided under the MIT License. |
| 68 | + |
| 69 | +## Contributing |
| 70 | +Please feel free to open issues or submit pull requests for any bug fixes or improvements. |
0 commit comments