UNPKG

mycoil

Version:

A 2Kb lib mimicking Facebook's Recoil

1 lines 2.3 kB
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).Mycoil=e()}}(function(){return function e(t,n,r){function i(u,f){if(!n[u]){if(!t[u]){var s="function"==typeof require&&require;if(!f&&s)return s(u,!0);if(o)return o(u,!0);var c=new Error("Cannot find module '"+u+"'");throw c.code="MODULE_NOT_FOUND",c}var a=n[u]={exports:{}};t[u][0].call(a.exports,function(e){return i(t[u][1][e]||e)},a,a.exports,e,t,n,r)}return n[u].exports}for(var o="function"==typeof require&&require,u=0;u<r.length;u++)i(r[u]);return i}({1:[function(e,t,n){(function(e){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var t=function(e,t){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return function(e,t){var n=[],r=!0,i=!1,o=void 0;try{for(var u,f=e[Symbol.iterator]();!(r=(u=f.next()).done)&&(n.push(u.value),!t||n.length!==t);r=!0);}catch(e){i=!0,o=e}finally{try{!r&&f.return&&f.return()}finally{if(i)throw o}}return n}(e,t);throw new TypeError("Invalid attempt to destructure non-iterable instance")};n.atom=function(e){i[e.key]={key:e.key,value:e.default,subscribers:[],get:function(){return this.value},set:function(e){this.value=e,this.subscribers.forEach(function(t){return t(e)})},subscribe:function(e){this.subscribers.push(e)},unsubscribe:function(e){this.subscribers=this.subscribers.filter(function(t){return t!==e})}}},n.useMycoilState=function(e){if(!i[e])throw new Error("There is no state matching key "+e);var n=i[e],o=(0,r.useState)(n.get()),u=t(o,2),f=(u[0],u[1]);return(0,r.useEffect)(function(){var e=function(e){f(e)};return n.subscribe(e),function(){n.unsubscribe(e)}},[]),[n.get(),function(e){n.set(e)}]},n.selector=function(e){o[e.key]=e.get},n.useMycoilValue=function(e){if(!o[e])throw new Error("There is no selector matching key "+e);return(0,o[e])({get:function(e){if(!i[e])throw new Error("There is no state matching key "+e);return i[e].get()}})};var r="undefined"!=typeof window?window.React:void 0!==e?e.React:null,i={},o={}}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{}]},{},[1])(1)});