|
1 | | -local a = require "async" |
2 | | -local co = coroutine |
3 | | -local uv = vim.loop |
| 1 | +local M = require("sync") |
4 | 2 |
|
| 3 | +local function lsp_request(callback) |
| 4 | + local timer = vim.loop.new_timer() |
5 | 5 |
|
6 | | ---#################### ########### #################### |
7 | | ---#################### Sync Region #################### |
8 | | ---#################### ########### #################### |
| 6 | + assert(timer) |
9 | 7 |
|
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) |
73 | 11 | end |
74 | 12 |
|
| 13 | +vim.cmd.messages("clear") |
75 | 14 |
|
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) |
131 | 18 |
|
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