Comparing version 2.3.0 to 2.3.1
2.3.1 / 2017-01-26 | ||
================== | ||
* fix: improve proxy handler and event listener (#24) | ||
2.3.0 / 2017-01-25 | ||
@@ -3,0 +8,0 @@ ================== |
114
lib/app.js
@@ -9,4 +9,4 @@ 'use strict'; | ||
const ready = require('get-ready'); | ||
const EventEmitter = require('events'); | ||
const detectPort = require('detect-port'); | ||
const debug = require('debug')('egg-mock'); | ||
const formatOptions = require('./format_options'); | ||
@@ -16,2 +16,6 @@ | ||
const INIT = Symbol('init'); | ||
const APP_INIT = Symbol('appInit'); | ||
const BIND_EVENT = Symbol('bindEvent'); | ||
const INIT_ON_LISTENER = Symbol('initOnListener'); | ||
const INIT_ONCE_LISTENER = Symbol('initOnceListener'); | ||
const MOCK_APP_METHOD = [ | ||
@@ -24,2 +28,3 @@ 'ready', | ||
'agent', | ||
'_app', | ||
'on', | ||
@@ -29,16 +34,16 @@ 'once', | ||
class MockApplication extends EventEmitter { | ||
class MockApplication { | ||
constructor(options) { | ||
super(); | ||
this.options = options; | ||
this.isReady = false; | ||
this.closed = false; | ||
this[APP_INIT] = false; | ||
this[INIT_ON_LISTENER] = new Set(); | ||
this[INIT_ONCE_LISTENER] = new Set(); | ||
ready.mixin(this); | ||
co(this[INIT].bind(this)) | ||
.then(() => { | ||
this.isReady = true; | ||
this.ready(true); | ||
}) | ||
.catch(err => this.emit('error', err)); | ||
.then(() => this.ready(true)) | ||
// TODO: implement ready(err) | ||
// condition for error event | ||
.catch(err => console.log(err)); | ||
} | ||
@@ -48,2 +53,3 @@ | ||
this.options.clusterPort = yield detectPort(); | ||
debug('get clusterPort %s', this.options.clusterPort); | ||
const egg = require(this.options.customEgg); | ||
@@ -53,9 +59,47 @@ | ||
const agent = this.agent = new Agent(this.options); | ||
debug('agent instantiate'); | ||
yield agent.ready(); | ||
debug('agent ready'); | ||
const Application = egg.Application; | ||
const app = this.app = new Application(this.options); | ||
const app = this._app = new Application(this.options); | ||
debug('app instantiate'); | ||
this[APP_INIT] = true; | ||
debug('this[APP_INIT] = true'); | ||
this[BIND_EVENT](); | ||
yield app.ready(); | ||
debug('app ready'); | ||
} | ||
[BIND_EVENT]() { | ||
for (const args of this[INIT_ON_LISTENER]) { | ||
debug('on(%s), use cache and pass to app', args); | ||
this._app.on(...args); | ||
} | ||
for (const args of this[INIT_ONCE_LISTENER]) { | ||
debug('once(%s), use cache and pass to app', args); | ||
this._app.on(...args); | ||
} | ||
} | ||
on(...args) { | ||
if (this[APP_INIT]) { | ||
debug('on(%s), pass to app', args); | ||
this._app.on(...args); | ||
} else { | ||
debug('on(%s), cache it because app has not init', args); | ||
this[INIT_ON_LISTENER].add(args); | ||
} | ||
} | ||
once(...args) { | ||
if (this[APP_INIT]) { | ||
debug('once(%s), pass to app', args); | ||
this._app.once(...args); | ||
} else { | ||
debug('once(%s), cache it because app has not init', args); | ||
this[INIT_ONCE_LISTENER].add(args); | ||
} | ||
} | ||
close() { | ||
@@ -65,5 +109,6 @@ this.closed = true; | ||
this.agent.close(), | ||
this.app.close(), | ||
this._app.close(), | ||
]).then(() => sleep(1000)); | ||
} | ||
} | ||
@@ -91,20 +136,43 @@ | ||
// don't delegate properties on MockApplication | ||
if (MOCK_APP_METHOD.includes(prop)) { | ||
return getProperty(target, prop); | ||
} | ||
if (MOCK_APP_METHOD.includes(prop)) return getProperty(target, prop); | ||
if (!target[APP_INIT]) throw new Error(`can't get ${prop} before ready`); | ||
// it's asyncrounus when agent and app are loading, | ||
// so should get the properties after loader ready | ||
if (target.isReady) { | ||
return getProperty(target.app, prop); | ||
} | ||
throw new Error(`can't get ${prop} before ready`); | ||
debug('proxy handler.get %s', prop); | ||
return target._app[prop]; | ||
}, | ||
set(target, prop, value) { | ||
if (MOCK_APP_METHOD.includes(prop)) return true; | ||
if (target.isReady) { | ||
target.app[prop] = value; | ||
return true; | ||
} | ||
throw new Error(`can't set ${prop} before ready`); | ||
if (!target[APP_INIT]) throw new Error(`can't set ${prop} before ready`); | ||
debug('proxy handler.set %s', prop); | ||
target._app[prop] = value; | ||
return true; | ||
}, | ||
defineProperty(target, prop, descriptor) { | ||
// can't define properties on MockApplication | ||
if (MOCK_APP_METHOD.includes(prop)) return true; | ||
if (!target[APP_INIT]) throw new Error(`can't defineProperty ${prop} before ready`); | ||
debug('proxy handler.defineProperty %s', prop); | ||
Object.defineProperty(target._app, prop, descriptor); | ||
return true; | ||
}, | ||
deleteProperty(target, prop) { | ||
// can't delete properties on MockApplication | ||
if (MOCK_APP_METHOD.includes(prop)) return true; | ||
if (!target[APP_INIT]) throw new Error(`can't delete ${prop} before ready`); | ||
debug('proxy handler.deleteProperty %s', prop); | ||
delete target._app[prop]; | ||
return true; | ||
}, | ||
getOwnPropertyDescriptor(target, prop) { | ||
if (MOCK_APP_METHOD.includes(prop)) return Object.getOwnPropertyDescriptor(target, prop); | ||
if (!target[APP_INIT]) throw new Error(`can't getOwnPropertyDescriptor ${prop} before ready`); | ||
debug('proxy handler.getOwnPropertyDescriptor %s', prop); | ||
return Object.getOwnPropertyDescriptor(target._app, prop); | ||
}, | ||
getPrototypeOf(target) { | ||
if (!target[APP_INIT]) throw new Error('can\'t getPrototypeOf before ready'); | ||
debug('proxy handler.getPrototypeOf %s'); | ||
return Object.getPrototypeOf(target._app); | ||
}, | ||
}); | ||
@@ -111,0 +179,0 @@ |
{ | ||
"name": "egg-mock", | ||
"version": "2.3.0", | ||
"version": "2.3.1", | ||
"eggPlugin": { | ||
@@ -5,0 +5,0 @@ "name": "egg-mock" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34169
833