Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(v1.0) Inspector communication channel #361

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 125 additions & 3 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,39 @@ function deprecate(callback, name, alternative) {
};
}

// debugger

var debugCounter = 0;
var debugBuffer = [];
var debugIndex = 0;
function debug() {
var args = array_slice(arguments);
args.push(new Date().toISOString());
if (inspector) {
inspector.postMessage(args);
} else {
debugBuffer[debugIndex] = args;
debugIndex++;
if (debugIndex >= 300) {
debugIndex = 0;
}
}
}

function inspectDebug() {
if (debugIndex === debugBuffer.length) {
debugIndex = 0;
}
for (var index = 0; index < 300; index++, debugIndex++) {
if (debugIndex > 300) {
debugIndex = 0;
}
if (index in debugBuffer) {
inspector.postMessage(debugBuffer[debugIndex]);
}
}
}

// end of shims
// beginning of real work

Expand Down Expand Up @@ -549,6 +582,10 @@ function defer() {
}
}

var debugId = debugCounter++;
promise.debugId = debugId;
debug("defer", debugId, filterStackString(promise.stack || ""));

// NOTE: we do the checks for `resolvedPromise` in each method, instead of
// consolidating them into `become`, since otherwise we'd create new
// promises with the lines `become(whatever(value))`. See e.g. GH-252.
Expand All @@ -563,6 +600,8 @@ function defer() {
});
}, void 0);

debug("resolve", promise.debugId, newPromise.debugId);

messages = void 0;
progressListeners = void 0;
}
Expand Down Expand Up @@ -595,6 +634,14 @@ function defer() {
return;
}

try {
debug("progress", debugId, progress);
} catch (error) {
// the progress object might not be serializable.
// we tried, oh well
debug("progress", debugId);
}

array_reduce(progressListeners, function (undefined, progressListener) {
nextTick(function () {
progressListener(progress);
Expand Down Expand Up @@ -971,6 +1018,7 @@ var unhandledReasonsDisplayed = false;
var trackUnhandledRejections = true;
function displayUnhandledReasons() {
if (
!inspector &&
!unhandledReasonsDisplayed &&
typeof window !== "undefined" &&
!window.Touch &&
Expand Down Expand Up @@ -1008,6 +1056,7 @@ function resetUnhandledRejections() {
}

function trackRejection(promise, reason) {

if (!trackUnhandledRejections) {
return;
}
Expand All @@ -1022,6 +1071,7 @@ function trackRejection(promise, reason) {
}

function untrackRejection(promise) {

if (!trackUnhandledRejections) {
return;
}
Expand Down Expand Up @@ -1050,6 +1100,46 @@ Q.stopUnhandledRejectionTracking = function () {

resetUnhandledRejections();

function addPortListener(port, handlers) {
port.addEventListener("message", function (event) {
var message = event.data;
if (Array.isArray(message) && message.length) {
var type = message[0];
var handler = handlers[type];
if (handler) {
handler.apply(event, message.slice(1));
}
}
});
}

if (typeof window !== "undefined" && typeof window.postMessage === "function") {

var inspector;

addPortListener(window, {
"can-watch-promises": function () {

Q.longStackSupport = true;

var channel = new MessageChannel();
var port = channel.port1;
window.postMessage(["promise-channel"], [channel.port2], window.location.origin);
addPortListener(port, {
"watching-promises": function () {
inspector = port;
inspectDebug();
}
});
port.start();

}
});

window.postMessage(["can-show-promises"], window.location.origin);

}

//// END UNHANDLED REJECTION TRACKING

/**
Expand All @@ -1058,20 +1148,39 @@ resetUnhandledRejections();
*/
Q.reject = reject;
function reject(reason) {
var message, stack;
if (reason && typeof reason === "object") {
if (reason.message) {
message = reason.message;
} else {
message = "" + reason;
}
if ("stack" in reason) {
stack = reason.stack;
}
} else {
message = "" + reason;
}
var rejection = Promise({
"when": function (rejected) {
// note that the error has been handled
if (rejected) {
untrackRejection(this);
}
return rejected ? rejected(reason) : this;
var resolution = Q(rejected ? rejected(reason) : this);
debug("handle", debugId, resolution.debugId);
return resolution;
}
}, function fallback() {
return this;
}, function inspect() {
return { state: "rejected", reason: reason };
});

var debugId = debugCounter++;
rejection.debugId = debugId;
debug("reject", debugId, message, filterStackString(stack || ""));

// Note that the reason has not been handled.
trackRejection(rejection, reason);

Expand All @@ -1084,7 +1193,7 @@ function reject(reason) {
*/
Q.fulfill = fulfill;
function fulfill(value) {
return Promise({
var promise = Promise({
"when": function () {
return value;
},
Expand Down Expand Up @@ -1115,6 +1224,10 @@ function fulfill(value) {
}, void 0, function inspect() {
return { state: "fulfilled", value: value };
});
var debugId = debugCounter++;
promise.debugId = debugId;
debug("fulfill", debugId);
return promise;
}

/**
Expand Down Expand Up @@ -1509,13 +1622,22 @@ function all(promises) {
promise,
function (value) {
promises[index] = value;
deferred.notify({
index: index,
value: 1,
length: promises.length
});
if (--countDown === 0) {
deferred.resolve(promises);
}
},
deferred.reject,
function (progress) {
deferred.notify({ index: index, value: progress });
deferred.notify({
index: index,
value: progress,
length: promises.length
});
}
);
}
Expand Down
8 changes: 5 additions & 3 deletions spec/q-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1158,9 +1158,11 @@ describe("all", function () {
return Q.all([deferred1.promise, deferred2.promise]).then(
function () {
expect(progressValues).toEqual([
{ index: 0, value: "a" },
{ index: 1, value: "b" },
{ index: 0, value: "c" }
{ index: 0, value: "a", length: 2 },
{ index: 1, value: "b", length: 2 },
{ index: 1, value: 1, length: 2 },
{ index: 0, value: "c", length: 2 },
{ index: 0, value: 1, length: 2 },
]);
},
undefined,
Expand Down