|
| 1 | +--- Allows chaining of async operations without callback hell |
| 2 | +--- |
1 | 3 | --- @class blink.cmp.Task
|
2 |
| ---- @field status 1 | 2 | 3 |
3 |
| ---- @field result any |
4 |
| ---- @field new fun(fn: fun(..., cb: fun(result: any, success: boolean | nil)), ...): blink.cmp.Task |
5 |
| ---- @field await fun(self: blink.cmp.Task, cb: fun(success: boolean, result: any)) |
| 4 | +--- @field status 1 | 2 | 3 | 4 |
| 5 | +--- @field result any | nil |
| 6 | +--- @field error any | nil |
| 7 | +--- @field new fun(fn: fun(resolve: fun(result: any), reject: fun(err: any))): blink.cmp.Task |
| 8 | +--- |
6 | 9 | --- @field cancel fun(self: blink.cmp.Task)
|
| 10 | +--- @field map fun(self: blink.cmp.Task, fn: fun(result: any): blink.cmp.Task | any) |
| 11 | +--- @field catch fun(self: blink.cmp.Task, fn: fun(err: any): blink.cmp.Task | any) |
| 12 | +--- |
| 13 | +--- @field on_completion fun(self: blink.cmp.Task, cb: fun(result: any)) |
| 14 | +--- @field on_failure fun(self: blink.cmp.Task, cb: fun(err: any)) |
| 15 | +--- @field on_cancel fun(self: blink.cmp.Task, cb: fun()) |
7 | 16 |
|
8 | 17 | local STATUS = {
|
9 | 18 | RUNNING = 1,
|
10 | 19 | COMPLETED = 2,
|
11 | 20 | FAILED = 3,
|
| 21 | + CANCELLED = 4, |
12 | 22 | }
|
13 | 23 |
|
14 |
| -local task = {} |
| 24 | +local task = { |
| 25 | + __task = true, |
| 26 | +} |
15 | 27 |
|
16 |
| -function task.new(fn, ...) |
| 28 | +function task.new(fn) |
17 | 29 | local self = setmetatable({}, { __index = task })
|
18 | 30 | self.status = STATUS.RUNNING
|
19 |
| - self._awaits = {} |
| 31 | + self._completion_cbs = {} |
| 32 | + self._failure_cbs = {} |
| 33 | + self._cancel_cbs = {} |
20 | 34 | self.result = nil
|
| 35 | + self.error = nil |
21 | 36 |
|
22 |
| - local arg = { ... } |
23 |
| - |
24 |
| - local success, cancel_or_err = pcall(function() |
25 |
| - local cb = function(result, success) |
26 |
| - if self.status ~= STATUS.RUNNING then return end |
27 |
| - if success == false then return self:cancel() end |
| 37 | + local resolve = function(result) |
| 38 | + if self.status ~= STATUS.RUNNING then return end |
28 | 39 |
|
29 |
| - self.status = STATUS.COMPLETED |
30 |
| - self.result = result |
| 40 | + self.status = STATUS.COMPLETED |
| 41 | + self.result = result |
31 | 42 |
|
32 |
| - for _, await_cb in ipairs(self._awaits) do |
33 |
| - await_cb(true, result) |
34 |
| - end |
| 43 | + for _, cb in ipairs(self._completion_cbs) do |
| 44 | + cb(result) |
35 | 45 | end
|
| 46 | + end |
| 47 | + |
| 48 | + local reject = function(err) |
| 49 | + if self.status ~= STATUS.RUNNING then return end |
36 | 50 |
|
37 |
| - -- todo: why doesnt unpack work? |
38 |
| - if #arg == 0 then |
39 |
| - return fn(cb) |
40 |
| - elseif #arg == 1 then |
41 |
| - return fn(arg[1], cb) |
42 |
| - elseif #arg == 2 then |
43 |
| - return fn(arg[1], arg[2], cb) |
| 51 | + self.status = STATUS.FAILED |
| 52 | + self.error = err |
| 53 | + |
| 54 | + for _, cb in ipairs(self._failure_cbs) do |
| 55 | + cb(err) |
44 | 56 | end
|
| 57 | + end |
45 | 58 |
|
46 |
| - return fn(unpack(arg), cb) |
47 |
| - end) |
| 59 | + local success, cancel_fn_or_err = pcall(function() return fn(resolve, reject) end) |
48 | 60 |
|
49 | 61 | if not success then
|
50 |
| - vim.print('Failed to create task :' .. cancel_or_err) |
51 |
| - self:cancel() |
52 |
| - elseif type(cancel_or_err) ~= 'function' then |
53 |
| - vim.print('Cancel is not a function') |
54 |
| - vim.print(cancel_or_err) |
55 |
| - self:cancel() |
56 |
| - else |
57 |
| - self._cancel = cancel_or_err |
| 62 | + reject(cancel_fn_or_err) |
| 63 | + elseif type(cancel_fn_or_err) == 'function' then |
| 64 | + self._cancel = cancel_fn_or_err |
58 | 65 | end
|
59 | 66 |
|
60 | 67 | return self
|
61 | 68 | end
|
62 | 69 |
|
63 | 70 | function task:cancel()
|
64 | 71 | if self.status ~= STATUS.RUNNING then return end
|
65 |
| - self.status = STATUS.FAILED |
| 72 | + self.status = STATUS.CANCELLED |
| 73 | + |
66 | 74 | if self._cancel ~= nil then self._cancel() end
|
| 75 | + for _, cb in ipairs(self._cancel_cbs) do |
| 76 | + cb() |
| 77 | + end |
| 78 | +end |
| 79 | + |
| 80 | +--- mappings |
| 81 | + |
| 82 | +function task:map(fn) |
| 83 | + local chained_task |
| 84 | + chained_task = task.new(function(resolve, reject) |
| 85 | + self:on_completion(function(result) |
| 86 | + local mapped_result = fn(result) |
| 87 | + if type(mapped_result) == 'table' and mapped_result.__task then |
| 88 | + mapped_result:on_completion(resolve) |
| 89 | + mapped_result:on_failure(reject) |
| 90 | + mapped_result:on_cancel(function() chained_task:cancel() end) |
| 91 | + return |
| 92 | + end |
| 93 | + resolve(mapped_result) |
| 94 | + end) |
| 95 | + self:on_failure(reject) |
| 96 | + self:on_cancel(function() chained_task:cancel() end) |
| 97 | + return function() chained_task:cancel() end |
| 98 | + end) |
| 99 | + return chained_task |
| 100 | +end |
| 101 | + |
| 102 | +function task:catch(fn) |
| 103 | + local chained_task |
| 104 | + chained_task = task.new(function(resolve, reject) |
| 105 | + self:on_completion(resolve) |
| 106 | + self:on_failure(function(err) |
| 107 | + local mapped_err = fn(err) |
| 108 | + if type(mapped_err) == 'table' and mapped_err.is_task then |
| 109 | + mapped_err:on_completion(resolve) |
| 110 | + mapped_err:on_failure(reject) |
| 111 | + mapped_err:on_cancel(function() chained_task:cancel() end) |
| 112 | + return |
| 113 | + end |
| 114 | + resolve(mapped_err) |
| 115 | + end) |
| 116 | + self:on_cancel(function() chained_task:cancel() end) |
| 117 | + return function() chained_task:cancel() end |
| 118 | + end) |
| 119 | + return chained_task |
| 120 | +end |
| 121 | + |
| 122 | +--- events |
67 | 123 |
|
68 |
| - for _, await_cb in ipairs(self._awaits) do |
69 |
| - await_cb(false) |
| 124 | +function task:on_completion(cb) |
| 125 | + if self.status == STATUS.COMPLETED then |
| 126 | + cb(self.result) |
| 127 | + elseif self.status == STATUS.RUNNING then |
| 128 | + table.insert(self._completion_cbs, cb) |
70 | 129 | end
|
| 130 | + return self |
71 | 131 | end
|
72 | 132 |
|
73 |
| -function task:await(cb) |
| 133 | +function task:on_failure(cb) |
74 | 134 | if self.status == STATUS.FAILED then
|
75 |
| - cb(false) |
76 |
| - elseif self.status == STATUS.COMPLETED then |
77 |
| - cb(true, self.result) |
78 |
| - else |
79 |
| - table.insert(self._awaits, cb) |
| 135 | + cb(self.error) |
| 136 | + elseif self.status == STATUS.RUNNING then |
| 137 | + table.insert(self._failure_cbs, cb) |
80 | 138 | end
|
81 | 139 | return self
|
82 | 140 | end
|
83 | 141 |
|
84 |
| -function task:map(f) |
85 |
| - return task.new(function(cb) |
86 |
| - self:await(function(success, result) |
87 |
| - if success then |
88 |
| - cb(f(result)) |
89 |
| - else |
90 |
| - cb(nil, false) |
91 |
| - end |
92 |
| - end) |
93 |
| - return function() self:cancel() end |
94 |
| - end) |
| 142 | +function task:on_cancel(cb) |
| 143 | + if self.status == STATUS.CANCELLED then |
| 144 | + cb() |
| 145 | + elseif self.status == STATUS.RUNNING then |
| 146 | + table.insert(self._cancel_cbs, cb) |
| 147 | + end |
| 148 | + return self |
95 | 149 | end
|
96 | 150 |
|
97 |
| -function task:map_error(f) |
98 |
| - return task.new(function(cb) |
99 |
| - self:await(function(success, result) |
100 |
| - if success then |
101 |
| - cb(result) |
102 |
| - else |
103 |
| - cb(f()) |
| 151 | +--- utils |
| 152 | + |
| 153 | +function task.await_all(tasks) |
| 154 | + return task.new(function(resolve) |
| 155 | + local results = {} |
| 156 | + |
| 157 | + local function resolve_if_completed() |
| 158 | + -- we can't check #results directly because a table like |
| 159 | + -- { [2] = { ... } } has a length of 2 |
| 160 | + for i = 1, #tasks do |
| 161 | + if results[i] == nil then return end |
104 | 162 | end
|
105 |
| - end) |
106 |
| - return function() self:cancel() end |
| 163 | + resolve(results) |
| 164 | + end |
| 165 | + |
| 166 | + for idx, task in ipairs(tasks) do |
| 167 | + task:on_completion(function(result) |
| 168 | + results[idx] = { status = STATUS.COMPLETED, result = result } |
| 169 | + resolve_if_completed() |
| 170 | + end) |
| 171 | + task:on_failure(function(err) |
| 172 | + results[idx] = { status = STATUS.FAILED, err = err } |
| 173 | + resolve_if_completed() |
| 174 | + end) |
| 175 | + task:on_cancel(function() |
| 176 | + results[idx] = { status = STATUS.CANCELLED } |
| 177 | + resolve_if_completed() |
| 178 | + end) |
| 179 | + end |
107 | 180 | end)
|
108 | 181 | end
|
109 | 182 |
|
|
0 commit comments