react-transporter
Advanced tools
Comparing version 1.0.0-alpha.0 to 1.0.0-alpha.1
@@ -427,3 +427,3 @@ 'use strict'; | ||
var container = options.container || function () { | ||
var getData = options.data || function () { | ||
return null; | ||
@@ -436,7 +436,7 @@ }; | ||
client = _useContext.client, | ||
node = _useContext.node; | ||
store = _useContext.store; | ||
try { | ||
var _Resource$all = Resource.all([function () { | ||
return container(node, values); | ||
return getData(store, values); | ||
}, function () { | ||
@@ -449,2 +449,6 @@ return component(); | ||
if (options.waitForAll) { | ||
store.waitForAll(); | ||
} | ||
return /*#__PURE__*/React.createElement(Component, resolvedValues); | ||
@@ -462,3 +466,3 @@ } catch (error) { | ||
if (!client.ssr) { | ||
return options.loading && /*#__PURE__*/React.createElement(options.loading); | ||
return options.loading; | ||
} | ||
@@ -478,7 +482,12 @@ | ||
if (client.ssr && async.current) { | ||
return options.loading && /*#__PURE__*/React.createElement(options.loading); | ||
return options.loading; | ||
} // Re-throw error if container should throw. | ||
if (options.throwOnError) { | ||
throw error; | ||
} // Render error page for synchronous error. | ||
return options.error && /*#__PURE__*/React.createElement(options.error, { | ||
return options.error && /*#__PURE__*/React.cloneElement(options.error, { | ||
error: error | ||
@@ -518,4 +527,4 @@ }); | ||
var applyRenderer = function applyRenderer(Component, options) { | ||
return options.renderer ? options.renderer(Component) : Component; | ||
var applyRenderer = function applyRenderer(component, renderer) { | ||
return renderer ? renderer(component) : component; | ||
}; | ||
@@ -529,3 +538,3 @@ | ||
function resolveComponent(component, options) { | ||
function resolveComponent(component, renderer) { | ||
// Handle React lazy and driveline/lazy | ||
@@ -564,3 +573,3 @@ // eslint-disable-next-line no-underscore-dangle | ||
return component.bundle().then(function (result) { | ||
return applyRenderer(resolveDefaultImport(result), options); | ||
return applyRenderer(resolveDefaultImport(result), renderer); | ||
}); | ||
@@ -571,6 +580,13 @@ }); | ||
return new SyncResource(applyRenderer(component, options)); | ||
return new SyncResource(applyRenderer(component, renderer)); | ||
} | ||
function createContainer(component, options) { | ||
function createContainer(config) { | ||
var component = config.component, | ||
options = _objectWithoutProperties(config, ["component"]); | ||
if (!component) { | ||
throw new Error("You must define a container \"component\"."); | ||
} | ||
var resource; | ||
@@ -580,3 +596,3 @@ | ||
if (!resource) { | ||
resource = resolveComponent(component, options); | ||
resource = resolveComponent(component, options.renderer); | ||
} | ||
@@ -599,3 +615,3 @@ | ||
_defineProperty(_assertThisInitialized(_this), "node", void 0); | ||
_defineProperty(_assertThisInitialized(_this), "store", void 0); | ||
@@ -617,3 +633,3 @@ _this.state = { | ||
value: function componentWillUnmount() { | ||
this.node.destroy(); | ||
this.store.destroy(); | ||
} | ||
@@ -627,3 +643,3 @@ }, { | ||
if (error) { | ||
return options.error && /*#__PURE__*/React.createElement(options.error, { | ||
return options.error && /*#__PURE__*/React.cloneElement(options.error, { | ||
error: error | ||
@@ -644,3 +660,3 @@ }); | ||
return /*#__PURE__*/React__default['default'].createElement(React__default['default'].Suspense, { | ||
fallback: options.loading && /*#__PURE__*/React.createElement(options.loading) | ||
fallback: options.loading | ||
}, handler); | ||
@@ -653,6 +669,6 @@ } | ||
client = _this$context.client, | ||
parent = _this$context.node; | ||
parentStore = _this$context.store; | ||
if (!this.node) { | ||
this.node = client.createNode(parent); | ||
if (!this.store) { | ||
this.store = client.createStore(parentStore); | ||
} | ||
@@ -663,18 +679,20 @@ | ||
client: client, | ||
node: this.node | ||
store: this.store | ||
} | ||
}, this.renderContainer()); | ||
} | ||
}], [{ | ||
key: "getDerivedStateFromError", | ||
value: function getDerivedStateFromError(error) { | ||
return { | ||
error: error | ||
}; | ||
} | ||
}]); | ||
return Container; | ||
}(React__default['default'].Component); | ||
}(React__default['default'].Component); // Add error boundary if container should not throw. | ||
if (!options.throwOnError) { | ||
Container.getDerivedStateFromError = function (error) { | ||
return { | ||
error: error | ||
}; | ||
}; | ||
} | ||
Container.contextType = TransporterContext; | ||
@@ -686,2 +704,27 @@ var name = component.displayName || component.name; | ||
function createNode(config) { | ||
var component = config.component, | ||
options = _objectWithoutProperties(config, ["component"]); | ||
if (!config.component) { | ||
throw new Error("You must define a container \"component\"."); | ||
} | ||
function ContainerNode(props) { | ||
var _useContext = React.useContext(TransporterContext), | ||
store = _useContext.store; | ||
var getValues = options.data || function () { | ||
return null; | ||
}; | ||
/*#__PURE__*/ | ||
React.createElement(component, getValues(store, props)); | ||
} | ||
var name = component.displayName || component.name; | ||
ContainerNode.displayName = name ? "ContainerNode(".concat(name, ")") : 'ContainerNode'; | ||
return ContainerNode; | ||
} | ||
var _this = undefined; | ||
@@ -1294,2 +1337,5 @@ | ||
_this = _super.call(this, message, options); | ||
_defineProperty(_assertThisInitialized(_this), "type", void 0); | ||
_this.name = 'TransporterError'; | ||
@@ -1646,8 +1692,8 @@ _this.type = type; | ||
var StoreNode = function StoreNode(parent, executeQuery) { | ||
var Store = function Store(parentStore, executeQuery) { | ||
var _this = this; | ||
_classCallCheck(this, StoreNode); | ||
_classCallCheck(this, Store); | ||
_defineProperty(this, "parent", void 0); | ||
_defineProperty(this, "parentStore", void 0); | ||
@@ -1710,7 +1756,7 @@ _defineProperty(this, "executeQuery", void 0); | ||
if (!request) { | ||
if (!_this.parent) { | ||
if (!_this.parentStore) { | ||
throw new Error("Query \"".concat(name, "\" was not found.")); | ||
} | ||
request = _this.parent.getRequest(name); | ||
request = _this.parentStore.getRequest(name); | ||
} | ||
@@ -1739,2 +1785,12 @@ | ||
_defineProperty(this, "waitForAll", function () { | ||
_this.requests.forEach(function (request) { | ||
request.read(); | ||
}); | ||
if (_this.parentStore) { | ||
_this.parentStore.waitForAll(); | ||
} | ||
}); | ||
_defineProperty(this, "getFragmentRequest", function (name, entry) { | ||
@@ -1747,3 +1803,3 @@ var request = Array.from(_this.requests.values()).find(function (_ref) { | ||
if (!request) { | ||
if (!_this.parent) { | ||
if (!_this.parentStore) { | ||
var stringifiedEntry = JSON.stringify(entry); | ||
@@ -1753,3 +1809,3 @@ throw new Error("Fragment \"".concat(name, "\" (entry: [").concat(stringifiedEntry, "]) was not found.")); | ||
request = _this.parent.getFragmentRequest(name, entry); | ||
request = _this.parentStore.getFragmentRequest(name, entry); | ||
} | ||
@@ -1831,4 +1887,4 @@ | ||
_defineProperty(this, "destroy", function () { | ||
// Remove network from parent's network children. | ||
_this.parent.removeChild(_this); // Delete requests. | ||
// Remove network from parent store network children. | ||
_this.parentStore.removeChild(_this); // Delete requests. | ||
@@ -1841,3 +1897,3 @@ | ||
this.parent = parent; | ||
this.parentStore = parentStore; | ||
this.executeQuery = executeQuery; | ||
@@ -1847,4 +1903,4 @@ this.requests = new Map(); | ||
if (parent) { | ||
parent.addChild(this); | ||
if (parentStore) { | ||
parentStore.addChild(this); | ||
} | ||
@@ -1868,3 +1924,3 @@ }; | ||
_defineProperty(this, "root", void 0); | ||
_defineProperty(this, "rootStore", void 0); | ||
@@ -1881,4 +1937,4 @@ _defineProperty(this, "cache", void 0); | ||
_defineProperty(this, "createNode", function (parent) { | ||
return new StoreNode(parent, _this.query); | ||
_defineProperty(this, "createStore", function (parentStore) { | ||
return new Store(parentStore, _this.query); | ||
}); | ||
@@ -1891,11 +1947,11 @@ | ||
_defineProperty(this, "refresh", function () { | ||
_this.root.refresh(); | ||
_this.rootStore.refresh(); | ||
}); | ||
_defineProperty(this, "reset", function () { | ||
_this.root.reset(); | ||
_this.rootStore.reset(); | ||
_this.queries = new Map(); | ||
_this.root.refresh(); | ||
_this.rootStore.refresh(); | ||
}); | ||
@@ -1914,3 +1970,3 @@ | ||
this.request = request; | ||
this.root = this.createNode(null); | ||
this.rootStore = this.createStore(null); | ||
this.cache = cache; | ||
@@ -1931,4 +1987,4 @@ this.ssr = ssr; | ||
var node = client.root; | ||
React.useSyncExternalStore(node.subscribe, function () {}, function () {} // TODO: Check this! | ||
var store = client.rootStore; | ||
React.useSyncExternalStore(store.subscribe, function () {}, function () {} // TODO: Check this! | ||
); | ||
@@ -1938,3 +1994,3 @@ return /*#__PURE__*/React__default['default'].createElement(TransporterContext.Provider, { | ||
client: client, | ||
node: node | ||
store: store | ||
} | ||
@@ -2017,6 +2073,8 @@ }, children); | ||
exports.Transporter = Transporter; | ||
exports.TransporterError = TransporterError; | ||
exports.TransporterProvider = TransporterProvider; | ||
exports.createContainer = createContainer; | ||
exports.createNode = createNode; | ||
exports.ref = ref; | ||
exports.useMutation = useMutation; | ||
exports.useReset = useReset; |
@@ -1,2 +0,2 @@ | ||
import React, { useRef, useContext, createElement, useSyncExternalStore } from 'react'; | ||
import React, { useRef, useContext, createElement, cloneElement, useSyncExternalStore } from 'react'; | ||
import { Platform } from 'react-native'; | ||
@@ -419,3 +419,3 @@ import { valueFromASTUntyped } from 'graphql/utilities/valueFromASTUntyped'; | ||
var container = options.container || function () { | ||
var getData = options.data || function () { | ||
return null; | ||
@@ -428,7 +428,7 @@ }; | ||
client = _useContext.client, | ||
node = _useContext.node; | ||
store = _useContext.store; | ||
try { | ||
var _Resource$all = Resource.all([function () { | ||
return container(node, values); | ||
return getData(store, values); | ||
}, function () { | ||
@@ -441,2 +441,6 @@ return component(); | ||
if (options.waitForAll) { | ||
store.waitForAll(); | ||
} | ||
return /*#__PURE__*/createElement(Component, resolvedValues); | ||
@@ -454,3 +458,3 @@ } catch (error) { | ||
if (!client.ssr) { | ||
return options.loading && /*#__PURE__*/createElement(options.loading); | ||
return options.loading; | ||
} | ||
@@ -470,7 +474,12 @@ | ||
if (client.ssr && async.current) { | ||
return options.loading && /*#__PURE__*/createElement(options.loading); | ||
return options.loading; | ||
} // Re-throw error if container should throw. | ||
if (options.throwOnError) { | ||
throw error; | ||
} // Render error page for synchronous error. | ||
return options.error && /*#__PURE__*/createElement(options.error, { | ||
return options.error && /*#__PURE__*/cloneElement(options.error, { | ||
error: error | ||
@@ -510,4 +519,4 @@ }); | ||
var applyRenderer = function applyRenderer(Component, options) { | ||
return options.renderer ? options.renderer(Component) : Component; | ||
var applyRenderer = function applyRenderer(component, renderer) { | ||
return renderer ? renderer(component) : component; | ||
}; | ||
@@ -521,3 +530,3 @@ | ||
function resolveComponent(component, options) { | ||
function resolveComponent(component, renderer) { | ||
// Handle React lazy and driveline/lazy | ||
@@ -556,3 +565,3 @@ // eslint-disable-next-line no-underscore-dangle | ||
return component.bundle().then(function (result) { | ||
return applyRenderer(resolveDefaultImport(result), options); | ||
return applyRenderer(resolveDefaultImport(result), renderer); | ||
}); | ||
@@ -563,6 +572,13 @@ }); | ||
return new SyncResource(applyRenderer(component, options)); | ||
return new SyncResource(applyRenderer(component, renderer)); | ||
} | ||
function createContainer(component, options) { | ||
function createContainer(config) { | ||
var component = config.component, | ||
options = _objectWithoutProperties(config, ["component"]); | ||
if (!component) { | ||
throw new Error("You must define a container \"component\"."); | ||
} | ||
var resource; | ||
@@ -572,3 +588,3 @@ | ||
if (!resource) { | ||
resource = resolveComponent(component, options); | ||
resource = resolveComponent(component, options.renderer); | ||
} | ||
@@ -591,3 +607,3 @@ | ||
_defineProperty(_assertThisInitialized(_this), "node", void 0); | ||
_defineProperty(_assertThisInitialized(_this), "store", void 0); | ||
@@ -609,3 +625,3 @@ _this.state = { | ||
value: function componentWillUnmount() { | ||
this.node.destroy(); | ||
this.store.destroy(); | ||
} | ||
@@ -619,3 +635,3 @@ }, { | ||
if (error) { | ||
return options.error && /*#__PURE__*/createElement(options.error, { | ||
return options.error && /*#__PURE__*/cloneElement(options.error, { | ||
error: error | ||
@@ -636,3 +652,3 @@ }); | ||
return /*#__PURE__*/React.createElement(React.Suspense, { | ||
fallback: options.loading && /*#__PURE__*/createElement(options.loading) | ||
fallback: options.loading | ||
}, handler); | ||
@@ -645,6 +661,6 @@ } | ||
client = _this$context.client, | ||
parent = _this$context.node; | ||
parentStore = _this$context.store; | ||
if (!this.node) { | ||
this.node = client.createNode(parent); | ||
if (!this.store) { | ||
this.store = client.createStore(parentStore); | ||
} | ||
@@ -655,18 +671,20 @@ | ||
client: client, | ||
node: this.node | ||
store: this.store | ||
} | ||
}, this.renderContainer()); | ||
} | ||
}], [{ | ||
key: "getDerivedStateFromError", | ||
value: function getDerivedStateFromError(error) { | ||
return { | ||
error: error | ||
}; | ||
} | ||
}]); | ||
return Container; | ||
}(React.Component); | ||
}(React.Component); // Add error boundary if container should not throw. | ||
if (!options.throwOnError) { | ||
Container.getDerivedStateFromError = function (error) { | ||
return { | ||
error: error | ||
}; | ||
}; | ||
} | ||
Container.contextType = TransporterContext; | ||
@@ -678,2 +696,27 @@ var name = component.displayName || component.name; | ||
function createNode(config) { | ||
var component = config.component, | ||
options = _objectWithoutProperties(config, ["component"]); | ||
if (!config.component) { | ||
throw new Error("You must define a container \"component\"."); | ||
} | ||
function ContainerNode(props) { | ||
var _useContext = useContext(TransporterContext), | ||
store = _useContext.store; | ||
var getValues = options.data || function () { | ||
return null; | ||
}; | ||
/*#__PURE__*/ | ||
createElement(component, getValues(store, props)); | ||
} | ||
var name = component.displayName || component.name; | ||
ContainerNode.displayName = name ? "ContainerNode(".concat(name, ")") : 'ContainerNode'; | ||
return ContainerNode; | ||
} | ||
var _this = undefined; | ||
@@ -1286,2 +1329,5 @@ | ||
_this = _super.call(this, message, options); | ||
_defineProperty(_assertThisInitialized(_this), "type", void 0); | ||
_this.name = 'TransporterError'; | ||
@@ -1638,8 +1684,8 @@ _this.type = type; | ||
var StoreNode = function StoreNode(parent, executeQuery) { | ||
var Store = function Store(parentStore, executeQuery) { | ||
var _this = this; | ||
_classCallCheck(this, StoreNode); | ||
_classCallCheck(this, Store); | ||
_defineProperty(this, "parent", void 0); | ||
_defineProperty(this, "parentStore", void 0); | ||
@@ -1702,7 +1748,7 @@ _defineProperty(this, "executeQuery", void 0); | ||
if (!request) { | ||
if (!_this.parent) { | ||
if (!_this.parentStore) { | ||
throw new Error("Query \"".concat(name, "\" was not found.")); | ||
} | ||
request = _this.parent.getRequest(name); | ||
request = _this.parentStore.getRequest(name); | ||
} | ||
@@ -1731,2 +1777,12 @@ | ||
_defineProperty(this, "waitForAll", function () { | ||
_this.requests.forEach(function (request) { | ||
request.read(); | ||
}); | ||
if (_this.parentStore) { | ||
_this.parentStore.waitForAll(); | ||
} | ||
}); | ||
_defineProperty(this, "getFragmentRequest", function (name, entry) { | ||
@@ -1739,3 +1795,3 @@ var request = Array.from(_this.requests.values()).find(function (_ref) { | ||
if (!request) { | ||
if (!_this.parent) { | ||
if (!_this.parentStore) { | ||
var stringifiedEntry = JSON.stringify(entry); | ||
@@ -1745,3 +1801,3 @@ throw new Error("Fragment \"".concat(name, "\" (entry: [").concat(stringifiedEntry, "]) was not found.")); | ||
request = _this.parent.getFragmentRequest(name, entry); | ||
request = _this.parentStore.getFragmentRequest(name, entry); | ||
} | ||
@@ -1823,4 +1879,4 @@ | ||
_defineProperty(this, "destroy", function () { | ||
// Remove network from parent's network children. | ||
_this.parent.removeChild(_this); // Delete requests. | ||
// Remove network from parent store network children. | ||
_this.parentStore.removeChild(_this); // Delete requests. | ||
@@ -1833,3 +1889,3 @@ | ||
this.parent = parent; | ||
this.parentStore = parentStore; | ||
this.executeQuery = executeQuery; | ||
@@ -1839,4 +1895,4 @@ this.requests = new Map(); | ||
if (parent) { | ||
parent.addChild(this); | ||
if (parentStore) { | ||
parentStore.addChild(this); | ||
} | ||
@@ -1860,3 +1916,3 @@ }; | ||
_defineProperty(this, "root", void 0); | ||
_defineProperty(this, "rootStore", void 0); | ||
@@ -1873,4 +1929,4 @@ _defineProperty(this, "cache", void 0); | ||
_defineProperty(this, "createNode", function (parent) { | ||
return new StoreNode(parent, _this.query); | ||
_defineProperty(this, "createStore", function (parentStore) { | ||
return new Store(parentStore, _this.query); | ||
}); | ||
@@ -1883,11 +1939,11 @@ | ||
_defineProperty(this, "refresh", function () { | ||
_this.root.refresh(); | ||
_this.rootStore.refresh(); | ||
}); | ||
_defineProperty(this, "reset", function () { | ||
_this.root.reset(); | ||
_this.rootStore.reset(); | ||
_this.queries = new Map(); | ||
_this.root.refresh(); | ||
_this.rootStore.refresh(); | ||
}); | ||
@@ -1906,3 +1962,3 @@ | ||
this.request = request; | ||
this.root = this.createNode(null); | ||
this.rootStore = this.createStore(null); | ||
this.cache = cache; | ||
@@ -1923,4 +1979,4 @@ this.ssr = ssr; | ||
var node = client.root; | ||
useSyncExternalStore(node.subscribe, function () {}, function () {} // TODO: Check this! | ||
var store = client.rootStore; | ||
useSyncExternalStore(store.subscribe, function () {}, function () {} // TODO: Check this! | ||
); | ||
@@ -1930,3 +1986,3 @@ return /*#__PURE__*/React.createElement(TransporterContext.Provider, { | ||
client: client, | ||
node: node | ||
store: store | ||
} | ||
@@ -2006,2 +2062,2 @@ }, children); | ||
export { ReferenceMap, Resource, Transporter, TransporterProvider, createContainer, ref, useMutation, useReset }; | ||
export { ReferenceMap, Resource, Transporter, TransporterError, TransporterProvider, createContainer, createNode, ref, useMutation, useReset }; |
@@ -425,3 +425,3 @@ (function (global, factory) { | ||
var container = options.container || function () { | ||
var getData = options.data || function () { | ||
return null; | ||
@@ -434,7 +434,7 @@ }; | ||
client = _useContext.client, | ||
node = _useContext.node; | ||
store = _useContext.store; | ||
try { | ||
var _Resource$all = Resource.all([function () { | ||
return container(node, values); | ||
return getData(store, values); | ||
}, function () { | ||
@@ -447,2 +447,6 @@ return component(); | ||
if (options.waitForAll) { | ||
store.waitForAll(); | ||
} | ||
return /*#__PURE__*/React.createElement(Component, resolvedValues); | ||
@@ -460,3 +464,3 @@ } catch (error) { | ||
if (!client.ssr) { | ||
return options.loading && /*#__PURE__*/React.createElement(options.loading); | ||
return options.loading; | ||
} | ||
@@ -476,7 +480,12 @@ | ||
if (client.ssr && async.current) { | ||
return options.loading && /*#__PURE__*/React.createElement(options.loading); | ||
return options.loading; | ||
} // Re-throw error if container should throw. | ||
if (options.throwOnError) { | ||
throw error; | ||
} // Render error page for synchronous error. | ||
return options.error && /*#__PURE__*/React.createElement(options.error, { | ||
return options.error && /*#__PURE__*/React.cloneElement(options.error, { | ||
error: error | ||
@@ -516,4 +525,4 @@ }); | ||
var applyRenderer = function applyRenderer(Component, options) { | ||
return options.renderer ? options.renderer(Component) : Component; | ||
var applyRenderer = function applyRenderer(component, renderer) { | ||
return renderer ? renderer(component) : component; | ||
}; | ||
@@ -527,3 +536,3 @@ | ||
function resolveComponent(component, options) { | ||
function resolveComponent(component, renderer) { | ||
// Handle React lazy and driveline/lazy | ||
@@ -562,3 +571,3 @@ // eslint-disable-next-line no-underscore-dangle | ||
return component.bundle().then(function (result) { | ||
return applyRenderer(resolveDefaultImport(result), options); | ||
return applyRenderer(resolveDefaultImport(result), renderer); | ||
}); | ||
@@ -569,6 +578,13 @@ }); | ||
return new SyncResource(applyRenderer(component, options)); | ||
return new SyncResource(applyRenderer(component, renderer)); | ||
} | ||
function createContainer(component, options) { | ||
function createContainer(config) { | ||
var component = config.component, | ||
options = _objectWithoutProperties(config, ["component"]); | ||
if (!component) { | ||
throw new Error("You must define a container \"component\"."); | ||
} | ||
var resource; | ||
@@ -578,3 +594,3 @@ | ||
if (!resource) { | ||
resource = resolveComponent(component, options); | ||
resource = resolveComponent(component, options.renderer); | ||
} | ||
@@ -597,3 +613,3 @@ | ||
_defineProperty(_assertThisInitialized(_this), "node", void 0); | ||
_defineProperty(_assertThisInitialized(_this), "store", void 0); | ||
@@ -615,3 +631,3 @@ _this.state = { | ||
value: function componentWillUnmount() { | ||
this.node.destroy(); | ||
this.store.destroy(); | ||
} | ||
@@ -625,3 +641,3 @@ }, { | ||
if (error) { | ||
return options.error && /*#__PURE__*/React.createElement(options.error, { | ||
return options.error && /*#__PURE__*/React.cloneElement(options.error, { | ||
error: error | ||
@@ -642,3 +658,3 @@ }); | ||
return /*#__PURE__*/React__default['default'].createElement(React__default['default'].Suspense, { | ||
fallback: options.loading && /*#__PURE__*/React.createElement(options.loading) | ||
fallback: options.loading | ||
}, handler); | ||
@@ -651,6 +667,6 @@ } | ||
client = _this$context.client, | ||
parent = _this$context.node; | ||
parentStore = _this$context.store; | ||
if (!this.node) { | ||
this.node = client.createNode(parent); | ||
if (!this.store) { | ||
this.store = client.createStore(parentStore); | ||
} | ||
@@ -661,18 +677,20 @@ | ||
client: client, | ||
node: this.node | ||
store: this.store | ||
} | ||
}, this.renderContainer()); | ||
} | ||
}], [{ | ||
key: "getDerivedStateFromError", | ||
value: function getDerivedStateFromError(error) { | ||
return { | ||
error: error | ||
}; | ||
} | ||
}]); | ||
return Container; | ||
}(React__default['default'].Component); | ||
}(React__default['default'].Component); // Add error boundary if container should not throw. | ||
if (!options.throwOnError) { | ||
Container.getDerivedStateFromError = function (error) { | ||
return { | ||
error: error | ||
}; | ||
}; | ||
} | ||
Container.contextType = TransporterContext; | ||
@@ -684,2 +702,27 @@ var name = component.displayName || component.name; | ||
function createNode(config) { | ||
var component = config.component, | ||
options = _objectWithoutProperties(config, ["component"]); | ||
if (!config.component) { | ||
throw new Error("You must define a container \"component\"."); | ||
} | ||
function ContainerNode(props) { | ||
var _useContext = React.useContext(TransporterContext), | ||
store = _useContext.store; | ||
var getValues = options.data || function () { | ||
return null; | ||
}; | ||
/*#__PURE__*/ | ||
React.createElement(component, getValues(store, props)); | ||
} | ||
var name = component.displayName || component.name; | ||
ContainerNode.displayName = name ? "ContainerNode(".concat(name, ")") : 'ContainerNode'; | ||
return ContainerNode; | ||
} | ||
var _this = undefined; | ||
@@ -1292,2 +1335,5 @@ | ||
_this = _super.call(this, message, options); | ||
_defineProperty(_assertThisInitialized(_this), "type", void 0); | ||
_this.name = 'TransporterError'; | ||
@@ -1644,8 +1690,8 @@ _this.type = type; | ||
var StoreNode = function StoreNode(parent, executeQuery) { | ||
var Store = function Store(parentStore, executeQuery) { | ||
var _this = this; | ||
_classCallCheck(this, StoreNode); | ||
_classCallCheck(this, Store); | ||
_defineProperty(this, "parent", void 0); | ||
_defineProperty(this, "parentStore", void 0); | ||
@@ -1708,7 +1754,7 @@ _defineProperty(this, "executeQuery", void 0); | ||
if (!request) { | ||
if (!_this.parent) { | ||
if (!_this.parentStore) { | ||
throw new Error("Query \"".concat(name, "\" was not found.")); | ||
} | ||
request = _this.parent.getRequest(name); | ||
request = _this.parentStore.getRequest(name); | ||
} | ||
@@ -1737,2 +1783,12 @@ | ||
_defineProperty(this, "waitForAll", function () { | ||
_this.requests.forEach(function (request) { | ||
request.read(); | ||
}); | ||
if (_this.parentStore) { | ||
_this.parentStore.waitForAll(); | ||
} | ||
}); | ||
_defineProperty(this, "getFragmentRequest", function (name, entry) { | ||
@@ -1745,3 +1801,3 @@ var request = Array.from(_this.requests.values()).find(function (_ref) { | ||
if (!request) { | ||
if (!_this.parent) { | ||
if (!_this.parentStore) { | ||
var stringifiedEntry = JSON.stringify(entry); | ||
@@ -1751,3 +1807,3 @@ throw new Error("Fragment \"".concat(name, "\" (entry: [").concat(stringifiedEntry, "]) was not found.")); | ||
request = _this.parent.getFragmentRequest(name, entry); | ||
request = _this.parentStore.getFragmentRequest(name, entry); | ||
} | ||
@@ -1829,4 +1885,4 @@ | ||
_defineProperty(this, "destroy", function () { | ||
// Remove network from parent's network children. | ||
_this.parent.removeChild(_this); // Delete requests. | ||
// Remove network from parent store network children. | ||
_this.parentStore.removeChild(_this); // Delete requests. | ||
@@ -1839,3 +1895,3 @@ | ||
this.parent = parent; | ||
this.parentStore = parentStore; | ||
this.executeQuery = executeQuery; | ||
@@ -1845,4 +1901,4 @@ this.requests = new Map(); | ||
if (parent) { | ||
parent.addChild(this); | ||
if (parentStore) { | ||
parentStore.addChild(this); | ||
} | ||
@@ -1866,3 +1922,3 @@ }; | ||
_defineProperty(this, "root", void 0); | ||
_defineProperty(this, "rootStore", void 0); | ||
@@ -1879,4 +1935,4 @@ _defineProperty(this, "cache", void 0); | ||
_defineProperty(this, "createNode", function (parent) { | ||
return new StoreNode(parent, _this.query); | ||
_defineProperty(this, "createStore", function (parentStore) { | ||
return new Store(parentStore, _this.query); | ||
}); | ||
@@ -1889,11 +1945,11 @@ | ||
_defineProperty(this, "refresh", function () { | ||
_this.root.refresh(); | ||
_this.rootStore.refresh(); | ||
}); | ||
_defineProperty(this, "reset", function () { | ||
_this.root.reset(); | ||
_this.rootStore.reset(); | ||
_this.queries = new Map(); | ||
_this.root.refresh(); | ||
_this.rootStore.refresh(); | ||
}); | ||
@@ -1912,3 +1968,3 @@ | ||
this.request = request; | ||
this.root = this.createNode(null); | ||
this.rootStore = this.createStore(null); | ||
this.cache = cache; | ||
@@ -1929,4 +1985,4 @@ this.ssr = ssr; | ||
var node = client.root; | ||
React.useSyncExternalStore(node.subscribe, function () {}, function () {} // TODO: Check this! | ||
var store = client.rootStore; | ||
React.useSyncExternalStore(store.subscribe, function () {}, function () {} // TODO: Check this! | ||
); | ||
@@ -1936,3 +1992,3 @@ return /*#__PURE__*/React__default['default'].createElement(TransporterContext.Provider, { | ||
client: client, | ||
node: node | ||
store: store | ||
} | ||
@@ -2015,4 +2071,6 @@ }, children); | ||
exports.Transporter = Transporter; | ||
exports.TransporterError = TransporterError; | ||
exports.TransporterProvider = TransporterProvider; | ||
exports.createContainer = createContainer; | ||
exports.createNode = createNode; | ||
exports.ref = ref; | ||
@@ -2019,0 +2077,0 @@ exports.useMutation = useMutation; |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("react-native"),require("graphql/utilities/valueFromASTUntyped")):"function"==typeof define&&define.amd?define(["exports","react","react-native","graphql/utilities/valueFromASTUntyped"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["react-transporter"]={},t.React,t.reactNative,t.valueFromASTUntyped)}(this,(function(t,e,r,n){"use strict";function i(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var o=i(e);function u(t){return(u="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function a(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function s(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function c(t,e,r){return e&&s(t.prototype,e),r&&s(t,r),t}function f(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function l(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function h(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?l(Object(r),!0).forEach((function(e){f(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):l(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}function d(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&p(t,e)}function v(t){return(v=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function p(t,e){return(p=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function y(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}function m(t,e,r){return(m=y()?Reflect.construct:function(t,e,r){var n=[null];n.push.apply(n,e);var i=new(Function.bind.apply(t,n));return r&&p(i,r.prototype),i}).apply(null,arguments)}function g(t){var e="function"==typeof Map?new Map:void 0;return(g=function(t){if(null===t||(r=t,-1===Function.toString.call(r).indexOf("[native code]")))return t;var r;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,n)}function n(){return m(t,arguments,v(this).constructor)}return n.prototype=Object.create(t.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),p(n,t)})(t)}function b(t,e){if(null==t)return{};var r,n,i=function(t,e){if(null==t)return{};var r,n,i={},o=Object.keys(t);for(n=0;n<o.length;n++)r=o[n],e.indexOf(r)>=0||(i[r]=t[r]);return i}(t,e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(t);for(n=0;n<o.length;n++)r=o[n],e.indexOf(r)>=0||Object.prototype.propertyIsEnumerable.call(t,r)&&(i[r]=t[r])}return i}function w(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function E(t,e){return!e||"object"!=typeof e&&"function"!=typeof e?w(t):e}function q(t){var e=y();return function(){var r,n=v(t);if(e){var i=v(this).constructor;r=Reflect.construct(n,arguments,i)}else r=n.apply(this,arguments);return E(this,r)}}function O(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(t)))return;var r=[],n=!0,i=!1,o=void 0;try{for(var u,a=t[Symbol.iterator]();!(n=(u=a.next()).done)&&(r.push(u.value),!e||r.length!==e);n=!0);}catch(t){i=!0,o=t}finally{try{n||null==a.return||a.return()}finally{if(i)throw o}}return r}(t,e)||j(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function S(t){return function(t){if(Array.isArray(t))return k(t)}(t)||function(t){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t))return Array.from(t)}(t)||j(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function j(t,e){if(t){if("string"==typeof t)return k(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?k(t,e):void 0}}function k(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}var R=o.default.createContext(),_="undefined"==typeof window,P=!r.Platform||"web"===r.Platform.OS,A="pending",x="fulfilled",F="rejected",C="__typename",T="id",U="__ref",M=function t(e){var r=this,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};a(this,t),f(this,"promise",void 0),f(this,"status",A),f(this,"response",void 0),f(this,"read",(function(){switch(r.status){case A:throw r.promise;case F:throw r.response;default:return r.response}}));var i=n.proxy,o=void 0!==i&&i;o?this.promise=new Promise((function(t,e){e(new Error("Proxied resource."))})):(this.promise=e(),this.promise.then((function(t){r.status=x,r.response=t}),(function(t){r.status=F,r.response=t})))};function Q(t){var r=t.component,n=t.options,i=t.values,o=n.container||function(){return null},u=e.useRef(!1),a=e.useContext(R),s=a.client,c=a.node;try{var f=O(M.all([function(){return o(c,i)},function(){return r()}]),2),l=f[0],h=f[1];return e.createElement(h,l)}catch(t){if(!_)throw t;if(t instanceof Promise){if(!s.ssr)return n.loading&&e.createElement(n.loading);throw u.current||(u.current=!0),t}return s.ssr&&u.current?n.loading&&e.createElement(n.loading):n.error&&e.createElement(n.error,{error:t})}}M.all=function(t){var e=[],r=t.map((function(t){try{return t()}catch(t){if(t instanceof Promise)return e.push(t);throw t}}));if(e.length>0)throw Promise.all(e);return r};var N=function t(e){var r=this;a(this,t),f(this,"status",x),f(this,"response",void 0),f(this,"read",(function(){return r.response})),this.response=e},B=function(t,e){return e.renderer?e.renderer(t):t},D=function(t){return t.__esModule?t.default:t.default||t};function I(t,e){if(t.payload&&t.payload._result){var r=t.payload._result;return"function"==typeof r?new M((function(){return r()})):_||!P?new M((function(){return t.load().then((function(){return t}))})):function(t){var e=t.resolve();return"undefined"!=typeof __webpack_modules__&&!!__webpack_modules__[e]}(r)?new N(D(r.requireSync())):new M((function(){return r.requireAsync().then((function(t){return D(t)}))}))}return t.bundle?new M((function(){return t.bundle().then((function(t){return B(D(t),e)}))})):new N(B(t,e))}var J=void 0,H=function(t,e){return e.filter((function(e){return e.__ref?!t.some((function(t){var r=O(t,2),n=r[0],i=r[1];return n===e[0]&&i===e[1]})):J.items.includes(e)}))},G=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];a(this,t),f(this,"items",void 0),this.items=e}return c(t,[{key:"prepend",value:function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];return this.items=[].concat(e,S(this.items)),this}},{key:"append",value:function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];return this.items=[].concat(S(this.items),e),this}},{key:"syncPrepend",value:function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];var n=H(this.items,e);return this.items=[].concat(e,S(n)),this}},{key:"syncAppend",value:function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];var n=H(this.items,e);return this.items=[].concat(S(n),e),this}},{key:"detach",value:function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];return this.items=H(this.items,e),this}},{key:"toArray",value:function(){return this.items}}]),t}();var L=function(t,e){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:function(){};return Object.keys(e).some((function(e){return void 0!==t[e]&&r(e)}))},V=function(t,e){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:function(t){return t};if(!r)return h(h({},t),e);var n=h({},t);return Object.values(e).forEach((function(e){var i=O(e,2),o=i[0],u=i[1];n[o]=void 0===t[o]?u:r(o)})),n},W=function t(e,r){if(!r)return e;if(Array.isArray(e))return e.map((function(e,n){return t(e,r[n])}));if("object"!==("undefined"==typeof value?"undefined":u(value))||e.__ref)return r;var n={};return Object.keys(e).forEach((function(i){n[i]=t(e[i],r[i])})),n},Y=function t(){var e=this,r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};a(this,t),f(this,"roots",void 0),f(this,"entities",void 0),f(this,"get",(function(t,r){var n,i,o=null===(n=e.entities[t])||void 0===n?void 0:n[r];return o?h((f(i={},C,t),f(i,T,r),i),o):null})),f(this,"getRoots",(function(){return e.roots})),f(this,"intersectWith",(function(t){var r=t.roots,n=t.entities;return L(e.roots,r),L(e.entities,n,(function(t){return L(e.entities[t],n[t],(function(r){return L(e.entities[t][r],n[t][r])}))}))})),f(this,"add",(function(t){var r=t.roots,n=t.entities;e.roots=V(e.roots,r),e.entities=V(e.entities,n,(function(t){return V(e.entities[t],n[t],(function(r){var i=e.entities[t][r],o=n[t][r],u=h({},i);return Object.keys(o).forEach((function(t){u[t]=W(i[t],o[t])})),u}))}))})),f(this,"merge",(function(r){var n=new t({roots:e.roots,entities:e.entities});return n.add(r),n})),f(this,"extract",(function(){return{roots:e.roots,entities:e.entities}}));var n=r.roots,i=void 0===n?{}:n,o=r.entities,u=void 0===o?{}:o;this.roots=i,this.entities=u},$=function(t,e){var r=O(e,2),n=r[0],i=r[1];return"".concat(t,".").concat(n,".").concat(i)},z=function t(){var e=this;a(this,t),f(this,"query",null),f(this,"fragments",{}),f(this,"getQuery",(function(){return e.query})),f(this,"setQuery",(function(t){e.query=t})),f(this,"getFragment",(function(t,r){var n=$(t,r);return e.fragments[n]})),f(this,"setFragment",(function(t,r,n){var i=$(t,r);e.fragments[i]=n})),f(this,"update",(function(t){var r=!1;return e.query&&e.query!==t.query&&(e.query=t.query,r=!0),Object.keys(e.fragments).forEach((function(n){e.fragments[n]!==t.fragments[n]&&(e.fragments[n]=t.fragments[n],r=!0)})),r}))},K=function(t){return null===t?null:"object"===u(t)?X(t):JSON.stringify(t)},X=function(t){var e=Object.keys(t);e.sort();var r=e.map((function(e){var r=K(t[e]);return null===r?r:'"'.concat(e,'":').concat(r)})).filter((function(t){return null!==t}));return 0===r.length?null:"{".concat(r.join(","),"}")};function Z(t,e){var r=X(e);return"".concat(t).concat(r?"(".concat(r,")"):"")}var tt=function(t){d(r,t);var e=q(r);function r(){var t;return a(this,r),(t=e.call(this,"Undefined value.")).name="ValueError",t}return r}(g(Error)),et=function t(e,r,i){var o=i.cache,u=i.handleFragment,a=o.request,s=a.ast,c=a.options,f={};return e.selections.forEach((function(e){if("Field"===e.kind){var o=function(t,e){var r=t.name.value;if(0===t.arguments.length)return r;var i={};return t.arguments.forEach((function(t){i[t.name.value]=n.valueFromASTUntyped(t.value,e)})),Z(r,i)}(e,c.variables);f[e.name.value]=rt(e.selectionSet,r[o],i)}if("InlineFragment"===e.kind&&console.log("TODO: inline fragment",e),"FragmentSpread"===e.kind){var a=s.definitions.find((function(t){return"FragmentDefinition"===t.kind&&t.name.value===e.name.value}));try{var l=t(a.selectionSet,r,i);Object.assign(f,u(a.name.value,r[C],r.id,l))}catch(t){if("ValueError"!==t.name)throw t;if(a.typeCondition.name.value===r[C])throw new Error('Fragment "'.concat(a.name.value,'" has an undefined value and has been skipped.'))}}})),f},rt=function(t,e,r){return void 0===t?e:Array.isArray(e)?e.map((function(e){return nt(t,e,r)})):nt(t,e,r)},nt=function(t,e,r){if(void 0===e)throw new tt;if(null===e)return null;var n=e.__ref;if(!n)return et(t,e,r);var i=r.cache,o=r.handleEntity,u=O(n,2),a=u[0],s=u[1],c=i.data.get(a,s);return o(a,s,et(t,c,r))};function it(t,e,r){var n=t.request.ast.definitions.find((function(t){return"OperationDefinition"===t.kind})),i=t.data.getRoots(),o={cache:t,handleFragment:e,handleEntity:r};try{return et(n.selectionSet,i,o)}catch(t){throw"ValueError"!==t.name?t:new Error('Query "'.concat(n.name.value,'" has an undefined value and has been skipped.'))}}var ot=function(t,e){return JSON.stringify(t)===JSON.stringify(e)};function ut(t){var e,r=new z,n=it(t,(function(e,n,i,o){var u,a=[n,i],s=null===(u=t.selectorSet)||void 0===u?void 0:u.getFragment(e,a);return r.setFragment(e,a,ot(o,s)?s:o),{}}),(function(t,e,r){var n;return h((f(n={},C,t),f(n,T,e),n),r)})),i=null===(e=t.selectorSet)||void 0===e?void 0:e.getQuery();return r.setQuery(ot(n,i)?i:n),r}var at=function t(e,r){var n=this;a(this,t),f(this,"request",void 0),f(this,"original",void 0),f(this,"updates",[]),f(this,"dirty",!1),f(this,"data",void 0),f(this,"selectorSet",void 0),f(this,"addUpdate",(function(t){var e=arguments.length>1&&void 0!==arguments[1]&&arguments[1];if(n.data.intersectWith(t)){n.dirty=!0;var r={optimistic:e,data:t,cache:n.data.merge(t)};n.data=t,n.updates=[].concat(S(n.updates),[r])}})),f(this,"removeUpdate",(function(t){var e=!1,r=[];n.updates.forEach((function(i){if(i.data===t)n.dirty=!0,e=!0;else if(e){var o=r.length,u=o>0?r[o-1].cache:n.original;r.push(h(h({},i),{},{cache:u.merge(t)}))}else r.push(i)})),n.updates=r})),f(this,"commit",(function(){var t,e,r;n.dirty&&(n.selectorSet=ut(n),n.updates.some((function(t){return t.optimistic}))||(n.original=(t=n,e=new Y,r=it(t,(function(t,e,r,n){return n}),(function(t,r,n){return e.add({entities:f({},C,f({},T,n))}),f({},U,[t,r])})),e.add({roots:r}),e),n.updates=[]),n.dirty=!1)})),this.request=e,this.original=r,this.data=r,this.selectorSet=ut(this)},st=function(t){d(r,t);var e=q(r);function r(t,n){var i,o=n.type,u=b(n,["type"]);return a(this,r),(i=e.call(this,t,u)).name="TransporterError",i.type=o,i}return r}(g(Error));function ct(t,e,r){return new Promise((function(n,i){var o;t(e.loc.source.body,r).then((function(t){return(o=t).json()})).then((function(t){if(!o.ok)throw new st("Request failed (HttpError - ".concat(o.status,")"),{type:"HttpError",cause:t});t.errors&&(t.errors.forEach((function(t){console.error("GraphQLError: ".concat(t.message))})),i(new st("Request failed (GraphQLError)",{type:"GraphQLError",cause:t.errors}))),n(t)})).catch((function(t){o&&o.ok?i(new st("".concat(t.message," (JsonError)"),{type:"JsonError",cause:t})):i(new st("Request failed (HttpError - ".concat(o?o.status:"Unknown",")"),{type:"HttpError",cause:t}))}))}))}var ft=function t(){var e=this;a(this,t),f(this,"promise",void 0),f(this,"read",(function(){throw e.promise})),this.promise=new Promise((function(){}))},lt=function t(e,r){var n=this,i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};a(this,t),f(this,"store",void 0),f(this,"ast",void 0),f(this,"options",void 0),f(this,"resource",void 0),f(this,"selectors",[]),f(this,"loading",!1),f(this,"aborted",!1),f(this,"cache",void 0),f(this,"read",(function(){n.resource.read()})),f(this,"isEqual",(function(t,e){return n.ast===t&&JSON.stringify(n.options.variables)===JSON.stringify(e)})),f(this,"sync",(function(){n.loading||n.aborted||n.cache.commit()})),f(this,"invalidate",(function(){n.aborted=!0,n.store.queries.delete(n.options.name)})),this.store=e,this.ast=r,this.options=i;var o=function(t){if(!n.aborted){var r=new Y(t.data),i=new Y({entities:r.entities});e.queries.forEach((function(t){t.addUpdate(i)})),n.cache=new at(n,r),e.queries.set(n.options.name,n.cache),e.refresh()}},u=e.cache,s=u[this.options.name];s?(this.resource=new N(s),o({data:s}),delete u[this.options.name]):(this.resource=!_||e.ssr?new M((function(){return ct(e.request,r,i.variables)})):new ft,this.loading=!0,this.resource.promise.then((function(t){n.loading=!1,_&&(u[n.options.name]=t),o(t)}),(function(){n.loading=!1,n.aborted=!0})))},ht=function(t){return t instanceof G?t.toArray():(e=t,"[object Date]"===Object.prototype.toString.call(e)?t.toISOString():t);var e},dt=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};a(this,t),f(this,"original",void 0),f(this,"values",{}),this.original=e}return c(t,[{key:"set",value:function(t,e){var r,n=Array.isArray(t)?Z.apply(void 0,S(t)):t;"function"==typeof e?("function"==typeof this.original&&(this.original=this.original()||{}),r=e(Array.isArray(this.original[n])?new G(this.original[n]):this.original[n])):r=e;this.values[n]=ht(r)}},{key:"fill",value:function(t){var e=this;Object.keys(t).forEach((function(r){e.set(r,t[r])}))}}]),t}();function vt(t,e,r){if(!e)return r;var n=r;return e({insert:function(t,e,r){var i=new dt;r(i),n.add({entities:f({},t,f({},e,i.values))})},update:function(e,r,i){var o=new dt((function(){return h(h({},function(t,e,r){var n=null;return t.queries.forEach((function(t){var i=t.data.get(e,r);if(i){if(t.updates.some((function(t){return t.optimistic&&t.data.get(e,r)})))throw new Error("Cannot perform update on optimistically updated entity. [".concat(e,".").concat(r,"]"));n=h(h({},n),i)}})),n}(t,e,r)),n.get(e,r))}));i(o),n.add({entities:f({},e,f({},r,o.values))})},updateRoots:function(e){var r=new dt((function(){return function(t){var e=null;return t.queries.forEach((function(t){var r=t.data.getRoots();e=h(h({},e),r)})),e}(t)}));e(r),n.add({roots:r.values})}}),n}var pt=function t(e,r){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};a(this,t),f(this,"mutation",void 0),f(this,"options",void 0),f(this,"resource",void 0),this.mutation=r,this.options=n;var i=vt(e,n.optimisticUpdater,new Y);e.queries.forEach((function(t){t.addUpdate(i,!0)})),e.refresh(),this.resource=new M((function(){return ct(e.request,r,n.variables)})),this.resource.promise.then((function(t){var r=vt(e,n.updater,new Y({entities:t.entities}));i&&e.queries.forEach((function(t){t.removeUpdate(i)})),e.queries.forEach((function(t){t.addUpdate(r)})),e.refresh()}),(function(t){t.message&&console.error("Mutation Error: ".concat(t.message)),i&&(e.queries.forEach((function(t){t.removeUpdate(i)})),e.refresh())}))},yt=function t(e,r){var n=this;a(this,t),f(this,"parent",void 0),f(this,"executeQuery",void 0),f(this,"children",[]),f(this,"requests",void 0),f(this,"selectorSetsByRequest",void 0),f(this,"listeners",[]),f(this,"preload",(function(t){var e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=r.name||(null===(e=t.definitions.find((function(t){return"OperationDefinition"===t.kind})))||void 0===e?void 0:e.name.value);if(!i)throw new Error("No query name found.");var o=n.requests.get(i);return o&&!o.isEqual(t,r.variables)&&(n.requests.delete(i),n.selectorSetsByRequest.delete(i),o.invalidate(),o=null),o||(o=n.executeQuery(t,h(h({},r),{},{name:i})),n.requests.set(i,o)),i})),f(this,"load",(function(t,e){var r=n.preload(t,e);return n.select(r)})),f(this,"getRequest",(function(t){var e=n.requests.get(t);if(!e){if(!n.parent)throw new Error('Query "'.concat(t,'" was not found.'));e=n.parent.getRequest(t)}return e})),f(this,"select",(function(t){var e=n.getRequest(t);e.read();var r=e.cache.selectorSet.getQuery();return n.selectorSetsByRequest.has(e)||n.selectorSetsByRequest.set(e,new z),n.selectorSetsByRequest.get(e).setQuery(r),r})),f(this,"getFragmentRequest",(function(t,e){var r=Array.from(n.requests.values()).find((function(r){var n=r.cache;return null==n?void 0:n.selectorSet.getFragment(t,e)}));if(!r){if(!n.parent){var i=JSON.stringify(e);throw new Error('Fragment "'.concat(t,'" (entry: [').concat(i,"]) was not found."))}r=n.parent.getFragmentRequest(t,e)}return r})),f(this,"selectFragment",(function(t,e){var r=n.getFragmentRequest(t,e);r.read();var i=r.cache.selectorSet.getFragment(t,e);return n.selectorSetsByRequest.has(r)||n.selectorSetsByRequest.set(r,new z),n.selectorSetsByRequest.get(r).setFragment(t,e,i),i})),f(this,"addChild",(function(t){n.children=[].concat(S(n.children),[t])})),f(this,"removeChild",(function(t){n.children=n.children.filter((function(e){return e!==t}))})),f(this,"subscribe",(function(t){return n.listeners=[].concat(S(n.listeners),[t]),function(){n.listeners=n.listeners.filter((function(e){return e!==t}))}})),f(this,"refresh",(function(){n.requests.forEach((function(t){t.sync()}));var t=!1;n.selectorSetsByRequest.forEach((function(e,r){e.update(r.cache.selectorSet)&&(t=!0)})),t?n.listeners.forEach((function(t){t()})):n.children.forEach((function(t){t.refresh()}))})),f(this,"reset",(function(){n.requests=new Map,n.children.forEach((function(t){t.reset()}))})),f(this,"destroy",(function(){n.parent.removeChild(n),n.requests.forEach((function(t){t.invalidate()}))})),this.parent=e,this.executeQuery=r,this.requests=new Map,this.selectorSetsByRequest=new Map,e&&e.addChild(this)};var mt="pending",gt="fulfilled";t.ReferenceMap=G,t.Resource=M,t.Transporter=function t(e){var r=this,n=e.request,i=e.cache,o=void 0===i?{}:i,u=e.ssr,s=void 0!==u&&u;a(this,t),f(this,"request",void 0),f(this,"root",void 0),f(this,"cache",void 0),f(this,"ssr",void 0),f(this,"queries",void 0),f(this,"query",(function(t,e){return new lt(r,t,e)})),f(this,"createNode",(function(t){return new yt(t,r.query)})),f(this,"mutate",(function(t,e){return new pt(r,t,e)})),f(this,"refresh",(function(){r.root.refresh()})),f(this,"reset",(function(){r.root.reset(),r.queries=new Map,r.root.refresh()})),f(this,"extract",(function(){var t={};return r.queries.forEach((function(e,r){t[r]=e.data.extract()})),t})),this.request=n,this.root=this.createNode(null),this.cache=o,this.ssr=s,this.queries=new Map},t.TransporterProvider=function(t){var r=t.children,n=t.client;if(!n)throw new Error("TransporterProvider: You must provide a Transporter instance.");var i=n.root;return e.useSyncExternalStore(i.subscribe,(function(){}),(function(){})),o.default.createElement(R.Provider,{value:{client:n,node:i}},r)},t.createContainer=function(t,r){var n,i=function(){return n||(n=I(t,r)),n.read()},u=function(t){d(u,t);var n=q(u);function u(t){var e;return a(this,u),f(w(e=n.call(this,t)),"node",void 0),e.state={error:null},e}return c(u,[{key:"componentDidCatch",value:function(t,e){console.error(t,e)}},{key:"componentWillUnmount",value:function(){this.node.destroy()}},{key:"renderContainer",value:function(){var t=this.context.client,n=this.state.error;if(n)return r.error&&e.createElement(r.error,{error:n});var u=o.default.createElement(Q,{component:i,options:r,values:this.props});return _&&!t.ssr?u:o.default.createElement(o.default.Suspense,{fallback:r.loading&&e.createElement(r.loading)},u)}},{key:"render",value:function(){var t=this.context,e=t.client,r=t.node;return this.node||(this.node=e.createNode(r)),o.default.createElement(R.Provider,{value:{client:e,node:this.node}},this.renderContainer())}}],[{key:"getDerivedStateFromError",value:function(t){return{error:t}}}]),u}(o.default.Component);u.contextType=R;var s=t.displayName||t.name;return u.displayName=s?"Container(".concat(s,")"):"Container",u},t.ref=function(t,e){return f({},U,[t,e])},t.useMutation=function(t,r){var n,i=e.useContext(R);if(!i)throw new Error('"useMutation" hook is used outside of TransporterContext.');return[function(e){if(n)throw new Error("Mutation was dispatched before.");return n=i.store.mutate(t,h(h({},r),e))},{get ok(){return n&&n.status!==mt?n.status===gt:null},get loading(){return!!n&&n.status===mt},get executed(){return!!n&&(n.status===gt||"rejected"===n.status)}}]},t.useReset=function(){var t=e.useContext(R);if(!t)throw new Error('"useReset" hook is used outside of TransporterContext.');var r=t.store;return function(){r.reset()}},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("react-native"),require("graphql/utilities/valueFromASTUntyped")):"function"==typeof define&&define.amd?define(["exports","react","react-native","graphql/utilities/valueFromASTUntyped"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["react-transporter"]={},t.React,t.reactNative,t.valueFromASTUntyped)}(this,(function(t,e,r,n){"use strict";function o(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var i=o(e);function u(t){return(u="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function a(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function s(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function c(t,e,r){return e&&s(t.prototype,e),r&&s(t,r),t}function f(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function l(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function h(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?l(Object(r),!0).forEach((function(e){f(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):l(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}function d(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&p(t,e)}function v(t){return(v=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function p(t,e){return(p=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function y(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}function m(t,e,r){return(m=y()?Reflect.construct:function(t,e,r){var n=[null];n.push.apply(n,e);var o=new(Function.bind.apply(t,n));return r&&p(o,r.prototype),o}).apply(null,arguments)}function g(t){var e="function"==typeof Map?new Map:void 0;return(g=function(t){if(null===t||(r=t,-1===Function.toString.call(r).indexOf("[native code]")))return t;var r;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,n)}function n(){return m(t,arguments,v(this).constructor)}return n.prototype=Object.create(t.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),p(n,t)})(t)}function w(t,e){if(null==t)return{};var r,n,o=function(t,e){if(null==t)return{};var r,n,o={},i=Object.keys(t);for(n=0;n<i.length;n++)r=i[n],e.indexOf(r)>=0||(o[r]=t[r]);return o}(t,e);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(t);for(n=0;n<i.length;n++)r=i[n],e.indexOf(r)>=0||Object.prototype.propertyIsEnumerable.call(t,r)&&(o[r]=t[r])}return o}function b(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function S(t,e){return!e||"object"!=typeof e&&"function"!=typeof e?b(t):e}function E(t){var e=y();return function(){var r,n=v(t);if(e){var o=v(this).constructor;r=Reflect.construct(n,arguments,o)}else r=n.apply(this,arguments);return S(this,r)}}function q(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(t)))return;var r=[],n=!0,o=!1,i=void 0;try{for(var u,a=t[Symbol.iterator]();!(n=(u=a.next()).done)&&(r.push(u.value),!e||r.length!==e);n=!0);}catch(t){o=!0,i=t}finally{try{n||null==a.return||a.return()}finally{if(o)throw i}}return r}(t,e)||j(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function O(t){return function(t){if(Array.isArray(t))return k(t)}(t)||function(t){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t))return Array.from(t)}(t)||j(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function j(t,e){if(t){if("string"==typeof t)return k(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?k(t,e):void 0}}function k(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}var R=i.default.createContext(),A="undefined"==typeof window,_=!r.Platform||"web"===r.Platform.OS,P="pending",x="fulfilled",F="rejected",C="__typename",T="id",U="__ref",M=function t(e){var r=this,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};a(this,t),f(this,"promise",void 0),f(this,"status",P),f(this,"response",void 0),f(this,"read",(function(){switch(r.status){case P:throw r.promise;case F:throw r.response;default:return r.response}}));var o=n.proxy,i=void 0!==o&&o;i?this.promise=new Promise((function(t,e){e(new Error("Proxied resource."))})):(this.promise=e(),this.promise.then((function(t){r.status=x,r.response=t}),(function(t){r.status=F,r.response=t})))};function N(t){var r=t.component,n=t.options,o=t.values,i=n.data||function(){return null},u=e.useRef(!1),a=e.useContext(R),s=a.client,c=a.store;try{var f=q(M.all([function(){return i(c,o)},function(){return r()}]),2),l=f[0],h=f[1];return n.waitForAll&&c.waitForAll(),e.createElement(h,l)}catch(t){if(!A)throw t;if(t instanceof Promise){if(!s.ssr)return n.loading;throw u.current||(u.current=!0),t}if(s.ssr&&u.current)return n.loading;if(n.throwOnError)throw t;return n.error&&e.cloneElement(n.error,{error:t})}}M.all=function(t){var e=[],r=t.map((function(t){try{return t()}catch(t){if(t instanceof Promise)return e.push(t);throw t}}));if(e.length>0)throw Promise.all(e);return r};var Q=function t(e){var r=this;a(this,t),f(this,"status",x),f(this,"response",void 0),f(this,"read",(function(){return r.response})),this.response=e},B=function(t,e){return e?e(t):t},D=function(t){return t.__esModule?t.default:t.default||t};function I(t,e){if(t.payload&&t.payload._result){var r=t.payload._result;return"function"==typeof r?new M((function(){return r()})):A||!_?new M((function(){return t.load().then((function(){return t}))})):function(t){var e=t.resolve();return"undefined"!=typeof __webpack_modules__&&!!__webpack_modules__[e]}(r)?new Q(D(r.requireSync())):new M((function(){return r.requireAsync().then((function(t){return D(t)}))}))}return t.bundle?new M((function(){return t.bundle().then((function(t){return B(D(t),e)}))})):new Q(B(t,e))}var J=void 0,H=function(t,e){return e.filter((function(e){return e.__ref?!t.some((function(t){var r=q(t,2),n=r[0],o=r[1];return n===e[0]&&o===e[1]})):J.items.includes(e)}))},G=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];a(this,t),f(this,"items",void 0),this.items=e}return c(t,[{key:"prepend",value:function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];return this.items=[].concat(e,O(this.items)),this}},{key:"append",value:function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];return this.items=[].concat(O(this.items),e),this}},{key:"syncPrepend",value:function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];var n=H(this.items,e);return this.items=[].concat(e,O(n)),this}},{key:"syncAppend",value:function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];var n=H(this.items,e);return this.items=[].concat(O(n),e),this}},{key:"detach",value:function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];return this.items=H(this.items,e),this}},{key:"toArray",value:function(){return this.items}}]),t}();var L=function(t,e){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:function(){};return Object.keys(e).some((function(e){return void 0!==t[e]&&r(e)}))},V=function(t,e){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:function(t){return t};if(!r)return h(h({},t),e);var n=h({},t);return Object.values(e).forEach((function(e){var o=q(e,2),i=o[0],u=o[1];n[i]=void 0===t[i]?u:r(i)})),n},W=function t(e,r){if(!r)return e;if(Array.isArray(e))return e.map((function(e,n){return t(e,r[n])}));if("object"!==("undefined"==typeof value?"undefined":u(value))||e.__ref)return r;var n={};return Object.keys(e).forEach((function(o){n[o]=t(e[o],r[o])})),n},Y=function t(){var e=this,r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};a(this,t),f(this,"roots",void 0),f(this,"entities",void 0),f(this,"get",(function(t,r){var n,o,i=null===(n=e.entities[t])||void 0===n?void 0:n[r];return i?h((f(o={},C,t),f(o,T,r),o),i):null})),f(this,"getRoots",(function(){return e.roots})),f(this,"intersectWith",(function(t){var r=t.roots,n=t.entities;return L(e.roots,r),L(e.entities,n,(function(t){return L(e.entities[t],n[t],(function(r){return L(e.entities[t][r],n[t][r])}))}))})),f(this,"add",(function(t){var r=t.roots,n=t.entities;e.roots=V(e.roots,r),e.entities=V(e.entities,n,(function(t){return V(e.entities[t],n[t],(function(r){var o=e.entities[t][r],i=n[t][r],u=h({},o);return Object.keys(i).forEach((function(t){u[t]=W(o[t],i[t])})),u}))}))})),f(this,"merge",(function(r){var n=new t({roots:e.roots,entities:e.entities});return n.add(r),n})),f(this,"extract",(function(){return{roots:e.roots,entities:e.entities}}));var n=r.roots,o=void 0===n?{}:n,i=r.entities,u=void 0===i?{}:i;this.roots=o,this.entities=u},$=function(t,e){var r=q(e,2),n=r[0],o=r[1];return"".concat(t,".").concat(n,".").concat(o)},z=function t(){var e=this;a(this,t),f(this,"query",null),f(this,"fragments",{}),f(this,"getQuery",(function(){return e.query})),f(this,"setQuery",(function(t){e.query=t})),f(this,"getFragment",(function(t,r){var n=$(t,r);return e.fragments[n]})),f(this,"setFragment",(function(t,r,n){var o=$(t,r);e.fragments[o]=n})),f(this,"update",(function(t){var r=!1;return e.query&&e.query!==t.query&&(e.query=t.query,r=!0),Object.keys(e.fragments).forEach((function(n){e.fragments[n]!==t.fragments[n]&&(e.fragments[n]=t.fragments[n],r=!0)})),r}))},K=function(t){return null===t?null:"object"===u(t)?X(t):JSON.stringify(t)},X=function(t){var e=Object.keys(t);e.sort();var r=e.map((function(e){var r=K(t[e]);return null===r?r:'"'.concat(e,'":').concat(r)})).filter((function(t){return null!==t}));return 0===r.length?null:"{".concat(r.join(","),"}")};function Z(t,e){var r=X(e);return"".concat(t).concat(r?"(".concat(r,")"):"")}var tt=function(t){d(r,t);var e=E(r);function r(){var t;return a(this,r),(t=e.call(this,"Undefined value.")).name="ValueError",t}return r}(g(Error)),et=function t(e,r,o){var i=o.cache,u=o.handleFragment,a=i.request,s=a.ast,c=a.options,f={};return e.selections.forEach((function(e){if("Field"===e.kind){var i=function(t,e){var r=t.name.value;if(0===t.arguments.length)return r;var o={};return t.arguments.forEach((function(t){o[t.name.value]=n.valueFromASTUntyped(t.value,e)})),Z(r,o)}(e,c.variables);f[e.name.value]=rt(e.selectionSet,r[i],o)}if("InlineFragment"===e.kind&&console.log("TODO: inline fragment",e),"FragmentSpread"===e.kind){var a=s.definitions.find((function(t){return"FragmentDefinition"===t.kind&&t.name.value===e.name.value}));try{var l=t(a.selectionSet,r,o);Object.assign(f,u(a.name.value,r[C],r.id,l))}catch(t){if("ValueError"!==t.name)throw t;if(a.typeCondition.name.value===r[C])throw new Error('Fragment "'.concat(a.name.value,'" has an undefined value and has been skipped.'))}}})),f},rt=function(t,e,r){return void 0===t?e:Array.isArray(e)?e.map((function(e){return nt(t,e,r)})):nt(t,e,r)},nt=function(t,e,r){if(void 0===e)throw new tt;if(null===e)return null;var n=e.__ref;if(!n)return et(t,e,r);var o=r.cache,i=r.handleEntity,u=q(n,2),a=u[0],s=u[1],c=o.data.get(a,s);return i(a,s,et(t,c,r))};function ot(t,e,r){var n=t.request.ast.definitions.find((function(t){return"OperationDefinition"===t.kind})),o=t.data.getRoots(),i={cache:t,handleFragment:e,handleEntity:r};try{return et(n.selectionSet,o,i)}catch(t){throw"ValueError"!==t.name?t:new Error('Query "'.concat(n.name.value,'" has an undefined value and has been skipped.'))}}var it=function(t,e){return JSON.stringify(t)===JSON.stringify(e)};function ut(t){var e,r=new z,n=ot(t,(function(e,n,o,i){var u,a=[n,o],s=null===(u=t.selectorSet)||void 0===u?void 0:u.getFragment(e,a);return r.setFragment(e,a,it(i,s)?s:i),{}}),(function(t,e,r){var n;return h((f(n={},C,t),f(n,T,e),n),r)})),o=null===(e=t.selectorSet)||void 0===e?void 0:e.getQuery();return r.setQuery(it(n,o)?o:n),r}var at=function t(e,r){var n=this;a(this,t),f(this,"request",void 0),f(this,"original",void 0),f(this,"updates",[]),f(this,"dirty",!1),f(this,"data",void 0),f(this,"selectorSet",void 0),f(this,"addUpdate",(function(t){var e=arguments.length>1&&void 0!==arguments[1]&&arguments[1];if(n.data.intersectWith(t)){n.dirty=!0;var r={optimistic:e,data:t,cache:n.data.merge(t)};n.data=t,n.updates=[].concat(O(n.updates),[r])}})),f(this,"removeUpdate",(function(t){var e=!1,r=[];n.updates.forEach((function(o){if(o.data===t)n.dirty=!0,e=!0;else if(e){var i=r.length,u=i>0?r[i-1].cache:n.original;r.push(h(h({},o),{},{cache:u.merge(t)}))}else r.push(o)})),n.updates=r})),f(this,"commit",(function(){var t,e,r;n.dirty&&(n.selectorSet=ut(n),n.updates.some((function(t){return t.optimistic}))||(n.original=(t=n,e=new Y,r=ot(t,(function(t,e,r,n){return n}),(function(t,r,n){return e.add({entities:f({},C,f({},T,n))}),f({},U,[t,r])})),e.add({roots:r}),e),n.updates=[]),n.dirty=!1)})),this.request=e,this.original=r,this.data=r,this.selectorSet=ut(this)},st=function(t){d(r,t);var e=E(r);function r(t,n){var o,i=n.type,u=w(n,["type"]);return a(this,r),f(b(o=e.call(this,t,u)),"type",void 0),o.name="TransporterError",o.type=i,o}return r}(g(Error));function ct(t,e,r){return new Promise((function(n,o){var i;t(e.loc.source.body,r).then((function(t){return(i=t).json()})).then((function(t){if(!i.ok)throw new st("Request failed (HttpError - ".concat(i.status,")"),{type:"HttpError",cause:t});t.errors&&(t.errors.forEach((function(t){console.error("GraphQLError: ".concat(t.message))})),o(new st("Request failed (GraphQLError)",{type:"GraphQLError",cause:t.errors}))),n(t)})).catch((function(t){i&&i.ok?o(new st("".concat(t.message," (JsonError)"),{type:"JsonError",cause:t})):o(new st("Request failed (HttpError - ".concat(i?i.status:"Unknown",")"),{type:"HttpError",cause:t}))}))}))}var ft=function t(){var e=this;a(this,t),f(this,"promise",void 0),f(this,"read",(function(){throw e.promise})),this.promise=new Promise((function(){}))},lt=function t(e,r){var n=this,o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};a(this,t),f(this,"store",void 0),f(this,"ast",void 0),f(this,"options",void 0),f(this,"resource",void 0),f(this,"selectors",[]),f(this,"loading",!1),f(this,"aborted",!1),f(this,"cache",void 0),f(this,"read",(function(){n.resource.read()})),f(this,"isEqual",(function(t,e){return n.ast===t&&JSON.stringify(n.options.variables)===JSON.stringify(e)})),f(this,"sync",(function(){n.loading||n.aborted||n.cache.commit()})),f(this,"invalidate",(function(){n.aborted=!0,n.store.queries.delete(n.options.name)})),this.store=e,this.ast=r,this.options=o;var i=function(t){if(!n.aborted){var r=new Y(t.data),o=new Y({entities:r.entities});e.queries.forEach((function(t){t.addUpdate(o)})),n.cache=new at(n,r),e.queries.set(n.options.name,n.cache),e.refresh()}},u=e.cache,s=u[this.options.name];s?(this.resource=new Q(s),i({data:s}),delete u[this.options.name]):(this.resource=!A||e.ssr?new M((function(){return ct(e.request,r,o.variables)})):new ft,this.loading=!0,this.resource.promise.then((function(t){n.loading=!1,A&&(u[n.options.name]=t),i(t)}),(function(){n.loading=!1,n.aborted=!0})))},ht=function(t){return t instanceof G?t.toArray():(e=t,"[object Date]"===Object.prototype.toString.call(e)?t.toISOString():t);var e},dt=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};a(this,t),f(this,"original",void 0),f(this,"values",{}),this.original=e}return c(t,[{key:"set",value:function(t,e){var r,n=Array.isArray(t)?Z.apply(void 0,O(t)):t;"function"==typeof e?("function"==typeof this.original&&(this.original=this.original()||{}),r=e(Array.isArray(this.original[n])?new G(this.original[n]):this.original[n])):r=e;this.values[n]=ht(r)}},{key:"fill",value:function(t){var e=this;Object.keys(t).forEach((function(r){e.set(r,t[r])}))}}]),t}();function vt(t,e,r){if(!e)return r;var n=r;return e({insert:function(t,e,r){var o=new dt;r(o),n.add({entities:f({},t,f({},e,o.values))})},update:function(e,r,o){var i=new dt((function(){return h(h({},function(t,e,r){var n=null;return t.queries.forEach((function(t){var o=t.data.get(e,r);if(o){if(t.updates.some((function(t){return t.optimistic&&t.data.get(e,r)})))throw new Error("Cannot perform update on optimistically updated entity. [".concat(e,".").concat(r,"]"));n=h(h({},n),o)}})),n}(t,e,r)),n.get(e,r))}));o(i),n.add({entities:f({},e,f({},r,i.values))})},updateRoots:function(e){var r=new dt((function(){return function(t){var e=null;return t.queries.forEach((function(t){var r=t.data.getRoots();e=h(h({},e),r)})),e}(t)}));e(r),n.add({roots:r.values})}}),n}var pt=function t(e,r){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};a(this,t),f(this,"mutation",void 0),f(this,"options",void 0),f(this,"resource",void 0),this.mutation=r,this.options=n;var o=vt(e,n.optimisticUpdater,new Y);e.queries.forEach((function(t){t.addUpdate(o,!0)})),e.refresh(),this.resource=new M((function(){return ct(e.request,r,n.variables)})),this.resource.promise.then((function(t){var r=vt(e,n.updater,new Y({entities:t.entities}));o&&e.queries.forEach((function(t){t.removeUpdate(o)})),e.queries.forEach((function(t){t.addUpdate(r)})),e.refresh()}),(function(t){t.message&&console.error("Mutation Error: ".concat(t.message)),o&&(e.queries.forEach((function(t){t.removeUpdate(o)})),e.refresh())}))},yt=function t(e,r){var n=this;a(this,t),f(this,"parentStore",void 0),f(this,"executeQuery",void 0),f(this,"children",[]),f(this,"requests",void 0),f(this,"selectorSetsByRequest",void 0),f(this,"listeners",[]),f(this,"preload",(function(t){var e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=r.name||(null===(e=t.definitions.find((function(t){return"OperationDefinition"===t.kind})))||void 0===e?void 0:e.name.value);if(!o)throw new Error("No query name found.");var i=n.requests.get(o);return i&&!i.isEqual(t,r.variables)&&(n.requests.delete(o),n.selectorSetsByRequest.delete(o),i.invalidate(),i=null),i||(i=n.executeQuery(t,h(h({},r),{},{name:o})),n.requests.set(o,i)),o})),f(this,"load",(function(t,e){var r=n.preload(t,e);return n.select(r)})),f(this,"getRequest",(function(t){var e=n.requests.get(t);if(!e){if(!n.parentStore)throw new Error('Query "'.concat(t,'" was not found.'));e=n.parentStore.getRequest(t)}return e})),f(this,"select",(function(t){var e=n.getRequest(t);e.read();var r=e.cache.selectorSet.getQuery();return n.selectorSetsByRequest.has(e)||n.selectorSetsByRequest.set(e,new z),n.selectorSetsByRequest.get(e).setQuery(r),r})),f(this,"waitForAll",(function(){n.requests.forEach((function(t){t.read()})),n.parentStore&&n.parentStore.waitForAll()})),f(this,"getFragmentRequest",(function(t,e){var r=Array.from(n.requests.values()).find((function(r){var n=r.cache;return null==n?void 0:n.selectorSet.getFragment(t,e)}));if(!r){if(!n.parentStore){var o=JSON.stringify(e);throw new Error('Fragment "'.concat(t,'" (entry: [').concat(o,"]) was not found."))}r=n.parentStore.getFragmentRequest(t,e)}return r})),f(this,"selectFragment",(function(t,e){var r=n.getFragmentRequest(t,e);r.read();var o=r.cache.selectorSet.getFragment(t,e);return n.selectorSetsByRequest.has(r)||n.selectorSetsByRequest.set(r,new z),n.selectorSetsByRequest.get(r).setFragment(t,e,o),o})),f(this,"addChild",(function(t){n.children=[].concat(O(n.children),[t])})),f(this,"removeChild",(function(t){n.children=n.children.filter((function(e){return e!==t}))})),f(this,"subscribe",(function(t){return n.listeners=[].concat(O(n.listeners),[t]),function(){n.listeners=n.listeners.filter((function(e){return e!==t}))}})),f(this,"refresh",(function(){n.requests.forEach((function(t){t.sync()}));var t=!1;n.selectorSetsByRequest.forEach((function(e,r){e.update(r.cache.selectorSet)&&(t=!0)})),t?n.listeners.forEach((function(t){t()})):n.children.forEach((function(t){t.refresh()}))})),f(this,"reset",(function(){n.requests=new Map,n.children.forEach((function(t){t.reset()}))})),f(this,"destroy",(function(){n.parentStore.removeChild(n),n.requests.forEach((function(t){t.invalidate()}))})),this.parentStore=e,this.executeQuery=r,this.requests=new Map,this.selectorSetsByRequest=new Map,e&&e.addChild(this)};var mt="pending",gt="fulfilled";t.ReferenceMap=G,t.Resource=M,t.Transporter=function t(e){var r=this,n=e.request,o=e.cache,i=void 0===o?{}:o,u=e.ssr,s=void 0!==u&&u;a(this,t),f(this,"request",void 0),f(this,"rootStore",void 0),f(this,"cache",void 0),f(this,"ssr",void 0),f(this,"queries",void 0),f(this,"query",(function(t,e){return new lt(r,t,e)})),f(this,"createStore",(function(t){return new yt(t,r.query)})),f(this,"mutate",(function(t,e){return new pt(r,t,e)})),f(this,"refresh",(function(){r.rootStore.refresh()})),f(this,"reset",(function(){r.rootStore.reset(),r.queries=new Map,r.rootStore.refresh()})),f(this,"extract",(function(){var t={};return r.queries.forEach((function(e,r){t[r]=e.data.extract()})),t})),this.request=n,this.rootStore=this.createStore(null),this.cache=i,this.ssr=s,this.queries=new Map},t.TransporterError=st,t.TransporterProvider=function(t){var r=t.children,n=t.client;if(!n)throw new Error("TransporterProvider: You must provide a Transporter instance.");var o=n.rootStore;return e.useSyncExternalStore(o.subscribe,(function(){}),(function(){})),i.default.createElement(R.Provider,{value:{client:n,store:o}},r)},t.createContainer=function(t){var r,n=t.component,o=w(t,["component"]);if(!n)throw new Error('You must define a container "component".');var u=function(){return r||(r=I(n,o.renderer)),r.read()},s=function(t){d(n,t);var r=E(n);function n(t){var e;return a(this,n),f(b(e=r.call(this,t)),"store",void 0),e.state={error:null},e}return c(n,[{key:"componentDidCatch",value:function(t,e){console.error(t,e)}},{key:"componentWillUnmount",value:function(){this.store.destroy()}},{key:"renderContainer",value:function(){var t=this.context.client,r=this.state.error;if(r)return o.error&&e.cloneElement(o.error,{error:r});var n=i.default.createElement(N,{component:u,options:o,values:this.props});return A&&!t.ssr?n:i.default.createElement(i.default.Suspense,{fallback:o.loading},n)}},{key:"render",value:function(){var t=this.context,e=t.client,r=t.store;return this.store||(this.store=e.createStore(r)),i.default.createElement(R.Provider,{value:{client:e,store:this.store}},this.renderContainer())}}]),n}(i.default.Component);o.throwOnError||(s.getDerivedStateFromError=function(t){return{error:t}}),s.contextType=R;var l=n.displayName||n.name;return s.displayName=l?"Container(".concat(l,")"):"Container",s},t.createNode=function(t){var r=t.component,n=w(t,["component"]);if(!t.component)throw new Error('You must define a container "component".');function o(t){var r=e.useContext(R).store;(n.data||function(){return null})(r,t)}var i=r.displayName||r.name;return o.displayName=i?"ContainerNode(".concat(i,")"):"ContainerNode",o},t.ref=function(t,e){return f({},U,[t,e])},t.useMutation=function(t,r){var n,o=e.useContext(R);if(!o)throw new Error('"useMutation" hook is used outside of TransporterContext.');return[function(e){if(n)throw new Error("Mutation was dispatched before.");return n=o.store.mutate(t,h(h({},r),e))},{get ok(){return n&&n.status!==mt?n.status===gt:null},get loading(){return!!n&&n.status===mt},get executed(){return!!n&&(n.status===gt||"rejected"===n.status)}}]},t.useReset=function(){var t=e.useContext(R);if(!t)throw new Error('"useReset" hook is used outside of TransporterContext.');var r=t.store;return function(){r.reset()}},Object.defineProperty(t,"__esModule",{value:!0})})); |
{ | ||
"name": "react-transporter", | ||
"version": "1.0.0-alpha.0", | ||
"version": "1.0.0-alpha.1", | ||
"description": "React.js GraphQL client", | ||
@@ -5,0 +5,0 @@ "author": "Markus Wetzel <markuswetzel@gmx.net>", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
194486
4833