Skip to content

Commit b839e55

Browse files
committed
Update readme and set up mops
1 parent fc61ef2 commit b839e55

4 files changed

Lines changed: 81 additions & 3 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.mops/matchers@7f95f69ae9a399cedf161d90738db822f315c07e/.gitignore

README.md

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,70 @@
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.

dfx.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"defaults": {
1111
"build": {
1212
"args": "",
13-
"packtool": "vessel sources"
13+
"packtool": "mops sources"
1414
},
1515
"replica": {
1616
"subnet_type": "system"

mops.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "star"
3+
version = "0.1.0"
4+
description = ""
5+
repository = ""
6+
7+
[dependencies]
8+
base = "https://github.com/dfinity/motoko-base#0d42146536771a60933786d8387b7c4ea4a1614b"
9+
matchers = "https://github.com/kritzcreek/motoko-matchers#7f95f69ae9a399cedf161d90738db822f315c07e"

0 commit comments

Comments
 (0)