UNPKG

3.36 kBJavaScriptView Raw
1!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("react-query"),require("jotai")):"function"==typeof define&&define.amd?define(["exports","react-query","jotai"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).jotaiQuery={},e.reactQuery,e.jotai)}(this,(function(e,n,t){"use strict";var r=t.atom(new n.QueryClient);var u=["type"];e.atomWithInfiniteQuery=function(e,i){void 0===i&&(i=function(e){return e(r)});var o=t.atom((function(r){var u=i(r),o="function"==typeof e?e(r):e,s=u.defaultQueryOptions(o),c=new n.InfiniteQueryObserver(u,s),b=c.getCurrentResult(),l=null,a=t.atom(void 0===b.data&&!1!==o.enabled?new Promise((function(e){l=e})):b),f=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&&n.isCancelledError(e.error)||(l?(setTimeout((function(){d.isMounted||(null==d.unsubscribe||d.unsubscribe(),d.unsubscribe=null)}),1e3),l(e),l=null):f(e))};return!1!==o.enabled&&(d.unsubscribe=c.subscribe(v)),a.onMount=function(e){return f=e,d.isMounted=!0,!1===o.enabled||d.unsubscribe||(d.unsubscribe=c.subscribe(v),v(c.getCurrentResult())),function(){return null==d.unsubscribe?void 0:d.unsubscribe()}},{options:o,resultAtom:a,observer:c,state:d}}),(function(e,n,t){var r=e(o),i=r.options,s=r.resultAtom,c=r.observer,b=r.state;if(!1!==i.enabled)switch(t.type){case"refetch":n(s,new Promise((function(){}))),b.isMounted||(null==b.unsubscribe||b.unsubscribe(),b.unsubscribe=null),t.type;var l=function(e,n){if(null==e)return{};var t,r,u={},i=Object.keys(e);for(r=0;r<i.length;r++)t=i[r],n.indexOf(t)>=0||(u[t]=e[t]);return u}(t,u);return void c.refetch(l).then((function(e){n(s,e)}));case"fetchPreviousPage":return void c.fetchPreviousPage();case"fetchNextPage":return void c.fetchNextPage()}}));return t.atom((function(e){var n=e(o),t=e(n.resultAtom);if(t.isError)throw t.error;return t.data}),(function(e,n,t){return n(o,t)}))},e.atomWithQuery=function(e,u){void 0===u&&(u=function(e){return e(r)});var i=t.atom((function(r){var i=u(r),o="function"==typeof e?e(r):e,s=i.defaultQueryOptions(o),c=new n.QueryObserver(i,s),b=c.getCurrentResult(),l=null,a=t.atom(void 0===b.data&&!1!==o.enabled?new Promise((function(e){l=e})):b),f=function(){throw new Error("setting result without mount")},d={isMounted:!1,unsubscribe:null},v=function(e){e.isFetching||!e.isError&&void 0===e.data||(l?(setTimeout((function(){d.isMounted||(null==d.unsubscribe||d.unsubscribe(),d.unsubscribe=null)}),1e3),l(e),l=null):f(e))};return!1!==o.enabled&&(d.unsubscribe=c.subscribe(v)),a.onMount=function(e){return f=e,d.isMounted=!0,!1===o.enabled||d.unsubscribe||(d.unsubscribe=c.subscribe(v),v(c.getCurrentResult())),function(){return null==d.unsubscribe?void 0:d.unsubscribe()}},{options:o,resultAtom:a,observer:c,state:d}}),(function(e,n,t){var r=e(i),u=r.options,o=r.resultAtom,s=r.observer,c=r.state;if(!1!==u.enabled)return"refetch"===t.type?(n(o,new Promise((function(){}))),c.isMounted||(null==c.unsubscribe||c.unsubscribe(),c.unsubscribe=null),s.refetch({cancelRefetch:!0}).then((function(e){n(o,e)}))):void 0}));return t.atom((function(e){var n=e(i),t=e(n.resultAtom);if(t.isError)throw t.error;return t.data}),(function(e,n,t){return n(i,t)}))},e.queryClientAtom=r,Object.defineProperty(e,"__esModule",{value:!0})}));