Skip to content

Commit 240fb97

Browse files
committed
Move performance.now polyfill to ReactDOMFrameScheduling
1 parent 72290f8 commit 240fb97

File tree

3 files changed

+15
-32
lines changed

3 files changed

+15
-32
lines changed

src/renderers/art/ReactARTFiberEntry.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,10 +534,7 @@ const ARTRenderer = ReactFiberReconciler({
534534
);
535535
},
536536

537-
now(): number {
538-
// TODO: Enable expiration by implementing this method.
539-
return 0;
540-
},
537+
now: ReactDOMFrameScheduling.now,
541538

542539
useSyncScheduling: true,
543540
});

src/renderers/dom/fiber/ReactDOMFiberEntry.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -165,22 +165,6 @@ function shouldAutoFocusHostComponent(type: string, props: Props): boolean {
165165
return false;
166166
}
167167

168-
// TODO: Better polyfill
169-
let now;
170-
if (
171-
typeof window !== 'undefined' &&
172-
window.performance &&
173-
typeof window.performance.now === 'function'
174-
) {
175-
now = function() {
176-
return performance.now();
177-
};
178-
} else {
179-
now = function() {
180-
return Date.now();
181-
};
182-
}
183-
184168
var DOMRenderer = ReactFiberReconciler({
185169
getRootHostContext(rootContainerInstance: Container): HostContext {
186170
let type;
@@ -449,7 +433,7 @@ var DOMRenderer = ReactFiberReconciler({
449433
}
450434
},
451435

452-
now,
436+
now: ReactDOMFrameScheduling.now,
453437

454438
canHydrateInstance(
455439
instance: Instance | TextInstance,

src/renderers/shared/ReactDOMFrameScheduling.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ if (__DEV__) {
3939
}
4040
}
4141

42+
let now: () => number;
43+
if (typeof performance === 'object' && typeof performance.now === 'function') {
44+
now = performance.now.bind(performance);
45+
} else {
46+
now = Date.now;
47+
}
48+
4249
// TODO: There's no way to cancel, because Fiber doesn't atm.
4350
let rIC: (callback: (deadline: Deadline) => void) => number;
4451

@@ -70,17 +77,11 @@ if (!ExecutionEnvironment.canUseDOM) {
7077
var activeFrameTime = 33;
7178

7279
var frameDeadlineObject = {
73-
timeRemaining: typeof performance === 'object' &&
74-
typeof performance.now === 'function'
75-
? function() {
76-
// We assume that if we have a performance timer that the rAF callback
77-
// gets a performance timer value. Not sure if this is always true.
78-
return frameDeadline - performance.now();
79-
}
80-
: function() {
81-
// As a fallback we use Date.now.
82-
return frameDeadline - Date.now();
83-
},
80+
timeRemaining() {
81+
// We assume that if we have a performance timer that the rAF callback
82+
// gets a performance timer value. Not sure if this is always true.
83+
return frameDeadline - now();
84+
},
8485
};
8586

8687
// We use the postMessage trick to defer idle work until after the repaint.
@@ -155,4 +156,5 @@ if (!ExecutionEnvironment.canUseDOM) {
155156
rIC = requestIdleCallback;
156157
}
157158

159+
exports.now = now;
158160
exports.rIC = rIC;

0 commit comments

Comments
 (0)