avoscloud-sdk
Advanced tools
Comparing version 0.4.9 to 0.5.0
@@ -0,1 +1,5 @@ | ||
# 0.5.0 日期: 2015 年 3 月 02 日 | ||
* 增强 `AV.Promise`,增加`done,catch,finally,AV.Promise.race` 等方法,兼容 Promise/A+ | ||
* 修复更新对象可能更新没有变更的属性的 Bug,减少请求流量。 | ||
# 0.4.9 日期: 2015 年 2 月 26 日 | ||
@@ -2,0 +6,0 @@ * 拆分 sdk,按照模块划分成多个文件。 |
@@ -15,2 +15,32 @@ var path = require('path'); | ||
var coreSources = [ | ||
'version.js', | ||
'underscore.js', | ||
'utils.js', | ||
'error.js', | ||
'event.js', | ||
'geopoint.js', | ||
'acl.js', | ||
'op.js', | ||
'relation.js', | ||
'promise.js', | ||
'file.js', | ||
'object.js', | ||
'role.js', | ||
'user.js', | ||
'query.js', | ||
'cloudfunction.js', | ||
'push.js', | ||
'status.js' | ||
]; | ||
var optionalSources = [ | ||
'facebook.js', | ||
'history.js', | ||
'router.js', | ||
'collection.js', | ||
'view.js' | ||
] | ||
getAVVersion = function() { | ||
@@ -29,35 +59,19 @@ return require('./lib/AV.js').AV.VERSION.replace('js', ''); | ||
gulp.task('concat', function() { | ||
var sources = [ | ||
'version.js', | ||
'underscore.js', | ||
'utils.js', | ||
'error.js', | ||
'event.js', | ||
'geopoint.js', | ||
'acl.js', | ||
'op.js', | ||
'relation.js', | ||
'promise.js', | ||
'file.js', | ||
'object.js', | ||
'role.js', | ||
'collection.js', | ||
'view.js', | ||
'user.js', | ||
'query.js', | ||
'facebook.js', | ||
'history.js', | ||
'router.js', | ||
'cloudfunction.js', | ||
'push.js', | ||
'status.js', | ||
]; | ||
return gulp.src(sources.map(function(filename) { return path.join('lib', filename); })) | ||
.pipe(order(sources)) | ||
.pipe(concat('av.js')) | ||
.pipe(gulp.dest('dist')); | ||
}); | ||
function concatGenerator(sources, file) { | ||
return function() { | ||
return gulp.src(sources.map(function(filename) { return path.join('lib', filename); })) | ||
.pipe(order(sources)) | ||
.pipe(concat(file)) | ||
.pipe(gulp.dest('dist')); | ||
} | ||
} | ||
gulp.task('concat', concatGenerator(coreSources.concat(optionalSources), 'av.js')); | ||
gulp.task('concat_core', concatGenerator(coreSources, 'av-core.js')); | ||
gulp.task('uglify', ['concat'], function() { | ||
gulp.src('dist/av-core.js') | ||
.pipe(uglify()) | ||
.pipe(rename('av-core-mini.js')) | ||
.pipe(gulp.dest('dist')); | ||
return gulp.src('dist/av.js') | ||
@@ -71,3 +85,3 @@ .pipe(uglify()) | ||
var version = getAVVersion(); | ||
return gulp.src(['dist/av.js', 'dist/av-mini.js']) | ||
return gulp.src(['dist/av.js', 'dist/av-mini.js', 'dist/av-core-mini.js', 'dist/av-core.js', 'readme.txt']) | ||
.pipe(tar('avos-javascript-sdk-' + version + '.tar')) | ||
@@ -117,2 +131,2 @@ .pipe(gzip()) | ||
gulp.task('release', ['concat', 'uglify', 'compress-scripts', 'docs', 'compress-docs']); | ||
gulp.task('release', ['concat', 'concat_core', 'uglify', 'compress-scripts', 'docs', 'compress-docs']); |
/*! | ||
* AVOSCloud JavaScript SDK | ||
* Version: 0.4.9 | ||
* Built: Mon Jun 03 2013 13:45:00 | ||
@@ -5,0 +4,0 @@ * https://leancloud.cn |
@@ -463,7 +463,9 @@ // AV.Object is analogous to the Java AVObject. | ||
!(value instanceof AV.File)) { | ||
value = value.toJSON ? value.toJSON() : value; | ||
var json = JSON.stringify(value); | ||
if (this._hashedJSON[key] !== json) { | ||
var wasSet = !! this._hashedJSON[key]; | ||
this._hashedJSON[key] = json; | ||
return true; | ||
return wasSet; | ||
} | ||
@@ -470,0 +472,0 @@ } |
@@ -11,3 +11,3 @@ (function(root) { | ||
* <p>Typical usage would be like:<pre> | ||
* query.findAsync().then(function(results) { | ||
* query.find().then(function(results) { | ||
* results[0].set("foo", "bar"); | ||
@@ -19,7 +19,20 @@ * return results[0].saveAsync(); | ||
* </pre></p> | ||
* | ||
* @see AV.Promise.prototype.next | ||
* <p>Another example:<pre> | ||
* var promise = new AV.Promise(function(resolve, reject) { | ||
* resolve(42); | ||
* }); | ||
* promise.then(function(value){ | ||
* console.log(value); | ||
* }).catch(function(error){ | ||
* console.error(error); | ||
* }); | ||
* </pre></p> | ||
* @param {Function} fn An optional function with two arguments resolve | ||
* and reject.The first argument fulfills the promise, | ||
* the second argument rejects it. We can call these | ||
* functions, once our operation is completed. | ||
* @see AV.Promise.prototype.then | ||
* @class | ||
*/ | ||
AV.Promise = function() { | ||
AV.Promise = function(fn) { | ||
this._resolved = false; | ||
@@ -29,2 +42,4 @@ this._rejected = false; | ||
this._rejectedCallbacks = []; | ||
this.doResolve(fn); | ||
}; | ||
@@ -34,2 +49,4 @@ | ||
_isPromisesAPlusCompliant: false, | ||
/** | ||
@@ -67,4 +84,22 @@ * Returns true iff the given object fulfils the Promise interface. | ||
* will fail with the last error. If they all succeed, then the returned | ||
* promise will succeed, with the result being an array with the results of | ||
* all the input promises. | ||
* promise will succeed, with the results being the results of all the input | ||
* promises. For example: <pre> | ||
* var p1 = AV.Promise.as(1); | ||
* var p2 = AV.Promise.as(2); | ||
* var p3 = AV.Promise.as(3); | ||
* | ||
* AV.Promise.when(p1, p2, p3).then(function(r1, r2, r3) { | ||
* console.log(r1); // prints 1 | ||
* console.log(r2); // prints 2 | ||
* console.log(r3); // prints 3 | ||
* });</pre> | ||
* | ||
* The input promises can also be specified as an array: <pre> | ||
* var promises = [p1, p2, p3]; | ||
* AV.Promise.when(promises).then(function(r1, r2, r3) { | ||
* console.log(r1); // prints 1 | ||
* console.log(r2); // prints 2 | ||
* console.log(r3); // prints 3 | ||
* }); | ||
* </pre> | ||
* @param {Array} promises a list of promises to wait for. | ||
@@ -81,2 +116,4 @@ * @return {AV.Promise} the new promise. | ||
} | ||
var isAll = _.last(arguments); | ||
isAll = AV._.isBoolean(isAll) ? isAll : false; | ||
@@ -91,3 +128,7 @@ var total = objects.length; | ||
if (total === 0) { | ||
return AV.Promise.as.apply(this, results); | ||
if(isAll) { | ||
return AV.Promise.as.call(this, results); | ||
} else { | ||
return AV.Promise.as.apply(this, results); | ||
} | ||
} | ||
@@ -97,9 +138,96 @@ | ||
var resolveOne = function() { | ||
var resolveOne = function(i) { | ||
total = total - 1; | ||
if(hadError && !promise._rejected && isAll) { | ||
promise.reject.call(promise, errors[i]); | ||
return; | ||
} | ||
if (total === 0) { | ||
if (hadError && !promise._rejected) { | ||
promise.reject.call(promise, errors); | ||
} else { | ||
if(isAll) { | ||
if(!promise._rejected) { | ||
promise.resolve.call(promise, results); | ||
} else { | ||
//It's rejected already, so we ignore it. | ||
} | ||
} else { | ||
promise.resolve.apply(promise, results); | ||
} | ||
} | ||
} | ||
}; | ||
AV._arrayEach(objects, function(object, i) { | ||
if (AV.Promise.is(object)) { | ||
object.then(function(result) { | ||
results[i] = result; | ||
resolveOne(i); | ||
}, function(error) { | ||
errors[i] = error; | ||
hadError = true; | ||
resolveOne(i); | ||
}); | ||
} else { | ||
results[i] = object; | ||
resolveOne(i); | ||
} | ||
}); | ||
return promise; | ||
}, | ||
/** | ||
* Returns a promise that resolves or rejects as soon as one | ||
* of the promises in the iterable resolves or rejects, with | ||
* the value or reason from that promise.Returns a new promise | ||
* that is fulfilled when one of the input promises. | ||
* For example: <pre> | ||
* var p1 = AV.Promise.as(1); | ||
* var p2 = AV.Promise.as(2); | ||
* var p3 = AV.Promise.as(3); | ||
* | ||
* AV.Promise.race(p1, p2, p3).then(function(result) { | ||
* console.log(result); // prints 1 | ||
* });</pre> | ||
* | ||
* The input promises can also be specified as an array: <pre> | ||
* var promises = [p1, p2, p3]; | ||
* AV.Promise.when(promises).then(function(result) { | ||
* console.log(result); // prints 1 | ||
* }); | ||
* </pre> | ||
* @param {Array} promises a list of promises to wait for. | ||
* @return {AV.Promise} the new promise. | ||
*/ | ||
race: function(promises) { | ||
// Allow passing in Promises as separate arguments instead of an Array. | ||
var objects; | ||
if (promises && AV._isNullOrUndefined(promises.length)) { | ||
objects = arguments; | ||
} else { | ||
objects = promises; | ||
} | ||
var total = objects.length; | ||
var hadError = false; | ||
var results = []; | ||
var errors = []; | ||
results.length = errors.length = objects.length; | ||
if (total === 0) { | ||
return AV.Promise.as.call(this); | ||
} | ||
var promise = new AV.Promise(); | ||
var resolveOne = function(i) { | ||
if (!promise._resolved && !promise._rejected) { | ||
if (hadError) { | ||
promise.reject(errors); | ||
promise.reject.call(promise, errors[i]); | ||
} else { | ||
promise.resolve.apply(promise, results); | ||
promise.resolve.call(promise, results[i]); | ||
} | ||
@@ -113,11 +241,11 @@ } | ||
results[i] = result; | ||
resolveOne(); | ||
resolveOne(i); | ||
}, function(error) { | ||
errors[i] = error; | ||
hadError = true; | ||
resolveOne(); | ||
resolveOne(i); | ||
}); | ||
} else { | ||
results[i] = object; | ||
resolveOne(); | ||
resolveOne(i); | ||
} | ||
@@ -146,2 +274,12 @@ }); | ||
/** | ||
* Just like AV.Promise.when, but it calls resolveCallbck function | ||
* with one results array and calls rejectCallback function as soon as any one | ||
* of the input promises rejects. | ||
* @see AV.Promise.when | ||
*/ | ||
AV.Promise.all = function(promises) { | ||
return AV.Promise.when(promises, true); | ||
}; | ||
_.extend(AV.Promise.prototype, /** @lends AV.Promise.prototype */ { | ||
@@ -168,2 +306,23 @@ | ||
doResolve: function(fn){ | ||
if (!fn) return; | ||
var done = false; | ||
var self = this; | ||
try { | ||
fn(function (value) { | ||
if (done) return; | ||
done = true; | ||
self.resolve.call(self, value); | ||
}, function (reason) { | ||
if (done) return; | ||
done = true; | ||
self.reject.call(self, reason); | ||
}) | ||
} catch (ex) { | ||
if (done) return; | ||
done = true; | ||
self.reject.call(self, ex); | ||
} | ||
}, | ||
/** | ||
@@ -212,3 +371,11 @@ * Marks this promise as fulfilled, firing any callbacks waiting on it. | ||
if (resolvedCallback) { | ||
result = [resolvedCallback.apply(this, result)]; | ||
if (AV.Promise._isPromisesAPlusCompliant) { | ||
try { | ||
result = [resolvedCallback.apply(this, result)]; | ||
} catch (e) { | ||
result = [AV.Promise.error(e)]; | ||
} | ||
} else { | ||
result = [resolvedCallback.apply(this, result)]; | ||
} | ||
} | ||
@@ -229,3 +396,11 @@ if (result.length === 1 && AV.Promise.is(result[0])) { | ||
if (rejectedCallback) { | ||
result = [rejectedCallback(error)]; | ||
if (AV.Promise._isPromisesAPlusCompliant) { | ||
try { | ||
result = [rejectedCallback(error)]; | ||
} catch (e) { | ||
result = [AV.Promise.error(e)]; | ||
} | ||
} else { | ||
result = [rejectedCallback(error)]; | ||
} | ||
if (result.length === 1 && AV.Promise.is(result[0])) { | ||
@@ -238,5 +413,7 @@ result[0].then(function() { | ||
} else { | ||
// A Promises/A+ compliant implementation would call: | ||
// promise.resolve.apply(promise, result); | ||
promise.reject(result[0]); | ||
if (AV.Promise._isPromisesAPlusCompliant) { | ||
promise.resolve.apply(promise, result); | ||
} else { | ||
promise.reject(result[0]); | ||
} | ||
} | ||
@@ -248,6 +425,26 @@ } else { | ||
var runLater = function(func) { | ||
func.call(); | ||
}; | ||
if (AV.Promise._isPromisesAPlusCompliant) { | ||
if (typeof(window) !== 'undefined' && window.setTimeout) { | ||
runLater = function(func) { | ||
window.setTimeout(func, 0); | ||
}; | ||
} else if (typeof(process) !== 'undefined' && process.nextTick) { | ||
runLater = function(func) { | ||
process.nextTick(func); | ||
}; | ||
} | ||
} | ||
var self = this; | ||
if (this._resolved) { | ||
wrappedResolvedCallback.apply(this, this._result); | ||
runLater(function() { | ||
wrappedResolvedCallback.apply(self, self._result); | ||
}); | ||
} else if (this._rejected) { | ||
wrappedRejectedCallback(this._error); | ||
runLater(function() { | ||
wrappedRejectedCallback(self._error); | ||
}); | ||
} else { | ||
@@ -262,2 +459,39 @@ this._resolvedCallbacks.push(wrappedResolvedCallback); | ||
/** | ||
* Add handlers to be called when the Promise object is rejected. | ||
* | ||
* @param {Function} rejectedCallback Function that is called when this | ||
* Promise is rejected with an error. | ||
* @return {AV.Promise} A new Promise that will be fulfilled after this | ||
* Promise is fulfilled and either callback has completed. If the callback | ||
* returned a Promise, then this Promise will not be fulfilled until that | ||
* one is. | ||
* @function | ||
*/ | ||
catch: function(onRejected) { | ||
return this.then(undefined, onRejected); | ||
}, | ||
/** | ||
* Add handlers to be called when the promise | ||
* is either resolved or rejected | ||
*/ | ||
always: function(callback) { | ||
return this.then(callback, callback); | ||
}, | ||
/** | ||
* Add handlers to be called when the Promise object is resolved | ||
*/ | ||
done: function(callback) { | ||
return this.then(callback); | ||
}, | ||
/** | ||
* Add handlers to be called when the Promise object is rejected | ||
*/ | ||
fail: function(callback) { | ||
return this.then(null, callback); | ||
}, | ||
/** | ||
* Run the given callbacks after this promise is fulfilled. | ||
@@ -332,2 +566,17 @@ * @param optionsOrCallback {} A Backbone-style options callback, or a | ||
/** | ||
* Alias of AV.Promise.prototype.always | ||
* @function | ||
* @see AV.Promise#always | ||
*/ | ||
AV.Promise.prototype.finally = AV.Promise.prototype.always; | ||
/** | ||
* Alias of AV.Promise.prototype.done | ||
* @function | ||
* @see AV.Promise#done | ||
*/ | ||
AV.Promise.prototype.try = AV.Promise.prototype.done; | ||
}(this)); |
(function(root) { | ||
root.AV = root.AV || {}; | ||
root.AV.VERSION = "js0.4.9"; | ||
root.AV.VERSION = "js0.5.0"; | ||
}(this)); |
{ | ||
"name": "avoscloud-sdk", | ||
"version": "0.4.9", | ||
"version": "0.5.0", | ||
"main": "./lib/av.js", | ||
@@ -5,0 +5,0 @@ "description": "AVOSCloud JavaScript SDK.", |
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
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
375446
46
10612
0