UNPKG

3.12 kBJavaScriptView Raw
1!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@tanstack/query-core"),require("jotai")):"function"==typeof define&&define.amd?define(["exports","@tanstack/query-core","jotai"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).jotaiQuery={},e.queryCore,e.jotai)}(this,(function(e,t,n){"use strict";var r=n.atom(new t.QueryClient);e.atomWithInfiniteQuery=function(e,u){void 0===u&&(u=function(e){return e(r)});var o=n.atom((function(r){var o=u(r),i="function"==typeof e?e(r):e,s=o.defaultQueryOptions(i),c=new t.InfiniteQueryObserver(o,s),l=c.getCurrentResult(),a=null,f=n.atom(void 0===l.data&&!1!==i.enabled?new Promise((function(e){a=e})):l),b=function(){throw new Error("setting result without mount")},d={isMounted:!1,unsubscribe:null},v=function(e){e.isFetching||!e.isError&&void 0===e.data||e.isError&&t.isCancelledError(e.error)||(a?(setTimeout((function(){d.isMounted||(null==d.unsubscribe||d.unsubscribe(),d.unsubscribe=null)}),1e3),a(e),a=null):b(e))};return!1!==i.enabled&&(d.unsubscribe=c.subscribe(v)),f.onMount=function(e){return b=e,d.isMounted=!0,!1===i.enabled||d.unsubscribe||(d.unsubscribe=c.subscribe(v),v(c.getCurrentResult())),function(){return null==d.unsubscribe?void 0:d.unsubscribe()}},{options:i,resultAtom:f,observer:c,state:d}}),(function(e,t,n){var r=e(o),u=r.options,i=r.resultAtom,s=r.observer,c=r.state;if(!1!==u.enabled)switch(n.type){case"refetch":return t(i,new Promise((function(){}))),c.isMounted||(null==c.unsubscribe||c.unsubscribe(),c.unsubscribe=null),void s.refetch(n.payload).then((function(e){t(i,e)}));case"fetchPreviousPage":return void s.fetchPreviousPage();case"fetchNextPage":return void s.fetchNextPage()}}));return n.atom((function(e){var t=e(o),n=e(t.resultAtom);if(n.isError)throw n.error;return n.data}),(function(e,t,n){return t(o,n)}))},e.atomWithQuery=function(e,u){void 0===u&&(u=function(e){return e(r)});var o=n.atom((function(e){var n=u(e),r=n.defaultQueryOptions();return new t.QueryObserver(n,r)})),i=n.atom((function(t){var r=u(t),i="function"==typeof e?e(t):e,s=r.defaultQueryOptions(i),c=t(o);c.destroy(),c.setOptions(s);var l=c.getCurrentResult(),a=null,f=n.atom(void 0===l.data&&!1!==i.enabled?new Promise((function(e){a=e})):l),b=null,d=null,v=function(){b||(null==d||d(),d=null)},m=function(e){if(!e.isFetching&&(e.isError||void 0!==e.data))if(a)setTimeout(v,1e3),a(e),a=null;else{if(!b)throw new Error("setting result without mount");b(e)}};return!1!==i.enabled&&(d=c.subscribe(m)),f.onMount=function(e){return b=e,!1===i.enabled||d||(d=c.subscribe(m),m(c.getCurrentResult())),function(){b=null,null==d||d()}},{options:i,resultAtom:f,unsubIfNotMounted:v}}),(function(e,t,n){var r=e(o),u=e(i),s=u.options,c=u.resultAtom,l=u.unsubIfNotMounted;if(!1!==s.enabled)return"refetch"===n.type?(t(c,new Promise((function(){}))),l(),r.refetch({cancelRefetch:!0}).then((function(e){t(c,e)}))):void 0}));return n.atom((function(e){var t=e(i),n=e(t.resultAtom);if(n.isError)throw n.error;return n.data}),(function(e,t,n){return t(i,n)}))},e.queryClientAtom=r,Object.defineProperty(e,"__esModule",{value:!0})}));