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: add plublic events #90

Merged
merged 1 commit into from
Sep 18, 2019
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
52 changes: 47 additions & 5 deletions packages/svrx/__tests__/spec/svrx.base.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const ioClient = require('socket.io-client');
const ffp = require('find-free-port');
const request = require('supertest');
const expect = require('expect.js');
Expand All @@ -6,6 +7,7 @@ const Koa = require('koa');
const svrx = require('../../index');
const Svrx = require('../../lib/svrx');
const CONFIGS = require('../../lib/config-list');
const { IO_PATH } = require('../../lib/shared/consts');

const Middleware = require('../../lib/middleware');

Expand Down Expand Up @@ -267,18 +269,58 @@ describe('Public API', () => {
expect(spy.calledOnce).to.equal(true);
});

it('svrx.reload()', async () => {
const spy = sinon.spy();
it('events plugin', (done) => {
let called = false;
const server = svrx({
open: false,
livereload: false,
});

server.on('plugin', async ({
io, router, config, middleware, injector, logger, events,
}) => {
expect(io).to.not.equal(undefined);
expect(config).to.not.equal(undefined);
expect(router).to.not.equal(undefined);
expect(middleware).to.not.equal(undefined);
expect(injector).to.not.equal(undefined);
expect(logger).to.not.equal(undefined);
expect(events).to.not.equal(undefined);
called = true;
});
server.start().then(() => {
expect(called).to.equal(true);
server.close().then(done);
});
});

it('svrx.reload()', (done) => {
const server = svrx({
open: false,
livereload: false,
});

server.on('file:change', spy);
server.start().then((port) => {
const socket = ioClient.connect(`http://localhost:${port}`, {
path: IO_PATH,
});
const handler = () => {
server.reload();
};

await server.reload();
const { io } = server.__svrx;

expect(spy.calledOnce).to.equal(true);
io.on('svrx', (payload) => {
expect(payload).to.equal('hello');
socket.close();
io.off('svrx');
server.close().then(done);
});
io._io.on('connection', handler);
socket.on('$message', (evt) => {
expect(evt.type).to.equal('file:change');
socket.emit('$message', { type: 'svrx', payload: 'hello' });
});
});
});
});
2 changes: 1 addition & 1 deletion packages/svrx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Svrx = require('./lib/svrx');

function mapSvrxToExports(ctx) {
function reload() {
return ctx.events.emit('file:change', {}, true);
return ctx.io.emit('file:change', {}, true);
}

function start() {
Expand Down
15 changes: 13 additions & 2 deletions packages/svrx/lib/svrx.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ class Svrx {
this.loader.on('error', (payload) => logger.error(payload));
this.loader.on('update', (payload) => logger.notify(`[routing update] ${payload}`));

this.router = exportsToPlugin(this.loader);

this.system = new PluginSystem({
router: exportsToPlugin(this.loader),
router: this.router,
middleware,
injector,
config,
Expand Down Expand Up @@ -141,6 +143,15 @@ class Svrx {
return this.system
.load(plugins)
.then(() => this.system.build())
.then(() => this.events.emit('plugin', {
middleware: this.middleware,
injector: this.injector,
logger,
config: this.config,
events: this.events,
router: this.router,
io: this.io,
}))
.then(() => {
middleware.add('$router', {
priority: PRIORITY.ROUTER,
Expand Down Expand Up @@ -203,7 +214,7 @@ ${'Local'.padStart(12)}: ${chalk.underline.blue(config.get('urls.local'))}

${'Plugins'.padStart(12)}: ${chalk.gray(this.system.getInstalledPluginNames().join(','))}
`);
events.emit('ready');
events.emit('ready', port);
}
}

Expand Down