Last active
December 11, 2015 01:38
-
-
Save RubenVerborgh/4524424 to your computer and use it in GitHub Desktop.
Demo implementation of eventually with flags.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (chaiModule) { | |
"use strict"; | |
// NodeJS | |
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") | |
module.exports = chaiModule; | |
// AMD | |
else if (typeof define === "function" && define.amd) | |
define(function () { return chaiModule; }); | |
// Other | |
else | |
chai.use(chaiModule); | |
}(function chaiAsPromised(chai, utils) { | |
"use strict"; | |
var Assertion = chai.Assertion, | |
assertionPrototype = Assertion.prototype, | |
expect = chai.expect; | |
Assertion.addProperty("eventually", function () { | |
utils.flag(this, "eventually", { handled: false }); | |
}); | |
var methodNames = Object.getOwnPropertyNames(assertionPrototype) | |
.filter(function (propertyName) { | |
var property = Object.getOwnPropertyDescriptor(assertionPrototype, propertyName); | |
return typeof property.value === "function"; | |
}); | |
methodNames.forEach(function (methodName) { | |
Assertion.overwriteMethod(methodName, function (_super) { | |
return function () { | |
var eventuallyFlags = utils.flag(this, "eventually"); | |
if (!eventuallyFlags || eventuallyFlags.handled) | |
return _super.apply(this, arguments); | |
eventuallyFlags.handled = true; | |
var object = utils.flag(this, "object"); | |
expect(object).to.have.property("then"); | |
expect(object.then).to.be.a("function"); | |
var assertion = this, | |
args = arguments; | |
object.then(function (result) { | |
var assertionError; | |
utils.flag(assertion, "object", result); | |
try { | |
_super.apply(assertion, args); | |
} | |
catch (error) { | |
assertionError = error; | |
} | |
if (eventuallyFlags.notify) | |
eventuallyFlags.notify.call(assertion, assertionError); | |
eventuallyFlags.done = true; | |
eventuallyFlags.error = assertionError; | |
}); | |
}; | |
}); | |
}); | |
Assertion.addMethod("notify", function (callback) { | |
var eventuallyFlags = utils.flag(this, "eventually"); | |
if (eventuallyFlags.done) | |
return callback(eventuallyFlags.error); | |
eventuallyFlags.notify = callback; | |
}); | |
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var chai = require("chai"); | |
var when = require("when"); | |
chai.should(); | |
chai.use(require(process.argv[2] || "./eventually-with-flags.js")); | |
var callbackCount = 1000; | |
for (var i = callbackCount; i > 0; i--) | |
when.resolve('x').should.eventually.equal('x').notify(function () { | |
if (!--callbackCount) | |
console.log('done'); | |
}); |
Author
RubenVerborgh
commented
Jan 13, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment