"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HistoryStore = void 0; var graphql_1 = require("graphql"); var query_1 = require("./query"); var MAX_QUERY_SIZE = 100000; var HistoryStore = (function () { function HistoryStore(storage, maxHistoryLength) { var _this = this; this.storage = storage; this.maxHistoryLength = maxHistoryLength; this.updateHistory = function (_a) { var query = _a.query, variables = _a.variables, headers = _a.headers, operationName = _a.operationName; if (!_this.shouldSaveQuery(query, variables, headers, _this.history.fetchRecent())) { return; } _this.history.push({ query: query, variables: variables, headers: headers, operationName: operationName, }); var historyQueries = _this.history.items; var favoriteQueries = _this.favorite.items; _this.queries = historyQueries.concat(favoriteQueries); }; this.deleteHistory = function (_a, clearFavorites) { var query = _a.query, variables = _a.variables, headers = _a.headers, operationName = _a.operationName, favorite = _a.favorite; if (clearFavorites === void 0) { clearFavorites = false; } function deleteFromStore(store) { var found = store.items.find(function (x) { return x.query === query && x.variables === variables && x.headers === headers && x.operationName === operationName; }); if (found) { store.delete(found); } } if (favorite || clearFavorites) { deleteFromStore(_this.favorite); } if (!favorite || clearFavorites) { deleteFromStore(_this.history); } _this.queries = __spreadArray(__spreadArray([], __read(_this.history.items), false), __read(_this.favorite.items), false); }; this.history = new query_1.QueryStore('queries', this.storage, this.maxHistoryLength); this.favorite = new query_1.QueryStore('favorites', this.storage, null); this.queries = __spreadArray(__spreadArray([], __read(this.history.fetchAll()), false), __read(this.favorite.fetchAll()), false); } HistoryStore.prototype.shouldSaveQuery = function (query, variables, headers, lastQuerySaved) { if (!query) { return false; } try { (0, graphql_1.parse)(query); } catch (_a) { return false; } if (query.length > MAX_QUERY_SIZE) { return false; } if (!lastQuerySaved) { return true; } if (JSON.stringify(query) === JSON.stringify(lastQuerySaved.query)) { if (JSON.stringify(variables) === JSON.stringify(lastQuerySaved.variables)) { if (JSON.stringify(headers) === JSON.stringify(lastQuerySaved.headers)) { return false; } if (headers && !lastQuerySaved.headers) { return false; } } if (variables && !lastQuerySaved.variables) { return false; } } return true; }; HistoryStore.prototype.toggleFavorite = function (_a) { var query = _a.query, variables = _a.variables, headers = _a.headers, operationName = _a.operationName, label = _a.label, favorite = _a.favorite; var item = { query: query, variables: variables, headers: headers, operationName: operationName, label: label, }; if (favorite) { item.favorite = false; this.favorite.delete(item); this.history.push(item); } else { item.favorite = true; this.favorite.push(item); this.history.delete(item); } this.queries = __spreadArray(__spreadArray([], __read(this.history.items), false), __read(this.favorite.items), false); }; HistoryStore.prototype.editLabel = function (_a, index) { var query = _a.query, variables = _a.variables, headers = _a.headers, operationName = _a.operationName, label = _a.label, favorite = _a.favorite; var item = { query: query, variables: variables, headers: headers, operationName: operationName, label: label, }; if (favorite) { this.favorite.edit(__assign(__assign({}, item), { favorite: favorite }), index); } else { this.history.edit(item, index); } this.queries = __spreadArray(__spreadArray([], __read(this.history.items), false), __read(this.favorite.items), false); }; return HistoryStore; }()); exports.HistoryStore = HistoryStore; //# sourceMappingURL=history.js.map