Added naive scheduleDeferredCallback implementation for RN (#11362)
* Added naive scheduleDeferredCallback implementation for RN * Fixed ReactNative shim's expected @provideModule
Brian Vaughn committed
Oct 25, 2017 at 14:13 UTC
c86ae4b46fc9ff338dae785f529b6a78571abba7
3 files changed
+72
-9
packages/react-native-renderer/src/ReactNativeFiberRenderer.js
+5
-7
@@ -21,6 +21,7 @@ const deepFreezeAndThrowOnMutationInDev = require('deepFreezeAndThrowOnMutationI
21
const ReactNativeAttributePayload = require('./ReactNativeAttributePayload');
22
const ReactNativeComponentTree = require('./ReactNativeComponentTree');
23
const ReactNativeFiberHostComponent = require('./ReactNativeFiberHostComponent');
24
+const ReactNativeFrameScheduling = require('./ReactNativeFrameScheduling');
25
const ReactNativeTagHandles = require('./ReactNativeTagHandles');
26
const ReactNativeViewConfigRegistry = require('./ReactNativeViewConfigRegistry');
27
const {
@@ -157,6 +158,8 @@ const NativeRenderer = ReactFiberReconciler({
158
return instance;
159
},
160
161
+ now: ReactNativeFrameScheduling.now,
162
+
163
prepareForCommit(): void {
164
// Noop
165
},
@@ -176,12 +179,12 @@ const NativeRenderer = ReactFiberReconciler({
179
// Noop
180
},
181
182
+ scheduleDeferredCallback: ReactNativeFrameScheduling.scheduleDeferredCallback,
183
+
184
shouldDeprioritizeSubtree(type: string, props: Props): boolean {
185
return false;
186
},
187
183
- scheduleDeferredCallback: global.requestIdleCallback,
184
-
188
shouldSetTextContent(type: string, props: Props): boolean {
189
// TODO (bvaughn) Revisit this decision.
190
// Always returning false simplifies the createInstance() implementation,
@@ -194,11 +197,6 @@ const NativeRenderer = ReactFiberReconciler({
197
198
useSyncScheduling: true,
199
197
- now(): number {
198
- // TODO: Enable expiration by implementing this method.
199
- return 0;
200
- },
201
-
200
mutation: {
201
appendChild(
202
parentInstance: Instance,
packages/react-native-renderer/src/ReactNativeFrameScheduling.js
new
+65
@@ -0,0 +1,65 @@
1
+/**
2
+ * Copyright (c) 2013-present, Facebook, Inc.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow
8
+ */
9
+
10
+'use strict';
11
+
12
+import type {Deadline} from 'react-reconciler';
13
+
14
+const hasNativePerformanceNow =
15
+ typeof performance === 'object' && typeof performance.now === 'function';
16
+
17
+const now = hasNativePerformanceNow
18
+ ? () => performance.now()
19
+ : () => Date.now();
20
+
21
+type Callback = (deadline: Deadline) => void;
22
+
23
+let isCallbackScheduled: boolean = false;
24
+let scheduledCallback: Callback | null = null;
25
+let frameDeadline: number = 0;
26
+
27
+const frameDeadlineObject: Deadline = {
28
+ timeRemaining: () => frameDeadline - now(),
29
+};
30
+
31
+function setTimeoutCallback() {
32
+ isCallbackScheduled = false;
33
+
34
+ // TODO (bvaughn) Hard-coded 5ms unblocks initial async testing.
35
+ // React API probably changing to boolean rather than time remaining.
36
+ // Longer-term plan is to rewrite this using shared memory,
37
+ // And just return the value of the bit as the boolean.
38
+ frameDeadline = now() + 5;
39
+
40
+ const callback = scheduledCallback;
41
+ scheduledCallback = null;
42
+ if (callback !== null) {
43
+ callback(frameDeadlineObject);
44
+ }
45
+}
46
+
47
+// RN has a poor polyfill for requestIdleCallback so we aren't using it.
48
+// This implementation is only intended for short-term use anyway.
49
+// We also don't implement cancel functionality b'c Fiber doesn't currently need it.
50
+function scheduleDeferredCallback(callback: Callback): number {
51
+ // We assume only one callback is scheduled at a time b'c that's how Fiber works.
52
+ scheduledCallback = callback;
53
+
54
+ if (!isCallbackScheduled) {
55
+ isCallbackScheduled = true;
56
+ setTimeout(setTimeoutCallback, 1);
57
+ }
58
+
59
+ return 0;
60
+}
61
+
62
+module.exports = {
63
+ now,
64
+ scheduleDeferredCallback,
65
+};
scripts/rollup/shims/react-native/ReactNative.js
+2
-2
@@ -14,9 +14,9 @@ import type {ReactNativeType} from 'ReactNativeTypes';
14
let ReactNative;
15
16
if (__DEV__) {
17
- ReactNative = require('ReactNativeFiber-dev');
17
+ ReactNative = require('ReactNativeRenderer-dev');
18
} else {
19
- ReactNative = require('ReactNativeFiber-prod');
19
+ ReactNative = require('ReactNativeRenderer-prod');
20
}
21
22
module.exports = (ReactNative: ReactNativeType);