YUKI Hiroshi
null+****@clear*****
Mon Dec 16 15:59:08 JST 2013
YUKI Hiroshi 2013-12-16 15:59:08 +0900 (Mon, 16 Dec 2013) New Revision: bb6957ecb5955181600c64ad9fbd8596bc789d38 https://github.com/droonga/express-droonga/commit/bb6957ecb5955181600c64ad9fbd8596bc789d38 Message: Define commands based on our custom mix-in system Modified files: lib/adapter/command.js Modified: lib/adapter/command.js (+90 -73) =================================================================== --- lib/adapter/command.js 2013-12-16 14:52:38 +0900 (210c234) +++ lib/adapter/command.js 2013-12-16 15:59:08 +0900 (691d674) @@ -3,125 +3,142 @@ var crypto = require('crypto'); function Command(options) { this._options = options || {}; - this._commandTypes = []; + this._commandTypes = this.constructor.commandTypes; } -Command.prototype = { - isInstanceOf: function(commandType) { +Command.commandTypes = []; +Command.extend = function(targetClass) { + targetClass.commandTypes = targetClass.commandTypes || [Command]; + targetClass.prototype.isInstanceOf = function(commandType) { return ( commandType && this._commandTypes.indexOf(commandType) > -1 ); - }, - get dataset() { - return this._options.dataset; - } -}; -Command.isInstance = function(modelInstance) { - return ( - modelInstance && - modelInstance._commandTypes && - modelInstance._commandTypes.indexOf(this) > -1 - ); + }; + Object.defineProperty(targetClass.prototype, 'dataset', { + get: function() { return this._options.onRequest; } + }); + targetClass.isInstance = function(modelInstance) { + return ( + modelInstance && + modelInstance._commandTypes && + modelInstance._commandTypes.indexOf(this) > -1 + ); + }; }; +Command.extend(Command); + function RequestResponse(options) { Command.apply(this, arguments); - this._commandTypes.push(RequestResponse); } -util.inherits(RequestResponse, Command); -Object.defineProperty(RequestResponse.prototype, 'onRequest', { - get: function() { return this._options.onRequest; } -}); -Object.defineProperty(RequestResponse.prototype, 'onResponse', { - get: function() { return this._options.onResponse; } -}); -RequestResponse.isInstance = Command.isInstance; +RequestResponse.extend = function(targetClass) { + Command.extend(targetClass); + targetClass.commandTypes.push(RequestResponse); + Object.defineProperty(targetClass.prototype, 'onRequest', { + get: function() { return this._options.onRequest; } + }); + Object.defineProperty(targetClass.prototype, 'onResponse', { + get: function() { return this._options.onResponse; } + }); +}; +RequestResponse.extend(RequestResponse); exports.RequestResponse = RequestResponse; function PublishSubscribe(options) { Command.apply(this, arguments); - this._commandTypes.push(PublishSubscribe); } -util.inherits(PublishSubscribe, Command); -Object.defineProperty(PublishSubscribe.prototype, 'onSubscribe', { - get: function() { return this._options.onSubscribe; } -}); -Object.defineProperty(PublishSubscribe.prototype, 'onSubscribed', { - get: function() { return this._options.onSubscribed; } -}); -Object.defineProperty(PublishSubscribe.prototype, 'onUnsubscribe', { - get: function() { return this._options.onUnsubscribe; } -}); -Object.defineProperty(PublishSubscribe.prototype, 'onUnsubscribed', { - get: function() { return this._options.onUnsubscribed; } -}); -Object.defineProperty(PublishSubscribe.prototype, 'notification', { - get: function() { return this._options.notification; } -}); -Object.defineProperty(PublishSubscribe.prototype, 'onNotify', { - get: function() { return this._options.onNotify; } -}); -PublishSubscribe.isInstance = Command.isInstance; +PublishSubscribe.extend = function(targetClass) { + Command.extend(targetClass); + targetClass.commandTypes.push(PublishSubscribe); + Object.defineProperty(targetClass.prototype, 'onSubscribe', { + get: function() { return this._options.onSubscribe; } + }); + Object.defineProperty(targetClass.prototype, 'onSubscribed', { + get: function() { return this._options.onSubscribed; } + }); + Object.defineProperty(targetClass.prototype, 'onUnsubscribe', { + get: function() { return this._options.onUnsubscribe; } + }); + Object.defineProperty(targetClass.prototype, 'onUnsubscribed', { + get: function() { return this._options.onUnsubscribed; } + }); + Object.defineProperty(targetClass.prototype, 'notification', { + get: function() { return this._options.notification; } + }); + Object.defineProperty(targetClass.prototype, 'onNotify', { + get: function() { return this._options.onNotify; } + }); +}; +PublishSubscribe.extend(PublishSubscribe); exports.PublishSubscribe = PublishSubscribe; function HTTPCommand(options) { Command.apply(this, arguments); - this._commandTypes.push(HTTPCommand); } -util.inherits(HTTPCommand, Command); -HTTPCommand.isInstance = Command.isInstance; -Object.defineProperty(HTTPCommand.prototype, 'path', { - get: function() { return this._options.path; } -}); -Object.defineProperty(HTTPCommand.prototype, 'method', { - get: function() { return this._options.method || 'GET'; } -}); -Object.defineProperty(HTTPCommand.prototype, 'onHandle', { - get: function() { return this._options.onHandle; } -}); +HTTPCommand.extend = function(targetClass) { + Command.extend(targetClass); + targetClass.commandTypes.push(HTTPCommand); + Object.defineProperty(targetClass.prototype, 'path', { + get: function() { return this._options.path; } + }); + Object.defineProperty(targetClass.prototype, 'method', { + get: function() { return this._options.method || 'GET'; } + }); +}; +HTTPCommand.extend(HTTPCommand); exports.HTTPCommand = HTTPCommand; function HTTPRequestResponse(options) { - HTTPCommand.apply(this, arguments); - this._commandTypes.push(HTTPRequestResponse); - this._commandTypes.push(RequestResponse); + Command.apply(this, arguments); } -util.inherits(HTTPRequestResponse, HTTPCommand); -util.inherits(HTTPRequestResponse, RequestResponse); -HTTPRequestResponse.isInstance = HTTPCommand.isInstance; +HTTPRequestResponse.extend = function(targetClass) { + HTTPCommand.extend(targetClass); + RequestResponse.extend(targetClass); + targetClass.commandTypes.push(HTTPRequestResponse); +}; +HTTPRequestResponse.extend(HTTPRequestResponse); exports.HTTPRequestResponse = HTTPRequestResponse; function SocketCommand() { + Command.apply(this, arguments); } -SocketCommand.isInstance = Command.isInstance; +SocketCommand.extend = function(targetClass) { + Command.extend(targetClass); + targetClass.commandTypes.push(SocketCommand); +}; +SocketCommand.extend(SocketCommand); exports.SocketCommand = SocketCommand; function SocketRequestResponse(options) { - RequestResponse.apply(this, arguments); - this._commandTypes.push(SocketCommand); - this._commandTypes.push(SocketRequestResponse); + Command.apply(this, arguments); } -util.inherits(SocketRequestResponse, RequestResponse); -SocketRequestResponse.isInstance = RequestResponse.isInstance; +SocketRequestResponse.extend = function(targetClass) { + Command.extend(targetClass); + RequestResponse.extend(targetClass); + targetClass.commandTypes.push(SocketRequestResponse); +}; +SocketRequestResponse.extend(SocketRequestResponse); exports.SocketRequestResponse = SocketRequestResponse; function SocketPublishSubscribe(options) { - PublishSubscribe.apply(this, arguments); - this._commandTypes.push(SocketCommand); - this._commandTypes.push(SocketPublishSubscribe); + Command.apply(this, arguments); } -util.inherits(SocketPublishSubscribe, PublishSubscribe); -SocketRequestResponse.isInstance = PublishSubscribe.isInstance; +SocketPublishSubscribe.extend = function(targetClass) { + Command.extend(targetClass); + PublishSubscribe.extend(targetClass); + targetClass.commandTypes.push(SocketPublishSubscribe); +}; +SocketPublishSubscribe.extend(SocketPublishSubscribe); exports.SocketPublishSubscribe = SocketPublishSubscribe; -------------- next part -------------- HTML����������������������������... 下载