Skip to content

Commit 7b52d95

Browse files
committed
tutorial
1 parent 012dd22 commit 7b52d95

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

lua/tutorial.lua

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
local a = require "async"
2+
local co = coroutine
3+
local uv = vim.loop
4+
5+
6+
--#################### ########### ####################
7+
--#################### Sync Region ####################
8+
--#################### ########### ####################
9+
10+
11+
-- sync version of pong
12+
local pong = function (thread)
13+
local nxt = nil
14+
nxt = function (cont, ...)
15+
if not cont
16+
then return ...
17+
else return nxt(co.resume(thread), ...)
18+
end
19+
end
20+
return nxt(co.resume(thread))
21+
end
22+
23+
24+
local sync_example = function ()
25+
26+
local thread = co.create(function ()
27+
local x = co.yield(1)
28+
print(x)
29+
local y, z = co.yield(2, 3)
30+
print(y)
31+
return 4, 5
32+
end)
33+
34+
local w, t = pong(thread)
35+
print(w, t)
36+
end
37+
38+
39+
--#################### ############ ####################
40+
--#################### Async Region ####################
41+
--#################### ############ ####################
42+
43+
44+
local timeout = function (ms, callback)
45+
local timer = assert(uv.new_timer())
46+
uv.timer_start(timer, ms, 0, function ()
47+
assert(uv.timer_stop(timer))
48+
assert(uv.close(timer))
49+
callback()
50+
end)
51+
end
52+
53+
54+
-- typical nodejs / luv function
55+
local echo_2 = function (msg1, msg2, callback)
56+
-- wait 200ms
57+
timeout(200, function ()
58+
callback(msg1, msg2)
59+
end)
60+
end
61+
62+
63+
-- thunkify echo_2
64+
local e2 = a.wrap(echo_2)
65+
66+
67+
local async_tasks_1 = a.sync(function ()
68+
local x, y = a.wait(e2(1, 2))
69+
print(x, y)
70+
return x + y
71+
end)
72+
73+
74+
local async_tasks_2 = function (val)
75+
return a.sync(function ()
76+
-- await all
77+
local w, z = a.wait_all{e2(val, val + 1), e2(val + 2, val + 3)}
78+
print(unpack(w), unpack(z))
79+
return 5
80+
end)
81+
end
82+
83+
84+
local async_example = a.sync(function ()
85+
-- composable, await other async thunks
86+
local u = a.wait(async_tasks_1)
87+
local v = a.wait(async_tasks_2(3))
88+
print(u + v)
89+
end)
90+
91+
92+
--#################### ############ ####################
93+
--#################### Loops Region ####################
94+
--#################### ############ ####################
95+
96+
97+
-- avoid textlock
98+
local main_loop = function (f)
99+
vim.schedule(f)
100+
end
101+
102+
103+
local vim_command = function ()
104+
end
105+
106+
107+
local textlock_fail = a.sync(function ()
108+
a.wait(e2(1, 2))
109+
vim_command()
110+
end)
111+
112+
113+
local textlock_succ = a.sync(function ()
114+
a.wait(e2(1, 2))
115+
a.wait(main_loop)
116+
vim_command()
117+
end)
118+
119+
120+
return {
121+
sync_example = sync_example,
122+
async_example = async_example,
123+
textlock_fail = textlock_fail,
124+
textlock_succ = textlock_succ,
125+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy