@ghostry/finity-core
Version:
Finite state management with exhaustiveness and transition enforcement for TypeScript.
2 lines • 6.14 kB
JavaScript
!function(t,r){if("object"==typeof exports&&"object"==typeof module)module.exports=r();else if("function"==typeof define&&define.amd)define([],r);else{var e=r();for(var n in e)("object"==typeof exports?exports:t)[n]=e[n]}}(self,function(){return function(){"use strict";var t={};t.d=function(r,e){for(var n in e)t.o(e,n)&&!t.o(r,n)&&Object.defineProperty(r,n,{enumerable:!0,get:e[n]})},t.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},t.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var r={};t.r(r),t.d(r,{assertNever:function(){return y},initialize:function(){return b},ExhaustiveSwitchInstance:function(){return u},noop:function(){return s}});var e=function(t){return{name:t,transitions:function(r){return{name:t,transitions:r}}}};function n(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=Array(r);e<r;e++)n[e]=t[e];return n}function o(t,r,e){return r in t?Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[r]=e,t}function i(t,r){return function(t){if(Array.isArray(t))return t}(t)||function(t,r){var e,n,o=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=o){var i=[],a=!0,u=!1;try{for(o=o.call(t);!(a=(e=o.next()).done)&&(i.push(e.value),!r||i.length!==r);a=!0);}catch(t){u=!0,n=t}finally{try{a||null==o.return||o.return()}finally{if(u)throw n}}return i}}(t,r)||a(t,r)||function(){throw TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(t,r){if(t){if("string"==typeof t)return n(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);if("Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e)return Array.from(e);if("Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e))return n(t,r)}}var u=function t(r,e){var u=this;if(!(this instanceof t))throw TypeError("Cannot call a class as a function");o(this,"state",void 0),o(this,"cases",void 0),o(this,"case",void 0),o(this,"use",void 0),this.state=r,this.cases=e,this.case=function(t,r){var e;return u.cases=((function(t){if(Array.isArray(t))return n(t)})(e=u.cases)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(e)||a(e)||function(){throw TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()).concat([[t,r]]),u},this.use=function(){var t=u.state(),r=u.cases.find(function(r){var e=i(r,2),n=e[0];return e[1],n.includes(t.kind)});if(r){var e=i(r,2);return(e[0],e[1])(t)}throw Error("[finity] Invalid state switch!")}};function c(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=Array(r);e<r;e++)n[e]=t[e];return n}function f(t){for(var r=1;r<arguments.length;r++){var e=null!=arguments[r]?arguments[r]:{},n=Object.keys(e);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(e).filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.forEach(function(r){var n;n=e[r],r in t?Object.defineProperty(t,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[r]=n})}return t}function l(t,r){return r=null!=r?r:{},Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):(function(t,r){var e=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e.push.apply(e,n)}return e})(Object(r)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}),t}var s=function(){},y=function(t){throw Error("Invalid code branch reached!",t)},b=function(t){var r=function(r){var e,n=(e=t.useState(function(){return r instanceof Function?r():r}),function(t){if(Array.isArray(t))return t}(e)||function(t,r){var e,n,o=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=o){var i=[],a=!0,u=!1;try{for(o=o.call(t);!(a=(e=o.next()).done)&&(i.push(e.value),i.length!==r);a=!0);}catch(t){u=!0,n=t}finally{try{a||null==o.return||o.return()}finally{if(u)throw n}}return i}}(e,2)||function(t,r){if(t){if("string"==typeof t)return c(t,2);var e=Object.prototype.toString.call(t).slice(8,-1);if("Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e)return Array.from(e);if("Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e))return c(t,r)}}(e,2)||function(){throw TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()),o=n[0],i=n[1];return{value:o,set:function(t){return i(function(r){return t})},update:function(t){return i(function(r){return t(r)})}}},n=function(t,e){var n=r(e);return l(f({},n),{value:function(){return l(f({},n.value()),{transition:function(r){var e=n.value().kind,o=r.kind;if(t.transitions[e].includes(o))n.set(r);else throw Error("[finity] Invalid state transition!")}})}})},o=function(t,r){var e=n(t,r);return function(t){for(var r=1;r<arguments.length;r++){var e=null!=arguments[r]?arguments[r]:{},n=Object.keys(e);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(e).filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.forEach(function(r){var n;n=e[r],r in t?Object.defineProperty(t,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[r]=n})}return t}({},(e.set,e.update,function(t,r){if(null==t)return{};var e,n,o=function(t,r){if(null==t)return{};var e,n,o={},i=Object.keys(t);for(n=0;n<i.length;n++)e=i[n],r.indexOf(e)>=0||(o[e]=t[e]);return o}(t,r);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(t);for(n=0;n<i.length;n++)e=i[n],!(r.indexOf(e)>=0)&&Object.prototype.propertyIsEnumerable.call(t,e)&&(o[e]=t[e])}return o}(e,["set","update"])))};return{defineTransitionalState:e,useState:r,useTransitionalState:n,useStrictlyTransitionalState:o,useEffect:function(t){return new u(t,[])}}};return r}()});
//# sourceMappingURL=index.js.map