Created
August 30, 2014 03:49
-
-
Save subtleGradient/2c8d6198d7ba6099766e to your computer and use it in GitHub Desktop.
Async callstacks for your own custom event system or RPC or whatever
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Created by Thomas Aylott on 2014-08-29 | |
| * Copyright 2014 Facebook, Inc. | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| * | |
| * | |
| * Hack to get async callstacks working with custom messaging systems | |
| * http://www.html5rocks.com/en/tutorials/developertools/async-call-stack/ | |
| * | |
| * Chrome supports async callstacks for native async code such as setTimeout and XMLHttpRequest. | |
| * However, the browser can't understand custom messaging systems such as JSON-RPC over WebSockets. | |
| * | |
| * This code takes advantage of the browser's native support for async callstacks in setInterval. | |
| * Pass in a normal function and you'll get an async compatible function in return. | |
| * | |
| * An interval will periodically check to see if the async function has been called. | |
| * Once it does, it'll indirectly trigger your original function. | |
| * Since your original function is called from inside the interval callback, | |
| * the browser connects the async callstack from your original function | |
| * through __wrapCallbackForDebugging and its callstack. | |
| */ | |
| (function() { | |
| // Abort unless this is a browser | |
| if (typeof window === 'undefined' || window.__proto__ == null) { | |
| return; | |
| } | |
| // This is the safest way to get a reference to the browser's native timer implementation | |
| var setInterval = window.__proto__.setInterval && window.__proto__.setInterval.bind(window); | |
| var clearInterval = window.__proto__.clearInterval && window.__proto__.clearInterval.bind(window); | |
| if (!(setInterval && clearInterval)) { | |
| return; | |
| } | |
| /*global*/__wrapCallbackForDebugging = function(originalCallback, displayName) { | |
| if (!originalCallback) { | |
| return originalCallback; | |
| } | |
| var callbackArgs, intervalID; | |
| var intervalCallback = function() { | |
| if (!callbackArgs) { | |
| // 2. The asyncCallback has not yet fired, keep checking periodically | |
| return; | |
| } | |
| // 4. The asyncCallback has fired, cleanup | |
| clearInterval(intervalID); | |
| var args = callbackArgs; | |
| callbackArgs = null; | |
| // 5. Call the original callback | |
| // If something triggers a breakpoint in the originalCallback, | |
| // the callstack will include the callstack from #1 instead of ending at #3 | |
| originalCallback.apply(null, args); | |
| }; | |
| intervalCallback.displayName = displayName; | |
| // 1. Any breakpoints in the originalCallback will trace back here | |
| intervalID = setInterval(intervalCallback, 0); | |
| return function asyncCallback() { | |
| // 3. Indirectly trigger the intervalCallback to call the originalCallback | |
| callbackArgs = arguments; | |
| }; | |
| }; | |
| }()); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DO NOT USE THIS IN PRODUCTION CODE!!!1!
This is for development/debugging purposes only!