Commit 2a43d27b authored by Yehuda Katz's avatar Yehuda Katz

Initial commit

parents
{
"predef": [
"console",
"require",
"equal",
"test",
"testBoth",
"testWithDefault",
"raises",
"deepEqual",
"start",
"stop",
"ok",
"strictEqual",
"module",
"expect",
],
"node" : true,
"browser" : true,
"boss" : true,
"curly": false,
"debug": false,
"devel": false,
"eqeqeq": true,
"evil": true,
"forin": false,
"immed": false,
"laxbreak": false,
"newcap": true,
"noarg": true,
"noempty": false,
"nonew": false,
"nomen": false,
"onevar": false,
"plusplus": false,
"regexp": false,
"undef": true,
"sub": true,
"strict": false,
"white": false,
"eqnull": true
}
def replace_adapter(filename)
contents = File.read(filename)
regex = Regexp.new("^" + Regexp.escape(%{var adapter = require("./adapters/q");}) + "$")
replacement = %{var adapter = require("./adapters/rsvp");}
open(filename, "w") do |file|
file.write contents.gsub(regex, replacement)
end
end
file "promise-tests" do
sh "git clone https://github.com/domenic/promise-tests.git"
end
task :update do
cd "promise-tests" do
sh "git reset --hard origin/master"
sh "git pull"
sh "npm install"
end
end
file "promise-tests/lib/adapters/rsvp.js" => ["promise-tests", :update] do
cp "tests/test-adapter.js", "promise-tests/lib/adapters/rsvp.js"
end
task :prepare_tests => "promise-tests/lib/adapters/rsvp.js"
%w(promises-a.js always-async.js resolution-races.js returning-a-promise.js).each do |test|
file test => "promise-tests/lib/adapters/rsvp.js" do
replace_adapter("promise-tests/lib/#{test}")
end
task :prepare_tests => test
end
task :test do
cd "promise-tests" do
sh "./node_modules/mocha/bin/mocha lib/promises-a.js lib/always-async.js lib/resolution-races.js lib/returning-a-promise.js --reporter spec"
end
end
task :default => :test
"use strict";
var browserGlobal = (typeof window !== 'undefined') ? window : {};
// Extract this into its own library
var MutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
var async;
if (typeof process !== 'undefined') {
async = function(callback, binding) {
process.nextTick(function() {
callback.call(binding);
});
};
} else if (MutationObserver) {
var queue = [];
var observer = new MutationObserver(function() {
var toProcess = queue.slice();
queue = [];
toProcess.forEach(function(tuple) {
var callback = tuple[0], binding = tuple[1];
callback.call(binding);
});
});
var element = document.createElement('div');
observer.observe(element, { attributes: true });
async = function(callback, binding) {
queue.push([callback, binding]);
element.setAttribute('drainQueue', 'drainQueue');
};
} else {
async = function(callback, binding) {
setTimeout(function() {
callback.call(binding);
}, 1);
};
}
var Event = exports.Event = function(type, options) {
this.type = type;
for (var option in options) {
if (!options.hasOwnProperty(option)) { continue; }
this[option] = options[option];
}
};
var indexOf = function(callbacks, callback) {
for (var i=0, l=callbacks.length; i<l; i++) {
if (callbacks[i][0] === callback) { return i; }
}
return -1;
};
var callbacksFor = function(object) {
var callbacks = object._promiseCallbacks;
if (!callbacks) {
callbacks = object._promiseCallbacks = {};
}
return callbacks;
};
var EventTarget = exports.EventTarget = {
mixin: function(object) {
object.on = this.on;
object.off = this.off;
object.trigger = this.trigger;
return object;
},
on: function(eventName, callback, binding) {
var allCallbacks = callbacksFor(this), callbacks;
binding = binding || this;
callbacks = allCallbacks[eventName];
if (!callbacks) {
callbacks = allCallbacks[eventName] = [];
}
if (indexOf(callbacks, callback) === -1) {
callbacks.push([callback, binding]);
}
},
off: function(eventName, callback) {
var allCallbacks = callbacksFor(this), callbacks;
if (!callback) {
allCallbacks[eventName] = [];
return;
}
callbacks = allCallbacks[eventName];
var index = indexOf(callbacks, callback);
if (index !== -1) { callbacks.splice(index, 1); }
},
trigger: function(eventName, options) {
var allCallbacks = callbacksFor(this),
callbacks, callbackTuple, callback, binding, event;
if (callbacks = allCallbacks[eventName]) {
for (var i=0, l=callbacks.length; i<l; i++) {
callbackTuple = callbacks[i];
callback = callbackTuple[0];
binding = callbackTuple[1];
if (typeof options !== 'object') {
options = { detail: options };
}
event = new Event(eventName, options);
callback.call(binding, event);
}
}
}
};
var Promise = exports.Promise = function() {
this.on('promise:resolved', function(event) {
this.trigger('success', { detail: event.detail });
}, this);
this.on('promise:failed', function(event) {
this.trigger('error', { detail: event.detail });
}, this);
};
var throwException = function() {
//throw new Error("You cannot resolve this Promise because it has already been resolved.");
};
var invokeCallback = function(type, promise, callback, event) {
var value, error;
if (callback) {
try {
value = callback(event.detail);
} catch(e) {
error = e;
}
} else {
value = event.detail;
}
if (value instanceof Promise) {
value.then(function(value) {
promise.resolve(value);
}, function(error) {
promise.reject(error);
});
} else if (callback && value) {
promise.resolve(value);
} else if (error) {
promise.reject(error);
} else {
promise[type](value);
}
};
Promise.prototype = {
then: function(done, fail) {
var thenPromise = new Promise();
this.on('promise:resolved', function(event) {
invokeCallback('resolve', thenPromise, done, event);
});
this.on('promise:failed', function(event) {
invokeCallback('reject', thenPromise, fail, event);
});
return thenPromise;
},
resolve: function(value) {
async(function() {
this.trigger('promise:resolved', { detail: value });
this.isResolved = value;
}, this);
this.resolve = throwException;
this.reject = throwException;
},
reject: function(value) {
async(function() {
this.trigger('promise:failed', { detail: value });
this.isRejected = value;
}, this);
this.resolve = throwException;
this.reject = throwException;
}
};
EventTarget.mixin(Promise.prototype);
"use strict";
var Promise = require("../../../lib/rsvp").Promise;
exports.fulfilled = function(value) {
var promise = new Promise();
promise.resolve(value);
return promise;
};
exports.rejected = function(error) {
var promise = new Promise();
promise.reject(error);
return promise;
};
exports.pending = function () {
var promise = new Promise();
return {
promise: promise,
fulfill: function(value) {
promise.resolve(value);
},
reject: function(error) {
promise.reject(error);
}
};
};
exports.throws = true;
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment