Ejegg has uploaded a new change for review. https://gerrit.wikimedia.org/r/183378
Change subject: Add promise library ...................................................................... Add promise library Change-Id: Ib6a6d8736b99aa6cc3edf1519f2b150e473dc48a --- A promise/.jshintrc A promise/.npmignore A promise/LICENSE A promise/Readme.md A promise/core.js A promise/index.js A promise/lib/core.js A promise/lib/done.js A promise/lib/es6-extensions.js A promise/lib/node-extensions.js A promise/node_modules/asap/LICENSE.md A promise/node_modules/asap/README.md A promise/node_modules/asap/asap.js A promise/node_modules/asap/package.json A promise/package.json A promise/polyfill-done.js A promise/polyfill.js 17 files changed, 872 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/dash/node_modules refs/changes/78/183378/1 diff --git a/promise/.jshintrc b/promise/.jshintrc new file mode 100644 index 0000000..abbe750 --- /dev/null +++ b/promise/.jshintrc @@ -0,0 +1,5 @@ +{ + "asi": true, + "node": true, + "strict": true +} diff --git a/promise/.npmignore b/promise/.npmignore new file mode 100644 index 0000000..6ffaded --- /dev/null +++ b/promise/.npmignore @@ -0,0 +1,6 @@ +components +node_modules +test +.gitignore +.travis.yml +component.json diff --git a/promise/LICENSE b/promise/LICENSE new file mode 100644 index 0000000..060e6c9 --- /dev/null +++ b/promise/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2014 Forbes Lindesay + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/promise/Readme.md b/promise/Readme.md new file mode 100644 index 0000000..27b4ecc --- /dev/null +++ b/promise/Readme.md @@ -0,0 +1,217 @@ +<a href="http://promises-aplus.github.com/promises-spec"><img src="http://promises-aplus.github.com/promises-spec/assets/logo-small.png" align="right" /></a> +# promise + +This is a simple implementation of Promises. It is a super set of ES6 Promises designed to have readable, performant code and to provide just the extensions that are absolutely necessary for using promises today. + +For detailed tutorials on its use, see www.promisejs.org + +[![Build Status](https://img.shields.io/travis/then/promise/master.svg)](https://travis-ci.org/then/promise) +[![Dependency Status](https://img.shields.io/gemnasium/then/promise.svg)](https://gemnasium.com/then/promise) +[![NPM version](https://img.shields.io/npm/v/promise.svg)](https://www.npmjs.org/package/promise) + +## Installation + +**Server:** + + $ npm install promise + +**Client:** + +You can use browserify on the client, or use the pre-compiled script that acts as a polyfill. + +```html +<script src="https://www.promisejs.org/polyfills/promise-4.0.0.js"></script> +``` + +Note that the [es5-shim](https://github.com/es-shims/es5-shim) must be loaded before this library to support browsers pre IE9. + +```html +<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/3.4.0/es5-shim.min.js"></script> +``` + +## Usage + +The example below shows how you can load the promise library (in a way that works on both client and server). It then demonstrates creating a promise from scratch. You simply call `new Promise(fn)`. There is a complete specification for what is returned by this method in [Promises/A+](http://promises-aplus.github.com/promises-spec/). + +```javascript +var Promise = require('promise'); + +var promise = new Promise(function (resolve, reject) { + get('http://www.google.com', function (err, res) { + if (err) reject(err); + else resolve(res); + }); +}); +``` + +## API + +Before all examples, you will need: + +```js +var Promise = require('promise'); +``` + +### new Promise(resolver) + +This creates and returns a new promise. `resolver` must be a function. The `resolver` function is passed two arguments: + + 1. `resolve` should be called with a single argument. If it is called with a non-promise value then the promise is fulfilled with that value. If it is called with a promise (A) then the returned promise takes on the state of that new promise (A). + 2. `reject` should be called with a single argument. The returned promise will be rejected with that argument. + +### Static Functions + + These methods are invoked by calling `Promise.methodName`. + +#### Promise.resolve(value) + +(deprecated aliases: `Promise.from(value)`, `Promise.cast(value)`) + +Converts values and foreign promises into Promises/A+ promises. If you pass it a value then it returns a Promise for that value. If you pass it something that is close to a promise (such as a jQuery attempt at a promise) it returns a Promise that takes on the state of `value` (rejected or fulfilled). + +#### Promise.all(array) + +Returns a promise for an array. If it is called with a single argument that `Array.isArray` then this returns a promise for a copy of that array with any promises replaced by their fulfilled values. Otherwise it returns a promise for an array that conatins its arguments, except with promises replaced by their resolution values. e.g. + +```js +Promise.all([Promise.from('a'), 'b', Promise.from('c')]) + .then(function (res) { + assert(res[0] === 'a') + assert(res[1] === 'b') + assert(res[2] === 'c') + }) + +Promise.all(Promise.from('a'), 'b', Promise.from('c')) + .then(function (res) { + assert(res[0] === 'a') + assert(res[1] === 'b') + assert(res[2] === 'c') + }) +``` + +#### Promise.denodeify(fn) + +_Non Standard_ + +Takes a function which accepts a node style callback and returns a new function that returns a promise instead. + +e.g. + +```javascript +var fs = require('fs') + +var read = Promise.denodeify(fs.readFile) +var write = Promise.denodeify(fs.writeFile) + +var p = read('foo.json', 'utf8') + .then(function (str) { + return write('foo.json', JSON.stringify(JSON.parse(str), null, ' '), 'utf8') + }) +``` + +#### Promise.nodeify(fn) + +_Non Standard_ + +The twin to `denodeify` is useful when you want to export an API that can be used by people who haven't learnt about the brilliance of promises yet. + +```javascript +module.exports = Promise.nodeify(awesomeAPI) +function awesomeAPI(a, b) { + return download(a, b) +} +``` + +If the last argument passed to `module.exports` is a function, then it will be treated like a node.js callback and not parsed on to the child function, otherwise the API will just return a promise. + +### Prototype Methods + +These methods are invoked on a promise instance by calling `myPromise.methodName` + +### Promise#then(onFulfilled, onRejected) + +This method follows the [Promises/A+ spec](http://promises-aplus.github.io/promises-spec/). It explains things very clearly so I recommend you read it. + +Either `onFulfilled` or `onRejected` will be called and they will not be called more than once. They will be passed a single argument and will always be called asynchronously (in the next turn of the event loop). + +If the promise is fulfilled then `onFulfilled` is called. If the promise is rejected then `onRejected` is called. + +The call to `.then` also returns a promise. If the handler that is called returns a promise, the promise returned by `.then` takes on the state of that returned promise. If the handler that is called returns a value that is not a promise, the promise returned by `.then` will be fulfilled with that value. If the handler that is called throws an exception then the promise returned by `.then` is rejected with that exception. + +#### Promise#done(onFulfilled, onRejected) + +_Non Standard_ + +The same semantics as `.then` except that it does not return a promise and any exceptions are re-thrown so that they can be logged (crashing the application in non-browser environments) + +#### Promise#nodeify(callback) + +_Non Standard_ + +If `callback` is `null` or `undefined` it just returns `this`. If `callback` is a function it is called with rejection reason as the first argument and result as the second argument (as per the node.js convention). + +This lets you write API functions that look like: + +```javascript +function awesomeAPI(foo, bar, callback) { + return internalAPI(foo, bar) + .then(parseResult) + .then(null, retryErrors) + .nodeify(callback) +} +``` + +People who use typical node.js style callbacks will be able to just pass a callback and get the expected behavior. The enlightened people can not pass a callback and will get awesome promises. + +## Extending Promises + + There are three options for extending the promises created by this library. + +### Inheritance + + You can use inheritance if you want to create your own complete promise library with this as your basic starting point, perfect if you have lots of cool features you want to add. Here is an example of a promise library called `Awesome`, which is built on top of `Promise` correctly. + +```javascript +var Promise = require('promise'); +function Awesome(fn) { + if (!(this instanceof Awesome)) return new Awesome(fn); + Promise.call(this, fn); +} +Awesome.prototype = Object.create(Promise.prototype); +Awesome.prototype.constructor = Awesome; + +//Awesome extension +Awesome.prototype.spread = function (cb) { + return this.then(function (arr) { + return cb.apply(this, arr); + }) +}; +``` + + N.B. if you fail to set the prototype and constructor properly or fail to do Promise.call, things can fail in really subtle ways. + +### Wrap + + This is the nuclear option, for when you want to start from scratch. It ensures you won't be impacted by anyone who is extending the prototype (see below). + +```javascript +function Uber(fn) { + if (!(this instanceof Uber)) return new Uber(fn); + var _prom = new Promise(fn); + this.then = _prom.then; +} + +Uber.prototype.spread = function (cb) { + return this.then(function (arr) { + return cb.apply(this, arr); + }) +}; +``` + +### Extending the Prototype + + In general, you should never extend the prototype of this promise implimenation because your extensions could easily conflict with someone elses extensions. However, this organisation will host a library of extensions which do not conflict with each other, so you can safely enable any of those. If you think of an extension that we don't provide and you want to write it, submit an issue on this repository and (if I agree) I'll set you up with a repository and give you permission to commit to it. + +## License + + MIT diff --git a/promise/core.js b/promise/core.js new file mode 100644 index 0000000..2573ec5 --- /dev/null +++ b/promise/core.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports = require('./lib/core.js'); + +console.error('require("promise/core") is deprecated, use require("promise/lib/core") instead.'); diff --git a/promise/index.js b/promise/index.js new file mode 100644 index 0000000..99b6ff5 --- /dev/null +++ b/promise/index.js @@ -0,0 +1,6 @@ +'use strict'; + +module.exports = require('./lib/core.js') +require('./lib/done.js') +require('./lib/es6-extensions.js') +require('./lib/node-extensions.js') \ No newline at end of file diff --git a/promise/lib/core.js b/promise/lib/core.js new file mode 100644 index 0000000..a2eb033 --- /dev/null +++ b/promise/lib/core.js @@ -0,0 +1,105 @@ +'use strict'; + +var asap = require('asap') + +module.exports = Promise; +function Promise(fn) { + if (typeof this !== 'object') throw new TypeError('Promises must be constructed via new') + if (typeof fn !== 'function') throw new TypeError('not a function') + var state = null + var value = null + var deferreds = [] + var self = this + + this.then = function(onFulfilled, onRejected) { + return new self.constructor(function(resolve, reject) { + handle(new Handler(onFulfilled, onRejected, resolve, reject)) + }) + } + + function handle(deferred) { + if (state === null) { + deferreds.push(deferred) + return + } + asap(function() { + var cb = state ? deferred.onFulfilled : deferred.onRejected + if (cb === null) { + (state ? deferred.resolve : deferred.reject)(value) + return + } + var ret + try { + ret = cb(value) + } + catch (e) { + deferred.reject(e) + return + } + deferred.resolve(ret) + }) + } + + function resolve(newValue) { + try { //Promise Resolution Procedure: https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure + if (newValue === self) throw new TypeError('A promise cannot be resolved with itself.') + if (newValue && (typeof newValue === 'object' || typeof newValue === 'function')) { + var then = newValue.then + if (typeof then === 'function') { + doResolve(then.bind(newValue), resolve, reject) + return + } + } + state = true + value = newValue + finale() + } catch (e) { reject(e) } + } + + function reject(newValue) { + state = false + value = newValue + finale() + } + + function finale() { + for (var i = 0, len = deferreds.length; i < len; i++) + handle(deferreds[i]) + deferreds = null + } + + doResolve(fn, resolve, reject) +} + + +function Handler(onFulfilled, onRejected, resolve, reject){ + this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null + this.onRejected = typeof onRejected === 'function' ? onRejected : null + this.resolve = resolve + this.reject = reject +} + +/** + * Take a potentially misbehaving resolver function and make sure + * onFulfilled and onRejected are only called once. + * + * Makes no guarantees about asynchrony. + */ +function doResolve(fn, onFulfilled, onRejected) { + var done = false; + try { + fn(function (value) { + if (done) return + done = true + onFulfilled(value) + }, function (reason) { + if (done) return + done = true + onRejected(reason) + }) + } catch (ex) { + if (done) return + done = true + onRejected(ex) + } +} diff --git a/promise/lib/done.js b/promise/lib/done.js new file mode 100644 index 0000000..c2d326b --- /dev/null +++ b/promise/lib/done.js @@ -0,0 +1,14 @@ +'use strict'; + +var Promise = require('./core.js') +var asap = require('asap') + +module.exports = Promise +Promise.prototype.done = function (onFulfilled, onRejected) { + var self = arguments.length ? this.then.apply(this, arguments) : this + self.then(null, function (err) { + asap(function () { + throw err + }) + }) +} \ No newline at end of file diff --git a/promise/lib/es6-extensions.js b/promise/lib/es6-extensions.js new file mode 100644 index 0000000..a970f86 --- /dev/null +++ b/promise/lib/es6-extensions.js @@ -0,0 +1,108 @@ +'use strict'; + +//This file contains the ES6 extensions to the core Promises/A+ API + +var Promise = require('./core.js') +var asap = require('asap') + +module.exports = Promise + +/* Static Functions */ + +function ValuePromise(value) { + this.then = function (onFulfilled) { + if (typeof onFulfilled !== 'function') return this + return new Promise(function (resolve, reject) { + asap(function () { + try { + resolve(onFulfilled(value)) + } catch (ex) { + reject(ex); + } + }) + }) + } +} +ValuePromise.prototype = Promise.prototype + +var TRUE = new ValuePromise(true) +var FALSE = new ValuePromise(false) +var NULL = new ValuePromise(null) +var UNDEFINED = new ValuePromise(undefined) +var ZERO = new ValuePromise(0) +var EMPTYSTRING = new ValuePromise('') + +Promise.resolve = function (value) { + if (value instanceof Promise) return value + + if (value === null) return NULL + if (value === undefined) return UNDEFINED + if (value === true) return TRUE + if (value === false) return FALSE + if (value === 0) return ZERO + if (value === '') return EMPTYSTRING + + if (typeof value === 'object' || typeof value === 'function') { + try { + var then = value.then + if (typeof then === 'function') { + return new Promise(then.bind(value)) + } + } catch (ex) { + return new Promise(function (resolve, reject) { + reject(ex) + }) + } + } + + return new ValuePromise(value) +} + +Promise.all = function (arr) { + var args = Array.prototype.slice.call(arr) + + return new Promise(function (resolve, reject) { + if (args.length === 0) return resolve([]) + var remaining = args.length + function res(i, val) { + try { + if (val && (typeof val === 'object' || typeof val === 'function')) { + var then = val.then + if (typeof then === 'function') { + then.call(val, function (val) { res(i, val) }, reject) + return + } + } + args[i] = val + if (--remaining === 0) { + resolve(args); + } + } catch (ex) { + reject(ex) + } + } + for (var i = 0; i < args.length; i++) { + res(i, args[i]) + } + }) +} + +Promise.reject = function (value) { + return new Promise(function (resolve, reject) { + reject(value); + }); +} + +Promise.race = function (values) { + return new Promise(function (resolve, reject) { + values.forEach(function(value){ + Promise.resolve(value).then(resolve, reject); + }) + }); +} + +/* Prototype Methods */ + +Promise.prototype['catch'] = function (onRejected) { + return this.then(null, onRejected); +} diff --git a/promise/lib/node-extensions.js b/promise/lib/node-extensions.js new file mode 100644 index 0000000..9e74c5d --- /dev/null +++ b/promise/lib/node-extensions.js @@ -0,0 +1,60 @@ +'use strict'; + +//This file contains then/promise specific extensions that are only useful for node.js interop + +var Promise = require('./core.js') +var asap = require('asap') + +module.exports = Promise + +/* Static Functions */ + +Promise.denodeify = function (fn, argumentCount) { + argumentCount = argumentCount || Infinity + return function () { + var self = this + var args = Array.prototype.slice.call(arguments) + return new Promise(function (resolve, reject) { + while (args.length && args.length > argumentCount) { + args.pop() + } + args.push(function (err, res) { + if (err) reject(err) + else resolve(res) + }) + fn.apply(self, args) + }) + } +} +Promise.nodeify = function (fn) { + return function () { + var args = Array.prototype.slice.call(arguments) + var callback = typeof args[args.length - 1] === 'function' ? args.pop() : null + var ctx = this + try { + return fn.apply(this, arguments).nodeify(callback, ctx) + } catch (ex) { + if (callback === null || typeof callback == 'undefined') { + return new Promise(function (resolve, reject) { reject(ex) }) + } else { + asap(function () { + callback.call(ctx, ex) + }) + } + } + } +} + +Promise.prototype.nodeify = function (callback, ctx) { + if (typeof callback != 'function') return this + + this.then(function (value) { + asap(function () { + callback.call(ctx, null, value) + }) + }, function (err) { + asap(function () { + callback.call(ctx, err) + }) + }) +} diff --git a/promise/node_modules/asap/LICENSE.md b/promise/node_modules/asap/LICENSE.md new file mode 100644 index 0000000..5d98ad8 --- /dev/null +++ b/promise/node_modules/asap/LICENSE.md @@ -0,0 +1,20 @@ + +Copyright 2009–2013 Contributors. All rights reserved. +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to +deal in the Software without restriction, including without limitation the +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +IN THE SOFTWARE. + diff --git a/promise/node_modules/asap/README.md b/promise/node_modules/asap/README.md new file mode 100644 index 0000000..9a42759 --- /dev/null +++ b/promise/node_modules/asap/README.md @@ -0,0 +1,81 @@ + +# ASAP + +This `asap` CommonJS package contains a single `asap` module that +exports a single `asap` function that executes a function **as soon as +possible**. + +```javascript +asap(function () { + // ... +}); +``` + +More formally, ASAP provides a fast event queue that will execute tasks +until it is empty before yielding to the JavaScript engine's underlying +event-loop. When the event queue becomes non-empty, ASAP schedules a +flush event, preferring for that event to occur before the JavaScript +engine has an opportunity to perform IO tasks or rendering, thus making +the first task and subsequent tasks semantically indistinguishable. +ASAP uses a variety of techniques to preserve this invariant on +different versions of browsers and NodeJS. + +By design, ASAP can starve the event loop on the theory that, if there +is enough work to be done synchronously, albeit in separate events, long +enough to starve input or output, it is a strong indicator that the +program needs to push back on scheduling more work. + +Take care. ASAP can sustain infinite recursive calls indefinitely +without warning. This is behaviorally equivalent to an infinite loop. +It will not halt from a stack overflow, but it *will* chew through +memory (which is an oddity I cannot explain at this time). Just as with +infinite loops, you can monitor a Node process for this behavior with a +heart-beat signal. As with infinite loops, a very small amount of +caution goes a long way to avoiding problems. + +```javascript +function loop() { + asap(loop); +} +loop(); +``` + +ASAP is distinct from `setImmediate` in that it does not suffer the +overhead of returning a handle and being possible to cancel. For a +`setImmediate` shim, consider [setImmediate][]. + +[setImmediate]: https://github.com/noblejs/setimmediate + +If a task throws an exception, it will not interrupt the flushing of +high-priority tasks. The exception will be postponed to a later, +low-priority event to avoid slow-downs, when the underlying JavaScript +engine will treat it as it does any unhandled exception. + +## Heritage + +ASAP has been factored out of the [Q][] asynchronous promise library. +It originally had a naïve implementation in terms of `setTimeout`, but +[Malte Ubl][NonBlocking] provided an insight that `postMessage` might be +useful for creating a high-priority, no-delay event dispatch hack. +Since then, Internet Explorer proposed and implemented `setImmediate`. +Robert Kratić began contributing to Q by measuring the performance of +the internal implementation of `asap`, paying particular attention to +error recovery. Domenic, Robert, and I collectively settled on the +current strategy of unrolling the high-priority event queue internally +regardless of what strategy we used to dispatch the potentially +lower-priority flush event. Domenic went on to make ASAP cooperate with +NodeJS domains. + +[Q]: https://github.com/kriskowal/q +[NonBlocking]: http://www.nonblocking.io/2011/06/windownexttick.html + +For further reading, Nicholas Zakas provided a thorough article on [The +Case for setImmediate][NCZ]. + +[NCZ]: http://www.nczonline.net/blog/2013/07/09/the-case-for-setimmediate/ + +## License + +Copyright 2009-2013 by Contributors +MIT License (enclosed) + diff --git a/promise/node_modules/asap/asap.js b/promise/node_modules/asap/asap.js new file mode 100644 index 0000000..2f85516 --- /dev/null +++ b/promise/node_modules/asap/asap.js @@ -0,0 +1,113 @@ + +// Use the fastest possible means to execute a task in a future turn +// of the event loop. + +// linked list of tasks (single, with head node) +var head = {task: void 0, next: null}; +var tail = head; +var flushing = false; +var requestFlush = void 0; +var isNodeJS = false; + +function flush() { + /* jshint loopfunc: true */ + + while (head.next) { + head = head.next; + var task = head.task; + head.task = void 0; + var domain = head.domain; + + if (domain) { + head.domain = void 0; + domain.enter(); + } + + try { + task(); + + } catch (e) { + if (isNodeJS) { + // In node, uncaught exceptions are considered fatal errors. + // Re-throw them synchronously to interrupt flushing! + + // Ensure continuation if the uncaught exception is suppressed + // listening "uncaughtException" events (as domains does). + // Continue in next event to avoid tick recursion. + if (domain) { + domain.exit(); + } + setTimeout(flush, 0); + if (domain) { + domain.enter(); + } + + throw e; + + } else { + // In browsers, uncaught exceptions are not fatal. + // Re-throw them asynchronously to avoid slow-downs. + setTimeout(function() { + throw e; + }, 0); + } + } + + if (domain) { + domain.exit(); + } + } + + flushing = false; +} + +if (typeof process !== "undefined" && process.nextTick) { + // Node.js before 0.9. Note that some fake-Node environments, like the + // Mocha test runner, introduce a `process` global without a `nextTick`. + isNodeJS = true; + + requestFlush = function () { + process.nextTick(flush); + }; + +} else if (typeof setImmediate === "function") { + // In IE10, Node.js 0.9+, or https://github.com/NobleJS/setImmediate + if (typeof window !== "undefined") { + requestFlush = setImmediate.bind(window, flush); + } else { + requestFlush = function () { + setImmediate(flush); + }; + } + +} else if (typeof MessageChannel !== "undefined") { + // modern browsers + // http://www.nonblocking.io/2011/06/windownexttick.html + var channel = new MessageChannel(); + channel.port1.onmessage = flush; + requestFlush = function () { + channel.port2.postMessage(0); + }; + +} else { + // old browsers + requestFlush = function () { + setTimeout(flush, 0); + }; +} + +function asap(task) { + tail = tail.next = { + task: task, + domain: isNodeJS && process.domain, + next: null + }; + + if (!flushing) { + flushing = true; + requestFlush(); + } +}; + +module.exports = asap; + diff --git a/promise/node_modules/asap/package.json b/promise/node_modules/asap/package.json new file mode 100644 index 0000000..f388f8b --- /dev/null +++ b/promise/node_modules/asap/package.json @@ -0,0 +1,38 @@ +{ + "name": "asap", + "version": "1.0.0", + "description": "High-priority task queue for Node.js and browsers", + "keywords": [ + "event", + "task", + "queue" + ], + "licenses": [ + { + "type": "MIT", + "url": "https://github.com/kriskowal/asap/raw/master/LICENSE.md" + } + ], + "main": "asap", + "_id": "asap@1.0.0", + "dist": { + "shasum": "b2a45da5fdfa20b0496fc3768cc27c12fa916a7d", + "tarball": "http://registry.npmjs.org/asap/-/asap-1.0.0.tgz" + }, + "_from": "asap@~1.0.0", + "_npmVersion": "1.2.15", + "_npmUser": { + "name": "kriskowal", + "email": "kris.ko...@cixar.com" + }, + "maintainers": [ + { + "name": "kriskowal", + "email": "kris.ko...@cixar.com" + } + ], + "directories": {}, + "_shasum": "b2a45da5fdfa20b0496fc3768cc27c12fa916a7d", + "_resolved": "https://registry.npmjs.org/asap/-/asap-1.0.0.tgz", + "readme": "ERROR: No README data found!" +} diff --git a/promise/package.json b/promise/package.json new file mode 100644 index 0000000..f114384 --- /dev/null +++ b/promise/package.json @@ -0,0 +1,53 @@ +{ + "name": "promise", + "version": "6.0.1", + "description": "Bare bones Promises/A+ implementation", + "main": "index.js", + "scripts": { + "test": "mocha --timeout 200 --slow 99999", + "test-resolve": "mocha test/resolver-tests.js -R spec --timeout 200 --slow 999999", + "test-extensions": "mocha test/extensions-tests.js -R spec --timeout 200 --slow 999999" + }, + "repository": { + "type": "git", + "url": "https://github.com/then/promise.git" + }, + "author": { + "name": "ForbesLindesay" + }, + "license": "MIT", + "devDependencies": { + "promises-aplus-tests": "*", + "better-assert": "*", + "mocha": "*" + }, + "dependencies": { + "asap": "~1.0.0" + }, + "gitHead": "7710d6d277aeeee2ac5d712a50a386621fc718bb", + "bugs": { + "url": "https://github.com/then/promise/issues" + }, + "homepage": "https://github.com/then/promise", + "_id": "promise@6.0.1", + "_shasum": "d475cff81c083a27fe87ae19952b72c1a6936237", + "_from": "promise@", + "_npmVersion": "1.4.21", + "_npmUser": { + "name": "forbeslindesay", + "email": "for...@lindeay.co.uk" + }, + "maintainers": [ + { + "name": "forbeslindesay", + "email": "for...@lindesay.co.uk" + } + ], + "dist": { + "shasum": "d475cff81c083a27fe87ae19952b72c1a6936237", + "tarball": "http://registry.npmjs.org/promise/-/promise-6.0.1.tgz" + }, + "directories": {}, + "_resolved": "https://registry.npmjs.org/promise/-/promise-6.0.1.tgz", + "readme": "ERROR: No README data found!" +} diff --git a/promise/polyfill-done.js b/promise/polyfill-done.js new file mode 100644 index 0000000..c7e529a --- /dev/null +++ b/promise/polyfill-done.js @@ -0,0 +1,12 @@ +// should work in any browser without browserify + +if (typeof Promise.prototype.done !== 'function') { + Promise.prototype.done = function (onFulfilled, onRejected) { + var self = arguments.length ? this.then.apply(this, arguments) : this + self.then(null, function (err) { + setTimeout(function () { + throw err + }, 0) + }) + } +} \ No newline at end of file diff --git a/promise/polyfill.js b/promise/polyfill.js new file mode 100644 index 0000000..2178d4d --- /dev/null +++ b/promise/polyfill.js @@ -0,0 +1,10 @@ +// not "use strict" so we can declare global "Promise" + +var asap = require('asap'); + +if (typeof Promise === 'undefined') { + Promise = require('./lib/core.js') + require('./lib/es6-extensions.js') +} + +require('./polyfill-done.js'); -- To view, visit https://gerrit.wikimedia.org/r/183378 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ib6a6d8736b99aa6cc3edf1519f2b150e473dc48a Gerrit-PatchSet: 1 Gerrit-Project: wikimedia/fundraising/dash/node_modules Gerrit-Branch: master Gerrit-Owner: Ejegg <eeggles...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits