fs/co: add fs.exists and wait for promise on co.clearInterval.
This commit is contained in:
parent
aeb3d8f352
commit
1c1e429383
@ -208,7 +208,9 @@ async function every(jobs) {
|
|||||||
function startInterval(func, time, self) {
|
function startInterval(func, time, self) {
|
||||||
const ctx = {
|
const ctx = {
|
||||||
timer: null,
|
timer: null,
|
||||||
stopped: false
|
stopped: false,
|
||||||
|
running: false,
|
||||||
|
resolve: null
|
||||||
};
|
};
|
||||||
|
|
||||||
const cb = async () => {
|
const cb = async () => {
|
||||||
@ -216,10 +218,14 @@ function startInterval(func, time, self) {
|
|||||||
ctx.timer = null;
|
ctx.timer = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
ctx.running = true;
|
||||||
await func.call(self);
|
await func.call(self);
|
||||||
} finally {
|
} finally {
|
||||||
|
ctx.running = false;
|
||||||
if (!ctx.stopped)
|
if (!ctx.stopped)
|
||||||
ctx.timer = setTimeout(cb, time);
|
ctx.timer = setTimeout(cb, time);
|
||||||
|
else if (ctx.resolve)
|
||||||
|
ctx.resolve();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -235,11 +241,21 @@ function startInterval(func, time, self) {
|
|||||||
|
|
||||||
function stopInterval(ctx) {
|
function stopInterval(ctx) {
|
||||||
assert(ctx);
|
assert(ctx);
|
||||||
|
|
||||||
if (ctx.timer != null) {
|
if (ctx.timer != null) {
|
||||||
clearTimeout(ctx.timer);
|
clearTimeout(ctx.timer);
|
||||||
ctx.timer = null;
|
ctx.timer = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.stopped = true;
|
ctx.stopped = true;
|
||||||
|
|
||||||
|
if (ctx.running) {
|
||||||
|
return new Promise((r) => {
|
||||||
|
ctx.resolve = r;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -22,8 +22,26 @@ exports.closeSync = fs.closeSync;
|
|||||||
exports.constants = fs.constants;
|
exports.constants = fs.constants;
|
||||||
exports.createReadStream = fs.createReadStream;
|
exports.createReadStream = fs.createReadStream;
|
||||||
exports.createWriteStream = fs.createWriteStream;
|
exports.createWriteStream = fs.createWriteStream;
|
||||||
exports.exists = co.promisify(fs.exists);
|
exports.exists = async (file) => {
|
||||||
exports.existsSync = fs.existsSync;
|
try {
|
||||||
|
await exports.stat(file);
|
||||||
|
return true;
|
||||||
|
} catch (e) {
|
||||||
|
if (e.code === 'ENOENT')
|
||||||
|
return false;
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
exports.existsSync = (file) => {
|
||||||
|
try {
|
||||||
|
exports.statSync(file);
|
||||||
|
return true;
|
||||||
|
} catch (e) {
|
||||||
|
if (e.code === 'ENOENT')
|
||||||
|
return false;
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
};
|
||||||
exports.fchmod = co.promisify(fs.fchmod);
|
exports.fchmod = co.promisify(fs.fchmod);
|
||||||
exports.fchmodSync = fs.fchmodSync;
|
exports.fchmodSync = fs.fchmodSync;
|
||||||
exports.fchown = co.promisify(fs.fchown);
|
exports.fchown = co.promisify(fs.fchown);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user