From 557e40e70386b5d46d0fcc5cb1d7e6e206c39262 Mon Sep 17 00:00:00 2001 From: leeluolee <87399126@163.com> Date: Fri, 30 Aug 2019 16:45:40 +0800 Subject: [PATCH] feat: add plublic events --- packages/svrx/__tests__/spec/svrx.base.js | 52 ++++++++++++++++++++--- packages/svrx/index.js | 2 +- packages/svrx/lib/svrx.js | 15 ++++++- 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/packages/svrx/__tests__/spec/svrx.base.js b/packages/svrx/__tests__/spec/svrx.base.js index f137779c..e85e0a58 100644 --- a/packages/svrx/__tests__/spec/svrx.base.js +++ b/packages/svrx/__tests__/spec/svrx.base.js @@ -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'); @@ -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'); @@ -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' }); + }); + }); }); }); diff --git a/packages/svrx/index.js b/packages/svrx/index.js index 181baba4..7b156837 100644 --- a/packages/svrx/index.js +++ b/packages/svrx/index.js @@ -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() { diff --git a/packages/svrx/lib/svrx.js b/packages/svrx/lib/svrx.js index 175f4bb8..fce8c38c 100644 --- a/packages/svrx/lib/svrx.js +++ b/packages/svrx/lib/svrx.js @@ -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, @@ -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, @@ -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); } }