/*!
 * monitorable v0.1.0-beta.2
 * (c) 2020-2021 Fierflame
 * @license MIT
 */
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).Monitorable={})}(this,(function(e){"use strict";let t,n;function r(e){"function"!=typeof t?console.error(e):t(e)}function o(e){t="function"==typeof e?e:void 0}function f(e){return Boolean(e&&["object","function"].includes(typeof e))}function u(e){return(...t)=>{try{e(...t)}catch(e){r(e)}}}function i(e){try{e()}catch(e){r(e)}}function c(e,t){if(e&&("function"==typeof e||"object"==typeof e))return"number"==typeof t?[e,String(t)]:"symbol"==typeof t||"string"==typeof t||"boolean"==typeof t?[e,t]:void 0}function l(e,t,n){if(e.has(t))return e.get(t);const r=n();return e.set(t,r),r}function s(e,t){if(!n)return;const r=c(e,t);if(!r)return;[e,t]=r;const o=l(n,e,()=>new Map);o.has(t)||o.set(t,!1)}function a(e,t,r){const o=n;n=e;try{return(null==r?void 0:r.postpone)?b(t,"priority"===r.postpone):t()}finally{n=o}}function p(e,t,n){if("function"==typeof t)return a(e,t,n);if("function"!=typeof n)throw new Error("fn needs to be a function");return a(e,n,t)}const y=new WeakMap;function d(e,t,n){var r;const o=null===(r=y.get(e))||void 0===r?void 0:r.get(t);if(!o)return;let f=[...o];n&&(f=f.filter(([,e])=>n(e))),f.forEach(([e])=>e())}let v;function g(e,t){const r=!t&&v||new Map,o=v;v=r;try{return e()}finally{v=o,r!==v&&function(e){for(const[r,o]of e.entries()){var t;const e=null===(t=n)||void 0===t?void 0:t.get(r);for(const t of o)d(r,t,e=>!e),(null==e?void 0:e.has(t))&&e.set(t,!0)}}(r)}}function b(e,t){if("function"==typeof e)return g(e,t);if("function"!=typeof t)throw new Error("fn needs to be a function");return g(t,e)}function h(e,t){const n=c(e,t);n&&([e,t]=n,!function(e,t){return!!v&&(l(v,e,()=>new Set).add(t),!0)}(e,t)?d(e,t):d(e,t,Boolean))}function m(e,t,n,r=!1){if("function"!=typeof n)return()=>{};const o=c(e,t);if(!o)return()=>{};[e,t]=o;const f=t;let i=y.get(e);i||(i=new Map,y.set(e,i));const s=l(i,f,()=>new Set),a=[u(n),r];s.add(a);let p=!1;return()=>{p||(p=!0,s.delete(a),s.size||i&&(i.delete(f),i.size||y.delete(e)))}}function P(e,t,n){let r;e=u(e);const o=null==n?void 0:n.postpone;let f=!1;function i(){if(f)return!1;if(f=!0,!r)return!0;const e=r;return r=void 0,e.forEach(e=>e()),!0}function c(){i()&&e(!0)}function l(){i()&&e(!1)}const s=new Map,a=p(s,()=>t(l),{postpone:o});return function(t){if(f)return!1;if(!t.size)return f=!0,e(!1);const o=[];for(let[n,r]of t)for(const[t,f]of r){if(f)return e(!0);o.push([n,t])}r=o.map(([e,t])=>m(e,t,c,null==n?void 0:n.disdeferable))}(s),(null==n?void 0:n.resultOnly)?a:{result:a,stop:l}}function R(e,t,n){if("function"!=typeof e)throw new Error("cb needs to be a function");if("function"==typeof t)return P(e,t,n);if("function"!=typeof n)throw new Error("fn needs to be a function");return P(e,n,t)}function _(e,t,n){let r;function o(){if(!r)return!1;const e=r;return r=void 0,e.forEach(e=>e()),!0}function f(){o()&&e(!0)}function i(...u){o();const i=new Map,c=p(i,()=>t(...u),n);return function(t){if(!t.size)return e(!1);const o=[];for(let[n,r]of t)for(const[t,f]of r){if(f)return e(!0);o.push([n,t])}r=o.map(([e,t])=>m(e,t,f,null==n?void 0:n.disdeferable))}(i),c}return e=u(e),i.stop=()=>{o()&&e(!1)},i}function w(e,t,n){if("function"==typeof t)return _(e,t,n);if("function"!=typeof n)throw new Error("fn needs to be a function");return _(e,n,t)}function O(e){return Boolean("function"==typeof e&&e.__$$__monitorable_value__$$__)}function S(){const e=this();return null==e?e:e.valueOf()}function j(...e){const t=this();return null==t?String(t):"function"==typeof t.toString?t.toString(...e):String(t)}function E(e){const t=this();return null==t?String(t):"function"==typeof t[Symbol.toPrimitive]?t[Symbol.toPrimitive](e):"string"===e?String(t):"number"===e?Number(t):t}function x(e,t,n=(()=>{}),r=(()=>{})){function o(e,n=!1){if(t)try{t(e,()=>{n=!0})}finally{n&&a()}}function f(){return s(u,"value"),e()}const u=(...e)=>e.length?(o(e[0],e[1]),e[0]):f();Reflect.defineProperty(u,"value",{get:f,set:o,enumerable:!0,configurable:!0}),Reflect.defineProperty(u,"valueOf",{value:S,enumerable:!0,configurable:!0}),Reflect.defineProperty(u,"toString",{value:j,enumerable:!0,configurable:!0}),Reflect.defineProperty(u,Symbol.toPrimitive,{value:E,enumerable:!0,configurable:!0});let c=new Set;function l(e,t){if(!c)return()=>{};const n=m(u,"value",()=>e(u,!1),t);let o=!1;const f=()=>{o||(o=!0,c&&c.delete(f),n(),i(()=>e(u,!0)))};return c.add(f),r(),()=>{o||(o=!0,c&&c.delete(f),n(),r())}}Reflect.defineProperty(u,"watch",{get:()=>l,set(){},configurable:!0});const a=()=>h(u,"value");a.has=()=>{var e;return Boolean(null===(e=c)||void 0===e?void 0:e.size)},a.stop=()=>{if(!c)return;const e=c;c=void 0;for(const t of[...e])t()},Reflect.defineProperty(u,"__$$__monitorable_value__$$__",{value:!0,configurable:!0});let p=!1;return u.stop=()=>{p||(p=!0,n(),a.stop())},{value:u,trigger:a}}function M(e){let t,n;const{value:r}=x(()=>n,(e,r)=>{e!==t&&(t=e,n=t,r())});return r(e),r}function V(e,t,n){var r,o;"function"!=typeof t&&(n=t,t=void 0);const f=t,u=null===(r=n)||void 0===r?void 0:r.postpone,i=null===(o=n)||void 0===o?void 0:o.deferable;let c,l,s,a=!1,p=!1;const y=w(e=>{p=!e,e&&s&&s()},e,{postpone:u,disdeferable:!i});function d(){p=!0;try{return c=y(),l=c,l}catch(e){throw a||(p=!1),e}}let v;return({value:v,trigger:s}=x(()=>p||a?l:d(),f&&(e=>f(e)),()=>{a||(a=!0,p||d())})),v}function $(e,t,n,r){function o(e,t){r&&r(e,t)}return V(()=>{const r=e[t];return void 0===r&&n?n():O(r)?r():r},r=>{const f=e[t];if(O(f))return f(r),void o(r,!0);void 0===f&&n&&n(r),o(r,!1)})}function z(e,t,n,r){if(!t)return(t,n,r)=>$(e,t,n,r);if(!Array.isArray(t))return $(e,t,n,r);const o=Object.create(null);for(const o of t){const t=$(e,o,n&&n(o),r&&((e,t)=>r(e,t,o)));Reflect.defineProperty(e,o,{get:()=>t(),set(e){t.value=e},configurable:!0,enumerable:!0})}return o}function A(e,t=Reflect.ownKeys(e),n){const r=Object.create(e);function o(e,t){n&&n(e,t)}const f=Array.isArray(t)?t:Reflect.ownKeys(t),u=Array.isArray(t)?e:t;for(const e of f){const t=u[e];O(t)&&Reflect.defineProperty(r,e,{get:()=>t(),set(n){t.value=n,o(n,e)},configurable:!0,enumerable:!0})}return r}function k(e,t){return V(()=>{const n=e[t];return O(n)?n():n},n=>{const r=e[t];O(r)?r(n):e[t]=n})}function B(e,t){return arguments.length>=2?k(e,t):t=>k(e,t)}function C(e){let t,n=!1;return(r,o)=>{if(o)return e(r,o);const f=r();f===t&&n||(n=!0,t=f,e(r,o))}}function K(e,t,n){return Reflect.defineProperty(e,t,{get:()=>(s(e,t),n),set(r){r!==n&&(n=r,h(e,t))},configurable:!0,enumerable:!0})}function D(e,t={},n){const r=n||null===t?Object.create(t):t;for(const t of e){let e=r[t];Reflect.defineProperty(r,t,{get:()=>(s(r,t),e),set(n){n!==e&&(e=n,h(r,t))},configurable:!0,enumerable:!0})}return r}function q(e,t){return s(e,t),e[t]}function I(e,t,n){return n===e[t]||(e[t]=n,h(e,t)),n}function N(e){return Boolean(e&&["object","function"].includes(typeof e))}let W;function F(e,t=0){if(!N(e))return e;const n=G(e),r=!0===t?1/0:t||0,o=new Proxy(n,{set(e,n,r,o){if(!1===t)return Reflect.set(e,n,r,o);const f=Reflect.has(e,n),u=Reflect.get(e,n,o),i=Reflect.set(e,n,r,F(o));return i?(f!==Reflect.has(e,n)&&h(o,!0),u!==Reflect.get(e,n,o)&&h(o,n),i):i},get(e,f,u){if(W===o&&"__monitorable__recover__"===f)return void(W=n);if(!1===t)return Reflect.get(e,f,u);s(u,f);const i=Reflect.get(e,f,F(u));return r>0?F(i,r-1):i},setPrototypeOf(e,n){if(!1===t)return Reflect.setPrototypeOf(e,n);const r=Reflect.getPrototypeOf(e),f=Reflect.setPrototypeOf(e,n);return f&&r!==n&&(h(e,!1),h(o,!1)),f},getPrototypeOf(e){if(!1===t)return Reflect.getPrototypeOf(e);s(e,!1),s(o,!1);const n=Reflect.getPrototypeOf(e);return r>0?F(n,r-1):n},defineProperty(e,n,r){if(!1===t)return Reflect.defineProperty(e,n,r);let f=!0;if("value"in r){const t=Reflect.getOwnPropertyDescriptor(e,n);t&&"value"in t&&G(r.value)===G(t.value)&&(f=!1)}const u=Reflect.defineProperty(e,n,r);return f&&u&&(h(e,n),h(o,n)),u},getOwnPropertyDescriptor:(e,n)=>(!1===t||(s(e,n),s(o,n)),Reflect.getOwnPropertyDescriptor(e,n)),deleteProperty(e,n){if(!1===t)return Reflect.deleteProperty(e,n);const r=Reflect.has(e,n),f=Reflect.deleteProperty(e,n);return r&&!Reflect.has(e,n)&&(h(e,n),h(e,!0),h(o,n),h(o,!0)),f},ownKeys:e=>(!1===t||(s(e,!0),s(o,!0)),Reflect.ownKeys(e)),has:(e,n)=>(!1===t||(s(e,!0),s(o,!0)),Reflect.has(e,n))});return o}function G(e){if(!e)return e;if(!N(e))return e;let t=e;try{W=e,t=e.__monitorable__recover__}catch(e){}return t=W,W=!1,t&&("object"==typeof t||"function"==typeof t)?t:e}function H(e,t){return G(e)===G(t)}var J=Object.freeze({__proto__:null,printError:r,setPrintError:o,encashable:f,safeify:u,safeCall:i,getIndexes:c,getMapValue:l,markRead:s,observe:p,postpone:b,markChange:h,watchProp:m,exec:R,monitor:w,isValue:O,value:M,computed:V,valueify:z,mixValue:A,asValue:B,merge:C,defineProperty:K,createObject:D,get:q,set:I,encase:F,recover:G,equal:H});e.asValue=B,e.computed=V,e.createObject=D,e.default=J,e.defineProperty=K,e.encase=F,e.encashable=f,e.equal=H,e.exec=R,e.get=q,e.getIndexes=c,e.getMapValue=l,e.isValue=O,e.markChange=h,e.markRead=s,e.merge=C,e.mixValue=A,e.monitor=w,e.observe=p,e.postpone=b,e.printError=r,e.recover=G,e.safeCall=i,e.safeify=u,e.set=I,e.setPrintError=o,e.value=M,e.valueify=z,e.watchProp=m,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=monitorable.min.js.map
