Skip to content

Commit 320d71d

Browse files
committed
Move performance.now polyfill to ReactDOMFrameScheduling
1 parent 010f2a4 commit 320d71d

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
@@ -532,10 +532,7 @@ const ARTRenderer = ReactFiberReconciler({
532532
);
533533
},
534534

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

540537
useSyncScheduling: true,
541538
});

src/renderers/dom/fiber/ReactDOMFiberEntry.js

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

162-
// TODO: Better polyfill
163-
let now;
164-
if (
165-
typeof window !== 'undefined' &&
166-
window.performance &&
167-
typeof window.performance.now === 'function'
168-
) {
169-
now = function() {
170-
return performance.now();
171-
};
172-
} else {
173-
now = function() {
174-
return Date.now();
175-
};
176-
}
177-
178162
var DOMRenderer = ReactFiberReconciler({
179163
getRootHostContext(rootContainerInstance: Container): HostContext {
180164
let type;
@@ -450,7 +434,7 @@ var DOMRenderer = ReactFiberReconciler({
450434
}
451435
},
452436

453-
now,
437+
now: ReactDOMFrameScheduling.now,
454438

455439
canHydrateInstance(
456440
instance: Instance | TextInstance,

src/renderers/shared/ReactDOMFrameScheduling.js

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

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

@@ -68,17 +75,11 @@ if (!ExecutionEnvironment.canUseDOM) {
6875
var activeFrameTime = 33;
6976

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

8485
// We use the postMessage trick to defer idle work until after the repaint.
@@ -153,4 +154,5 @@ if (!ExecutionEnvironment.canUseDOM) {
153154
rIC = requestIdleCallback;
154155
}
155156

157+
exports.now = now;
156158
exports.rIC = rIC;

0 commit comments

Comments
 (0)