Skip to content

Commit 9a110eb

Browse files
authored
Cleaned up 'schedule' API wrt interactions and subscriber ref: (facebook#13561)
* Removed 'private' ref methods from UMD forwarding API * Replaced getters with exported constants since they were no longer referenced for UMD forwarding
1 parent fb88fd9 commit 9a110eb

File tree

11 files changed

+50
-95
lines changed

11 files changed

+50
-95
lines changed

fixtures/tracking/script.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ function checkSchedulerTrackingAPI() {
4848
runTest(document.getElementById('checkSchedulerTrackingAPI'), () => {
4949
if (
5050
typeof ScheduleTracking === 'undefined' ||
51-
typeof ScheduleTracking.__getInteractionsRef !== 'function' ||
52-
typeof ScheduleTracking.__getSubscriberRef !== 'function' ||
5351
typeof ScheduleTracking.unstable_clear !== 'function' ||
5452
typeof ScheduleTracking.unstable_getCurrent !== 'function' ||
5553
typeof ScheduleTracking.unstable_getThreadID !== 'function' ||
@@ -62,7 +60,7 @@ function checkSchedulerTrackingAPI() {
6260
try {
6361
let interactionsSet;
6462
ScheduleTracking.unstable_track('test', 123, () => {
65-
interactionsSet = ScheduleTracking.__getInteractionsRef().current;
63+
interactionsSet = ScheduleTracking.unstable_getCurrent();
6664
});
6765
if (interactionsSet.size !== 1) {
6866
throw null;

packages/react-reconciler/src/ReactFiberScheduler.js

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@
1010
import type {Fiber} from './ReactFiber';
1111
import type {Batch, FiberRoot} from './ReactFiberRoot';
1212
import type {ExpirationTime} from './ReactFiberExpirationTime';
13-
import type {
14-
Interaction,
15-
InteractionsRef,
16-
SubscriberRef,
17-
} from 'schedule/src/Tracking';
13+
import type {Interaction} from 'schedule/src/Tracking';
1814

19-
import {__getInteractionsRef, __getSubscriberRef} from 'schedule/tracking';
15+
import {__interactionsRef, __subscriberRef} from 'schedule/tracking';
2016
import {
2117
invokeGuardedCallback,
2218
hasCaughtError,
@@ -166,13 +162,6 @@ export type Thenable = {
166162

167163
const {ReactCurrentOwner} = ReactSharedInternals;
168164

169-
let interactionsRef: InteractionsRef = (null: any);
170-
let subscriberRef: SubscriberRef = (null: any);
171-
if (enableSchedulerTracking) {
172-
interactionsRef = __getInteractionsRef();
173-
subscriberRef = __getSubscriberRef();
174-
}
175-
176165
let didWarnAboutStateTransition;
177166
let didWarnSetStateChildContext;
178167
let warnAboutUpdateOnUnmounted;
@@ -570,8 +559,8 @@ function commitRoot(root: FiberRoot, finishedWork: Fiber): void {
570559
if (enableSchedulerTracking) {
571560
// Restore any pending interactions at this point,
572561
// So that cascading work triggered during the render phase will be accounted for.
573-
prevInteractions = interactionsRef.current;
574-
interactionsRef.current = root.memoizedInteractions;
562+
prevInteractions = __interactionsRef.current;
563+
__interactionsRef.current = root.memoizedInteractions;
575564

576565
// We are potentially finished with the current batch of interactions.
577566
// So we should clear them out of the pending interaction map.
@@ -766,12 +755,12 @@ function commitRoot(root: FiberRoot, finishedWork: Fiber): void {
766755
onCommit(root, earliestRemainingTimeAfterCommit);
767756

768757
if (enableSchedulerTracking) {
769-
interactionsRef.current = prevInteractions;
758+
__interactionsRef.current = prevInteractions;
770759

771760
let subscriber;
772761

773762
try {
774-
subscriber = subscriberRef.current;
763+
subscriber = __subscriberRef.current;
775764
if (subscriber !== null && root.memoizedInteractions.size > 0) {
776765
const threadID = computeThreadID(
777766
committedExpirationTime,
@@ -1176,8 +1165,8 @@ function renderRoot(
11761165
if (enableSchedulerTracking) {
11771166
// We're about to start new tracked work.
11781167
// Restore pending interactions so cascading work triggered during the render phase will be accounted for.
1179-
prevInteractions = interactionsRef.current;
1180-
interactionsRef.current = root.memoizedInteractions;
1168+
prevInteractions = __interactionsRef.current;
1169+
__interactionsRef.current = root.memoizedInteractions;
11811170
}
11821171

11831172
// Check if we're starting from a fresh stack, or if we're resuming from
@@ -1220,7 +1209,7 @@ function renderRoot(
12201209
root.memoizedInteractions = interactions;
12211210

12221211
if (interactions.size > 0) {
1223-
const subscriber = subscriberRef.current;
1212+
const subscriber = __subscriberRef.current;
12241213
if (subscriber !== null) {
12251214
const threadID = computeThreadID(
12261215
expirationTime,
@@ -1305,7 +1294,7 @@ function renderRoot(
13051294

13061295
if (enableSchedulerTracking) {
13071296
// Tracked work is done for now; restore the previous interactions.
1308-
interactionsRef.current = prevInteractions;
1297+
__interactionsRef.current = prevInteractions;
13091298
}
13101299

13111300
// We're done performing work. Time to clean up.
@@ -1614,13 +1603,13 @@ function retrySuspendedRoot(
16141603
if (rootExpirationTime !== NoWork) {
16151604
if (enableSchedulerTracking) {
16161605
// Restore previous interactions so that new work is associated with them.
1617-
let prevInteractions = interactionsRef.current;
1618-
interactionsRef.current = root.memoizedInteractions;
1606+
let prevInteractions = __interactionsRef.current;
1607+
__interactionsRef.current = root.memoizedInteractions;
16191608
// Because suspense timeouts do not decrement the interaction count,
16201609
// Continued suspense work should also not increment the count.
16211610
storeInteractionsForExpirationTime(root, rootExpirationTime, false);
16221611
requestWork(root, rootExpirationTime);
1623-
interactionsRef.current = prevInteractions;
1612+
__interactionsRef.current = prevInteractions;
16241613
} else {
16251614
requestWork(root, rootExpirationTime);
16261615
}
@@ -1687,7 +1676,7 @@ function storeInteractionsForExpirationTime(
16871676
return;
16881677
}
16891678

1690-
const interactions = interactionsRef.current;
1679+
const interactions = __interactionsRef.current;
16911680
if (interactions.size > 0) {
16921681
const pendingInteractions = root.pendingInteractionMap.get(expirationTime);
16931682
if (pendingInteractions != null) {
@@ -1710,7 +1699,7 @@ function storeInteractionsForExpirationTime(
17101699
}
17111700
}
17121701

1713-
const subscriber = subscriberRef.current;
1702+
const subscriber = __subscriberRef.current;
17141703
if (subscriber !== null) {
17151704
const threadID = computeThreadID(
17161705
expirationTime,

packages/react/src/ReactSharedInternals.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
unstable_scheduleWork,
1313
} from 'schedule';
1414
import {
15-
__getInteractionsRef,
16-
__getSubscriberRef,
15+
__interactionsRef,
16+
__subscriberRef,
1717
unstable_clear,
1818
unstable_getCurrent,
1919
unstable_getThreadID,
@@ -44,8 +44,8 @@ if (__UMD__) {
4444
unstable_scheduleWork,
4545
},
4646
ScheduleTracking: {
47-
__getInteractionsRef,
48-
__getSubscriberRef,
47+
__interactionsRef,
48+
__subscriberRef,
4949
unstable_clear,
5050
unstable_getCurrent,
5151
unstable_getThreadID,

packages/schedule/npm/umd/schedule-tracking.development.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@
1616
? define(['react'], factory) // eslint-disable-line no-undef
1717
: (global.ScheduleTracking = factory(global));
1818
})(this, function(global) {
19-
function __getInteractionsRef() {
20-
return global.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ScheduleTracking.__getInteractionsRef.apply(
21-
this,
22-
arguments
23-
);
24-
}
25-
26-
function __getSubscriberRef() {
27-
return global.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ScheduleTracking.__getSubscriberRef.apply(
28-
this,
29-
arguments
30-
);
31-
}
32-
3319
function unstable_clear() {
3420
return global.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ScheduleTracking.unstable_clear.apply(
3521
this,
@@ -82,8 +68,6 @@
8268
}
8369

8470
return Object.freeze({
85-
__getInteractionsRef: __getInteractionsRef,
86-
__getSubscriberRef: __getSubscriberRef,
8771
unstable_clear: unstable_clear,
8872
unstable_getCurrent: unstable_getCurrent,
8973
unstable_getThreadID: unstable_getThreadID,

packages/schedule/npm/umd/schedule-tracking.production.min.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@
1616
? define(['react'], factory) // eslint-disable-line no-undef
1717
: (global.ScheduleTracking = factory(global));
1818
})(this, function(global) {
19-
function __getInteractionsRef() {
20-
return global.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ScheduleTracking.__getInteractionsRef.apply(
21-
this,
22-
arguments
23-
);
24-
}
25-
26-
function __getSubscriberRef() {
27-
return global.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ScheduleTracking.__getSubscriberRef.apply(
28-
this,
29-
arguments
30-
);
31-
}
32-
3319
function unstable_clear() {
3420
return global.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ScheduleTracking.unstable_clear.apply(
3521
this,
@@ -82,8 +68,6 @@
8268
}
8369

8470
return Object.freeze({
85-
__getInteractionsRef: __getInteractionsRef,
86-
__getSubscriberRef: __getSubscriberRef,
8771
unstable_clear: unstable_clear,
8872
unstable_getCurrent: unstable_getCurrent,
8973
unstable_getThreadID: unstable_getThreadID,

packages/schedule/src/Tracking.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,7 @@ if (enableSchedulerTracking) {
7979
};
8080
}
8181

82-
// These values are exported for libraries with advanced use cases (i.e. React).
83-
// They should not typically be accessed directly.
84-
export function __getInteractionsRef(): InteractionsRef {
85-
return interactionsRef;
86-
}
87-
88-
export function __getSubscriberRef(): SubscriberRef {
89-
return subscriberRef;
90-
}
82+
export {interactionsRef as __interactionsRef, subscriberRef as __subscriberRef};
9183

9284
export function unstable_clear(callback: Function): any {
9385
if (!enableSchedulerTracking) {

packages/schedule/src/TrackingSubscriptions.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77
* @flow
88
*/
99

10-
import type {Interaction, Subscriber, SubscriberRef} from './Tracking';
10+
import type {Interaction, Subscriber} from './Tracking';
1111

1212
import {enableSchedulerTracking} from 'shared/ReactFeatureFlags';
13-
import {__getSubscriberRef} from 'schedule/tracking';
13+
import {__subscriberRef} from 'schedule/tracking';
1414

15-
let subscriberRef: SubscriberRef = (null: any);
1615
let subscribers: Set<Subscriber> = (null: any);
1716
if (enableSchedulerTracking) {
18-
subscriberRef = __getSubscriberRef();
1917
subscribers = new Set();
2018
}
2119

@@ -24,7 +22,7 @@ export function unstable_subscribe(subscriber: Subscriber): void {
2422
subscribers.add(subscriber);
2523

2624
if (subscribers.size === 1) {
27-
subscriberRef.current = {
25+
__subscriberRef.current = {
2826
onInteractionScheduledWorkCompleted,
2927
onInteractionTracked,
3028
onWorkCanceled,
@@ -41,7 +39,7 @@ export function unstable_unsubscribe(subscriber: Subscriber): void {
4139
subscribers.delete(subscriber);
4240

4341
if (subscribers.size === 0) {
44-
subscriberRef.current = null;
42+
__subscriberRef.current = null;
4543
}
4644
}
4745
}

packages/schedule/src/__tests__/ScheduleUMDBundle-test.internal.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,20 @@ describe('Scheduling UMD bundle', () => {
1616
jest.resetModules();
1717
});
1818

19+
function filterPrivateKeys(name) {
20+
return !name.startsWith('_');
21+
}
22+
1923
function validateForwardedAPIs(api, forwardedAPIs) {
20-
const apiKeys = Object.keys(api).sort();
24+
const apiKeys = Object.keys(api)
25+
.filter(filterPrivateKeys)
26+
.sort();
2127
forwardedAPIs.forEach(forwardedAPI => {
22-
expect(Object.keys(forwardedAPI).sort()).toEqual(apiKeys);
28+
expect(
29+
Object.keys(forwardedAPI)
30+
.filter(filterPrivateKeys)
31+
.sort(),
32+
).toEqual(apiKeys);
2333
});
2434
}
2535

packages/schedule/src/__tests__/Tracking-test.internal.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,11 @@ describe('Tracking', () => {
299299

300300
it('should expose the current set of interactions to be externally manipulated', () => {
301301
SchedulerTracking.unstable_track('outer event', currentTime, () => {
302-
expect(SchedulerTracking.__getInteractionsRef().current).toBe(
302+
expect(SchedulerTracking.__interactionsRef.current).toBe(
303303
SchedulerTracking.unstable_getCurrent(),
304304
);
305305

306-
SchedulerTracking.__getInteractionsRef().current = new Set([
306+
SchedulerTracking.__interactionsRef.current = new Set([
307307
{name: 'override event'},
308308
]);
309309

@@ -315,7 +315,7 @@ describe('Tracking', () => {
315315

316316
it('should expose a subscriber ref to be externally manipulated', () => {
317317
SchedulerTracking.unstable_track('outer event', currentTime, () => {
318-
expect(SchedulerTracking.__getSubscriberRef()).toEqual({
318+
expect(SchedulerTracking.__subscriberRef).toEqual({
319319
current: null,
320320
});
321321
});
@@ -368,7 +368,7 @@ describe('Tracking', () => {
368368

369369
describe('advanced integration', () => {
370370
it('should not create unnecessary objects', () => {
371-
expect(SchedulerTracking.__getInteractionsRef()).toBe(null);
371+
expect(SchedulerTracking.__interactionsRef).toBe(null);
372372
});
373373
});
374374
});

packages/schedule/src/__tests__/TrackingSubscriptions-test.internal.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ describe('TrackingSubscriptions', () => {
112112
it('should lazily subscribe to tracking and unsubscribe again if there are no external subscribers', () => {
113113
loadModules({enableSchedulerTracking: true, autoSubscribe: false});
114114

115-
expect(SchedulerTracking.__getSubscriberRef().current).toBe(null);
115+
expect(SchedulerTracking.__subscriberRef.current).toBe(null);
116116
SchedulerTracking.unstable_subscribe(firstSubscriber);
117-
expect(SchedulerTracking.__getSubscriberRef().current).toBeDefined();
117+
expect(SchedulerTracking.__subscriberRef.current).toBeDefined();
118118
SchedulerTracking.unstable_subscribe(secondSubscriber);
119-
expect(SchedulerTracking.__getSubscriberRef().current).toBeDefined();
119+
expect(SchedulerTracking.__subscriberRef.current).toBeDefined();
120120
SchedulerTracking.unstable_unsubscribe(secondSubscriber);
121-
expect(SchedulerTracking.__getSubscriberRef().current).toBeDefined();
121+
expect(SchedulerTracking.__subscriberRef.current).toBeDefined();
122122
SchedulerTracking.unstable_unsubscribe(firstSubscriber);
123-
expect(SchedulerTracking.__getSubscriberRef().current).toBe(null);
123+
expect(SchedulerTracking.__subscriberRef.current).toBe(null);
124124
});
125125

126126
describe('error handling', () => {

0 commit comments

Comments
 (0)