Skip to content

Commit 58a5f2f

Browse files
committed
wip
1 parent 6eadb6e commit 58a5f2f

18 files changed

+923
-2
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
cpu: arm64
2929
- os: windows
3030
cpu: amd64
31-
branch: [version-2-0, version-2-2, devel]
31+
branch: [version-1-6, version-2-0, version-2-2, devel]
3232
include:
3333
- target:
3434
os: linux

.github/workflows/docs.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: Docgen
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- docs
7+
workflow_dispatch:
8+
9+
jobs:
10+
build:
11+
timeout-minutes: 20
12+
13+
name: 'Generate & upload documentation'
14+
runs-on: 'ubuntu-latest'
15+
continue-on-error: true
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v4
19+
with:
20+
submodules: true
21+
- uses: actions-rs/[email protected]
22+
with:
23+
crate: mdbook
24+
use-tool-cache: true
25+
version: "0.4.51"
26+
- uses: actions-rs/[email protected]
27+
with:
28+
crate: mdbook-toc
29+
use-tool-cache: true
30+
version: "0.14.2"
31+
- uses: actions-rs/[email protected]
32+
with:
33+
crate: mdbook-open-on-gh
34+
use-tool-cache: true
35+
version: "2.4.3"
36+
- uses: actions-rs/[email protected]
37+
with:
38+
crate: mdbook-admonish
39+
use-tool-cache: true
40+
version: "1.20.0"
41+
42+
- uses: jiro4989/setup-nim-action@v1
43+
with:
44+
nim-version: '1.6.20'
45+
46+
- name: Generate doc
47+
run: |
48+
nim --version
49+
nimble --version
50+
nimble install -dy
51+
nimble mdbook
52+
nimble docs || true
53+
54+
- name: Deploy
55+
uses: peaceiris/actions-gh-pages@v3
56+
with:
57+
github_token: ${{ secrets.GITHUB_TOKEN }}
58+
publish_dir: ./docs/book
59+
force_orphan: true

docs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
book

docs/book.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[book]
2+
authors = ["Jacek Sieka"]
3+
language = "en"
4+
multilingual = false
5+
src = "src"
6+
title = "json_serialization"
7+
8+
[preprocessor.toc]
9+
command = "mdbook-toc"
10+
renderer = ["html"]
11+
max-level = 2
12+
13+
[preprocessor.open-on-gh]
14+
command = "mdbook-open-on-gh"
15+
renderer = ["html"]
16+
17+
[output.html]
18+
git-repository-url = "https://github.com/status-im/nim-json-serialization/"
19+
git-branch = "master"
20+
additional-css = ["open-in.css"]

docs/open-in.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
footer {
2+
font-size: 0.8em;
3+
text-align: center;
4+
border-top: 1px solid black;
5+
padding: 5px 0;
6+
}
7+

docs/src/SUMMARY.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
- [Introduction](./introduction.md)
2+
- [Examples](./examples.md)
3+
4+
# User guide
5+
6+
- [Core concepts](./concepts.md)
7+
- [`async` functions](async_procs.md)
8+
- [Errors and exceptions](./error_handling.md)
9+
- [Threads](./threads.md)
10+
- [Tips, tricks and best practices](./tips.md)
11+
- [Porting code to `chronos`](./porting.md)
12+
- [HTTP server middleware](./http_server_middleware.md)
13+
14+
# Developer guide
15+
16+
- [Updating this book](./book.md)

