@d3vtool/hooks
Version:
Collection of custom React hooks to simplify tasks in your React projects.
2 lines (1 loc) • 991 B
JavaScript
var r=function(){function e(t){this.onChangeListeners=new Set,this.listeners=new Set,this.currentState=t}return e.prototype.getCurrentState=function(){return this.currentState},e.prototype.setCurrentState=function(t){this.currentState=t,this.onChangeListeners.forEach(function(n){return n()})},e.prototype.attachListener=function(t){this.listeners.add(t)},e.prototype.detachListener=function(t){this.listeners.delete(t)},e.prototype.notifyListener=function(t){this.currentState=t,this.listeners.forEach(function(n){return n(t)}),this.onChangeListeners.forEach(function(n){return n()})},e.prototype.onChange=function(t){this.onChangeListeners.add(t)},e.prototype.removeOnChange=function(t){this.onChangeListeners.delete(t)},e}();export{r as Hub};export function createHub(e){return new r(e instanceof Function?e():e)}export function createComputedHub(e,t){var n=new r(t(e.getCurrentState()));return e.onChange(function(){var o=e.getCurrentState(),i=t(o);n.notifyListener(i)}),n}
;