From 8c75c214d7ed22692d901b4d798b7165fa5fae71 Mon Sep 17 00:00:00 2001 From: jgw96 Date: Tue, 7 Mar 2017 16:12:02 -0600 Subject: [PATCH] chore(util): add requestIdleCallback polyfill --- src/util/test/util.spec.ts | 8 ++++++++ src/util/util.ts | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/src/util/test/util.spec.ts b/src/util/test/util.spec.ts index 91def51a34..301086d7b8 100644 --- a/src/util/test/util.spec.ts +++ b/src/util/test/util.spec.ts @@ -340,4 +340,12 @@ describe('util', () => { }); + describe('requestIdleCallback polyfill', () => { + + it('should return a value lazily', () => { + expect(util.requestIonicCallback(() => { return 1; })).toEqual(1); + }); + + }); + }); diff --git a/src/util/util.ts b/src/util/util.ts index b2fb6488e8..070665399a 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -179,6 +179,15 @@ function _assert(actual: any, reason: string) { } } +/** @private */ +export function requestIonicCallback(functionToLazy: any) { + if ('requestIdleCallback' in window) { + return (window as any).requestIdleCallback(functionToLazy); + } else { + return setTimeout(functionToLazy, 500); + } +} + /** @private */ export { _assert as assert};