Skip to content

Commit 5f214e1

Browse files
Merge pull request then#84 from then/beat-bluebird
Beat bluebird
2 parents 99b25e8 + 53c26bd commit 5f214e1

File tree

11 files changed

+243
-196
lines changed

11 files changed

+243
-196
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
components
22
build
33
node_modules
4+
/lib

Readme.md

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This is a simple implementation of Promises. It is a super set of ES6 Promises
55

66
For detailed tutorials on its use, see www.promisejs.org
77

8+
**N.B.** This promise exposes internals via underscore (`_`) prefixed properties. If you use these, your code will break with each new release.
9+
810
[![travis][travis-image]][travis-url]
911
[![dep][dep-image]][dep-url]
1012
[![npm][npm-image]][npm-url]
@@ -170,55 +172,6 @@ function awesomeAPI(foo, bar, callback) {
170172

171173
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.
172174

173-
## Extending Promises
174-
175-
There are three options for extending the promises created by this library.
176-
177-
### Inheritance
178-
179-
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.
180-
181-
```javascript
182-
var Promise = require('promise');
183-
function Awesome(fn) {
184-
if (!(this instanceof Awesome)) return new Awesome(fn);
185-
Promise.call(this, fn);
186-
}
187-
Awesome.prototype = Object.create(Promise.prototype);
188-
Awesome.prototype.constructor = Awesome;
189-
190-
//Awesome extension
191-
Awesome.prototype.spread = function (cb) {
192-
return this.then(function (arr) {
193-
return cb.apply(this, arr);
194-
})
195-
};
196-
```
197-
198-
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.
199-
200-
### Wrap
201-
202-
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).
203-
204-
```javascript
205-
function Uber(fn) {
206-
if (!(this instanceof Uber)) return new Uber(fn);
207-
var _prom = new Promise(fn);
208-
this.then = _prom.then;
209-
}
210-
211-
Uber.prototype.spread = function (cb) {
212-
return this.then(function (arr) {
213-
return cb.apply(this, arr);
214-
})
215-
};
216-
```
217-
218-
### Extending the Prototype
219-
220-
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.
221-
222175
## License
223176

224177
MIT

build.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
var fs = require('fs');
4+
var rimraf = require('rimraf');
5+
var acorn = require('acorn');
6+
var walk = require('acorn/dist/walk');
7+
8+
var ids = [];
9+
var names = {};
10+
11+
function getIdFor(name) {
12+
if (name in names) return names[name];
13+
var id;
14+
do {
15+
id = '_' + Math.floor(Math.random() * 100);
16+
} while (ids.indexOf(id) !== -1)
17+
ids.push(id);
18+
names[name] = id;
19+
return id;
20+
}
21+
22+
function fixup(src) {
23+
var ast = acorn.parse(src);
24+
src = src.split('');
25+
walk.simple(ast, {
26+
MemberExpression: function (node) {
27+
if (node.computed) return;
28+
if (node.property.type !== 'Identifier') return;
29+
if (node.property.name[0] !== '_') return;
30+
replace(node.property, getIdFor(node.property.name));
31+
}
32+
});
33+
function source(node) {
34+
return src.slice(node.start, node.end).join('');
35+
}
36+
function replace(node, str) {
37+
for (var i = node.start; i < node.end; i++) {
38+
src[i] = '';
39+
}
40+
src[node.start] = str;
41+
}
42+
return src.join('');
43+
}
44+
rimraf.sync(__dirname + '/lib/');
45+
fs.mkdirSync(__dirname + '/lib/');
46+
fs.readdirSync(__dirname + '/src').forEach(function (filename) {
47+
var src = fs.readFileSync(__dirname + '/src/' + filename, 'utf8');
48+
var out = fixup(src);
49+
fs.writeFileSync(__dirname + '/lib/' + filename, out);
50+
});

lib/core.js

Lines changed: 0 additions & 128 deletions
This file was deleted.

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
"description": "Bare bones Promises/A+ implementation",
55
"main": "index.js",
66
"scripts": {
7-
"test": "mocha --timeout 200 --slow 99999 -R dot && npm run test-memory-leak",
7+
"pretest": "node build",
8+
"pretest-resolve": "node build",
9+
"pretest-extensions": "node build",
10+
"pretest-memory-leak": "node build",
11+
"test": "mocha --bail --timeout 200 --slow 99999 -R dot && npm run test-memory-leak",
812
"test-resolve": "mocha test/resolver-tests.js --timeout 200 --slow 999999",
913
"test-extensions": "mocha test/extensions-tests.js --timeout 200 --slow 999999",
1014
"test-memory-leak": "node --expose-gc test/memory-leak.js"
@@ -16,9 +20,11 @@
1620
"author": "ForbesLindesay",
1721
"license": "MIT",
1822
"devDependencies": {
19-
"promises-aplus-tests": "*",
23+
"acorn": "^1.0.1",
2024
"better-assert": "*",
21-
"mocha": "*"
25+
"mocha": "*",
26+
"promises-aplus-tests": "*",
27+
"rimraf": "^2.3.2"
2228
},
2329
"dependencies": {
2430
"asap": "~2.0.1"

0 commit comments

Comments
 (0)