-
Notifications
You must be signed in to change notification settings - Fork 45.7k
/
ReactMutableSource.js
73 lines (60 loc) · 2.29 KB
/
ReactMutableSource.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {ExpirationTime} from 'react-reconciler/src/ReactFiberExpirationTime';
import invariant from 'shared/invariant';
export type MutableSource = $NonMaybeType<mixed>;
export type Version = $NonMaybeType<mixed>;
export type MutableSourceHookConfig<S> = {|
getSnapshot: () => S,
getVersion: () => $NonMaybeType<mixed>,
subscribe: (callback: Function) => () => void,
|};
// Tracks expiration time for all mutable sources with pending updates.
// Used to determine if a source is safe to read during updates.
// If there are no entries in this map for a given source,
// or if the current render’s expiration time is ≤ this value,
// it is safe to read from the source without tearing.
export type MutableSourcePendingUpdateMap = Map<MutableSource, ExpirationTime>;
// Tracks the version of each source at the time it was most recently read.
// Used to determine if a source is safe to read from before it has been subscribed to.
// Version number is only used for sources that have not yet been subscribed to,
// since the mechanism for determining safety after subscription is expiration time.
type MutableSourceWorkInProgressVersionMap = Map<MutableSource, Version>;
let workInProgressVersionMap: null | MutableSourceWorkInProgressVersionMap = null;
export function resetWorkInProgressVersionMap(): void {
workInProgressVersionMap = null;
}
export function initializeWorkInProgressVersionMap(): void {
workInProgressVersionMap = new Map();
}
export function getWorkInProgressVersion(
source: MutableSource,
): null | Version {
invariant(
workInProgressVersionMap !== null,
'Expected a work-in-progress version map.',
);
const version = ((workInProgressVersionMap: any): MutableSourceWorkInProgressVersionMap).get(
source,
);
return version === undefined ? null : version;
}
export function setWorkInProgressVersion(
source: MutableSource,
version: Version,
): void {
invariant(
workInProgressVersionMap !== null,
'Expected a work-in-progress version map.',
);
((workInProgressVersionMap: any): MutableSourceWorkInProgressVersionMap).set(
source,
version,
);
}