Skip to content

Commit

Permalink
[build] 0.3.0
Browse files Browse the repository at this point in the history
what:
1. change @Waituntil arguments
2. add @Runnable
3. add @classify
why:
how:
  • Loading branch information
toxic-johann committed Jul 10, 2017
1 parent 4949d1f commit 6d7d771
Show file tree
Hide file tree
Showing 5 changed files with 420 additions and 103 deletions.
175 changes: 141 additions & 34 deletions lib/toxic-decorators.browser.js
@@ -1,6 +1,6 @@

/**
* toxic-decorators v0.2.3
* toxic-decorators v0.3.0
* (c) 2017 toxic-johann
* Released under GPL-3.0
*/
Expand Down Expand Up @@ -1270,7 +1270,7 @@ exports.default = function (arr) {
var _toConsumableArray = unwrapExports(toConsumableArray);

/**
* toxic-utils v0.1.1
* toxic-utils v0.1.3
* (c) 2017 toxic-johann
* Released under MIT
*/
Expand Down Expand Up @@ -1365,7 +1365,9 @@ function isAccessorDescriptor(desc) {
* to check if the descirptor is an data descriptor
* @param {descriptor} desc it should be a descriptor better
*/

function isDataDescriptor(desc) {
return !!desc && desc.hasOwnProperty('value') && isBoolean(desc.configurable) && isBoolean(desc.enumerable) && isBoolean(desc.writable);
}
/**
* to check if the descirptor is an initiallizer descriptor
* @param {descriptor} desc it should be a descriptor better
Expand Down Expand Up @@ -1458,7 +1460,7 @@ function getDeepProperty(obj, keys) {

var getOwnKeys = isFunction(getOwnPropertySymbols) ? function (obj) {
// $FlowFixMe: do not support symwbol yet
return getOwnPropertyNames(obj).concat(getOwnPropertySymbols(obj));
return Array.from(getOwnPropertyNames(obj).concat(getOwnPropertySymbols(obj)));
} : getOwnPropertyNames;

// $FlowFixMe: In some environment, Object.getOwnPropertyDescriptors has been implemented;
Expand Down Expand Up @@ -1531,10 +1533,10 @@ function accessor() {
var hasOriginGet = isFunction(originGet);
var hasOriginSet = isFunction(originSet);
if (!hasOriginGet && hasGet) {
warn("You are trying to set getter via @accessor on one property without getter. That's not a good idea.");
warn('You are trying to set getter via @accessor on ' + prop + ' without getter. That\'s not a good idea.');
}
if (!hasOriginSet && hasSet) {
warn("You are trying to set setter via @accessor on one property without setter. That's not a good idea.");
warn('You are trying to set setter via @accessor on ' + prop + ' without setter. That\'s not a good idea.');
}
var getter = hasOriginGet || hasGet ? function () {
var _this = this;
Expand Down Expand Up @@ -1852,15 +1854,18 @@ function initialize() {
}
if (isAccessorDescriptor(descriptor)) {
var hasBeenReset = false;
var originSet = descriptor.set;

return accessor({
get: function get(value) {
if (hasBeenReset) return value;
return bind(fn, this)(value);
},
set: function set(value) {

set: originSet ? function (value) {
hasBeenReset = true;
return value;
}
} : undefined
})(obj, prop, descriptor);
}
/**
Expand Down Expand Up @@ -2010,34 +2015,52 @@ function alias(other, key, option) {

var defineProperty$3 = Object.defineProperty;

function autobindClass() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$exclude = _ref.exclude,
exclude = _ref$exclude === undefined ? [] : _ref$exclude;

if (!isArray(exclude)) throw new TypeError('options.exclude must be an array');
return function (Klass) {
if (!isFunction(Klass)) throw new TypeError('@autobindClass can only be used on class');
var prototype = Klass.prototype;

if (isVoid(prototype)) throw new Error('The prototype of the class is empty, please check it');

var descs = getOwnPropertyDescriptors(prototype);
var keys = getOwnKeys(prototype);

for (var i = 0, len = keys.length; i < len; i++) {
var key = keys[i];
// $FlowFixMe: not support symbol yet
var desc = descs[key];
if (key === 'constructor' || !isFunction(desc.value) ||
// $FlowFixMe: not support symbol yet
exclude.indexOf(key) > -1) continue;
// $FlowFixMe: not support symbol yet
defineProperty$3(prototype, key, autobind(prototype, key, desc));
function classify(decorator) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
requirement = _ref.requirement,
_ref$customArgs = _ref.customArgs,
customArgs = _ref$customArgs === undefined ? false : _ref$customArgs;

return function () {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}

var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref2$exclude = _ref2.exclude,
exclude = _ref2$exclude === undefined ? [] : _ref2$exclude,
_ref2$include = _ref2.include,
include = _ref2$include === undefined ? [] : _ref2$include,
_ref2$construct = _ref2.construct,
construct = _ref2$construct === undefined ? false : _ref2$construct,
_ref2$self = _ref2.self,
self = _ref2$self === undefined ? false : _ref2$self;

if (!isArray(exclude)) throw new TypeError('options.exclude must be an array');
if (!isArray(include)) throw new TypeError('options.include must be an array');
return function (Klass) {
var isClass = isFunction(Klass);
if (!self && !isClass) throw new TypeError('@' + decorator.name + 'Class can only be used on class');
if (self && isPrimitive(Klass)) throw new TypeError('@' + decorator.name + 'Class must be used on non-primitive type value in \'self\' mode');
var prototype = self ? Klass : Klass.prototype;
if (isVoid(prototype)) throw new Error('The prototype of the ' + Klass.name + ' is empty, please check it');
var descs = getOwnPropertyDescriptors(prototype);
getOwnKeys(prototype).concat(include).forEach(function (key) {
var desc = descs[key];
if (key === 'constructor' && !construct || self && isClass && ['name', 'length', 'prototype'].indexOf(key) > -1 || exclude.indexOf(key) > -1 || isFunction(requirement) && requirement(prototype, key, desc, { self: self }) === false) return;
defineProperty$3(prototype, key, (customArgs ? decorator.apply(undefined, toConsumableArray$1(args)) : decorator)(prototype, key, desc));
});
};
};
}

var autobindClass = classify(autobind, {
requirement: function requirement(obj, prop, desc) {
// $FlowFixMe: it's data descriptor now
return isDataDescriptor(desc) && isFunction(desc.value);
}
});

var mapStore = void 0;
// save bound function for super
function getBoundSuper(obj, fn) {
Expand Down Expand Up @@ -2179,7 +2202,10 @@ function frozen(obj, prop, descriptor) {
var getOwnPropertyDescriptor$2 = Object.getOwnPropertyDescriptor;
var defineProperty$5 = Object.defineProperty;

function waituntil(key, other) {
function waituntil(key) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
other = _ref.other;

if (!isFunction(key) && !isPromise(key) && !isString(key)) throw new TypeError('@waitUntil only accept Function, Promise or String');
return function (obj, prop, descriptor) {
if (descriptor === undefined) {
Expand Down Expand Up @@ -2407,7 +2433,6 @@ var Hooks = function () {

var descriptor = getOwnPropertyDescriptor$3(obj, key);
if (descriptor.writable === false || descriptor.configurable === false) return;
// $FlowFixMe: flow do not support symbol yet
var hooks = new Hooks({ prop: key, handler: handler, deep: deep });
defineProperty$8(obj, key, compressMultipleDecorators(accessor({
set: function set$$1(value) {
Expand Down Expand Up @@ -2486,6 +2511,46 @@ function nonextendable(obj, prop, descriptor) {
})(obj, prop, descriptor);
}

function runnable(key) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
other = _ref.other,
backup = _ref.backup;

if (!isFunction(key) && !isString(key)) throw new TypeError('@runnable only accept Function or String');
return function (obj, prop, descriptor) {
if (descriptor === undefined) {
throw new Error('@runnable must used on descriptor, are you using it on undefined property?');
}
var _value = descriptor.value,
configurable = descriptor.configurable;

if (!isFunction(_value)) throw new TypeError('@runnable can only be used on method, but not ' + _value + ' on ' + prop);
var canIRun = isFunction(key) ? key : function () {
var keys = key.split('.');
var originTarget = isPrimitive(other) ? this : other;
return getDeepProperty(originTarget, keys);
};
backup = isFunction(backup) ? backup : function () {};
return {
value: function value() {
if (bind(canIRun, this).apply(undefined, arguments) === true) {
return bind(_value, this).apply(undefined, arguments);
} else {
// $FlowFixMe: I have reassign it when it's not a function
return bind(backup, this).apply(undefined, arguments);
}
},

// function should not be enmuerable
enumerable: false,
configurable: configurable,
// as we have delay this function
// it's not a good idea to change it
writable: false
};
};
}

function enumerable(obj, prop, descriptor) {
if (descriptor === undefined) {
return {
Expand Down Expand Up @@ -2621,6 +2686,42 @@ function number$1() {
return accessor({ set: args, get: args });
}

var before$1 = classify(before, {
requirement: function requirement(obj, prop, desc) {
// $FlowFixMe: it's data descriptor now
return isDataDescriptor(desc) && isFunction(desc.value);
},

customArgs: true
});

var after$1 = classify(after, {
requirement: function requirement(obj, prop, desc) {
// $FlowFixMe: it's data descriptor now
return isDataDescriptor(desc) && isFunction(desc.value);
},

customArgs: true
});

var runnable$1 = classify(runnable, {
requirement: function requirement(obj, prop, desc) {
// $FlowFixMe: it's data descriptor now
return isDataDescriptor(desc) && isFunction(desc.value);
},

customArgs: true
});

var waituntil$1 = classify(waituntil, {
requirement: function requirement(obj, prop, desc) {
// $FlowFixMe: it's data descriptor now
return isDataDescriptor(desc) && isFunction(desc.value);
},

customArgs: true
});

var defineProperty$9 = Object.defineProperty;
var getOwnPropertyDescriptor$4 = Object.getOwnPropertyDescriptor;

Expand Down Expand Up @@ -2680,6 +2781,7 @@ exports.lazyInit = lazyInit;
exports.lock = lock;
exports.watch = watch;
exports.nonextendable = nonextendable;
exports.runnable = runnable;
exports.enumerable = enumerable;
exports.nonenumerable = nonenumerable;
exports.nonconfigurable = nonconfigurable;
Expand All @@ -2692,7 +2794,12 @@ exports.alwaysBoolean = boolean$1;
exports.alwaysArray = array$1;
exports.alwaysNumber = number$1;
exports.autobindClass = autobindClass;
exports.beforeClass = before$1;
exports.afterClass = after$1;
exports.runnableClass = runnable$1;
exports.waituntilClass = waituntil$1;
exports.applyDecorators = applyDecorators;
exports.classify = classify;
exports.getOwnKeys = getOwnKeys;
exports.getOwnPropertyDescriptors = getOwnPropertyDescriptors;

Expand Down

0 comments on commit 6d7d771

Please sign in to comment.