Skip to content

Commit

Permalink
further refactoring of .then, very pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
wizardwerdna committed Apr 20, 2013
1 parent ef06bca commit 365f968
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
22 changes: 10 additions & 12 deletions covenant.coffee
Expand Up @@ -9,16 +9,19 @@ root.Covenant = class Covenant

root.Core = class Core extends Covenant
constructor: (resolver=->)->
return (new Core(resolver)) unless this instanceof Covenant
return (new Core(resolver)) unless this instanceof Core
throw new TypeError("resolver must be a function") unless typeof resolver == 'function'
@_buildPromise(resolver)
_buildPromise: (resolver) ->
@state = new PendingState
@promise = @promise ? new Covenant(@then)
try
resolver.call(this, @resolve, @reject, this)
catch e
@reject e
then: (onFulfill, onReject) => new @constructor (resolve, reject) =>
@state.applyThen(onFulfill, onReject, resolve, reject)
@state.schedule (state) ->
state.then(onFulfill, onReject).then(resolve, reject)
fulfill: (value) => @state = @state.fulfill(value)
reject: (reason) => @state = @state.reject(reason)
resolve: (value) =>
Expand All @@ -44,32 +47,27 @@ class PendingState
constructor: -> @pendeds = []
fulfill: (value) -> new FulfilledState value, @pendeds
reject: (reason) -> new RejectedState reason, @pendeds
applyThen: (onFulfilled, onRejected, resolve, reject) ->
@pendeds.push (state) ->
state.then(onFulfilled, onRejected).then(resolve, reject)
schedule: (f)=> @pendeds.push f

class CompletedState
constructor: (pendeds=[]) ->
enqueue => pended(@) for pended in pendeds
fulfill: -> @
reject: -> @
then: (valueOrReason, onFulfilledOrRejected) ->
then: (onFulfilledOrRejected, valueOrReason) ->
try
if typeof onFulfilledOrRejected isnt 'function'
@
else
new FulfilledState onFulfilledOrRejected(valueOrReason)
catch e
new RejectedState e
applyThen: (onFulfilled, onRejected, resolve, reject) ->
enqueue => @.then(onFulfilled, onRejected).then(resolve, reject)
schedule: (f)=> enqueue => f(@)

class FulfilledState extends CompletedState
constructor: (@value, pended) -> super pended
then: (onFulfill, onReject) ->
super(@value, onFulfill)
then: (onFulfill, onReject) -> super(onFulfill, @value)

class RejectedState extends CompletedState
constructor: (@reason, pended) -> super pended
then: (onFulfill, onReject) ->
super(@reason, onReject)
then: (onFulfill, onReject) -> super(onReject, @reason)
43 changes: 27 additions & 16 deletions covenant.js
Expand Up @@ -27,8 +27,7 @@
__extends(Core, _super);

function Core(resolver) {
var _ref,
_this = this;
var _this = this;
if (resolver == null) {
resolver = function() {};
}
Expand All @@ -47,25 +46,32 @@
this.then = function(onFulfill, onReject) {
return Core.prototype.then.apply(_this, arguments);
};
if (!(this instanceof Covenant)) {
if (!(this instanceof Core)) {
return new Core(resolver);
}
if (typeof resolver !== 'function') {
throw new TypeError("resolver must be a function");
}
this._buildPromise(resolver);
}

Core.prototype._buildPromise = function(resolver) {
var _ref;
this.state = new PendingState;
this.promise = (_ref = this.promise) != null ? _ref : new Covenant(this.then);
try {
resolver.call(this, this.resolve, this.reject, this);
return resolver.call(this, this.resolve, this.reject, this);
} catch (e) {
this.reject(e);
return this.reject(e);
}
}
};

Core.prototype.then = function(onFulfill, onReject) {
var _this = this;
return new this.constructor(function(resolve, reject) {
return _this.state.applyThen(onFulfill, onReject, resolve, reject);
return _this.state.schedule(function(state) {
return state.then(onFulfill, onReject).then(resolve, reject);
});
});
};

Expand Down Expand Up @@ -120,6 +126,10 @@
PendingState = (function() {

function PendingState() {
var _this = this;
this.schedule = function(f) {
return PendingState.prototype.schedule.apply(_this, arguments);
};
this.pendeds = [];
}

Expand All @@ -131,10 +141,8 @@
return new RejectedState(reason, this.pendeds);
};

PendingState.prototype.applyThen = function(onFulfilled, onRejected, resolve, reject) {
return this.pendeds.push(function(state) {
return state.then(onFulfilled, onRejected).then(resolve, reject);
});
PendingState.prototype.schedule = function(f) {
return this.pendeds.push(f);
};

return PendingState;
Expand All @@ -148,6 +156,9 @@
if (pendeds == null) {
pendeds = [];
}
this.schedule = function(f) {
return CompletedState.prototype.schedule.apply(_this, arguments);
};
enqueue(function() {
var pended, _i, _len, _results;
_results = [];
Expand All @@ -167,7 +178,7 @@
return this;
};

CompletedState.prototype.then = function(valueOrReason, onFulfilledOrRejected) {
CompletedState.prototype.then = function(onFulfilledOrRejected, valueOrReason) {
try {
if (typeof onFulfilledOrRejected !== 'function') {
return this;
Expand All @@ -179,10 +190,10 @@
}
};

CompletedState.prototype.applyThen = function(onFulfilled, onRejected, resolve, reject) {
CompletedState.prototype.schedule = function(f) {
var _this = this;
return enqueue(function() {
return _this.then(onFulfilled, onRejected).then(resolve, reject);
return f(_this);
});
};

Expand All @@ -200,7 +211,7 @@
}

FulfilledState.prototype.then = function(onFulfill, onReject) {
return FulfilledState.__super__.then.call(this, this.value, onFulfill);
return FulfilledState.__super__.then.call(this, onFulfill, this.value);
};

return FulfilledState;
Expand All @@ -217,7 +228,7 @@
}

RejectedState.prototype.then = function(onFulfill, onReject) {
return RejectedState.__super__.then.call(this, this.reason, onReject);
return RejectedState.__super__.then.call(this, onReject, this.reason);
};

return RejectedState;
Expand Down
2 changes: 1 addition & 1 deletion covenant.map
Expand Up @@ -6,5 +6,5 @@
"covenant.coffee"
],
"names": [],
"mappings": ";;AAAA;CAAA,KAAA,oFAAA;KAAA;;oSAAA;CAAA;CAAA,CAAA,CAAQ,CAAR,IAAA;;AAGG,CAHH,CAEA,CAAe,CAAX,CACsB,CAAvB,CADH,EAE0B,CADvB,EAAA;CAC2C,CAAM,EAAjB,MAAA,CAAA;CAJnC,EAIyB;;CAJzB,CAMA,CAAsB,CAAlB,IAAJ;CACC;CAAc,EAAA,CAAA,cAAE;CAAS,EAAT,CAAA,EAAD,GAAM;CAArB,IAAc;;CAAd;;CAPD;;CAAA,CASA,CAAkB,CAAd;CACH;CAAA;;CAAc,EAAA,CAAA,IAAA,MAAC;CACZ,GAAA,MAAA;SAAA,GAAA;;GADqB,KAAT,CAAS;QACrB;CAAA,QAiByB;;CAjBzB;CAAA,QAYQ;;CAZR;CAAA,QAWO;;CAXP;CAAA,QAUQ;;CAVR;CAAA,CAQgB,OAAX;;CARL;AAAA,CAAA,GAAA,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