Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass custom options #4

Merged
merged 5 commits into from
Feb 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,17 @@ There's a built-in [development mode](https://github.com/eggjs/egg-watcher/blob/

### Customize Watching Mode

Firstly define your custom event source like this:
Say we want to build a custom event source plugin (package name: `egg-watcher-custom`, eggPlugin.name: `watcherCustom`).

Firstly define our custom event source like this:

```js
// custom_event_source.js
// {plugin_root}/lib/custom_event_source.js
const Base = require('sdk-base');
class CustomEventSource extends Base {
// `opts` comes from app.config[${eventSourceName}]
// `eventSourceName` will be registered later in
// `config.watcher.eventSources` as the key shown below
constructor(opts) {
super(opts);
this.ready(true);
Expand Down Expand Up @@ -99,7 +104,7 @@ Then add your custom event source to config:
// config.default.js
exports.watcher = {
eventSources: {
custom: require('../custom'),
custom: require('../lib/custom_event_source'),
},
};
```
Expand All @@ -111,9 +116,14 @@ Choose to use your custom watching mode in your desired env.
exports.watcher = {
type: 'custom',
};

// this will pass to your CustomEventSource constructor as opts
exports.watcherCustom = {
// foo: 'bar',
};
```

If possible, plugins named like `egg-watcher-${customName}`(`egg-watcher-vagrant` eg.) are welcomed.
If possible, plugins named like `egg-watcher-${customName}`(`egg-watcher-vagrant` eg.) are recommended.

## Questions & Suggestions

Expand Down
8 changes: 4 additions & 4 deletions agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const Watcher = require('./lib/watcher');

module.exports = agent => {
const logger = agent.coreLogger;
const config = agent.config.watcher;
config.logger = agent.coreLogger;

const watcher = agent.watcher = agent.cluster(Watcher)
.delegate('watch', 'subscribe')
.create(config)
.on('error', e => logger.error(e));
.create(agent.config)
.on('info', logger.info.bind(logger))
.on('warn', logger.warn.bind(logger))
.on('error', logger.error.bind(logger));

agent.beforeStart(function* () {
yield watcher.ready();
Expand Down
9 changes: 5 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ const Watcher = require('./lib/watcher');

module.exports = app => {
const logger = app.coreLogger;
const config = app.config.watcher;

const watcher = app.watcher = app.cluster(Watcher)
.delegate('watch', 'subscribe')
.create(config)
.on('error', e => logger.error(e));
.delegate('watch', 'subscribe')
.create(app.config)
.on('info', logger.info.bind(logger))
.on('warn', logger.warn.bind(logger))
.on('error', logger.error.bind(logger));

app.beforeStart(function* () {
yield watcher.ready();
Expand Down
10 changes: 5 additions & 5 deletions lib/event-sources/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ const Base = require('sdk-base');

class DefaultEventSource extends Base {

constructor(options) {
constructor() {
super();
this.logger = options.logger;
this.logger.warn('[egg-watcher] defaultEventSource watcher will NOT take effect');
// delay emit so that can be listened
setImmediate(() => this.emit('warn', '[egg-watcher] defaultEventSource watcher will NOT take effect'));
this.ready(true);
}

watch() {
this.logger.warn('[egg-watcher] using defaultEventSource watcher.watch does NOTHING');
this.emit('warn', '[egg-watcher] using defaultEventSource watcher.watch() does NOTHING');
}

unwatch() {
this.logger.warn('[egg-watcher] using defaultEventSource watcher.unwatch does NOTHING');
this.emit('warn', '[egg-watcher] using defaultEventSource watcher.unwatch() does NOTHING');
}

}
Expand Down
14 changes: 11 additions & 3 deletions lib/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@

const Base = require('sdk-base');
const utils = require('./utils');
const camelcase = require('camelcase');

module.exports = class Watcher extends Base {
constructor(options) {
constructor(config) {
super();

const options = config.watcher;

let EventSource = options.eventSources[options.type];
if (typeof EventSource === 'string') {
EventSource = require(EventSource);
}

this._eventSource = new EventSource(options)
// chokidar => watcherChokidar
const key = camelcase('watcher', options.type);
const eventSourceOpts = config[key];
this._eventSource = new EventSource(eventSourceOpts)
.on('change', this._onChange.bind(this))
.on('fuzzy-change', this._onFuzzyChange.bind(this))
.on('error', this.emit.bind(this, 'error'));
.on('info', msg => this.emit('info', msg))
.on('warn', msg => this.emit('warn', msg))
.on('error', err => this.emit('error', err));

this._eventSource.ready(() => this.ready(true));
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"autod": "autod"
},
"dependencies": {
"camelcase": "^4.0.0",
"detect-port": "^1.1.0",
"sdk-base": "^3.0.1",
"wt": "^1.1.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
exports.watcher = {
type: 'custom',
};

exports.watcherCustom = {
foo: 'bar',
};

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const path = require('path');

exports['custom-event-source'] = {
exports.watcherCustom = {
enable: true,
path: path.join(__dirname, '../plugins/custom-event-source'),
path: path.join(__dirname, '../plugins/egg-watcher-custom'),
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ const Base = require('sdk-base');

class CustomEventSource extends Base {

constructor(/* options */) {
constructor(options) {
super();
this._options = options;
this.ready(true);
}

watch(path) {
this.emit('info', 'info12345');
this.emit('warn', 'warn12345');
this._h = setInterval(() => {
this.emit('change', {
path,
foo: this._options.foo,
});
}, 1000);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "egg-watcher-custom",
"eggPlugin": {
"name": "watcherCustom",
"dep": [
"watcher"
]
}
}
5 changes: 5 additions & 0 deletions test/fixtures/apps/watcher-type-default/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = app => {
app.watcher.watch('xx', () => {});
};
17 changes: 11 additions & 6 deletions test/watcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ describe('test/watcher.test.js', () => {
app && app.close();
});

it('should warn user if config.watcher.type is default', done => {
it('should warn user if config.watcher.type is default', function* () {
app = mm.cluster({
plugin: 'watcher',
baseDir: 'apps/watcher-type-default',
});
app.ready(() => {
const content = fs.readFileSync(__dirname + '/fixtures/apps/watcher-type-default/logs/watcher-type-default/egg-agent.log').toString();
assert(content.includes('defaultEventSource watcher will NOT take effect'));
done();
});
yield app.ready();
const content = fs.readFileSync(__dirname + '/fixtures/apps/watcher-type-default/logs/watcher-type-default/egg-agent.log', 'utf8');
assert(content.includes('defaultEventSource watcher will NOT take effect'));
});

it('should work if config.watcher.type is custom', done => {
Expand All @@ -31,6 +29,13 @@ describe('test/watcher.test.js', () => {
app.ready(() => {
app.watcher.watch('xxxx', info => {
assert(info.path === 'xxxx');

// ensure use config.custom
assert(info.foo === 'bar');

const content = fs.readFileSync(__dirname + '/fixtures/apps/watcher-custom-event-source/logs/watcher-custom-event-source/egg-agent.log', 'utf8');
assert(content.includes('warn12345'));
assert(content.includes('info12345'));
/*
// TODO wait unsubscribe implementaion in cluster-client
co(function* () {
Expand Down