Skip to content

Instantly share code, notes, and snippets.

@aseemk
Created August 10, 2011 00:31
Show Gist options
  • Save aseemk/1135642 to your computer and use it in GitHub Desktop.
Save aseemk/1135642 to your computer and use it in GitHub Desktop.
Streamline bug? A piece of code is somehow called twice.
var __global = typeof global !== 'undefined' ? global : window;
function __cb(_, fn){ var ctx = __global.__context; return function(err, result){ __global.__context = ctx; if (err) return _(err); try { return fn(null, result); } catch (ex) { return __propagate(_, ex); } } }
function __future(fn, args, i){ var done, err, result; var cb = function(e, r){ done = true; err = e, result = r; }; args = Array.prototype.slice.call(args); args[i] = function(e, r){ cb(e, r); }; fn.apply(this, args); return function(_){ if (done) _.call(this, err, result); else cb = _.bind(this); } .bind(this); }
function __propagate(_, err){ try { _(err); } catch (ex) { __trap(ex); } }
function __trap(err){ if (err) { if (__global.__context && __global.__context.errorHandler) __global.__context.errorHandler(err); else console.error("UNCAUGHT EXCEPTION: " + err.message + "\n" + err.stack); } }
/* 1 */ this.register = function __1(user, thing, oldStatus, newStatus, _) {
if (!_) {
return __future.call(this, __1, arguments, 4);
}
;
var event, __this = this;
/* 3 */ if ((oldStatus == null)) {
/* 4 */ oldStatus = "";
}
;
/* 6 */ if ((newStatus == null)) {
/* 7 */ newStatus = "";
}
;
/* 9 */ console.log("GETTING CALLED");
/* 10 */ return __this.getRecent(user, thing, __cb(_, function(__0, __1) {
/* 10 */ event = __1;
return (function(__then) {
/* 11 */ if (event) {
return (function(__then) {
/* 12 */ if (((event.oldStatus === oldStatus) && (event.newStatus === newStatus))) {
/* 13 */ console.log("message for case 1 here");
/* 14 */ return event.save(__cb(_, function() {
return _(null);
/* 14 */ }), true);
}
else {
__then();
}
;
})(function() {
return (function(__then) {
/* 17 */ if (((event.oldStatus === newStatus) && (event.newStatus === oldStatus))) {
return (function(__then) {
/* 18 */ if (event.stats.numComments) {
/* 19 */ console.log("message for case 2 here");
__then();
}
else {
/* 21 */ console.log("message for case 3 here");
/* 22 */ return event["delete"](__cb(_, function() {
return _(null);
}));
}
;
})(__then);
}
else {
__then();
}
;
})(function() {
return (function(__then) {
/* 26 */ if (((event.newStatus === oldStatus) && (event.oldStatus !== newStatus))) {
return (function(__then) {
/* 27 */ if (event.stats.numComments) {
/* 28 */ console.log("message for case 4 here");
__then();
}
else {
/* 30 */ console.log("message for case 5 here");
/* 31 */ return event["delete"](__cb(_, function() {
/* 32 */ oldStatus = event.oldStatus;
__then();
}));
}
;
})(__then);
}
else {
__then();
}
;
})(__then);
});
});
}
else {
__then();
}
;
})(function() {
/* 36 */ event = new StatusChangeEvent({
/* 37 */ oldStatus: oldStatus,
/* 38 */ newStatus: newStatus
});
/* 40 */ console.log("SAVING!");
/* 41 */ return event.save(_);
});
}));
};
@register = (user, thing, oldStatus='', newStatus='', _) ->
console.log "GETTING CALLED"
# see if we already have an existing and recent status change event:
event = @getRecent user, thing, _
# if we do...
if event
# if it's for the same status change, don't create a new event;
# just update this one.
if event.oldStatus is oldStatus and event.newStatus is newStatus
console.log "message for case 1 here"
event.save _, true # force save
return
# if it's the exact opposite of this status change, simply delete
# the existing event -- but only if it has no comments.
if event.oldStatus is newStatus and event.newStatus is oldStatus
if event.stats.numComments
console.log "message for case 2 here"
else
console.log "message for case 3 here"
event.delete _
return
# otherwise, if this is a pivot of the recent event, cancel out
# the pivot by deleting the old event and modifying this one.
# again, only if the old event has no comments.
if event.newStatus is oldStatus and event.oldStatus isnt newStatus
if event.stats.numComments
console.log "message for case 4 here"
else
console.log "message for case 5 here"
event.delete _
oldStatus = event.oldStatus
# don't return; still need to create the new event!
# if we're here, we still need to create a new event, so do so:
event = new StatusChangeEvent
oldStatus: oldStatus
newStatus: newStatus
console.log "SAVING!"
event.save _
# void method
return
this.register = function(user, thing, oldStatus, newStatus, _) {
var event;
if (oldStatus == null) {
oldStatus = '';
}
if (newStatus == null) {
newStatus = '';
}
console.log("GETTING CALLED");
event = this.getRecent(user, thing, _);
if (event) {
if (event.oldStatus === oldStatus && event.newStatus === newStatus) {
console.log("message for case 1 here");
event.save(_, true);
return;
}
if (event.oldStatus === newStatus && event.newStatus === oldStatus) {
if (event.stats.numComments) {
console.log("message for case 2 here");
} else {
console.log("message for case 3 here");
event["delete"](_);
return;
}
}
if (event.newStatus === oldStatus && event.oldStatus !== newStatus) {
if (event.stats.numComments) {
console.log("message for case 4 here");
} else {
console.log("message for case 5 here");
event["delete"](_);
oldStatus = event.oldStatus;
}
}
}
event = new StatusChangeEvent({
oldStatus: oldStatus,
newStatus: newStatus
});
console.log("SAVING!");
event.save(_);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment