relay-forms-nodeps 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc.js +5 -0
- package/LICENSE +21 -0
- package/README.md +124 -0
- package/lib/RelayFormsTypes.d.ts +37 -0
- package/lib/RelayFormsTypes.js +5 -0
- package/lib/Utils.d.ts +11 -0
- package/lib/Utils.js +87 -0
- package/lib/es/RelayFormsTypes.d.ts +37 -0
- package/lib/es/RelayFormsTypes.js +0 -0
- package/lib/es/Utils.d.ts +11 -0
- package/lib/es/Utils.js +81 -0
- package/lib/es/index.d.ts +9 -0
- package/lib/es/index.js +7 -0
- package/lib/es/relay/RelayModernRecord.d.ts +107 -0
- package/lib/es/relay/RelayModernRecord.js +183 -0
- package/lib/es/relay/RelayModernSelector.d.ts +11 -0
- package/lib/es/relay/RelayModernSelector.js +29 -0
- package/lib/es/relay/RelayModernStore.d.ts +51 -0
- package/lib/es/relay/RelayModernStore.js +355 -0
- package/lib/es/relay/RelayReader.d.ts +14 -0
- package/lib/es/relay/RelayReader.js +86 -0
- package/lib/es/relay/RelayRecordProxy.d.ts +28 -0
- package/lib/es/relay/RelayRecordProxy.js +57 -0
- package/lib/es/relay/RelayRecordSource.d.ts +28 -0
- package/lib/es/relay/RelayRecordSource.js +70 -0
- package/lib/es/relay/RelayRecordSourceMutator.d.ts +36 -0
- package/lib/es/relay/RelayRecordSourceMutator.js +75 -0
- package/lib/es/relay/RelayRecordSourceProxy.d.ts +25 -0
- package/lib/es/relay/RelayRecordSourceProxy.js +52 -0
- package/lib/es/relay/RelayReferenceMarker.d.ts +10 -0
- package/lib/es/relay/RelayReferenceMarker.js +54 -0
- package/lib/es/relay/RelayStoreProvider.d.ts +7 -0
- package/lib/es/relay/RelayStoreProvider.js +16 -0
- package/lib/es/relay/RelayStoreUtils.d.ts +51 -0
- package/lib/es/relay/RelayStoreUtils.js +199 -0
- package/lib/es/relay/RelayTypes.d.ts +92 -0
- package/lib/es/relay/RelayTypes.js +0 -0
- package/lib/es/relay/queries.d.ts +86 -0
- package/lib/es/relay/queries.js +68 -0
- package/lib/es/useForceUpdate.d.ts +1 -0
- package/lib/es/useForceUpdate.js +8 -0
- package/lib/es/useFormSetValue.d.ts +2 -0
- package/lib/es/useFormSetValue.js +84 -0
- package/lib/es/useFormState.d.ts +2 -0
- package/lib/es/useFormState.js +49 -0
- package/lib/es/useFormSubmit.d.ts +2 -0
- package/lib/es/useFormSubmit.js +105 -0
- package/lib/es/useFormValue.d.ts +2 -0
- package/lib/es/useFormValue.js +19 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +20 -0
- package/lib/relay/RelayModernRecord.d.ts +107 -0
- package/lib/relay/RelayModernRecord.js +188 -0
- package/lib/relay/RelayModernSelector.d.ts +11 -0
- package/lib/relay/RelayModernSelector.js +36 -0
- package/lib/relay/RelayModernStore.d.ts +51 -0
- package/lib/relay/RelayModernStore.js +359 -0
- package/lib/relay/RelayReader.d.ts +14 -0
- package/lib/relay/RelayReader.js +92 -0
- package/lib/relay/RelayRecordProxy.d.ts +28 -0
- package/lib/relay/RelayRecordProxy.js +60 -0
- package/lib/relay/RelayRecordSource.d.ts +28 -0
- package/lib/relay/RelayRecordSource.js +75 -0
- package/lib/relay/RelayRecordSourceMutator.d.ts +36 -0
- package/lib/relay/RelayRecordSourceMutator.js +80 -0
- package/lib/relay/RelayRecordSourceProxy.d.ts +25 -0
- package/lib/relay/RelayRecordSourceProxy.js +57 -0
- package/lib/relay/RelayReferenceMarker.d.ts +10 -0
- package/lib/relay/RelayReferenceMarker.js +60 -0
- package/lib/relay/RelayStoreProvider.d.ts +7 -0
- package/lib/relay/RelayStoreProvider.js +22 -0
- package/lib/relay/RelayStoreUtils.d.ts +51 -0
- package/lib/relay/RelayStoreUtils.js +208 -0
- package/lib/relay/RelayTypes.d.ts +92 -0
- package/lib/relay/RelayTypes.js +5 -0
- package/lib/relay/queries.d.ts +86 -0
- package/lib/relay/queries.js +73 -0
- package/lib/umd/relay-forms-nodeps.js +1478 -0
- package/lib/umd/relay-forms-nodeps.js.map +1 -0
- package/lib/umd/relay-forms-nodeps.min.js +2 -0
- package/lib/umd/relay-forms-nodeps.min.js.map +1 -0
- package/lib/useForceUpdate.d.ts +1 -0
- package/lib/useForceUpdate.js +14 -0
- package/lib/useFormSetValue.d.ts +2 -0
- package/lib/useFormSetValue.js +90 -0
- package/lib/useFormState.d.ts +2 -0
- package/lib/useFormState.js +54 -0
- package/lib/useFormSubmit.d.ts +2 -0
- package/lib/useFormSubmit.js +110 -0
- package/lib/useFormValue.d.ts +2 -0
- package/lib/useFormValue.js +25 -0
- package/package.json +89 -0
- package/replace.config.js +5 -0
- package/rollup.config.js +92 -0
- package/tsconfig.esm.json +7 -0
- package/yarn-error.log +127 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
var __assign = this && this.__assign || function () {
|
|
2
|
+
__assign = Object.assign || function (t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
6
|
+
}
|
|
7
|
+
return t;
|
|
8
|
+
};
|
|
9
|
+
return __assign.apply(this, arguments);
|
|
10
|
+
};
|
|
11
|
+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
|
|
12
|
+
/**
|
|
13
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
14
|
+
*
|
|
15
|
+
* This source code is licensed under the MIT license found in the
|
|
16
|
+
* LICENSE file in the root directory of this source tree.
|
|
17
|
+
*
|
|
18
|
+
* @flow
|
|
19
|
+
* @format
|
|
20
|
+
*/
|
|
21
|
+
import { areEqual, RelayStoreUtils } from './RelayStoreUtils';
|
|
22
|
+
var ID_KEY = RelayStoreUtils.ID_KEY,
|
|
23
|
+
REF_KEY = RelayStoreUtils.REF_KEY,
|
|
24
|
+
REFS_KEY = RelayStoreUtils.REFS_KEY;
|
|
25
|
+
/**
|
|
26
|
+
* @public
|
|
27
|
+
*
|
|
28
|
+
* Low-level record manipulation methods.
|
|
29
|
+
*
|
|
30
|
+
* A note about perf: we use long-hand property access rather than computed
|
|
31
|
+
* properties in this file for speed ie.
|
|
32
|
+
*
|
|
33
|
+
* const object = {};
|
|
34
|
+
* object[KEY] = value;
|
|
35
|
+
* record[storageKey] = object;
|
|
36
|
+
*
|
|
37
|
+
* instead of:
|
|
38
|
+
*
|
|
39
|
+
* record[storageKey] = {
|
|
40
|
+
* [KEY]: value,
|
|
41
|
+
* };
|
|
42
|
+
*
|
|
43
|
+
* The latter gets transformed by Babel into something like:
|
|
44
|
+
*
|
|
45
|
+
* function _defineProperty(obj, key, value) {
|
|
46
|
+
* if (key in obj) {
|
|
47
|
+
* Object.defineProperty(obj, key, {
|
|
48
|
+
* value: value,
|
|
49
|
+
* enumerable: true,
|
|
50
|
+
* configurable: true,
|
|
51
|
+
* writable: true,
|
|
52
|
+
* });
|
|
53
|
+
* } else {
|
|
54
|
+
* obj[key] = value;
|
|
55
|
+
* }
|
|
56
|
+
* return obj;
|
|
57
|
+
* }
|
|
58
|
+
*
|
|
59
|
+
* record[storageKey] = _defineProperty({}, KEY, value);
|
|
60
|
+
*
|
|
61
|
+
* A quick benchmark shows that computed property access is an order of
|
|
62
|
+
* magnitude slower (times in seconds for 100,000 iterations):
|
|
63
|
+
*
|
|
64
|
+
* best avg sd
|
|
65
|
+
* computed 0.02175 0.02292 0.00113
|
|
66
|
+
* manual 0.00110 0.00123 0.00008
|
|
67
|
+
*/
|
|
68
|
+
/**
|
|
69
|
+
* @public
|
|
70
|
+
*
|
|
71
|
+
* Create a new record.
|
|
72
|
+
*/
|
|
73
|
+
function create(dataID) {
|
|
74
|
+
// See perf note above for why we aren't using computed property access.
|
|
75
|
+
var record = {};
|
|
76
|
+
record[ID_KEY] = dataID;
|
|
77
|
+
return record;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* @public
|
|
81
|
+
*
|
|
82
|
+
* Get a scalar (non-link) field value.
|
|
83
|
+
*/
|
|
84
|
+
function getValue(record, storageKey) {
|
|
85
|
+
return record[storageKey];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* @private
|
|
89
|
+
*
|
|
90
|
+
* Get the value of a field as a reference to another record. Throws if the
|
|
91
|
+
* field has a different type.
|
|
92
|
+
*/
|
|
93
|
+
function getLinkedRecordKey(record, storageKey, key) {
|
|
94
|
+
var link = record[storageKey];
|
|
95
|
+
if (link == null) {
|
|
96
|
+
return link;
|
|
97
|
+
}
|
|
98
|
+
return link[key];
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* @public
|
|
102
|
+
*
|
|
103
|
+
* Get the value of a field as a reference to another record. Throws if the
|
|
104
|
+
* field has a different type.
|
|
105
|
+
*/
|
|
106
|
+
function getLinkedRecordID(record, storageKey) {
|
|
107
|
+
return getLinkedRecordKey(record, storageKey, REF_KEY);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* @public
|
|
111
|
+
*
|
|
112
|
+
* Get the value of a field as a list of references to other records. Throws if
|
|
113
|
+
* the field has a different type.
|
|
114
|
+
*/
|
|
115
|
+
function getLinkedRecordIDs(record, storageKey) {
|
|
116
|
+
return getLinkedRecordKey(record, storageKey, REFS_KEY);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* @public
|
|
120
|
+
*
|
|
121
|
+
* Compares the fields of a previous and new record, returning either the
|
|
122
|
+
* previous record if all fields are equal or a new record (with merged fields)
|
|
123
|
+
* if any fields have changed.
|
|
124
|
+
*/
|
|
125
|
+
function update(prevRecord, nextRecord) {
|
|
126
|
+
var updated = null;
|
|
127
|
+
var keys = Object.keys(nextRecord);
|
|
128
|
+
for (var ii = 0; ii < keys.length; ii++) {
|
|
129
|
+
var key = keys[ii];
|
|
130
|
+
if (updated || !areEqual(prevRecord[key], nextRecord[key])) {
|
|
131
|
+
updated = updated !== null ? updated : __assign({}, prevRecord);
|
|
132
|
+
updated[key] = nextRecord[key];
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return updated !== null ? updated : prevRecord;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* @public
|
|
139
|
+
*
|
|
140
|
+
* Set the value of a storageKey to a scalar.
|
|
141
|
+
* YES
|
|
142
|
+
*/
|
|
143
|
+
function setValue(record, storageKey, value) {
|
|
144
|
+
record[storageKey] = value;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* @private
|
|
148
|
+
*
|
|
149
|
+
* Get the value of a field as a reference to another record. Throws if the
|
|
150
|
+
* field has a different type.
|
|
151
|
+
*/
|
|
152
|
+
function setLinkedRecordKey(record, storageKey, linkedID, key) {
|
|
153
|
+
// See perf note above for why we aren't using computed property access.
|
|
154
|
+
var link = {};
|
|
155
|
+
link[key] = linkedID;
|
|
156
|
+
record[storageKey] = link;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* @public
|
|
160
|
+
*
|
|
161
|
+
* Set the value of a field to a reference to another record.
|
|
162
|
+
*/
|
|
163
|
+
function setLinkedRecordID(record, storageKey, linkedID) {
|
|
164
|
+
setLinkedRecordKey(record, storageKey, linkedID, REF_KEY);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* @public
|
|
168
|
+
*
|
|
169
|
+
* Set the value of a field to a list of references other records.
|
|
170
|
+
*/
|
|
171
|
+
function setLinkedRecordIDs(record, storageKey, linkedIDs) {
|
|
172
|
+
setLinkedRecordKey(record, storageKey, linkedIDs, REFS_KEY);
|
|
173
|
+
}
|
|
174
|
+
export var RelayModernRecord = {
|
|
175
|
+
create: create,
|
|
176
|
+
getLinkedRecordID: getLinkedRecordID,
|
|
177
|
+
getLinkedRecordIDs: getLinkedRecordIDs,
|
|
178
|
+
getValue: getValue,
|
|
179
|
+
setValue: setValue,
|
|
180
|
+
setLinkedRecordID: setLinkedRecordID,
|
|
181
|
+
setLinkedRecordIDs: setLinkedRecordIDs,
|
|
182
|
+
update: update
|
|
183
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
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 strict-local
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
export declare function getSingularSelector(fragment: any, item: any): any;
|
|
11
|
+
export declare function createOperationDescriptor(request: any): any;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
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 strict-local
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
// flowlint ambiguous-object-type:error
|
|
11
|
+
import { RelayStoreUtils } from './RelayStoreUtils';
|
|
12
|
+
var ID_KEY = RelayStoreUtils.ID_KEY;
|
|
13
|
+
function createReaderSelector(node, dataID) {
|
|
14
|
+
return {
|
|
15
|
+
//kind: 'SingularReaderSelector',
|
|
16
|
+
dataID: dataID,
|
|
17
|
+
node: node
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export function getSingularSelector(fragment, item) {
|
|
21
|
+
return createReaderSelector(fragment, item[ID_KEY]);
|
|
22
|
+
}
|
|
23
|
+
export function createOperationDescriptor(request) {
|
|
24
|
+
var operationDescriptor = {
|
|
25
|
+
fragment: createReaderSelector(request.fragment, RelayStoreUtils.ROOT_ID),
|
|
26
|
+
identifier: request.identifier
|
|
27
|
+
};
|
|
28
|
+
return operationDescriptor;
|
|
29
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
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 strict-local
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
import { MutableRecordSource, Store } from './RelayTypes';
|
|
11
|
+
export declare function createStore(): RelayModernStore;
|
|
12
|
+
/**
|
|
13
|
+
* @public
|
|
14
|
+
*
|
|
15
|
+
* An implementation of the `Store` interface defined in `RelayStoreTypes`.
|
|
16
|
+
*
|
|
17
|
+
* Note that a Store takes ownership of all records provided to it: other
|
|
18
|
+
* objects may continue to hold a reference to such records but may not mutate
|
|
19
|
+
* them. The static Relay core is architected to avoid mutating records that may have been
|
|
20
|
+
* passed to a store: operations that mutate records will either create fresh
|
|
21
|
+
* records or clone existing records and modify the clones. Record immutability
|
|
22
|
+
* is also enforced in development mode by freezing all records passed to a store.
|
|
23
|
+
*/
|
|
24
|
+
export declare class RelayModernStore implements Store {
|
|
25
|
+
_gcRun: any;
|
|
26
|
+
_recordSource: MutableRecordSource;
|
|
27
|
+
_roots: any;
|
|
28
|
+
_updatedRecordIDs: any;
|
|
29
|
+
_currentWriteEpoch: any;
|
|
30
|
+
_subs: any;
|
|
31
|
+
constructor(source: MutableRecordSource);
|
|
32
|
+
getSource(): MutableRecordSource;
|
|
33
|
+
commitUpdate(updater: any): void;
|
|
34
|
+
retain(operation: any): {
|
|
35
|
+
dispose: () => void;
|
|
36
|
+
};
|
|
37
|
+
lookup(selector: any): {
|
|
38
|
+
data: any;
|
|
39
|
+
seenRecords: any;
|
|
40
|
+
selector: any;
|
|
41
|
+
};
|
|
42
|
+
notify(): void;
|
|
43
|
+
_updateSub(source: any, subscription: any, updatedRecordIDs: any, hasUpdatedRecords: boolean): void;
|
|
44
|
+
publish(source: any): void;
|
|
45
|
+
subscribe(snapshot: any, callback: (snapshot: any) => void): {
|
|
46
|
+
dispose: () => void;
|
|
47
|
+
};
|
|
48
|
+
scheduleGC(): void;
|
|
49
|
+
_gcStep: () => void;
|
|
50
|
+
_collect(): Generator<void, void, void>;
|
|
51
|
+
}
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*
|
|
8
|
+
* @flow strict-local
|
|
9
|
+
* @format
|
|
10
|
+
*/
|
|
11
|
+
// flowlint ambiguous-object-type:error
|
|
12
|
+
'use strict';
|
|
13
|
+
|
|
14
|
+
var __generator = this && this.__generator || function (thisArg, body) {
|
|
15
|
+
var _ = {
|
|
16
|
+
label: 0,
|
|
17
|
+
sent: function () {
|
|
18
|
+
if (t[0] & 1) throw t[1];
|
|
19
|
+
return t[1];
|
|
20
|
+
},
|
|
21
|
+
trys: [],
|
|
22
|
+
ops: []
|
|
23
|
+
},
|
|
24
|
+
f,
|
|
25
|
+
y,
|
|
26
|
+
t,
|
|
27
|
+
g;
|
|
28
|
+
return g = {
|
|
29
|
+
next: verb(0),
|
|
30
|
+
"throw": verb(1),
|
|
31
|
+
"return": verb(2)
|
|
32
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
|
|
33
|
+
return this;
|
|
34
|
+
}), g;
|
|
35
|
+
function verb(n) {
|
|
36
|
+
return function (v) {
|
|
37
|
+
return step([n, v]);
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function step(op) {
|
|
41
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
42
|
+
while (_) try {
|
|
43
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
44
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
45
|
+
switch (op[0]) {
|
|
46
|
+
case 0:
|
|
47
|
+
case 1:
|
|
48
|
+
t = op;
|
|
49
|
+
break;
|
|
50
|
+
case 4:
|
|
51
|
+
_.label++;
|
|
52
|
+
return {
|
|
53
|
+
value: op[1],
|
|
54
|
+
done: false
|
|
55
|
+
};
|
|
56
|
+
case 5:
|
|
57
|
+
_.label++;
|
|
58
|
+
y = op[1];
|
|
59
|
+
op = [0];
|
|
60
|
+
continue;
|
|
61
|
+
case 7:
|
|
62
|
+
op = _.ops.pop();
|
|
63
|
+
_.trys.pop();
|
|
64
|
+
continue;
|
|
65
|
+
default:
|
|
66
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
67
|
+
_ = 0;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
71
|
+
_.label = op[1];
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
75
|
+
_.label = t[1];
|
|
76
|
+
t = op;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
if (t && _.label < t[2]) {
|
|
80
|
+
_.label = t[2];
|
|
81
|
+
_.ops.push(op);
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
if (t[2]) _.ops.pop();
|
|
85
|
+
_.trys.pop();
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
op = body.call(thisArg, _);
|
|
89
|
+
} catch (e) {
|
|
90
|
+
op = [6, e];
|
|
91
|
+
y = 0;
|
|
92
|
+
} finally {
|
|
93
|
+
f = t = 0;
|
|
94
|
+
}
|
|
95
|
+
if (op[0] & 5) throw op[1];
|
|
96
|
+
return {
|
|
97
|
+
value: op[0] ? op[1] : void 0,
|
|
98
|
+
done: true
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
import { RelayModernRecord } from './RelayModernRecord';
|
|
103
|
+
import { relayRead } from './RelayReader';
|
|
104
|
+
import { RelayRecordSource } from './RelayRecordSource';
|
|
105
|
+
import { RelayRecordSourceMutator } from './RelayRecordSourceMutator';
|
|
106
|
+
import { RelayRecordSourceProxy } from './RelayRecordSourceProxy';
|
|
107
|
+
import { mark } from './RelayReferenceMarker';
|
|
108
|
+
import { hasOverlappingIDs, recycleNodesInto, RelayStoreUtils } from './RelayStoreUtils';
|
|
109
|
+
var resolvedPromise = Promise.resolve();
|
|
110
|
+
/**
|
|
111
|
+
* An alternative to setImmediate based on Promise.
|
|
112
|
+
*/
|
|
113
|
+
function resolveImmediate(callback) {
|
|
114
|
+
resolvedPromise.then(callback).catch(throwNext);
|
|
115
|
+
}
|
|
116
|
+
function throwNext(error) {
|
|
117
|
+
setTimeout(function () {
|
|
118
|
+
throw error;
|
|
119
|
+
}, 0);
|
|
120
|
+
}
|
|
121
|
+
var ROOT_ID = RelayStoreUtils.ROOT_ID;
|
|
122
|
+
export function createStore() {
|
|
123
|
+
return new RelayModernStore(new RelayRecordSource());
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* @public
|
|
127
|
+
*
|
|
128
|
+
* An implementation of the `Store` interface defined in `RelayStoreTypes`.
|
|
129
|
+
*
|
|
130
|
+
* Note that a Store takes ownership of all records provided to it: other
|
|
131
|
+
* objects may continue to hold a reference to such records but may not mutate
|
|
132
|
+
* them. The static Relay core is architected to avoid mutating records that may have been
|
|
133
|
+
* passed to a store: operations that mutate records will either create fresh
|
|
134
|
+
* records or clone existing records and modify the clones. Record immutability
|
|
135
|
+
* is also enforced in development mode by freezing all records passed to a store.
|
|
136
|
+
*/
|
|
137
|
+
var RelayModernStore = /** @class */function () {
|
|
138
|
+
function RelayModernStore(source) {
|
|
139
|
+
var _this = this;
|
|
140
|
+
this._gcStep = function () {
|
|
141
|
+
if (_this._gcRun) {
|
|
142
|
+
if (_this._gcRun.next().done) {
|
|
143
|
+
_this._gcRun = null;
|
|
144
|
+
} else {
|
|
145
|
+
resolveImmediate(_this._gcStep);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
this._currentWriteEpoch = 0;
|
|
150
|
+
this._gcRun = null;
|
|
151
|
+
this._recordSource = source;
|
|
152
|
+
this._roots = new Map();
|
|
153
|
+
this._updatedRecordIDs = new Set();
|
|
154
|
+
this._subs = new Set();
|
|
155
|
+
initializeRecordSource(this._recordSource);
|
|
156
|
+
}
|
|
157
|
+
RelayModernStore.prototype.getSource = function () {
|
|
158
|
+
return this._recordSource;
|
|
159
|
+
};
|
|
160
|
+
RelayModernStore.prototype.commitUpdate = function (updater) {
|
|
161
|
+
var sink = new RelayRecordSource();
|
|
162
|
+
var mutator = new RelayRecordSourceMutator(this.getSource(), sink);
|
|
163
|
+
var recordSourceProxy = new RelayRecordSourceProxy(mutator);
|
|
164
|
+
updater(recordSourceProxy);
|
|
165
|
+
this.publish(sink);
|
|
166
|
+
this.notify();
|
|
167
|
+
};
|
|
168
|
+
RelayModernStore.prototype.retain = function (operation) {
|
|
169
|
+
var _this = this;
|
|
170
|
+
var id = operation.identifier;
|
|
171
|
+
var disposed = false;
|
|
172
|
+
var dispose = function () {
|
|
173
|
+
// Ensure each retain can only dispose once
|
|
174
|
+
if (disposed) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
disposed = true;
|
|
178
|
+
// For Flow: guard against the entry somehow not existing
|
|
179
|
+
var rootEntry = _this._roots.get(id);
|
|
180
|
+
if (rootEntry == null) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
// Decrement the ref count: if it becomes zero it is eligible
|
|
184
|
+
// for release.
|
|
185
|
+
rootEntry.refCount -= 1;
|
|
186
|
+
if (rootEntry.refCount === 0) {
|
|
187
|
+
_this._roots.delete(id);
|
|
188
|
+
_this.scheduleGC();
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
var rootEntry = this._roots.get(id);
|
|
192
|
+
if (rootEntry != null) {
|
|
193
|
+
// If we've previously retained this operation, increment the refCount
|
|
194
|
+
rootEntry.refCount += 1;
|
|
195
|
+
} else {
|
|
196
|
+
// Otherwise create a new entry for the operation
|
|
197
|
+
this._roots.set(id, {
|
|
198
|
+
operation: operation,
|
|
199
|
+
refCount: 1
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
return {
|
|
203
|
+
dispose: dispose
|
|
204
|
+
};
|
|
205
|
+
};
|
|
206
|
+
RelayModernStore.prototype.lookup = function (selector) {
|
|
207
|
+
var source = this.getSource();
|
|
208
|
+
return relayRead(source, selector);
|
|
209
|
+
};
|
|
210
|
+
// This method will return a list of updated owners from the subscriptions
|
|
211
|
+
RelayModernStore.prototype.notify = function () {
|
|
212
|
+
var _this = this;
|
|
213
|
+
this._currentWriteEpoch += 1;
|
|
214
|
+
var updateIds = this._updatedRecordIDs;
|
|
215
|
+
var hasUpdatedRecords = updateIds.size !== 0;
|
|
216
|
+
var source = this.getSource();
|
|
217
|
+
this._subs.forEach(function (subscription) {
|
|
218
|
+
_this._updateSub(source, subscription, updateIds, hasUpdatedRecords);
|
|
219
|
+
});
|
|
220
|
+
this._updatedRecordIDs.clear();
|
|
221
|
+
};
|
|
222
|
+
RelayModernStore.prototype._updateSub = function (source, subscription, updatedRecordIDs, hasUpdatedRecords) {
|
|
223
|
+
var callback = subscription.callback,
|
|
224
|
+
snapshot = subscription.snapshot;
|
|
225
|
+
var hasOverlappingUpdates = hasUpdatedRecords && hasOverlappingIDs(snapshot.seenRecords, updatedRecordIDs);
|
|
226
|
+
if (hasOverlappingUpdates) {
|
|
227
|
+
var nextSnapshot = relayRead(source, snapshot.selector);
|
|
228
|
+
var nextData = recycleNodesInto(snapshot.data, nextSnapshot.data);
|
|
229
|
+
nextSnapshot = {
|
|
230
|
+
data: nextData,
|
|
231
|
+
seenRecords: nextSnapshot.seenRecords,
|
|
232
|
+
selector: nextSnapshot.selector
|
|
233
|
+
};
|
|
234
|
+
subscription.snapshot = nextSnapshot;
|
|
235
|
+
if (nextSnapshot.data !== snapshot.data) {
|
|
236
|
+
callback(nextSnapshot);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
RelayModernStore.prototype.publish = function (source) {
|
|
241
|
+
var target = this.getSource();
|
|
242
|
+
updateTargetFromSource(target, source, this._updatedRecordIDs);
|
|
243
|
+
};
|
|
244
|
+
RelayModernStore.prototype.subscribe = function (snapshot, callback) {
|
|
245
|
+
var _this = this;
|
|
246
|
+
var subscription = {
|
|
247
|
+
callback: callback,
|
|
248
|
+
snapshot: snapshot
|
|
249
|
+
};
|
|
250
|
+
var dispose = function () {
|
|
251
|
+
_this._subs.delete(subscription);
|
|
252
|
+
};
|
|
253
|
+
this._subs.add(subscription);
|
|
254
|
+
return {
|
|
255
|
+
dispose: dispose
|
|
256
|
+
};
|
|
257
|
+
};
|
|
258
|
+
RelayModernStore.prototype.scheduleGC = function () {
|
|
259
|
+
if (this._gcRun) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
this._gcRun = this._collect();
|
|
263
|
+
resolveImmediate(this._gcStep);
|
|
264
|
+
};
|
|
265
|
+
RelayModernStore.prototype._collect = function () {
|
|
266
|
+
var startEpoch, references, _i, _a, operation, storeIDs, ii, dataID;
|
|
267
|
+
return __generator(this, function (_b) {
|
|
268
|
+
switch (_b.label) {
|
|
269
|
+
case 0:
|
|
270
|
+
if (!true) return [3 /*break*/, 5];
|
|
271
|
+
startEpoch = this._currentWriteEpoch;
|
|
272
|
+
references = new Set();
|
|
273
|
+
_i = 0, _a = this._roots.values();
|
|
274
|
+
_b.label = 1;
|
|
275
|
+
case 1:
|
|
276
|
+
if (!(_i < _a.length)) return [3 /*break*/, 4];
|
|
277
|
+
operation = _a[_i].operation;
|
|
278
|
+
mark(this._recordSource, operation.fragment, references);
|
|
279
|
+
// Yield for other work after each operation
|
|
280
|
+
return [4 /*yield*/];
|
|
281
|
+
case 2:
|
|
282
|
+
// Yield for other work after each operation
|
|
283
|
+
_b.sent();
|
|
284
|
+
// If the store was updated, restart
|
|
285
|
+
if (startEpoch !== this._currentWriteEpoch) {
|
|
286
|
+
return [3 /*break*/, 0];
|
|
287
|
+
}
|
|
288
|
+
_b.label = 3;
|
|
289
|
+
case 3:
|
|
290
|
+
_i++;
|
|
291
|
+
return [3 /*break*/, 1];
|
|
292
|
+
case 4:
|
|
293
|
+
// Sweep records without references
|
|
294
|
+
if (references.size === 0) {
|
|
295
|
+
// Short-circuit if *nothing* is referenced
|
|
296
|
+
this._recordSource.clear();
|
|
297
|
+
} else {
|
|
298
|
+
storeIDs = this._recordSource.getRecordIDs();
|
|
299
|
+
for (ii = 0; ii < storeIDs.length; ii++) {
|
|
300
|
+
dataID = storeIDs[ii];
|
|
301
|
+
if (!references.has(dataID)) {
|
|
302
|
+
this._recordSource.remove(dataID);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return [2 /*return*/];
|
|
307
|
+
case 5:
|
|
308
|
+
return [2 /*return*/];
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
return RelayModernStore;
|
|
314
|
+
}();
|
|
315
|
+
export { RelayModernStore };
|
|
316
|
+
function initializeRecordSource(target) {
|
|
317
|
+
if (!target.has(ROOT_ID)) {
|
|
318
|
+
var rootRecord = RelayModernRecord.create(ROOT_ID);
|
|
319
|
+
target.set(ROOT_ID, rootRecord);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Updates the target with information from source, also updating a mapping of
|
|
324
|
+
* which records in the target were changed as a result.
|
|
325
|
+
* Additionally, will mark records as invalidated at the current write epoch
|
|
326
|
+
* given the set of record ids marked as stale in this update.
|
|
327
|
+
*/
|
|
328
|
+
function updateTargetFromSource(target, source, updatedRecordIDs) {
|
|
329
|
+
// Update the target based on the changes present in source
|
|
330
|
+
var dataIDs = source.getRecordIDs();
|
|
331
|
+
for (var ii = 0; ii < dataIDs.length; ii++) {
|
|
332
|
+
var dataID = dataIDs[ii];
|
|
333
|
+
var sourceRecord = source.get(dataID);
|
|
334
|
+
var targetRecord = target.get(dataID);
|
|
335
|
+
if (sourceRecord && targetRecord) {
|
|
336
|
+
// ReactFlightClientResponses are lazy and only materialize when readRoot
|
|
337
|
+
// is called when we read the field, so if the record is a Flight field
|
|
338
|
+
// we always use the new record's data regardless of whether
|
|
339
|
+
// it actually changed. Let React take care of reconciliation instead.
|
|
340
|
+
var nextRecord = RelayModernRecord.update(targetRecord, sourceRecord);
|
|
341
|
+
if (nextRecord !== targetRecord) {
|
|
342
|
+
updatedRecordIDs.add(dataID);
|
|
343
|
+
target.set(dataID, nextRecord);
|
|
344
|
+
}
|
|
345
|
+
} else if (sourceRecord === null) {
|
|
346
|
+
target.delete(dataID);
|
|
347
|
+
if (targetRecord !== null) {
|
|
348
|
+
updatedRecordIDs.add(dataID);
|
|
349
|
+
}
|
|
350
|
+
} else if (sourceRecord) {
|
|
351
|
+
target.set(dataID, sourceRecord);
|
|
352
|
+
updatedRecordIDs.add(dataID);
|
|
353
|
+
} // don't add explicit undefined
|
|
354
|
+
}
|
|
355
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
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 strict-local
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
export declare function relayRead(recordSource: any, selector: any): {
|
|
11
|
+
data: any;
|
|
12
|
+
seenRecords: any;
|
|
13
|
+
selector: any;
|
|
14
|
+
};
|