Regenerate nvim config
This commit is contained in:
@ -0,0 +1,6 @@
|
||||
# Examples
|
||||
|
||||
Make sure that modules can be found under the `package.path` and `package.cpath`.
|
||||
|
||||
If can't find modules, please run `export LUA_PATH="$(dirname $PWD)/lua/?.lua;;"` under CWD in shell
|
||||
or install the modules except for this project.
|
||||
@ -0,0 +1,29 @@
|
||||
local promise = require('promise')
|
||||
local M = {}
|
||||
local fn = vim.fn
|
||||
|
||||
function M.action(action, ...)
|
||||
local args = {...}
|
||||
return promise(function(resolve, reject)
|
||||
table.insert(args, function(err, res)
|
||||
if err ~= vim.NIL then
|
||||
reject(err)
|
||||
else
|
||||
if res == vim.NIL then
|
||||
res = nil
|
||||
end
|
||||
resolve(res)
|
||||
end
|
||||
end)
|
||||
fn.CocActionAsync(action, unpack(args))
|
||||
end)
|
||||
end
|
||||
|
||||
function M.runCommand(name, ...)
|
||||
return M.action('runCommand', name, ...)
|
||||
end
|
||||
|
||||
--
|
||||
-- M.action('showOutline', true)
|
||||
|
||||
return M
|
||||
@ -0,0 +1,58 @@
|
||||
async function defuse(ms) {
|
||||
return new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
resolve(ms)
|
||||
}, ms)
|
||||
})
|
||||
}
|
||||
|
||||
async function bomb(ms) {
|
||||
return new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
reject(ms)
|
||||
}, ms)
|
||||
})
|
||||
}
|
||||
|
||||
async function race() {
|
||||
return Promise.race([
|
||||
defuse(500 + Math.ceil(Math.random() * 500)),
|
||||
bomb(800 + Math.ceil(Math.random() * 200)),
|
||||
])
|
||||
}
|
||||
|
||||
async function play() {
|
||||
console.info('Game start!')
|
||||
let cnt = 0
|
||||
try {
|
||||
while (true) {
|
||||
let ms = await race()
|
||||
cnt = cnt + ms
|
||||
console.info(`Defuse after ${ms}ms~`)
|
||||
}
|
||||
} catch (msErr) {
|
||||
cnt = cnt + msErr
|
||||
console.info(`Bomb after ${msErr}ms~`)
|
||||
}
|
||||
|
||||
console.info(`Game end after ${cnt}ms!`)
|
||||
|
||||
await {
|
||||
then: function(resolve, reject) {
|
||||
setTimeout(() => {
|
||||
reject(this.message)
|
||||
}, 1000)
|
||||
},
|
||||
message: 'try to throw an error :)'
|
||||
}
|
||||
}
|
||||
|
||||
Promise.resolve().then((value) => {
|
||||
console.info('In next tick')
|
||||
})
|
||||
|
||||
console.info('In main')
|
||||
|
||||
play().finally(() => {
|
||||
console.info('Before throwing UnhandledPromiseRejection on finally!')
|
||||
})
|
||||
@ -0,0 +1,87 @@
|
||||
---@diagnostic disable: unused-local
|
||||
local uv = require('luv')
|
||||
local async = require('async')
|
||||
local promise = require('promise')
|
||||
|
||||
math.randomseed(math.ceil(uv.uptime()))
|
||||
|
||||
local function setTimeout(callback, ms)
|
||||
local timer = uv.new_timer()
|
||||
timer:start(ms, 0, function()
|
||||
timer:close()
|
||||
callback()
|
||||
end)
|
||||
return timer
|
||||
end
|
||||
|
||||
local function defuse(ms)
|
||||
return promise:new(function(resolve, reject)
|
||||
setTimeout(function()
|
||||
resolve(ms)
|
||||
end, ms)
|
||||
end)
|
||||
end
|
||||
|
||||
local function bomb(ms)
|
||||
-- getmetatable(promise).__call = promise.new
|
||||
return promise(function(resolve, reject)
|
||||
setTimeout(function()
|
||||
reject(ms)
|
||||
end, ms)
|
||||
end)
|
||||
end
|
||||
|
||||
local function race()
|
||||
return async(function()
|
||||
return promise.race({
|
||||
defuse(math.random(500, 1000)),
|
||||
bomb(math.random(800, 1000))
|
||||
})
|
||||
end)
|
||||
end
|
||||
|
||||
local notify = vim and vim.notify or print
|
||||
|
||||
local function play()
|
||||
return async(function()
|
||||
-- We are not in the next tick until first `await` is called.
|
||||
notify('Game start!')
|
||||
local cnt = 0
|
||||
xpcall(function()
|
||||
while true do
|
||||
local ms = await(race())
|
||||
cnt = cnt + ms
|
||||
notify(('Defuse after %dms~'):format(ms))
|
||||
end
|
||||
end, function(msErr)
|
||||
cnt = cnt + msErr
|
||||
notify(('Bomb after %dms~'):format(msErr))
|
||||
end)
|
||||
|
||||
notify(('Game end after %dms!'):format(cnt))
|
||||
|
||||
await {
|
||||
thenCall = function(self, resolve, reject)
|
||||
setTimeout(function()
|
||||
reject(self.message)
|
||||
end, 1000)
|
||||
end,
|
||||
message = 'try to throw an error :)'
|
||||
}
|
||||
end)
|
||||
end
|
||||
|
||||
promise.resolve():thenCall(function(value)
|
||||
notify('In next tick')
|
||||
end)
|
||||
|
||||
notify('In main')
|
||||
|
||||
play():finally(function()
|
||||
print('Before throwing UnhandledPromiseRejection on finally!')
|
||||
end)
|
||||
|
||||
-- uv.run will be called automatically under Neovim main loop
|
||||
if not vim then
|
||||
uv.run()
|
||||
end
|
||||
@ -0,0 +1,47 @@
|
||||
---@diagnostic disable: redefined-local
|
||||
local uv = require('luv')
|
||||
local promise = require('promise')
|
||||
local mpack = require('mpack')
|
||||
|
||||
local asyncHandle
|
||||
local thread
|
||||
promise(function(resolve, reject)
|
||||
asyncHandle = uv.new_async(function(err, data)
|
||||
asyncHandle:close()
|
||||
if err then
|
||||
reject((mpack.unpack or mpack.decode)(err))
|
||||
else
|
||||
resolve((mpack.unpack or mpack.decode)(data))
|
||||
end
|
||||
end)
|
||||
end):thenCall(function(value)
|
||||
print(('Getting resolved value: %s from %s'):format(value[1], thread))
|
||||
end, function(reason)
|
||||
print(('Getting rejected reason: %s from %s'):format(reason[1], thread))
|
||||
end)
|
||||
|
||||
thread = uv.new_thread(function(delay, asyn)
|
||||
local uv = require('luv')
|
||||
local mpack = require('mpack')
|
||||
local promise = require('promise')
|
||||
math.randomseed(math.ceil(uv.uptime()))
|
||||
promise(function(resolve, reject)
|
||||
print(tostring(uv.thread_self()) .. ' is running.')
|
||||
promise.loop.setTimeout(function()
|
||||
if math.random(1, 2) == 1 then
|
||||
resolve({'succeeded'})
|
||||
else
|
||||
reject({'failed'})
|
||||
end
|
||||
end, delay)
|
||||
end):thenCall(function(value)
|
||||
uv.async_send(asyn, nil, (mpack.pack or mpack.encode)(value))
|
||||
end):catch(function(reason)
|
||||
uv.async_send(asyn, (mpack.pack or mpack.encode)(reason))
|
||||
end)
|
||||
uv.run()
|
||||
end, 1000, asyncHandle)
|
||||
|
||||
if not vim then
|
||||
uv.run()
|
||||
end
|
||||
@ -0,0 +1,23 @@
|
||||
local uv = require('luv')
|
||||
local uva = require('uva')
|
||||
local async = require('async')
|
||||
|
||||
local function readFile(path)
|
||||
return async(function()
|
||||
local fd = await(uva.open(path, 'r', 438))
|
||||
local stat = await(uva.fstat(fd))
|
||||
local data = await(uva.read(fd, stat.size, 0))
|
||||
await(uva.close(fd))
|
||||
return data
|
||||
end)
|
||||
end
|
||||
|
||||
local currentPath = debug.getinfo(1, 'S').source:sub(2)
|
||||
print('Reading ' .. currentPath .. '......\n')
|
||||
readFile(currentPath):thenCall(function(value)
|
||||
print(value)
|
||||
end)
|
||||
|
||||
if not vim then
|
||||
uv.run()
|
||||
end
|
||||
@ -0,0 +1,74 @@
|
||||
---@class UvFS
|
||||
local M = {}
|
||||
|
||||
local uv = require('luv')
|
||||
local promise = require('promise')
|
||||
local compat = require('promise-async.compat')
|
||||
|
||||
local function wrap(name, argc)
|
||||
return function(...)
|
||||
local argv = {...}
|
||||
return promise(function(resolve, reject)
|
||||
argv[argc] = function(err, data)
|
||||
if err then
|
||||
reject(err)
|
||||
else
|
||||
resolve(data)
|
||||
end
|
||||
end
|
||||
uv[name](compat.unpack(argv))
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
M.close = wrap('fs_close', 2)
|
||||
M.open = wrap('fs_open', 4)
|
||||
M.read = wrap('fs_read', 4)
|
||||
M.unlink = wrap('fs_unlink', 2)
|
||||
M.write = wrap('fs_write', 4)
|
||||
M.mkdir = wrap('fs_mkdir', 3)
|
||||
M.mkdtemp = wrap('fs_mkdtemp', 2)
|
||||
M.mkstemp = wrap('fs_mkstemp', 2)
|
||||
M.rmdir = wrap('fs_rmdir', 2)
|
||||
M.scandir = wrap('fs_scandir', 2)
|
||||
M.stat = wrap('fs_stat', 2)
|
||||
M.fstat = wrap('fs_fstat', 2)
|
||||
M.lstat = wrap('fs_lstat', 2)
|
||||
M.rename = wrap('fs_rename', 3)
|
||||
M.fsync = wrap('fs_fsync', 2)
|
||||
M.fdatasync = wrap('fs_fdatasync', 2)
|
||||
M.ftruncate = wrap('fs_ftruncate', 3)
|
||||
M.sendfile = wrap('fs_sendfile', 5)
|
||||
M.access = wrap('fs_access', 3)
|
||||
M.chmod = wrap('fs_chmod', 3)
|
||||
M.fchmod = wrap('fs_fchmod', 3)
|
||||
M.utime = wrap('fs_utime', 4)
|
||||
M.futime = wrap('fs_futime', 4)
|
||||
M.lutime = wrap('fs_lutime', 4)
|
||||
M.link = wrap('fs_link', 3)
|
||||
M.symlink = wrap('fs_symlink', 4)
|
||||
M.readlink = wrap('fs_readlink', 2)
|
||||
M.realpath = wrap('fs_realpath', 2)
|
||||
M.chown = wrap('fs_chown', 4)
|
||||
M.fchown = wrap('fs_fchown', 4)
|
||||
M.lchown = wrap('fs_lchown', 4)
|
||||
M.copyfile = wrap('fs_copyfile', 4)
|
||||
|
||||
-- TODO
|
||||
M.opendir = function(path, entries)
|
||||
return promise(function(resolve, reject)
|
||||
uv.fs_opendir(path, function(err, data)
|
||||
if err then
|
||||
reject(err)
|
||||
else
|
||||
resolve(data)
|
||||
end
|
||||
end, entries)
|
||||
end)
|
||||
end
|
||||
|
||||
M.readdir = wrap('fs_readdir', 2)
|
||||
M.closedir = wrap('fs_closedir', 2)
|
||||
M.statfs = wrap('fs_statfs', 2)
|
||||
|
||||
return M
|
||||
@ -0,0 +1,21 @@
|
||||
local uv = require('luv')
|
||||
local uva = require('uva')
|
||||
local async = require('async')
|
||||
|
||||
local function writeFile(path, data)
|
||||
return async(function()
|
||||
local path_ = path .. '_'
|
||||
local fd = await(uva.open(path_, 'w', 438))
|
||||
await(uva.write(fd, data, -1))
|
||||
await(uva.close(fd))
|
||||
pcall(await, uva.rename(path_, path))
|
||||
end)
|
||||
end
|
||||
|
||||
local path = debug.getinfo(1, 'S').source:sub(2) .. '__'
|
||||
print('Writing ' .. path .. '......\n')
|
||||
writeFile(path, 'write some texts :)\n')
|
||||
|
||||
if not vim then
|
||||
uv.run()
|
||||
end
|
||||
Reference in New Issue
Block a user