@libre/atom
Advanced tools
Comparing version 1.1.0 to 1.2.0
let nextAtomUid = 0; | ||
const stateByAtomId = Object.create(null); | ||
const validatorByAtomId = Object.create(null); | ||
const changeHandlersByAtomId = {}; | ||
/** @ignore */ | ||
@@ -24,3 +25,54 @@ function _useNextAtomId() { | ||
} | ||
/** @ignore */ | ||
function _initChangeHandlerDict(atom) { | ||
changeHandlersByAtomId[atom["$$id"]] = {}; | ||
} | ||
/** @ignore */ | ||
function _addChangeHandler(atom, key, handler) { | ||
if (typeof changeHandlersByAtomId[atom["$$id"]][key] === "function") { | ||
throw new Error(`Change handler already registered for key "${key}" on ${atom}.\nRemove the existing handler before registering a new one.`); | ||
} | ||
changeHandlersByAtomId[atom["$$id"]][key] = handler; | ||
} | ||
/** @ignore */ | ||
function _removeChangeHandler(atom, key) { | ||
delete changeHandlersByAtomId[atom["$$id"]][key]; | ||
} | ||
/** @ignore */ | ||
function _runChangeHandlers(atom) { | ||
Object.keys(changeHandlersByAtomId[atom["$$id"]]).forEach(k => { | ||
changeHandlersByAtomId[atom["$$id"]][k](_getState(atom)); | ||
}); | ||
} | ||
/** | ||
* Registers a function to be run each time the state of `atom` changes. | ||
* | ||
* Will throw an Error if `key` is already taken by another handler. | ||
* | ||
* @example | ||
```js | ||
import {Atom, addChangeHandler, swap} from '@libre/atom' | ||
const countAtom = Atom.of({ count: 0 }) | ||
addChangeHandler(countAtom, "log", (state) => { console.log(state) }) | ||
swap(countAtom, (state) => ({ count: state.count + 1 })) | ||
// stdout logs: { count: 1 } | ||
``` | ||
*/ | ||
function addChangeHandler(atom, key, handler) { | ||
_addChangeHandler(atom, key, handler); | ||
} | ||
/** | ||
* Deletes the `key` and the handler associated with `key` so that it not longer runs | ||
* when the state of `atom` changes. | ||
*/ | ||
function removeChangeHandler(atom, key) { | ||
_removeChangeHandler(atom, key); | ||
} | ||
/** @ignore */ | ||
@@ -60,2 +112,3 @@ const expectedAtomButGot = "Expected an Atom instances, but got:"; | ||
_setValidator(this, validator); | ||
_initChangeHandlerDict(this); | ||
return this; | ||
@@ -165,2 +218,3 @@ } | ||
_setState(atom, nextState); | ||
_runChangeHandlers(atom); | ||
} | ||
@@ -237,6 +291,7 @@ } | ||
_setState(atom, nextState); | ||
_runChangeHandlers(atom); | ||
} | ||
} | ||
export { Atom, deref, getValidator, set, setValidator, swap }; | ||
export { addChangeHandler, removeChangeHandler, Atom, deref, getValidator, set, setValidator, swap }; | ||
//# sourceMappingURL=index.esm.js.map |
@@ -1,2 +0,2 @@ | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(t["@libre/atom"]={})}(this,function(t){"use strict";var i=0,r=Object.create(null),e=Object.create(null);function a(t){return r[t.$$id]}function u(t,n){r[t.$$id]=n}function d(t){return e[t.$$id]}function f(t,n){e[t.$$id]=n}var n="Expected an Atom instances, but got:";function c(t){return JSON.stringify(t,null," ")}function s(t){if(!(t instanceof o))throw TypeError(n+"\n\n"+c(t))}var o=function(){function r(t,n){var r=(void 0===n?{}:n).validator;if((r=r||function(){return!0})(t))return Object.defineProperty(this,"$$id",{value:i++}),u(this,t),f(this,r),this;var e="Atom initialized with invalid state:\n\n"+c(t)+"\n\naccording to validator function:\n"+r+"\n\n",o=Error(e);throw o.name="AtomInvalidStateError",o}return r.of=function(t,n){return new r(t,n)},r.prototype.toString=function(){return"Atom<"+c(a(this))+">"},r.prototype.inspect=function(){return this.toString()},r}();t.Atom=o,t.deref=function(t){return s(t),a(t)},t.getValidator=function(t){return s(t),d(t)},t.set=function(t,n){s(t);var r=d(t);if(!r(n)){var e="Attempted to set the state of\n\n"+t+"\n\nwith:\n\n"+c(n)+"\n\nbut it did not pass validator:\n"+r+"\n\n",o=Error(e);throw o.name="AtomInvalidStateError",o}u(t,n)},t.setValidator=function(t,n){if(s(t),!n(a(t))){var r=Error("Could not set validator on\n\n"+t+"\n\nbecause current state would be invalid according to new validator:\n"+n+"\n\n");throw r.name="AtomInvalidStateError",r}f(t,n)},t.swap=function(t,n){s(t);var r=n(a(t)),e=d(t);if(!e(r)){var o="swap updateFn\n"+n+"\n\nattempted to swap the state of\n\n"+t+"\n\nwith:\n\n"+c(r)+"\n\nbut it did not pass validator:\n"+e+"\n\n",i=Error(o);throw i.name="AtomInvalidStateError",i}u(t,r)},Object.defineProperty(t,"__esModule",{value:!0})}); | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(n["@libre/atom"]={})}(this,function(n){"use strict";var i=0,e=Object.create(null),r=Object.create(null),a={};function d(n){return e[n.$$id]}function u(n,t){e[n.$$id]=t}function f(n){return r[n.$$id]}function c(n,t){r[n.$$id]=t}function s(t){Object.keys(a[t.$$id]).forEach(function(n){a[t.$$id][n](d(t))})}var t="Expected an Atom instances, but got:";function l(n){return JSON.stringify(n,null," ")}function v(n){if(!(n instanceof o))throw TypeError(t+"\n\n"+l(n))}var o=function(){function e(n,t){var e=(void 0===t?{}:t).validator;if((e=e||function(){return!0})(n))return Object.defineProperty(this,"$$id",{value:i++}),u(this,n),c(this,e),a[this.$$id]={},this;var r="Atom initialized with invalid state:\n\n"+l(n)+"\n\naccording to validator function:\n"+e+"\n\n",o=Error(r);throw o.name="AtomInvalidStateError",o}return e.of=function(n,t){return new e(n,t)},e.prototype.toString=function(){return"Atom<"+l(d(this))+">"},e.prototype.inspect=function(){return this.toString()},e}();n.addChangeHandler=function(n,t,e){!function(n,t,e){if("function"==typeof a[n.$$id][t])throw new Error('Change handler already registered for key "'+t+'" on '+n+".\nRemove the existing handler before registering a new one.");a[n.$$id][t]=e}(n,t,e)},n.removeChangeHandler=function(n,t){var e;e=t,delete a[n.$$id][e]},n.Atom=o,n.deref=function(n){return v(n),d(n)},n.getValidator=function(n){return v(n),f(n)},n.set=function(n,t){v(n);var e=f(n);if(!e(t)){var r="Attempted to set the state of\n\n"+n+"\n\nwith:\n\n"+l(t)+"\n\nbut it did not pass validator:\n"+e+"\n\n",o=Error(r);throw o.name="AtomInvalidStateError",o}u(n,t),s(n)},n.setValidator=function(n,t){if(v(n),!t(d(n))){var e=Error("Could not set validator on\n\n"+n+"\n\nbecause current state would be invalid according to new validator:\n"+t+"\n\n");throw e.name="AtomInvalidStateError",e}c(n,t)},n.swap=function(n,t){v(n);var e=t(d(n)),r=f(n);if(!r(e)){var o="swap updateFn\n"+t+"\n\nattempted to swap the state of\n\n"+n+"\n\nwith:\n\n"+l(e)+"\n\nbut it did not pass validator:\n"+r+"\n\n",i=Error(o);throw i.name="AtomInvalidStateError",i}u(n,e),s(n)},Object.defineProperty(n,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=index.umd.js.map |
@@ -0,1 +1,2 @@ | ||
export { addChangeHandler, removeChangeHandler } from "./changeHandler"; | ||
export { Atom } from "./atom"; | ||
@@ -2,0 +3,0 @@ export { AtomState } from "./internal-types"; |
@@ -13,1 +13,9 @@ import { Atom } from "./atom"; | ||
export declare function _setValidator<S>(atom: Atom<S>, validator: NonNullable<AtomConstructorOptions<S>["validator"]>): void; | ||
/** @ignore */ | ||
export declare function _initChangeHandlerDict(atom: Atom<any>): void; | ||
/** @ignore */ | ||
export declare function _addChangeHandler<S>(atom: Atom<S>, key: string, handler: (state: S) => void): void; | ||
/** @ignore */ | ||
export declare function _removeChangeHandler<S>(atom: Atom<S>, key: string): void; | ||
/** @ignore */ | ||
export declare function _runChangeHandlers<S>(atom: Atom<S>): void; |
@@ -43,3 +43,4 @@ { | ||
"typedoc": "^0.13.0", | ||
"typescript": "^3.1.6" | ||
"typescript": "^3.1.6", | ||
"utility-types": "^2.1.0" | ||
}, | ||
@@ -92,3 +93,3 @@ "files": [ | ||
"unpkg": "https://unpkg.com/@libre/atom", | ||
"version": "1.1.0" | ||
"version": "1.2.0" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
59318
22
511
0
28