From cf246b4bf04c998b9314be3f8ae17f605f9d447d Mon Sep 17 00:00:00 2001 From: Julian Gonggrijp Date: Tue, 10 Dec 2024 15:32:45 +0100 Subject: [PATCH 1/2] Sophisticate the fastTimeout conditional export (close #2) --- src/fastTimeout.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/fastTimeout.js b/src/fastTimeout.js index 908e925..6659922 100644 --- a/src/fastTimeout.js +++ b/src/fastTimeout.js @@ -40,5 +40,6 @@ function enable() { } var hasSetImmediate = (typeof setImmediate === 'function'); +var hasPostMessage = (typeof self.postMessage === 'function'); -export default hasSetImmediate ? setImmediate : enable(); +export default hasPostMessage ? enable() : hasSetImmediate ? setImmediate : _.defer; From c0dafe9f1c67b549dad3660e1d95d8665121ee16 Mon Sep 17 00:00:00 2001 From: Julian Gonggrijp Date: Tue, 10 Dec 2024 15:37:09 +0100 Subject: [PATCH 2/2] Document the new conditional fastTimeout export (#2) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2ca3e6f..ddfbee0 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ Alternatively, if you are directly using the library as a browser embed: ## Compatibility -While `fastTimeout` is based on `postMessage`, it automatically falls back to `setImmediate` in environments where that function is defined. Hence, you can transparently use it in Node.js as well. +While `fastTimeout` is based on `postMessage`, it automatically falls back to `setImmediate` in environments where that function is defined. Hence, you can transparently use it in Node.js as well. In environments where neither `postMessage` nor `setImmediate` is defined, it falls back to plain `setTimeout`; you get a performance degradation in that case, but at least your code will still work. ## Reference