Skip to content

Commit

Permalink
Breaking: Rename events to be more specific
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Apr 28, 2019
1 parent 2ce73f6 commit 638c18b
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 71 deletions.
46 changes: 39 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,24 +446,24 @@ A function called after your application is executed. When invoked, `this` will

### events

#### beforeRequire(name)
#### `on('preload:before', function(name) {})`

Emitted before a module is pre-load. (But for only a module which is specified by `opts.require`.)

```js
var Hacker = new Liftoff({name:'hacker', require:'coffee-script'});
Hacker.on('beforeRequire', function (name) {
Hacker.on('preload:before', function (name) {
console.log('Requiring external module: '+name+'...');
});
```

#### require(name, module)
#### `on('preload:success', function(name, module) {})`

Emitted when a module has been pre-loaded.

```js
var Hacker = new Liftoff({name:'hacker'});
Hacker.on('require', function (name, module) {
Hacker.on('preload:success', function (name, module) {
console.log('Required external module: '+name+'...');
// automatically register coffee-script extensions
if (name === 'coffee-script') {
Expand All @@ -472,18 +472,50 @@ Hacker.on('require', function (name, module) {
});
```

#### requireFail(name, err)
#### `on('preload:failure', function(name, err) {})`

Emitted when a requested module cannot be preloaded.

```js
var Hacker = new Liftoff({name:'hacker'});
Hacker.on('requireFail', function (name, err) {
Hacker.on('preload:failure', function (name, err) {
console.log('Unable to load:', name, err);
});
```

#### respawn(flags, child)
#### `on('loader:success, function(name, module) {})`

Emitted when a loader that matches an extension has been loaded.

```js
var Hacker = new Liftoff({
name: 'hacker',
extensions: {
'.ts': 'ts-node/register'
}
});
Hacker.on('loader:success', function (name, module) {
console.log('Required external module: '+name+'...');
});
```

#### `on('loader:failure', function(name, err) {})`

Emitted when no loader for an extension can be loaded. Emits an error for each failed loader.

```js
var Hacker = new Liftoff({
name: 'hacker',
extensions: {
'.ts': 'ts-node/register'
}
});
Hacker.on('loader:failure', function (name, err) {
console.log('Unable to load:', name, err);
});
```

#### `on('respawn', function(flags, child) {})`

