Skip to content

Commit

Permalink
fix: log info before starting repl
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Steblyuk committed Jul 22, 2021
1 parent e2d84b0 commit bebec16
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './debug-in-contexts';
export * from './create-debugger-api';
export * from './extend-context';
export * from './types';
25 changes: 13 additions & 12 deletions src/debug-in-contexts.ts → src/create-debugger-api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type {Bindings, Logger} from './types';
import {extendContext} from './extend-context';

export function debugInContexts(
contexts: object[],
export function createDebuggerAPI(
bindings: Bindings,
apiNamespace: string,
logger: Logger
Expand Down Expand Up @@ -33,19 +32,21 @@ export function debugInContexts(
extensionMap.set(key, getValue);
}
extensionMap.set(apiNamespace, () => api);

const contextCleanups = contexts.map((context) => (
extendContext(context as Record<string, unknown>, extensionMap, logger)
));
logger.info(
`Async function is paused for debugging. The following variables ` +
`are available: [${[...extensionMap.keys()].sort().join(', ')}].`
);
const teardown = () => {
logger.info('Async function execution is resumed.');
for (const cleanup of contextCleanups) {
cleanup();
}

const applyToContexts = (contexts: object[]) => {
const contextCleanups = contexts.map((context) => (
extendContext(context as Record<string, unknown>, extensionMap, logger)
));
return () => {
logger.info('Async function execution is resumed.');
for (const cleanup of contextCleanups) {
cleanup();
}
};
};
return {teardown, resultPromise, api};
return {applyToContexts, resultPromise, api};
}
9 changes: 5 additions & 4 deletions src/debug-async-browser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {debugInContexts} from './debug-in-contexts';
import {createDebuggerAPI} from './create-debugger-api';
import type {Bindings, DebugAsyncCommonOptions, Logger} from './types';

export const debugAsync = createDebugAsyncBrowser();
Expand Down Expand Up @@ -31,10 +31,11 @@ export function createDebugAsyncBrowser(
logger: {info = console.info, warn = console.warn} = defaultLogger
} = options;
const logger: Logger = {info, warn};

const {resultPromise, teardown} = (
debugInContexts(contexts, bindings, apiNamespace, logger)
const {resultPromise, applyToContexts} = (
createDebuggerAPI(bindings, apiNamespace, logger)
);
const teardown = applyToContexts(contexts);

try {
isBeingDebugged = true;
return await resultPromise;
Expand Down
10 changes: 5 additions & 5 deletions src/debug-async-node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {start as startRepl, ReplOptions} from 'repl';
import {debugInContexts} from './debug-in-contexts';
import {createDebuggerAPI} from './create-debugger-api';
import type {Bindings, DebugAsyncCommonOptions, Logger} from './types';

export const debugAsync = createDebugAsyncNode();
Expand Down Expand Up @@ -36,12 +36,12 @@ export function createDebugAsyncNode(
replOptions = replOptionsDefault
} = options;
const logger: Logger = {info, warn};

const repl = startRepl(replOptions);
const {resultPromise, teardown, api} = (
debugInContexts([...contexts, repl.context], bindings, apiNamespace, logger)
const {resultPromise, applyToContexts, api} = (
createDebuggerAPI(bindings, apiNamespace, logger)
);
const repl = startRepl(replOptions);
repl.once('exit', api.resumeExecution);
const teardown = applyToContexts([...contexts, repl.context]);

try {
isBeingDebugged = true;
Expand Down

0 comments on commit bebec16

Please sign in to comment.