UNPKG

3.56 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.typeOwnersReducer = void 0;
5var _commonTags = require("common-tags");
6var _reportOnce = require("../../utils/report-once");
7function setTypeOwner(typeName, plugin, typeOwners, fullNode) {
8 const ownerName = (plugin === null || plugin === void 0 ? void 0 : plugin.name) || (fullNode === null || fullNode === void 0 ? void 0 : fullNode.internal.owner);
9 if (!ownerName) {
10 (0, _reportOnce.reportOnce)(`No plugin owner for type "${typeName}"`);
11 return typeOwners;
12 }
13 const existingOwnerTypes = typeOwners.pluginsToTypes.get(ownerName);
14 if (!existingOwnerTypes) {
15 typeOwners.pluginsToTypes.set(ownerName, new Set([typeName]));
16 } else {
17 existingOwnerTypes.add(typeName);
18 }
19 const existingTypeOwnerNameByTypeName = typeOwners.typesToPlugins.get(typeName);
20 if (!existingTypeOwnerNameByTypeName) {
21 typeOwners.typesToPlugins.set(typeName, ownerName);
22 } else if (existingTypeOwnerNameByTypeName !== ownerName) {
23 throw new Error((0, _commonTags.stripIndent)`
24 The plugin "${ownerName}" created a node of a type owned by another plugin.
25
26 The node type "${typeName}" is owned by "${existingTypeOwnerNameByTypeName}".
27
28 If you copy and pasted code from elsewhere, you'll need to pick a new type name
29 for your new node(s).
30
31 ${fullNode ? (0, _commonTags.stripIndent)(`The node object passed to "createNode":
32
33 ${JSON.stringify(fullNode, null, 4)}\n`) : ``}
34 The plugin creating the node:
35
36 ${JSON.stringify(plugin, null, 4)}
37 `);
38 }
39 return typeOwners;
40}
41const typeOwnersReducer = (typeOwners = {
42 pluginsToTypes: new Map(),
43 typesToPlugins: new Map()
44}, action) => {
45 switch (action.type) {
46 case `DELETE_NODE`:
47 {
48 const {
49 plugin,
50 payload: internalNode
51 } = action;
52 if (plugin && internalNode && !action.isRecursiveChildrenDelete) {
53 const pluginName = plugin.name;
54 const previouslyRecordedOwnerName = typeOwners.typesToPlugins.get(internalNode.internal.type);
55 if (internalNode && previouslyRecordedOwnerName && previouslyRecordedOwnerName !== pluginName) {
56 throw new Error((0, _commonTags.stripIndent)`
57 The plugin "${pluginName}" deleted a node of a type owned by another plugin.
58
59 The node type "${internalNode.internal.type}" is owned by "${previouslyRecordedOwnerName}".
60
61 The node object passed to "deleteNode":
62
63 ${JSON.stringify(internalNode, null, 4)}
64
65 The plugin deleting the node:
66
67 ${JSON.stringify(plugin, null, 4)}
68 `);
69 }
70 }
71 return typeOwners;
72 }
73 case `CREATE_NODE`:
74 {
75 const {
76 plugin,
77 oldNode,
78 payload: node
79 } = action;
80 const {
81 owner,
82 type
83 } = node.internal;
84 setTypeOwner(type, plugin, typeOwners, node);
85
86 // If the node has been created in the past, check that
87 // the current plugin is the same as the previous.
88 if (oldNode && oldNode.internal.owner !== owner) {
89 throw new Error((0, _commonTags.stripIndent)`
90 Nodes can only be updated by their owner. Node "${node.id}" is
91 owned by "${oldNode.internal.owner}" and another plugin "${owner}"
92 tried to update it.
93 `);
94 }
95 return typeOwners;
96 }
97 default:
98 return typeOwners;
99 }
100};
101exports.typeOwnersReducer = typeOwnersReducer;
102//# sourceMappingURL=type-owners.js.map
\No newline at end of file