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};