1 | import { __assign, __spreadArray } from "tslib";
|
2 | import { visit } from "graphql";
|
3 | import { wrap } from "optimism";
|
4 | import { cacheSizes, getFragmentDefinitions, } from "../../utilities/index.js";
|
5 | import { WeakCache } from "@wry/caches";
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | export 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 | }
|
19 | var FragmentRegistry = (function () {
|
20 |
|
21 |
|
22 |
|
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 |
|
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 ,
|
62 | })).dirty;
|
63 | this.transform = wrap(proto.transform.bind(this), {
|
64 | cache: WeakCache,
|
65 | max: cacheSizes["fragmentRegistry.transform"] ||
|
66 | 2000 ,
|
67 | });
|
68 | this.findFragmentSpreads = wrap(proto.findFragmentSpreads.bind(this), {
|
69 | cache: WeakCache,
|
70 | max: cacheSizes["fragmentRegistry.findFragmentSpreads"] ||
|
71 | 4000 ,
|
72 | });
|
73 | };
|
74 | |
75 |
|
76 |
|
77 |
|
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 |
|
101 |
|
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 |
|
\ | No newline at end of file |