query.service.js 1.2 KB

12345678910111213141516171819202122232425
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.QueryService = void 0;
  4. const query_core_1 = require("@tanstack/query-core");
  5. class QueryService {
  6. constructor(queryClient) {
  7. this.queryClient = queryClient !== null && queryClient !== void 0 ? queryClient : new query_core_1.QueryClient();
  8. this.queryClient.mount();
  9. }
  10. subscribeQuery({ options, listener, }) {
  11. const defaultedOptions = this.queryClient.defaultQueryOptions(options);
  12. const queryObserver = new query_core_1.QueryObserver(this.queryClient, defaultedOptions);
  13. const result = queryObserver.getOptimisticResult(defaultedOptions);
  14. const unsubscribe = queryObserver.subscribe(listener);
  15. return { result, unsubscribe, queryObserver };
  16. }
  17. subscribeMutation({ options, listener, }) {
  18. const defaultedOptions = this.queryClient.defaultMutationOptions(options);
  19. const mutationObserver = new query_core_1.MutationObserver(this.queryClient, defaultedOptions);
  20. const result = mutationObserver.getCurrentResult();
  21. const unsubscribe = mutationObserver.subscribe(listener);
  22. return { result, unsubscribe, mutationObserver };
  23. }
  24. }
  25. exports.QueryService = QueryService;