UNPKG

@gravity-ui/data-source

Version:
259 lines (258 loc) 10.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClientDataManager = void 0; var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _core = require("@normy/core"); var _reactQuery = require("@tanstack/react-query"); var _core2 = require("../core"); var _checkMutationObjectsKeys = require("./utils/checkMutationObjectsKeys"); var _normalize = require("./utils/normalize"); var _parseQueryKey = require("./utils/parseQueryKey"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } var ClientDataManager = exports.ClientDataManager = /*#__PURE__*/function () { function ClientDataManager() { var _config$defaultOption, _config$defaultOption2, _this = this; var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; (0, _classCallCheck2.default)(this, ClientDataManager); this.queryClient = void 0; this.normalizer = void 0; this.queryNormalizer = void 0; this.normalizerConfig = void 0; this.normalizerConfig = config.normalizerConfig; this.queryClient = new _reactQuery.QueryClient((0, _objectSpread2.default)((0, _objectSpread2.default)({}, config), {}, { defaultOptions: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, config.defaultOptions), {}, { queries: (0, _objectSpread2.default)({ networkMode: 'always' }, (_config$defaultOption = config.defaultOptions) === null || _config$defaultOption === void 0 ? void 0 : _config$defaultOption.queries), mutations: (0, _objectSpread2.default)({ networkMode: 'always' }, (_config$defaultOption2 = config.defaultOptions) === null || _config$defaultOption2 === void 0 ? void 0 : _config$defaultOption2.mutations) }) })); this.normalizer = this.createNormalize(config.normalizerConfig); this.queryNormalizer = (0, _normalize.createQueryNormalizer)(this.normalizer, this.queryClient, config.normalizerConfig, function (data) { return _this.optimisticUpdate(data); }, function (data) { return _this.invalidateData(data); }); } return (0, _createClass2.default)(ClientDataManager, [{ key: "optimisticUpdate", value: function optimisticUpdate(mutationData, queryKey, queryData) { var _this2 = this; if (!this.normalizer) { return; } if (queryKey && queryData) { this.optimisticUpdateQuery(queryKey, queryData); return; } var queriesToUpdate = this.normalizer.getQueriesToUpdate(mutationData); queriesToUpdate.forEach(function (query) { var parsedQueryKey = (0, _parseQueryKey.parseQueryKey)(query.queryKey); _this2.optimisticUpdateQuery(parsedQueryKey, query.data); }); } }, { key: "invalidateData", value: function invalidateData(data, queryKey) { var _this3 = this; if (!this.normalizer) { return; } if (queryKey) { this.invalidateQuery(queryKey); return; } var queriesToUpdate = this.normalizer.getQueriesToUpdate(data); queriesToUpdate.forEach(function (query) { var parsedQueryKey = (0, _parseQueryKey.parseQueryKey)(query.queryKey); _this3.invalidateQuery(parsedQueryKey); }); } }, { key: "update", value: function update(data) { var _this4 = this; if (!this.normalizer) { return; } var _ref = (0, _typeof2.default)(this.normalizerConfig) === 'object' ? this.normalizerConfig : { optimistic: false, invalidate: false }, globalOptimistic = _ref.optimistic, globalInvalidate = _ref.invalidate; var queriesToUpdate = this.normalizer.getQueriesToUpdate(data); if (queriesToUpdate.length === 0) { var completeness = (0, _checkMutationObjectsKeys.checkMutationObjectsKeys)(data, this.normalizer); var dependentQueries = this.normalizer.getDependentQueries(data); if (completeness.needsRefetch) { dependentQueries.forEach(function (queryKeyString) { var _cachedQuery$meta; var parsedQueryKey = (0, _parseQueryKey.parseQueryKey)(queryKeyString); var cachedQuery = _this4.queryClient.getQueryCache().find({ queryKey: parsedQueryKey }); var _ref2 = (_cachedQuery$meta = cachedQuery === null || cachedQuery === void 0 ? void 0 : cachedQuery.meta) !== null && _cachedQuery$meta !== void 0 ? _cachedQuery$meta : {}, invalidate = _ref2.invalidate; if (invalidate === true || invalidate === undefined && globalInvalidate === true) { _this4.invalidateData(data, parsedQueryKey); } }); } return; } queriesToUpdate.forEach(function (query) { var _cachedQuery$meta2; var parsedQueryKey = (0, _parseQueryKey.parseQueryKey)(query.queryKey); var cachedQuery = _this4.queryClient.getQueryCache().find({ queryKey: parsedQueryKey }); var _ref3 = (_cachedQuery$meta2 = cachedQuery === null || cachedQuery === void 0 ? void 0 : cachedQuery.meta) !== null && _cachedQuery$meta2 !== void 0 ? _cachedQuery$meta2 : {}, optimistic = _ref3.optimistic, invalidate = _ref3.invalidate; if (optimistic === true || optimistic === undefined && globalOptimistic === true) { _this4.optimisticUpdate(data, parsedQueryKey, query.data); } if (invalidate === true || invalidate === undefined && globalInvalidate === true) { _this4.invalidateData(data, parsedQueryKey); } }); } }, { key: "invalidateTag", value: function invalidateTag(tag, invalidateOptions) { return this.invalidateQueries({ predicate: function predicate(_ref4) { var queryKey = _ref4.queryKey; return (0, _core2.hasTag)(queryKey, tag); } }, invalidateOptions); } }, { key: "invalidateTags", value: function invalidateTags(tags, invalidateOptions) { return this.invalidateQueries({ predicate: function predicate(_ref5) { var queryKey = _ref5.queryKey; return tags.every(function (tag) { return (0, _core2.hasTag)(queryKey, tag); }); } }, invalidateOptions); } }, { key: "invalidateSource", value: function invalidateSource(dataSource, invalidateOptions) { return this.invalidateQueries({ // First element is a data source name queryKey: [dataSource.name] }, invalidateOptions); } }, { key: "resetSource", value: function resetSource(dataSource) { return this.queryClient.resetQueries({ // First element is a data source name queryKey: [dataSource.name] }); } }, { key: "invalidateParams", value: function invalidateParams(dataSource, params, invalidateOptions) { return this.invalidateQueries({ queryKey: (0, _core2.composeFullKey)(dataSource, params), exact: true }, invalidateOptions); } }, { key: "resetParams", value: function resetParams(dataSource, params) { return this.queryClient.resetQueries({ queryKey: (0, _core2.composeFullKey)(dataSource, params), exact: true }); } }, { key: "invalidateSourceTags", value: function invalidateSourceTags(dataSource, params, invalidateOptions) { return this.invalidateQueries({ // Last element is a full key queryKey: (0, _core2.composeFullKey)(dataSource, params).slice(0, -1) }, invalidateOptions); } }, { key: "invalidateQueries", value: function invalidateQueries(filters, invalidateOptions) { var _this5 = this; var invalidate = function invalidate() { return _this5.queryClient.invalidateQueries(filters); }; this.repeatInvalidate(invalidate, invalidateOptions === null || invalidateOptions === void 0 ? void 0 : invalidateOptions.repeat); return invalidate(); } }, { key: "repeatInvalidate", value: function repeatInvalidate(invalidate, repeat) { if (!repeat) { return; } for (var i = 1; i <= repeat.count; i++) { setTimeout(invalidate, repeat.interval * i); } } }, { key: "createNormalize", value: function createNormalize(config) { if (!config) { return undefined; } if (config === true) { return (0, _core.createNormalizer)({}); } return (0, _core.createNormalizer)(config); } }, { key: "invalidateQuery", value: function invalidateQuery(queryKey) { var cachedQuery = this.queryClient.getQueryCache().find({ queryKey: queryKey }); if ((cachedQuery === null || cachedQuery === void 0 ? void 0 : cachedQuery.state.fetchStatus) !== 'fetching' && (cachedQuery === null || cachedQuery === void 0 ? void 0 : cachedQuery.state.status) === 'success' && !(cachedQuery !== null && cachedQuery !== void 0 && cachedQuery.state.isInvalidated)) { this.queryClient.invalidateQueries({ queryKey: queryKey }); } } }, { key: "optimisticUpdateQuery", value: function optimisticUpdateQuery(queryKey, queryData) { var cachedQuery = this.queryClient.getQueryCache().find({ queryKey: queryKey }); var dataUpdatedAt = cachedQuery === null || cachedQuery === void 0 ? void 0 : cachedQuery.state.dataUpdatedAt; var isInvalidated = cachedQuery === null || cachedQuery === void 0 ? void 0 : cachedQuery.state.isInvalidated; var error = cachedQuery === null || cachedQuery === void 0 ? void 0 : cachedQuery.state.error; var status = cachedQuery === null || cachedQuery === void 0 ? void 0 : cachedQuery.state.status; this.queryClient.setQueryData(queryKey, function () { return queryData; }, { updatedAt: dataUpdatedAt }); cachedQuery === null || cachedQuery === void 0 || cachedQuery.setState({ isInvalidated: isInvalidated, error: error, status: status }); } }]); }(); // #sourceMappingURL=ClientDataManager.js.map