Skip to content

Commit b4c328b

Browse files
committed
refactor!: change the file name to avoid conflicts with lazy async lib
1 parent fc6049a commit b4c328b

File tree

3 files changed

+24
-130
lines changed

3 files changed

+24
-130
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ end
4949
This is how you can call this asynchronous API without a callback
5050

5151
```lua
52+
local M = require('sync')
53+
5254
M.sync(function()
5355
local response = M.wait_handle_error(M.wrap(lsp_request)())
5456
end).run()
@@ -68,6 +70,8 @@ stack traceback:
6870
### When error handler is defined
6971

7072
```lua
73+
local M = require('sync')
74+
7175
local main = M.sync(function()
7276
local response = M.wait_handle_error(M.wrap(lsp_request)())
7377
end)
@@ -86,6 +90,8 @@ error occured test6.lua:105: something went wrong
8690
### When nested
8791

8892
```lua
93+
local M = require('sync')
94+
8995
local nested = M.sync(function()
9096
local response = M.wait_handle_error(M.wrap(lsp_request)())
9197
end)
File renamed without changes.

lua/tutorial.lua

Lines changed: 18 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,25 @@
1-
local a = require "async"
2-
local co = coroutine
3-
local uv = vim.loop
1+
local M = require("sync")
42

3+
local function lsp_request(callback)
4+
local timer = vim.loop.new_timer()
55

6-
--#################### ########### ####################
7-
--#################### Sync Region ####################
8-
--#################### ########### ####################
6+
assert(timer)
97

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, z)
31-
local f = co.yield(4)
32-
print(f)
33-
end)
34-
35-
pong(thread)
36-
end
37-
38-
39-
--#################### ############ ####################
40-
--#################### Async Region ####################
41-
--#################### ############ ####################
42-
43-
44-
local timeout = function (ms, callback)
45-
local timer = uv.new_timer()
46-
uv.timer_start(timer, ms, 0, function ()
47-
uv.timer_stop(timer)
48-
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 = function()
68-
return a.sync(function ()
69-
local x, y = a.wait(e2(1, 2))
70-
print(x, y)
71-
return x + y
72-
end)
8+
timer:start(2000, 0, function()
9+
callback("something went wrong")
10+
end)
7311
end
7412

13+
vim.cmd.messages("clear")
7514

76-
local async_tasks_2 = function (val)
77-
return a.sync(function ()
78-
-- await all
79-
local w, z = a.wait_all{e2(val, val + 1), e2(val + 2, val + 3)}
80-
print(unpack(w))
81-
print(unpack(z))
82-
return function ()
83-
return 4
84-
end
85-
end)
86-
end
87-
88-
89-
local async_example = function ()
90-
return a.sync(function ()
91-
-- composable, await other async thunks
92-
local u = a.wait(async_tasks_1())
93-
local v = a.wait(async_tasks_2(3))
94-
print(u + v())
95-
end)
96-
end
97-
98-
99-
--#################### ############ ####################
100-
--#################### Loops Region ####################
101-
--#################### ############ ####################
102-
103-
104-
-- avoid textlock
105-
local main_loop = function (f)
106-
vim.schedule(f)
107-
end
108-
109-
110-
local vim_command = function ()
111-
vim.api.nvim_command[[echom 'calling vim command']]
112-
end
113-
114-
115-
local textlock_fail = function()
116-
return a.sync(function ()
117-
a.wait(e2(1, 2))
118-
vim_command()
119-
end)
120-
end
121-
122-
123-
local textlock_succ = function ()
124-
return a.sync(function ()
125-
a.wait(e2(1, 2))
126-
a.wait(main_loop)
127-
vim_command()
128-
end)
129-
end
130-
15+
local nested = M.sync(function()
16+
local response = M.wait_handle_error(M.wrap(lsp_request)())
17+
end)
13118

132-
return {
133-
sync_example = sync_example,
134-
async_example = async_example,
135-
textlock_fail = textlock_fail,
136-
textlock_succ = textlock_succ,
137-
}
19+
M.sync(function()
20+
M.wait_handle_error(nested.run)
21+
end)
22+
.catch(function(err)
23+
print("parent error handler " .. err)
24+
end)
25+
.run()

0 commit comments

Comments
 (0)