UNPKG

@gravity-ui/data-source

Version:
252 lines 9.9 kB
import _typeof from "@babel/runtime/helpers/typeof"; import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import { createNormalizer } from '@normy/core'; import { QueryClient } from '@tanstack/react-query'; import { composeFullKey, hasTag } from '../core'; import { checkMutationObjectsKeys } from './utils/checkMutationObjectsKeys'; import { createQueryNormalizer } from './utils/normalize'; import { parseQueryKey } from './utils/parseQueryKey'; export var ClientDataManager = /*#__PURE__*/function () { function ClientDataManager() { var _config$defaultOption, _config$defaultOption2, _this = this; var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; _classCallCheck(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 QueryClient(_objectSpread(_objectSpread({}, config), {}, { defaultOptions: _objectSpread(_objectSpread({}, config.defaultOptions), {}, { queries: _objectSpread({ networkMode: 'always' }, (_config$defaultOption = config.defaultOptions) === null || _config$defaultOption === void 0 ? void 0 : _config$defaultOption.queries), mutations: _objectSpread({ networkMode: 'always' }, (_config$defaultOption2 = config.defaultOptions) === null || _config$defaultOption2 === void 0 ? void 0 : _config$defaultOption2.mutations) }) })); this.normalizer = this.createNormalize(config.normalizerConfig); this.queryNormalizer = createQueryNormalizer(this.normalizer, this.queryClient, config.normalizerConfig, function (data) { return _this.optimisticUpdate(data); }, function (data) { return _this.invalidateData(data); }); } return _createClass(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 = 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 = parseQueryKey(query.queryKey); _this3.invalidateQuery(parsedQueryKey); }); } }, { key: "update", value: function update(data) { var _this4 = this; if (!this.normalizer) { return; } var _ref = _typeof(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 = checkMutationObjectsKeys(data, this.normalizer); var dependentQueries = this.normalizer.getDependentQueries(data); if (completeness.needsRefetch) { dependentQueries.forEach(function (queryKeyString) { var _cachedQuery$meta; var parsedQueryKey = 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 = 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 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 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: composeFullKey(dataSource, params), exact: true }, invalidateOptions); } }, { key: "resetParams", value: function resetParams(dataSource, params) { return this.queryClient.resetQueries({ queryKey: composeFullKey(dataSource, params), exact: true }); } }, { key: "invalidateSourceTags", value: function invalidateSourceTags(dataSource, params, invalidateOptions) { return this.invalidateQueries({ // Last element is a full key queryKey: 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 createNormalizer({}); } return 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