UNPKG

5.75 kBJavaScriptView Raw
1import { __assign, __spreadArray } from "tslib";
2import { visit } from "graphql";
3import { wrap } from "optimism";
4import { cacheSizes, getFragmentDefinitions, } from "../../utilities/index.js";
5import { WeakCache } from "@wry/caches";
6// As long as createFragmentRegistry is not imported or used, the
7// FragmentRegistry example implementation provided below should not be bundled
8// (by tree-shaking bundlers like Rollup), because the implementation of
9// InMemoryCache refers only to the TypeScript interface FragmentRegistryAPI,
10// never the concrete implementation FragmentRegistry (which is deliberately not
11// exported from this module).
12export function createFragmentRegistry() {
13 var fragments = [];
14 for (var _i = 0; _i < arguments.length; _i++) {
15 fragments[_i] = arguments[_i];
16 }
17 return new (FragmentRegistry.bind.apply(FragmentRegistry, __spreadArray([void 0], fragments, false)))();
18}
19var FragmentRegistry = /** @class */ (function () {
20 // Call `createFragmentRegistry` instead of invoking the
21 // FragmentRegistry constructor directly. This reserves the constructor for
22 // future configuration of the FragmentRegistry.
23 function FragmentRegistry() {
24 var fragments = [];
25 for (var _i = 0; _i < arguments.length; _i++) {
26 fragments[_i] = arguments[_i];
27 }
28 this.registry = Object.create(null);
29 this.resetCaches();
30 if (fragments.length) {
31 this.register.apply(this, fragments);
32 }
33 }
34 FragmentRegistry.prototype.register = function () {
35 var _this = this;
36 var fragments = [];
37 for (var _i = 0; _i < arguments.length; _i++) {
38 fragments[_i] = arguments[_i];
39 }
40 var definitions = new Map();
41 fragments.forEach(function (doc) {
42 getFragmentDefinitions(doc).forEach(function (node) {
43 definitions.set(node.name.value, node);
44 });
45 });
46 definitions.forEach(function (node, name) {
47 if (node !== _this.registry[name]) {
48 _this.registry[name] = node;
49 _this.invalidate(name);
50 }
51 });
52 return this;
53 };
54 // Overridden in the resetCaches method below.
55 FragmentRegistry.prototype.invalidate = function (name) { };
56 FragmentRegistry.prototype.resetCaches = function () {
57 var proto = FragmentRegistry.prototype;
58 this.invalidate = (this.lookup = wrap(proto.lookup.bind(this), {
59 makeCacheKey: function (arg) { return arg; },
60 max: cacheSizes["fragmentRegistry.lookup"] ||
61 1000 /* defaultCacheSizes["fragmentRegistry.lookup"] */,
62 })).dirty; // This dirty function is bound to the wrapped lookup method.
63 this.transform = wrap(proto.transform.bind(this), {
64 cache: WeakCache,
65 max: cacheSizes["fragmentRegistry.transform"] ||
66 2000 /* defaultCacheSizes["fragmentRegistry.transform"] */,
67 });
68 this.findFragmentSpreads = wrap(proto.findFragmentSpreads.bind(this), {
69 cache: WeakCache,
70 max: cacheSizes["fragmentRegistry.findFragmentSpreads"] ||
71 4000 /* defaultCacheSizes["fragmentRegistry.findFragmentSpreads"] */,
72 });
73 };
74 /*
75 * Note:
76 * This method is only memoized so it can serve as a dependency to `tranform`,
77 * so calling `invalidate` will invalidate cache entries for `transform`.
78 */
79 FragmentRegistry.prototype.lookup = function (fragmentName) {
80 return this.registry[fragmentName] || null;
81 };
82 FragmentRegistry.prototype.transform = function (document) {
83 var _this = this;
84 var defined = new Map();
85 getFragmentDefinitions(document).forEach(function (def) {
86 defined.set(def.name.value, def);
87 });
88 var unbound = new Set();
89 var enqueue = function (spreadName) {
90 if (!defined.has(spreadName)) {
91 unbound.add(spreadName);
92 }
93 };
94 var enqueueChildSpreads = function (node) {
95 return Object.keys(_this.findFragmentSpreads(node)).forEach(enqueue);
96 };
97 enqueueChildSpreads(document);
98 var missing = [];
99 var map = Object.create(null);
100 // This Set forEach loop can be extended during iteration by adding
101 // additional strings to the unbound set.
102 unbound.forEach(function (fragmentName) {
103 var knownFragmentDef = defined.get(fragmentName);
104 if (knownFragmentDef) {
105 enqueueChildSpreads((map[fragmentName] = knownFragmentDef));
106 }
107 else {
108 missing.push(fragmentName);
109 var def = _this.lookup(fragmentName);
110 if (def) {
111 enqueueChildSpreads((map[fragmentName] = def));
112 }
113 }
114 });
115 if (missing.length) {
116 var defsToAppend_1 = [];
117 missing.forEach(function (name) {
118 var def = map[name];
119 if (def) {
120 defsToAppend_1.push(def);
121 }
122 });
123 if (defsToAppend_1.length) {
124 document = __assign(__assign({}, document), { definitions: document.definitions.concat(defsToAppend_1) });
125 }
126 }
127 return document;
128 };
129 FragmentRegistry.prototype.findFragmentSpreads = function (root) {
130 var spreads = Object.create(null);
131 visit(root, {
132 FragmentSpread: function (node) {
133 spreads[node.name.value] = node;
134 },
135 });
136 return spreads;
137 };
138 return FragmentRegistry;
139}());
140//# sourceMappingURL=fragmentRegistry.js.map
\No newline at end of file