Skip to content

Commit

Permalink
Merge pull request #2 from quavedev/feature/fibers-flags-log-using
Browse files Browse the repository at this point in the history
Create flags to enable logs when call methods with fibers is in use.
  • Loading branch information
denihs authored Jul 28, 2022
2 parents 6103848 + 9fd9078 commit fac1110
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
37 changes: 31 additions & 6 deletions fibers.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,46 @@ function setupAsyncHacks(Fiber) {
}
}

function wrapFunction(fn) {
return function() {
function logUsingFibers(fibersMethod) {
const logUseFibersLevel = +(process.env.ENABLE_LOG_USE_FIBERS || 0);

if (!logUseFibersLevel) return;

if (logUseFibersLevel === 1) {
console.warn(`[FIBERS_LOG] Using ${fibersMethod}.`);
return;
}

const { LOG_USE_FIBERS_INCLUDE_IN_PATH } = process.env;
const stackFromError = new Error(`[FIBERS_LOG] Using ${fibersMethod}.`).stack;

if (
!LOG_USE_FIBERS_INCLUDE_IN_PATH ||
stackFromError.includes(LOG_USE_FIBERS_INCLUDE_IN_PATH)
) {
console.warn(stackFromError);
}
}

function wrapFunction(fn, fibersMethod) {
return function () {
logUsingFibers(fibersMethod);
var stack = getAndClearStack();
try {
return fn.apply(this, arguments);
} finally {
restoreStack(stack);
}
}
};
}

// Monkey patch methods which may long jump
Fiber.yield = wrapFunction(Fiber.yield);
Fiber.prototype.run = wrapFunction(Fiber.prototype.run);
Fiber.prototype.throwInto = wrapFunction(Fiber.prototype.throwInto);
Fiber.yield = wrapFunction(Fiber.yield, "Fiber.yield");
Fiber.prototype.run = wrapFunction(Fiber.prototype.run, "Fiber.run");
Fiber.prototype.throwInto = wrapFunction(
Fiber.prototype.throwInto,
"Fiber.throwInto"
);

} catch (err) {
return;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fibers",
"version": "5.0.0",
"version": "5.0.2",
"description": "Cooperative multi-tasking for Javascript",
"keywords": [
"fiber",
Expand Down

0 comments on commit fac1110

Please sign in to comment.