-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexamples.lua
127 lines (110 loc) · 3.07 KB
/
examples.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
local M = {}
local coop = require("coop")
local sleep = require("coop.uv-utils").sleep
local copcall = require("coop.coroutine-utils").copcall
local await_any = require("coop.control").await_any
local as_completed = require("coop.control").as_completed
local uv = require("coop.uv")
--- Gets the size of a file.
---
--- This is a task function.
---
---@async
---@param path string the path to the file
---@return boolean success
---@return number|string size_or_err
local get_file_size = function(path)
local err, fd_or_err = uv.fs_open(path, "r", 0)
if err then
return err, fd_or_err
end
local fd = fd_or_err
local err_fstat, stat_or_err = uv.fs_fstat(fd)
uv.fs_close(fd)
if err_fstat then
return err_fstat, stat_or_err
end
return true, stat_or_err.size
end
--- Runs a nonblocking search for a README file and returns its size.
---
--- This is a task function.
---
---@async
---@return boolean success
---@return number|string size_or_err
M.search_for_readme = function()
local opendir_err, dir_or_err = uv.fs_opendir(".", 200)
if opendir_err then
return false, dir_or_err
end
local dir = dir_or_err
local readdir_err, entries_or_err = uv.fs_readdir(dir)
uv.fs_closedir(dir)
if readdir_err then
return false, entries_or_err
end
local entries = entries_or_err
for _, entry in ipairs(entries) do
if entry.name == "README.md" then
local file_size_err, file_size_or_err = get_file_size(entry.name)
return file_size_err, file_size_or_err
end
end
return false, "Could not find README.md."
end
--- Sorts a list of numbers by waiting for the time of each number.
---
--- This is a good example of true parallelism enabled by this framework (each timer is working in parallel) and
--- `as_complete` for synchronization.
---
---@async
---@param values number[] the values to sort
---@return number[] sorted_values the sorted values
M.sort_with_time = function(values)
local tasks = {}
-- For each number, create a task that sleeps for that number of milliseconds.
for i, value in ipairs(values) do
tasks[i] = coop.spawn(function()
sleep(value)
return value
end)
end
local sorted_results = {}
for t in as_completed(tasks) do
sorted_results[#sorted_results + 1] = t()
end
return sorted_results
end
--- Runs a simulated parallel search.
---
--- This example shows cancellation and error handling.
---
---@async
---@return string result the result of the fast task
---@return string result the result of the slow task
M.run_parallel_search = function()
local slow_tf = function()
local success, err_msg = copcall(sleep, 5000)
if not success and err_msg == "cancelled" then
return "cancelled"
else
return "slow"
end
end
local fast_tf = function()
local success, err_msg = copcall(sleep, 30)
if not success and err_msg == "cancelled" then
return "cancelled"
else
return "fast"
end
end
local slow_task, fast_task = coop.spawn(slow_tf), coop.spawn(fast_tf)
local done_task, remaining_tasks = await_any({ slow_task, fast_task })
for _, task in ipairs(remaining_tasks) do
task:cancel()
end
return done_task(), remaining_tasks[1]()
end
return M