docs/src/async_procs.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Async procedures
2+
3+
Async procedures are those that interact with `chronos` to cooperatively
4+
suspend and resume their execution depending on the completion of other
5+
async procedures, timers, tasks on other threads or asynchronous I/O scheduled
6+
with the operating system.
7+
8+
Async procedures are marked with the `{.async.}` pragma and return a `Future`
9+
indicating the state of the operation.
10+
11+
<!-- toc -->
12+
13+
## The `async` pragma
14+
15+
The `{.async.}` pragma will transform a procedure (or a method) returning a
16+
`Future` into a closure iterator. If there is no return type specified,
17+
`Future[void]` is returned.
18+
19+
```nim
20+
proc p() {.async.} =
21+
await sleepAsync(100.milliseconds)
22+
23+
echo p().type # prints "Future[system.void]"
24+
```
25+
26+
## `await` keyword
27+
28+
The `await` keyword operates on `Future` instances typically returned from an
29+
`async` procedure.
30+
31+
Whenever `await` is encountered inside an async procedure, control is given
32+
back to the dispatcher for as many steps as it's necessary for the awaited
33+
future to complete, fail or be cancelled. `await` calls the
34+
equivalent of `Future.read()` on the completed future to return the
35+
encapsulated value when the operation finishes.
36+
37+
```nim
38+
proc p1() {.async.} =
39+
await sleepAsync(1.seconds)
40+
41+
proc p2() {.async.} =
42+
await sleepAsync(1.seconds)
43+
44+
proc p3() {.async.} =
45+
let
46+
fut1 = p1()
47+
fut2 = p2()
48+
# Just by executing the async procs, both resulting futures entered the
49+
# dispatcher queue and their "clocks" started ticking.
50+
await fut1
51+
await fut2
52+
# Only one second passed while awaiting them both, not two.
53+
54+
waitFor p3()
55+
```
56+
57+
```admonition warning
58+
Because `async` procedures are executed concurrently, they are subject to many
59+
of the same risks that typically accompany multithreaded programming.
60+
61+
In particular, if two `async` procedures have access to the same mutable state,
62+
the value before and after `await` might not be the same as the order of execution is not guaranteed!
63+
```
64+
65+
## Raw async procedures
66+
67+
Raw async procedures are those that interact with `chronos` via the `Future`
68+
type but whose body does not go through the async transformation.
69+
70+
Such functions are created by adding `raw: true` to the `async` parameters:
71+
72+
```nim
73+
proc rawAsync(): Future[void] {.async: (raw: true).} =
74+
let fut = newFuture[void]("rawAsync")
75+
fut.complete()
76+
fut
77+
```
78+
79+
Raw functions must not raise exceptions directly - they are implicitly declared
80+
as `raises: []` - instead they should store exceptions in the returned `Future`:
81+
82+
```nim
83+
proc rawFailure(): Future[void] {.async: (raw: true).} =
84+
let fut = newFuture[void]("rawAsync")
85+
fut.fail((ref ValueError)(msg: "Oh no!"))
86+
fut
87+
```
88+
89+
Raw procedures can also use checked exceptions:
90+
91+
```nim
92+
proc rawAsyncRaises(): Future[void] {.async: (raw: true, raises: [IOError]).} =
93+
let fut = newFuture[void]()
94+
assert not (compiles do: fut.fail((ref ValueError)(msg: "uh-uh")))
95+
fut.fail((ref IOError)(msg: "IO"))
96+
fut
97+
```
98+
99+
## Callbacks and closures
100+
101+
Callback/closure types are declared using the `async` annotation as usual:
102+
103+
```nim
104+
type MyCallback = proc(): Future[void] {.async.}
105+
106+
proc runCallback(cb: MyCallback) {.async: (raises: []).} =
107+
try:
108+
await cb()
109+
except CatchableError:
110+
discard # handle errors as usual
111+
```
112+
113+
When calling a callback, it is important to remember that it may raise exceptions that need to be handled.
114+
115+
Checked exceptions can be used to limit the exceptions that a callback can
116+
raise:
117+
118+
```nim
119+
type MyEasyCallback = proc(): Future[void] {.async: (raises: []).}
120+
121+
proc runCallback(cb: MyEasyCallback) {.async: (raises: [])} =
122+
await cb()
123+
```

docs/src/book.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Updating this book

0 commit comments

Comments
 (0)