abstract-object
Advanced tools
Comparing version 1.9.1 to 2.0.0
134
lib/Error.js
(function() { | ||
var AbstractError, Err, NotImplementedError, createError, errors, firstLower, inherits, k, kCorruption, kIOError, kInvalidArgument, kInvalidFormat, kInvalidType, kNotFound, kNotOpened, kNotSupported, kOk; | ||
module.exports = require('abstract-error'); | ||
inherits = require("inherits-ex/lib/inherits"); | ||
firstLower = function(s) { | ||
return s[0].toLowerCase() + s.substring(1); | ||
}; | ||
module.exports.AbstractError = AbstractError = (function() { | ||
inherits(AbstractError, Error); | ||
function AbstractError(msg, errno) { | ||
Error.call(this, msg); | ||
this.code = errno; | ||
this.message = msg; | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, arguments.callee); | ||
} | ||
} | ||
return AbstractError; | ||
})(); | ||
module.exports.NotImplementedError = NotImplementedError = (function() { | ||
inherits(NotImplementedError, AbstractError); | ||
function NotImplementedError() { | ||
AbstractError.call(this, "NotImplemented", kNotSupported); | ||
} | ||
return NotImplementedError; | ||
})(); | ||
kOk = 0; | ||
kNotFound = 1; | ||
kCorruption = 2; | ||
kNotSupported = 3; | ||
kInvalidArgument = 4; | ||
kIOError = 5; | ||
kNotOpened = 6; | ||
kInvalidType = 7; | ||
kInvalidFormat = 8; | ||
errors = { | ||
Ok: kOk, | ||
NotFound: kNotFound, | ||
Corruption: kCorruption, | ||
NotSupported: kNotSupported, | ||
InvalidArgument: kInvalidArgument, | ||
IO: kIOError, | ||
NotOpened: kNotOpened, | ||
InvalidType: kInvalidType, | ||
InvalidFormat: kInvalidFormat | ||
}; | ||
module.exports.createError = createError = function(aType, aErrorCode, ErrorClass) { | ||
var Err; | ||
if (ErrorClass == null) { | ||
ErrorClass = AbstractError; | ||
} | ||
ErrorClass[aType] = aErrorCode; | ||
ErrorClass["is" + aType] = (function(aErrorCode, aType) { | ||
return function(err) { | ||
return err.code === aErrorCode || ((err.code == null) && err.message && err.message.substring(0, aType.length) === aType); | ||
}; | ||
})(aErrorCode, aType); | ||
ErrorClass.prototype[firstLower(aType)] = (function(aIsMethodName, ErrorClass) { | ||
return function() { | ||
return ErrorClass[aIsMethodName](this); | ||
}; | ||
})("is" + aType, ErrorClass); | ||
return Err = (function() { | ||
inherits(Err, ErrorClass); | ||
Err.prototype.name = aType + 'Error'; | ||
function Err(msg, aCode) { | ||
if (typeof aCode !== 'number') { | ||
aCode = aErrorCode; | ||
} | ||
if ((msg == null) || msg === "") { | ||
msg = aType; | ||
} | ||
Err.__super__.constructor.call(this, msg, aCode); | ||
} | ||
return Err; | ||
})(); | ||
}; | ||
for (k in errors) { | ||
Err = createError(k, errors[k]); | ||
if (errors[k] > 0) { | ||
module.exports[k + "Error"] = Err; | ||
} | ||
/* the error code | ||
AbstractError[k] = errors[k] | ||
#generate AbstractError.isNotFound(err) class methods: | ||
AbstractError["is" + k] = ((i, aType) -> | ||
(err) -> | ||
err.code is i or (not err.code? and err.message and err.message.substring(0, aType.length) is aType) | ||
)(errors[k], k) | ||
#generate AbstractError.notFound() instance methods: | ||
AbstractError::[firstLower(k)] = ((aType) -> | ||
-> | ||
AbstractError[aType] this | ||
)("is" + k) | ||
if errors[k] > 0 | ||
Err = ((i, aType) -> | ||
(msg) -> | ||
msg = aType if not msg? or msg is "" | ||
AbstractError.call this, msg, i | ||
)(errors[k], k) | ||
inherits Err, AbstractError | ||
#generate NotFoundError,... Classes | ||
module.exports[k + "Error"] = Err | ||
*/ | ||
} | ||
}).call(this); | ||
//# sourceMappingURL=Error.js.map |
(function() { | ||
var AbstractObject, EventEmitter, OBJECT_STATES, OBJECT_STATES_STR, createObject, createObjectWith, defineProperty, inherits, isArray, isFunction, isUndefined; | ||
module.exports = require('./eventable-object'); | ||
EventEmitter = require("events-ex"); | ||
inherits = require("inherits-ex/lib/inherits"); | ||
createObject = require("inherits-ex/lib/createObject"); | ||
createObjectWith = require("inherits-ex/lib/createObjectWith"); | ||
isArray = require("util-ex/lib/is/type/array"); | ||
isFunction = require("util-ex/lib/is/type/function"); | ||
isUndefined = require("util-ex/lib/is/type/undefined"); | ||
defineProperty = require("util-ex/lib/defineProperty"); | ||
OBJECT_STATES = { | ||
initing: 1, | ||
inited: 2, | ||
destroying: 0, | ||
destroyed: null | ||
}; | ||
OBJECT_STATES_STR = ["destroying", "initing", "inited"]; | ||
module.exports = AbstractObject = (function() { | ||
var s, vState, vStateName; | ||
inherits(AbstractObject, EventEmitter); | ||
AbstractObject.prototype.OBJECT_STATES = OBJECT_STATES; | ||
defineProperty(AbstractObject.prototype, "objectState", null, { | ||
get: function() { | ||
var vState; | ||
vState = this._objectState_; | ||
if (vState == null) { | ||
return "destroyed"; | ||
} else { | ||
return OBJECT_STATES_STR[vState]; | ||
} | ||
} | ||
}); | ||
for (vStateName in OBJECT_STATES) { | ||
vState = OBJECT_STATES[vStateName]; | ||
s = 'is' + vStateName[0].toUpperCase() + vStateName.slice(1); | ||
AbstractObject.prototype[s] = (function(aState) { | ||
return function() { | ||
return this._objectState_ === aState; | ||
}; | ||
})(vState); | ||
} | ||
AbstractObject.prototype.setObjectState = function(value, emitted) { | ||
if (emitted == null) { | ||
emitted = true; | ||
} | ||
this._objectState_ = OBJECT_STATES[value]; | ||
if (emitted) { | ||
this.emit(value, this); | ||
} | ||
}; | ||
AbstractObject.prototype.changeObjectState = function(value, emitted) { | ||
if (emitted == null) { | ||
emitted = true; | ||
} | ||
this._objectState_ = value; | ||
if (emitted) { | ||
if (value == null) { | ||
return this.emit("destroyed", this); | ||
} else { | ||
return this.emit(OBJECT_STATES_STR[value], this); | ||
} | ||
} | ||
}; | ||
AbstractObject.prototype.initialize = function() { | ||
if (isFunction(this.init)) { | ||
console.error("init method is deprecated, pls use initialize instead"); | ||
if (!AbstractObject.prototype.init) { | ||
AbstractObject.prototype.init = (function() {}); | ||
} | ||
return this.init.apply(this, arguments); | ||
} | ||
}; | ||
AbstractObject.prototype.finalize = function() { | ||
if (isFunction(this.final)) { | ||
console.error("final method is deprecated, pls use finalize instead"); | ||
if (!AbstractObject.prototype.final) { | ||
AbstractObject.prototype.final = (function() {}); | ||
} | ||
return this.final.apply(this, arguments); | ||
} | ||
}; | ||
function AbstractObject() { | ||
defineProperty(this, '_objectState_', null); | ||
this.changeObjectState(OBJECT_STATES.initing); | ||
this.setMaxListeners(Infinity); | ||
if (this.initialize.apply(this, arguments) !== true) { | ||
this.changeObjectState(OBJECT_STATES.inited); | ||
} | ||
} | ||
AbstractObject.prototype.destroy = function() { | ||
this.changeObjectState(OBJECT_STATES.destroying); | ||
this.finalize.apply(this, arguments); | ||
this.changeObjectState(OBJECT_STATES.destroyed); | ||
return this.removeAllListeners(); | ||
}; | ||
AbstractObject.prototype.free = function() { | ||
return this.destroy.apply(this, arguments); | ||
}; | ||
AbstractObject.prototype.dispatch = function(event, args, callback) { | ||
if (isUndefined(callback) && isFunction(args)) { | ||
callback = args; | ||
args = []; | ||
} else if (!isArray(args)) { | ||
args = [args]; | ||
} | ||
if (callback && callback.apply(this, args) !== false) { | ||
return; | ||
} | ||
args.splice(0, 0, event); | ||
return this.emit.apply(this, args); | ||
}; | ||
AbstractObject.prototype.dispatchError = function(error, callback) { | ||
if (callback && callback(error) !== false) { | ||
return; | ||
} | ||
return this.emit('error', error); | ||
}; | ||
AbstractObject.create = createObject; | ||
AbstractObject.createWith = createObjectWith; | ||
return AbstractObject; | ||
})(); | ||
}).call(this); | ||
//# sourceMappingURL=Object.js.map |
(function() { | ||
var AbstractObject, RefObject, defineProperty, inherits; | ||
module.exports = require('./eventable-ref-object'); | ||
AbstractObject = require("./Object"); | ||
inherits = require("inherits-ex/lib/inherits"); | ||
defineProperty = require("util-ex/lib/defineProperty"); | ||
module.exports = RefObject = (function() { | ||
function RefObject() {} | ||
inherits(RefObject, AbstractObject); | ||
RefObject.prototype.initialize = function() { | ||
defineProperty(this, 'RefCount', 0); | ||
if (this.init) { | ||
console.error("init method is deprecated, pls use initialize instead"); | ||
if (!RefObject.prototype.init) { | ||
RefObject.prototype.init = (function() {}); | ||
} | ||
return this.init.apply(this, arguments); | ||
} | ||
}; | ||
RefObject.prototype.addRef = function() { | ||
return ++this.RefCount; | ||
}; | ||
RefObject.prototype.release = function() { | ||
var result; | ||
result = --this.RefCount; | ||
if (result < 0) { | ||
this.destroy.apply(this, arguments); | ||
} | ||
return result; | ||
}; | ||
RefObject.prototype.free = RefObject.prototype.release; | ||
return RefObject; | ||
})(); | ||
}).call(this); | ||
//# sourceMappingURL=RefObject.js.map |
{ | ||
"name": "abstract-object", | ||
"version": "1.9.1", | ||
"version": "2.0.0", | ||
"description": "AbstractObject with Object State Events Support, RefObject with RefCount and AddRef/Release Support.", | ||
@@ -19,9 +19,9 @@ "homepage": "https://github.com/snowyu/abstract-object", | ||
"dependencies": { | ||
"events-ex": "~0.9.4", | ||
"abstract-error": "^1.0.1", | ||
"events-ex": "~0.9.8", | ||
"inherits-ex": "~1.0.6", | ||
"util-ex": "~0.2.5" | ||
"util-ex": "~0.2.8" | ||
}, | ||
"devDependencies": { | ||
"chai": "~1.10.0", | ||
"coffee-script": "~1.8.0", | ||
"connect-livereload": "*", | ||
@@ -28,0 +28,0 @@ "grunt": "*", |
### AbtractObject [![Build Status](https://img.shields.io/travis/snowyu/abstract-object/master.png)](http://travis-ci.org/snowyu/abstract-object) [![npm](https://img.shields.io/npm/v/abstract-object.svg)](https://npmjs.org/package/abstract-object) [![downloads](https://img.shields.io/npm/dm/abstract-object.svg)](https://npmjs.org/package/abstract-object) [![license](https://img.shields.io/npm/l/abstract-object.svg)](https://npmjs.org/package/abstract-object) | ||
AbstractObject with Object State Events Supports and `free` method provides. | ||
AbstractObject with Object State Supports and `free` method provides. | ||
The derived class should overwrite the `initialize` and `finalize` methods. | ||
# Changes | ||
## V2.x | ||
* **<broken change>** separate eventable from AbstractObject | ||
+ the new EventableObject can be as AbstractObject@v1.x | ||
* **<broken change>** separate eventable from RefObject too | ||
+ the new EventableRefObject can be as RefObject@v1.x | ||
+ add the eventable function to eventable any class('abstract-object/eventable'). | ||
* use the eventable plugin([events-ex](https://github.com/snowyu/events-ex.js)) to implement eventable object. | ||
- move all util functions to [util-ex](https://github.com/snowyu/util-ex.js) | ||
- move enhanced inheritance Functions to [inherits-ex](https://github.com/snowyu/inherits-ex.js). | ||
- move AbstractError to [abstract-error](https://github.com/snowyu/abstract-error.js) | ||
## V1.x | ||
* AbstractObject inherits from EventEmitter. | ||
* RefObject inherits from AbstractObject | ||
* AbstractError | ||
# AbstractObject | ||
* Methods: | ||
@@ -26,19 +49,22 @@ * `create`(class, ...): the `create` class method uses to create a new object instance(the util.createObject is the same function). | ||
* `...`: optional arguments will be passed into final method to process. | ||
* `dispatch`(event, args[, callback]): dispath an event or callback | ||
* `event`: the event name | ||
* `args`: the args are passed to event or callback | ||
* `callback`: optional, it will not dispatch event if the callback is exists, unless the callback return false. | ||
* `dispatchError`(error[, callback]): | ||
* `error`: the error instance. | ||
* `callback`: optional, it will not dispatch `'error'` event if the callback is exists, unless the callback return false. | ||
* `isIniting`(), `isInited`(),`isDestroying`(), `isDestroyed`() object state testing methods: | ||
* to test object state | ||
* Events: | ||
* `'initing'`: emit before the initialize method | ||
* `'inited'`: emit after the initialize method | ||
* `'destroying'`: emit before the finalize method | ||
* `'destroyed'`: emit after the finalize method | ||
* only for EventableObject: | ||
* Methods: | ||
* `dispatch`(event, args[, callback]): dispath an event or callback | ||
* `event`: the event name | ||
* `args`: the args are passed to event or callback | ||
* `callback`: optional, it will not dispatch event if the callback is exists, unless the callback return false. | ||
* `dispatchError`(error[, callback]): | ||
* `error`: the error instance. | ||
* `callback`: optional, it will not dispatch `'error'` event if the callback is exists, unless the callback return false. | ||
* Events: | ||
* `'initing'`: emit before the initialize method | ||
* `'inited'`: emit after the initialize method | ||
* `'destroying'`: emit before the finalize method | ||
* `'destroyed'`: emit after the finalize method | ||
# RefObject | ||
@@ -49,5 +75,5 @@ | ||
* methods: | ||
* `release`/`free`: Decrements reference count for this instance. | ||
* `release()`/`free()`: Decrements reference count for this instance. | ||
If it is becoming less than 0, the object would be (self) destroyed. | ||
* `addRef`: Increments the reference count for this instance | ||
* `addRef()`: Increments the reference count for this instance | ||
and returns the new reference count. | ||
@@ -61,3 +87,3 @@ | ||
RefObject = require('abstract-object/RefObject') | ||
inherits = require('abstract-object/lib/util/inherits') | ||
inherits = require('inherits-ex') | ||
createObject = AbstractObject.createObject | ||
@@ -93,3 +119,3 @@ | ||
var RefObject = require('abstract-object/RefObject') | ||
var util = require('abstract-object/lib/util') | ||
var inherits = require('inherits-ex') | ||
var createObject = AbstractObject.createObject | ||
@@ -106,3 +132,3 @@ | ||
util.inherits(MyObject, RefObject) | ||
inherits(MyObject, RefObject) | ||
@@ -112,3 +138,3 @@ | ||
//super call | ||
MyObject.__super__.initialize.call(this); | ||
MyObject.__super__.initialize.call(this) | ||
this.a = a | ||
@@ -126,2 +152,4 @@ this.b = b | ||
Moved to [abstract-error](https://github.com/snowyu/abstract-error.js). | ||
## AbstractError | ||
@@ -211,1 +239,5 @@ | ||
Moved to [inherits-ex](https://github.com/snowyu/inherits-ex.js). | ||
# Util Functions | ||
Moved to [util-ex](https://github.com/snowyu/util-ex.js). |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
19
97
236
63631
4
400
1
+ Addedabstract-error@^1.0.1
+ Addedabstract-error@1.0.2(transitive)
+ Addedinherits-ex@1.1.8(transitive)
Updatedevents-ex@~0.9.8
Updatedutil-ex@~0.2.8