app/assets/javascripts/faye-authentication.js in faye-authentication-1.8.2 vs app/assets/javascripts/faye-authentication.js in faye-authentication-1.9.0

- old
+ new

@@ -5,10 +5,11 @@ this._outbox = {}; this._options = options || {}; this._options.retry_delay = this._options.retry_delay || 1000; this._waiting_signatures = []; this._timer = null; + this.logger = Faye.logger; } FayeAuthentication.prototype.endpoint = function() { return (this._endpoint); }; @@ -31,20 +32,20 @@ $.each(messages, function(key, params) { var signature = $.grep(response.signatures || [], function(e) { return (e.channel == params.channel && e.clientId == params.clientId); })[0]; if (typeof signature === 'undefined') { - self.error('No signature found in ajax reply for channel ' + params.channel + ' and clientId ' + params.clientId); + self.logger.error('No signature found in ajax reply for channel ' + params.channel + ' and clientId ' + params.clientId); } else if (signature && !signature.signature) { - self.error('Error when fetching signature for channel ' + params.channel + ' and clientId ' + params.clientId + ', error was : "' + signature.error + '"'); + self.logger.error('Error when fetching signature for channel ' + params.channel + ' and clientId ' + params.clientId + ', error was : "' + signature.error + '"'); } - Faye.Promise.resolve(self._signatures[params.clientId][params.channel], signature ? signature.signature : null); + FayeAuthentication.Promise.resolve(self._signatures[params.clientId][params.channel], signature ? signature.signature : null); }); }, 'json').fail(function(xhr, textStatus, e) { - self.error('Failure when trying to fetch JWT signature for data "' + JSON.stringify(messages) + '", error was : ' + textStatus); + self.logger.error('Failure when trying to fetch JWT signature for data "' + JSON.stringify(messages) + '", error was : ' + textStatus); $.each(messages, function(key, params) { - Faye.Promise.resolve(self._signatures[params.clientId][params.channel], null); + FayeAuthentication.Promise.resolve(self._signatures[params.clientId][params.channel], null); }); }); }; FayeAuthentication.prototype.signMessage = function(message, callback) { @@ -62,11 +63,11 @@ self._outbox[message.id] = {message: message, clientId: clientId}; } callback(message); }); } else { - var promise = self._signatures[clientId][channel] = new Faye.Promise(); + var promise = self._signatures[clientId][channel] = new FayeAuthentication.Promise(); promise.then(function(signature) { message.signature = signature; if (!message.retried) { self._outbox[message.id] = {message: message, clientId: clientId}; } @@ -93,11 +94,11 @@ if (message.channel == '/meta/subscribe' || message.channel.lastIndexOf('/meta/', 0) !== 0) { if(this._options.whitelist) { try { return (!this._options.whitelist(subscription_or_channel)); } catch (e) { - this.error("Error caught when evaluating whitelist function : " + e.message); + this.logger.error("Error caught when evaluating whitelist function : " + e.message); } } return (true); } return (false); @@ -118,8 +119,334 @@ } else { callback(message); } }; -$(function() { - Faye.extend(FayeAuthentication.prototype, Faye.Logging); -}); +(function() { +'use strict'; + +var timeout = setTimeout, defer; + +if (typeof setImmediate === 'function') + defer = function(fn) { setImmediate(fn) }; +else if (typeof process === 'object' && process.nextTick) + defer = function(fn) { process.nextTick(fn) }; +else + defer = function(fn) { timeout(fn, 0) }; + +var PENDING = 0, + FULFILLED = 1, + REJECTED = 2; + +var RETURN = function(x) { return x }, + THROW = function(x) { throw x }; + +var Promise = function(task) { + this._state = PENDING; + this._onFulfilled = []; + this._onRejected = []; + + if (typeof task !== 'function') return; + var self = this; + + task(function(value) { fulfill(self, value) }, + function(reason) { reject(self, reason) }); +}; + +Promise.prototype.then = function(onFulfilled, onRejected) { + var next = new Promise(); + registerOnFulfilled(this, onFulfilled, next); + registerOnRejected(this, onRejected, next); + return next; +}; + +var registerOnFulfilled = function(promise, onFulfilled, next) { + if (typeof onFulfilled !== 'function') onFulfilled = RETURN; + var handler = function(value) { invoke(onFulfilled, value, next) }; + + if (promise._state === PENDING) { + promise._onFulfilled.push(handler); + } else if (promise._state === FULFILLED) { + handler(promise._value); + } +}; + +var registerOnRejected = function(promise, onRejected, next) { + if (typeof onRejected !== 'function') onRejected = THROW; + var handler = function(reason) { invoke(onRejected, reason, next) }; + + if (promise._state === PENDING) { + promise._onRejected.push(handler); + } else if (promise._state === REJECTED) { + handler(promise._reason); + } +}; + +var invoke = function(fn, value, next) { + defer(function() { _invoke(fn, value, next) }); +}; + +var _invoke = function(fn, value, next) { + var outcome; + + try { + outcome = fn(value); + } catch (error) { + return reject(next, error); + } + + if (outcome === next) { + reject(next, new TypeError('Recursive promise chain detected')); + } else { + fulfill(next, outcome); + } +}; + +var fulfill = Promise.fulfill = Promise.resolve = function(promise, value) { + var called = false, type, then; + + try { + type = typeof value; + then = value !== null && (type === 'function' || type === 'object') && value.then; + + if (typeof then !== 'function') return _fulfill(promise, value); + + then.call(value, function(v) { + if (!(called ^ (called = true))) return; + fulfill(promise, v); + }, function(r) { + if (!(called ^ (called = true))) return; + reject(promise, r); + }); + } catch (error) { + if (!(called ^ (called = true))) return; + reject(promise, error); + } +}; + +var _fulfill = function(promise, value) { + if (promise._state !== PENDING) return; + + promise._state = FULFILLED; + promise._value = value; + promise._onRejected = []; + + var onFulfilled = promise._onFulfilled, fn; + while (fn = onFulfilled.shift()) fn(value); +}; + +var reject = Promise.reject = function(promise, reason) { + if (promise._state !== PENDING) return; + + promise._state = REJECTED; + promise._reason = reason; + promise._onFulfilled = []; + + var onRejected = promise._onRejected, fn; + while (fn = onRejected.shift()) fn(reason); +}; + +Promise.all = function(promises) { + return new Promise(function(fulfill, reject) { + var list = [], + n = promises.length, + i; + + if (n === 0) return fulfill(list); + + for (i = 0; i < n; i++) (function(promise, i) { + Promise.fulfilled(promise).then(function(value) { + list[i] = value; + if (--n === 0) fulfill(list); + }, reject); + })(promises[i], i); + }); +}; + +Promise.defer = defer; + +Promise.deferred = Promise.pending = function() { + var tuple = {}; + + tuple.promise = new Promise(function(fulfill, reject) { + tuple.fulfill = tuple.resolve = fulfill; + tuple.reject = reject; + }); + return tuple; +}; + +Promise.fulfilled = Promise.resolved = function(value) { + return new Promise(function(fulfill, reject) { fulfill(value) }); +}; + +Promise.rejected = function(reason) { + return new Promise(function(fulfill, reject) { reject(reason) }); +}; + +FayeAuthentication.Promise = Promise; + +})(); + +(function() { +'use strict'; + +var timeout = setTimeout, defer; + +if (typeof setImmediate === 'function') + defer = function(fn) { setImmediate(fn) }; +else if (typeof process === 'object' && process.nextTick) + defer = function(fn) { process.nextTick(fn) }; +else + defer = function(fn) { timeout(fn, 0) }; + +var PENDING = 0, + FULFILLED = 1, + REJECTED = 2; + +var RETURN = function(x) { return x }, + THROW = function(x) { throw x }; + +var Promise = function(task) { + this._state = PENDING; + this._onFulfilled = []; + this._onRejected = []; + + if (typeof task !== 'function') return; + var self = this; + + task(function(value) { fulfill(self, value) }, + function(reason) { reject(self, reason) }); +}; + +Promise.prototype.then = function(onFulfilled, onRejected) { + var next = new Promise(); + registerOnFulfilled(this, onFulfilled, next); + registerOnRejected(this, onRejected, next); + return next; +}; + +var registerOnFulfilled = function(promise, onFulfilled, next) { + if (typeof onFulfilled !== 'function') onFulfilled = RETURN; + var handler = function(value) { invoke(onFulfilled, value, next) }; + + if (promise._state === PENDING) { + promise._onFulfilled.push(handler); + } else if (promise._state === FULFILLED) { + handler(promise._value); + } +}; + +var registerOnRejected = function(promise, onRejected, next) { + if (typeof onRejected !== 'function') onRejected = THROW; + var handler = function(reason) { invoke(onRejected, reason, next) }; + + if (promise._state === PENDING) { + promise._onRejected.push(handler); + } else if (promise._state === REJECTED) { + handler(promise._reason); + } +}; + +var invoke = function(fn, value, next) { + defer(function() { _invoke(fn, value, next) }); +}; + +var _invoke = function(fn, value, next) { + var outcome; + + try { + outcome = fn(value); + } catch (error) { + return reject(next, error); + } + + if (outcome === next) { + reject(next, new TypeError('Recursive promise chain detected')); + } else { + fulfill(next, outcome); + } +}; + +var fulfill = Promise.fulfill = Promise.resolve = function(promise, value) { + var called = false, type, then; + + try { + type = typeof value; + then = value !== null && (type === 'function' || type === 'object') && value.then; + + if (typeof then !== 'function') return _fulfill(promise, value); + + then.call(value, function(v) { + if (!(called ^ (called = true))) return; + fulfill(promise, v); + }, function(r) { + if (!(called ^ (called = true))) return; + reject(promise, r); + }); + } catch (error) { + if (!(called ^ (called = true))) return; + reject(promise, error); + } +}; + +var _fulfill = function(promise, value) { + if (promise._state !== PENDING) return; + + promise._state = FULFILLED; + promise._value = value; + promise._onRejected = []; + + var onFulfilled = promise._onFulfilled, fn; + while (fn = onFulfilled.shift()) fn(value); +}; + +var reject = Promise.reject = function(promise, reason) { + if (promise._state !== PENDING) return; + + promise._state = REJECTED; + promise._reason = reason; + promise._onFulfilled = []; + + var onRejected = promise._onRejected, fn; + while (fn = onRejected.shift()) fn(reason); +}; + +Promise.all = function(promises) { + return new Promise(function(fulfill, reject) { + var list = [], + n = promises.length, + i; + + if (n === 0) return fulfill(list); + + for (i = 0; i < n; i++) (function(promise, i) { + Promise.fulfilled(promise).then(function(value) { + list[i] = value; + if (--n === 0) fulfill(list); + }, reject); + })(promises[i], i); + }); +}; + +Promise.defer = defer; + +Promise.deferred = Promise.pending = function() { + var tuple = {}; + + tuple.promise = new Promise(function(fulfill, reject) { + tuple.fulfill = tuple.resolve = fulfill; + tuple.reject = reject; + }); + return tuple; +}; + +Promise.fulfilled = Promise.resolved = function(value) { + return new Promise(function(fulfill, reject) { fulfill(value) }); +}; + +Promise.rejected = function(reason) { + return new Promise(function(fulfill, reject) { reject(reason) }); +}; + +FayeAuthentication.Promise = Promise; + +})();