UNPKG

6.76 kBSource Map (JSON)View Raw
1{"version":3,"file":"type-owners.js","names":["setTypeOwner","typeName","plugin","typeOwners","fullNode","ownerName","name","internal","owner","reportOnce","existingOwnerTypes","pluginsToTypes","get","set","Set","add","existingTypeOwnerNameByTypeName","typesToPlugins","Error","stripIndent","JSON","stringify","typeOwnersReducer","Map","action","type","payload","internalNode","isRecursiveChildrenDelete","pluginName","previouslyRecordedOwnerName","oldNode","node","id"],"sources":["../../../src/redux/reducers/type-owners.ts"],"sourcesContent":["import { stripIndent } from \"common-tags\"\nimport { reportOnce } from \"../../utils/report-once\"\nimport {\n ActionsUnion,\n IGatsbyNode,\n IGatsbyPlugin,\n IGatsbyState,\n} from \"../types\"\n\nfunction setTypeOwner(\n typeName: string,\n plugin: IGatsbyPlugin,\n typeOwners: IGatsbyState[\"typeOwners\"],\n fullNode?: IGatsbyNode\n): IGatsbyState[\"typeOwners\"] {\n const ownerName = plugin?.name || fullNode?.internal.owner\n\n if (!ownerName) {\n reportOnce(`No plugin owner for type \"${typeName}\"`)\n return typeOwners\n }\n\n const existingOwnerTypes = typeOwners.pluginsToTypes.get(ownerName)\n\n if (!existingOwnerTypes) {\n typeOwners.pluginsToTypes.set(ownerName, new Set([typeName]))\n } else {\n existingOwnerTypes.add(typeName)\n }\n\n const existingTypeOwnerNameByTypeName =\n typeOwners.typesToPlugins.get(typeName)\n\n if (!existingTypeOwnerNameByTypeName) {\n typeOwners.typesToPlugins.set(typeName, ownerName)\n } else if (existingTypeOwnerNameByTypeName !== ownerName) {\n throw new Error(stripIndent`\n The plugin \"${ownerName}\" created a node of a type owned by another plugin.\n\n The node type \"${typeName}\" is owned by \"${existingTypeOwnerNameByTypeName}\".\n\n If you copy and pasted code from elsewhere, you'll need to pick a new type name\n for your new node(s).\n\n ${\n fullNode\n ? stripIndent(\n `The node object passed to \"createNode\":\n\n ${JSON.stringify(fullNode, null, 4)}\\n`\n )\n : ``\n }\n The plugin creating the node:\n\n ${JSON.stringify(plugin, null, 4)}\n `)\n }\n\n return typeOwners\n}\n\nexport const typeOwnersReducer = (\n typeOwners: IGatsbyState[\"typeOwners\"] = {\n pluginsToTypes: new Map(),\n typesToPlugins: new Map(),\n } as IGatsbyState[\"typeOwners\"],\n action: ActionsUnion\n): IGatsbyState[\"typeOwners\"] => {\n switch (action.type) {\n case `DELETE_NODE`: {\n const { plugin, payload: internalNode } = action\n\n if (plugin && internalNode && !action.isRecursiveChildrenDelete) {\n const pluginName = plugin.name\n\n const previouslyRecordedOwnerName = typeOwners.typesToPlugins.get(\n internalNode.internal.type\n )\n\n if (\n internalNode &&\n previouslyRecordedOwnerName &&\n previouslyRecordedOwnerName !== pluginName\n ) {\n throw new Error(stripIndent`\n The plugin \"${pluginName}\" deleted a node of a type owned by another plugin.\n\n The node type \"${\n internalNode.internal.type\n }\" is owned by \"${previouslyRecordedOwnerName}\".\n\n The node object passed to \"deleteNode\":\n\n ${JSON.stringify(internalNode, null, 4)}\n\n The plugin deleting the node:\n\n ${JSON.stringify(plugin, null, 4)}\n `)\n }\n }\n\n return typeOwners\n }\n case `CREATE_NODE`: {\n const { plugin, oldNode, payload: node } = action\n const { owner, type } = node.internal\n\n setTypeOwner(type, plugin, typeOwners, node)\n\n // If the node has been created in the past, check that\n // the current plugin is the same as the previous.\n if (oldNode && oldNode.internal.owner !== owner) {\n throw new Error(\n stripIndent`\n Nodes can only be updated by their owner. Node \"${node.id}\" is\n owned by \"${oldNode.internal.owner}\" and another plugin \"${owner}\"\n tried to update it.\n `\n )\n }\n\n return typeOwners\n }\n\n default:\n return typeOwners\n }\n}\n"],"mappings":";;;;AAAA;AACA;AAQA,SAASA,YAAY,CACnBC,QAAgB,EAChBC,MAAqB,EACrBC,UAAsC,EACtCC,QAAsB,EACM;EAC5B,MAAMC,SAAS,GAAG,CAAAH,MAAM,aAANA,MAAM,uBAANA,MAAM,CAAEI,IAAI,MAAIF,QAAQ,aAARA,QAAQ,uBAARA,QAAQ,CAAEG,QAAQ,CAACC,KAAK;E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