'reset' 事件


当重置 REPL 的上下文时会触发 'reset' 事件。 每当接收到 .clear 命令作为输入时,就会发生这种情况,除非 REPL 使用默认评估器并且 repl.REPLServer 实例是在 useGlobal 选项设置为 true 的情况下创建的。 将使用对 context 对象的引用作为唯一参数调用监听器回调。

这可以主要用于将 REPL 上下文重新初始化为一些预定义的状态:

const repl = require('repl');

function initializeContext(context) {
  context.m = 'test';
}

const r = repl.start({ prompt: '> ' });
initializeContext(r.context);

r.on('reset', initializeContext);

当执行此代码时,可以修改全局 'm' 变量,然后使用 .clear 命令将其重置为其初始值:

$ ./node example.js
> m
'test'
> m = 1
1
> m
1
> .clear
Clearing context...
> m
'test'
>

The 'reset' event is emitted when the REPL's context is reset. This occurs whenever the .clear command is received as input unless the REPL is using the default evaluator and the repl.REPLServer instance was created with the useGlobal option set to true. The listener callback will be called with a reference to the context object as the only argument.

This can be used primarily to re-initialize REPL context to some pre-defined state:

const repl = require('repl');

function initializeContext(context) {
  context.m = 'test';
}

const r = repl.start({ prompt: '> ' });
initializeContext(r.context);

r.on('reset', initializeContext);

When this code is executed, the global 'm' variable can be modified but then reset to its initial value using the .clear command:

$ ./node example.js
> m
'test'
> m = 1
1
> m
1
> .clear
Clearing context...
> m
'test'
>