Emitted when Liftoff re-spawns your process (when a [`v8flags`](#optsv8flags) is detected).

Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ util.inherits(Liftoff, EE);

Liftoff.prototype.requireLocal = function(module, basedir) {
try {
this.emit('beforeRequire', module);
this.emit('preload:before', module);
var result = require(resolve.sync(module, { basedir: basedir }));
this.emit('require', module, result);
this.emit('preload:success', module, result);
return result;
} catch (e) {
this.emit('requireFail', module, e);
this.emit('preload:failure', module, e);
}
};

Expand Down
4 changes: 2 additions & 2 deletions lib/register_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function(eventEmitter, extensions, configPath, cwd) {
var autoloads = rechoir.prepare(extensions, configPath, cwd, true);
if (autoloads instanceof Error) { // Only errors
autoloads.failures.forEach(function(failed) {
eventEmitter.emit('requireFail', failed.moduleName, failed.error);
eventEmitter.emit('loader:failure', failed.moduleName, failed.error);
});
return;
}
Expand All @@ -20,5 +20,5 @@ module.exports = function(eventEmitter, extensions, configPath, cwd) {
}

var succeeded = autoloads[autoloads.length - 1];
eventEmitter.emit('require', succeeded.moduleName, succeeded.module);
eventEmitter.emit('loader:success', succeeded.moduleName, succeeded.module);
};
4 changes: 2 additions & 2 deletions test/fixtures/respawn_and_require.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Test.on('respawn', function(flags, proc) {
console.log('saw respawn', flags);
});

Test.on('require', function(name) {
console.log('require', name);
Test.on('preload:success', function(name) {
console.log('preload:success', name);
});

Test.prepare({
Expand Down
45 changes: 21 additions & 24 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,18 +274,18 @@ describe('Liftoff', function() {
it('should attempt pre-loading local modules if they are requested', function(done) {
var app = new Liftoff({ name: 'test' });
var logs = [];
app.on('require', function(moduleName, module) {
app.on('preload:success', function(moduleName, module) {
expect(moduleName).to.equal('coffeescript/register');
expect(module).to.equal(require('coffeescript/register'));
logs.push('require');
logs.push('preload:success');
});
app.on('requireFail', function(moduleName, err) {
app.on('preload:failure', function(moduleName, err) {
done(err);
});
app.prepare({ require: ['coffeescript/register'] }, function(env) {
app.execute(env, function(env) {
expect(env.require).to.deep.equal(['coffeescript/register']);
expect(logs).to.deep.equal(['require']);
expect(logs).to.deep.equal(['preload:success']);
done();
});
});
Expand All @@ -294,18 +294,18 @@ describe('Liftoff', function() {
it('should attempt pre-loading a local module if it is requested', function(done) {
var app = new Liftoff({ name: 'test' });
var logs = [];
app.on('require', function(moduleName, module) {
app.on('preload:success', function(moduleName, module) {
expect(moduleName).to.equal('coffeescript/register');
expect(module).to.equal(require('coffeescript/register'));
logs.push('require');
logs.push('preload:success');
});
app.on('requireFail', function(moduleName, err) {
app.on('preload:failure', function(moduleName, err) {
done(err);
});
app.prepare({ require: 'coffeescript/register' }, function(env) {
app.execute(env, function(env) {
expect(env.require).to.deep.equal(['coffeescript/register']);
expect(logs).to.deep.equal(['require']);
expect(logs).to.deep.equal(['preload:success']);
done();
});
});
Expand All @@ -314,18 +314,15 @@ describe('Liftoff', function() {
it('should attempt pre-loading local modules but fail', function(done) {
var app = new Liftoff({ name: 'test' });
var logs = [];
app.on('require', function(/* moduleName, module */) {
done();
});
app.on('requireFail', function(moduleName, err) {
app.on('preload:failure', function(moduleName, err) {
expect(moduleName).to.equal('badmodule');
expect(err).to.not.equal(null);
logs.push('requireFail');
logs.push('preload:failure');
});
app.prepare({ require: 'badmodule' }, function(env) {
app.execute(env, function(env) {
expect(env.require).to.deep.equal(['badmodule']);
expect(logs).to.deep.equal(['requireFail']);
expect(logs).to.deep.equal(['preload:failure']);
done();
});
});
Expand All @@ -340,21 +337,21 @@ describe('Liftoff', function() {
expect(stderr).to.equal('');
expect(stdout).to.equal(
'saw respawn [ \'--lazy\' ]\n' +
'require coffeescript/register\n' +
'preload:success coffeescript/register\n' +
'execute\n' +
'');
done();
}
});

it('should emit `beforeRequire` and `require` with the name of the module and the required module', function(done) {
it('should emit `preload:before` and `preload:success` with the name of the module and the required module', function(done) {
var requireTest = new Liftoff({ name: 'require' });
var isEmittedBeforeRequired = false;
requireTest.on('beforeRequire', function(name) {
requireTest.on('preload:before', function(name) {
expect(name).to.equal('mocha');
isEmittedBeforeRequired = true;
});
requireTest.on('require', function(name, module) {
requireTest.on('preload:success', function(name, module) {
expect(name).to.equal('mocha');
expect(module).to.equal(require('mocha'));
expect(isEmittedBeforeRequired).to.equal(true);
Expand All @@ -363,14 +360,14 @@ describe('Liftoff', function() {
requireTest.requireLocal('mocha', __dirname);
});

it('should emit `beforeRequire` and `requireFail` with an error if a module can\'t be found.', function(done) {
var requireFailTest = new Liftoff({ name: 'requireFail' });
it('should emit `preload:before` and `preload:failure` with an error if a module can\'t be found.', function(done) {
var requireFailTest = new Liftoff({ name: 'preload:failure' });
var isEmittedBeforeRequired = false;
requireFailTest.on('beforeRequire', function(name) {
requireFailTest.on('preload:before', function(name) {
expect(name).to.equal('badmodule');
isEmittedBeforeRequired = true;
});
requireFailTest.on('requireFail', function(name) {
requireFailTest.on('preload:failure', function(name) {
expect(name).to.equal('badmodule');
expect(isEmittedBeforeRequired).to.equal(true);
done();
Expand Down Expand Up @@ -550,10 +547,10 @@ describe('Liftoff', function() {
},
},
});
app.on('requireFail', function(moduleName, error) {
app.on('loader:failure', function(moduleName, error) {
logFailure.push({ moduleName: moduleName, error: error });
});
app.on('require', function(moduleName, module) {
app.on('loader:success', function(moduleName, module) {
logRequire.push({ moduleName: moduleName, module: module });
});
app.prepare({}, function(env) {
Expand Down
Loading

0 comments on commit 638c18b

Please sign in to comment.