jotai
Version:
👻 Next gen state management that will spook you
2 lines (1 loc) • 3.1 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@urql/core"),require("jotai"),require("wonka")):"function"==typeof define&&define.amd?define(["exports","@urql/core","jotai","wonka"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).jotaiUrql={},t.core,t.jotai,t.wonka)}(this,(function(t,e,n,r){"use strict";var o="object"==typeof process&&process.env.JOTAI_URQL_DEFAULT_URL||"/graphql",u=n.atom(e.createClient({url:o}));function i(){return i=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t},i.apply(this,arguments)}t.atomWithMutation=function(t,e){void 0===e&&(e=function(t){return t(u)});var r=n.atom(new Promise((function(){})));return n.atom((function(t){return t(r)}),(function(n,o,u){o(r,new Promise((function(){})));var i=e(n),a=t(n);i.mutation(a,u.variables,u.context).toPromise().then((function(t){o(r,t),null==u.callback||u.callback(t)})).catch((function(){}))}))},t.atomWithQuery=function(t,e){void 0===e&&(e=function(t){return t(u)});var o=n.atom((function(){return{}})),a=function(t,e,o,u){var a=null,c=n.atom(new Promise((function(t){a=t})));t.resultAtom=c;var s=function(){throw new Error("setting result without mount")},l=function(e){if(c===t.resultAtom){if(!function(t){return"data"in t}(e))throw new Error("result does not have data");a?(a(e),a=null):s(e)}};return e.query(o.query,o.variables,i({},o.requestPolicy&&{requestPolicy:o.requestPolicy},o.context,u)).toPromise().then(l).catch((function(){})),c.onMount=function(t){s=t;var n=r.pipe(e.query(o.query,o.variables,i({},o.requestPolicy&&{requestPolicy:o.requestPolicy},o.context,u)),r.skip(1),r.subscribe(l));return function(){return n.unsubscribe()}},c},c=n.atom((function(n){var r=t(n);if(r.pause)return null;var u=e(n);return{resultAtom:a(n(o),u,r),client:u,args:r}})),s=n.atom(null);return n.atom((function(t){var e=t(c);if(!e)return null;var n=e.resultAtom,r=t(s);return r&&r.oldResultAtom===n&&(n=r.newResultAtom),t(n)}),(function(t,e,n){if("reexecute"===n.type){var r=t(c);if(!r)throw new Error("query is paused");var u=r.resultAtom,i=r.client,l=r.args;e(s,{oldResultAtom:u,newResultAtom:a(t(o),i,l,n.opts)})}}))},t.atomWithSubscription=function(t,e){void 0===e&&(e=function(t){return t(u)});var o=n.atom((function(o){var u=t(o);if(u.pause)return{args:u};var i=e(o),a=null,c=n.atom(new Promise((function(t){a=t}))),s=function(){throw new Error("setting result without mount")},l=function(t){if(!function(t){return"data"in t}(t))throw new Error("result does not have data");a?(a(t),a=null):s(t)},f=r.pipe(i.subscription(u.query,u.variables,u.context),r.subscribe(l)),m=setTimeout((function(){m=null,f.unsubscribe()}),1e3);return c.onMount=function(t){var e;return s=t,m?(clearTimeout(m),e=f):e=r.pipe(i.subscription(u.query,u.variables,u.context),r.subscribe(l)),function(){return e.unsubscribe()}},{resultAtom:c,args:u}}));return n.atom((function(t){var e=t(o).resultAtom;return e?t(e):null}))},t.clientAtom=u,Object.defineProperty(t,"__esModule",{value:!0})}));