Skip to content

Commit

Permalink
fix: add polyfill for requestIdleCallback in reearth/core (#537)
Browse files Browse the repository at this point in the history
Co-authored-by: Piyush Chauhan <lemon@Piyushs-MacBook-Pro.local>
  • Loading branch information
pyshx and Piyush Chauhan committed Mar 14, 2023
1 parent 3653e25 commit c4722fe
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/core/Map/Layer/hooks.ts
Expand Up @@ -104,10 +104,26 @@ export default function useHooks({
prevForceUpdatableData.current = forceUpdatableData;
}, [layer, forceUpdateFeatures]);

// idleCallback is still experimental in ios
const ctx = typeof window !== "undefined" ? window : global;
const requestIdleCallbackShim = (
callback: (arg0: { didTimeout: boolean; timeRemaining: () => number }) => void,
) => {
const start = Date.now();
return ctx.setTimeout(function () {
callback({
didTimeout: false,
timeRemaining: function () {
return Math.max(0, 12 - (Date.now() - start));
},
});
});
};
const requestIdleCallback = ctx.requestIdleCallback || requestIdleCallbackShim;
// Clear expression cache if layer is unmounted
useEffect(
() => () => {
window.requestIdleCallback(() => {
requestIdleCallback(() => {
// This is a little heavy task, and not critical for main functionality, so we can run this at idle time.
computedLayer?.originalFeatures.forEach(f => {
clearAllExpressionCaches(
Expand Down

0 comments on commit c4722fe

Please sign in to comment.