UNPKG

2.23 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var globals = require('../../utilities/globals');
6var React = require('rehackt');
7var utilities = require('../../utilities');
8var tslib = require('tslib');
9
10function _interopNamespace(e) {
11 if (e && e.__esModule) return e;
12 var n = Object.create(null);
13 if (e) {
14 for (var k in e) {
15 n[k] = e[k];
16 }
17 }
18 n["default"] = e;
19 return Object.freeze(n);
20}
21
22var React__namespace = /*#__PURE__*/_interopNamespace(React);
23
24var contextKey = utilities.canUseSymbol ? Symbol.for("__APOLLO_CONTEXT__") : "__APOLLO_CONTEXT__";
25function getApolloContext() {
26 globals.invariant("createContext" in React__namespace, 46);
27 var context = React__namespace.createContext[contextKey];
28 if (!context) {
29 Object.defineProperty(React__namespace.createContext, contextKey, {
30 value: (context = React__namespace.createContext({})),
31 enumerable: false,
32 writable: false,
33 configurable: true,
34 });
35 context.displayName = "ApolloContext";
36 }
37 return context;
38}
39var resetApolloContext = getApolloContext;
40
41var ApolloConsumer = function (props) {
42 var ApolloContext = getApolloContext();
43 return (React__namespace.createElement(ApolloContext.Consumer, null, function (context) {
44 globals.invariant(context && context.client, 45);
45 return props.children(context.client);
46 }));
47};
48
49var ApolloProvider = function (_a) {
50 var client = _a.client, children = _a.children;
51 var ApolloContext = getApolloContext();
52 var parentContext = React__namespace.useContext(ApolloContext);
53 var context = React__namespace.useMemo(function () {
54 return tslib.__assign(tslib.__assign({}, parentContext), { client: client || parentContext.client });
55 }, [parentContext, client]);
56 globals.invariant(context.client, 47);
57 return (React__namespace.createElement(ApolloContext.Provider, { value: context }, children));
58};
59
60exports.ApolloConsumer = ApolloConsumer;
61exports.ApolloProvider = ApolloProvider;
62exports.getApolloContext = getApolloContext;
63exports.resetApolloContext = resetApolloContext;
64//# sourceMappingURL=context.cjs.map