@iore8655/react-bus
Version:
@iore8655/react-bus is a global/local state management by pub/sub
94 lines (93 loc) • 3.82 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.useStateBusSelector = exports.createStateBus = void 0;
var react_1 = require("react");
var lodash_1 = __importDefault(require("lodash"));
var react_bus_core_1 = require("./react-bus-core");
var StateBus = /** @class */ (function (_super) {
__extends(StateBus, _super);
function StateBus(initialState) {
var _this = _super.call(this) || this;
_this.initialState = lodash_1["default"].cloneDeep(initialState);
_this.state = lodash_1["default"].cloneDeep(initialState);
return _this;
}
StateBus.prototype.rerender = function () {
Object.values(this.subscribers).forEach(function (subscriber) { return subscriber.callback(); });
};
StateBus.prototype.setState = function (props) {
this.state = __assign(__assign({}, this.state), props);
};
StateBus.prototype.getState = function () {
return this.state;
};
StateBus.prototype.reset = function (props) {
if (props === void 0) { props = {}; }
this.state = lodash_1["default"].cloneDeep(__assign(__assign({}, this.initialState), props));
this.rerender();
};
StateBus.prototype.restore = function (newState) {
this.state = lodash_1["default"].cloneDeep(newState);
this.rerender();
};
StateBus.prototype.dispatch = function (props) {
if (props instanceof Function) {
props(this.state);
this.setState({});
}
if (props instanceof Object) {
this.setState(props);
}
this.rerender();
};
return StateBus;
}(react_bus_core_1.Bus));
function createStateBus(initialState) {
return new StateBus(initialState);
}
exports.createStateBus = createStateBus;
function useStateBusSelector(stateBus, selector) {
if (selector === void 0) { selector = function (state) { return state; }; }
var _a = react_1.useState({}), forceUpdate = _a[1];
var value = react_1.useRef(selector(stateBus.getState()));
var subId = react_1.useMemo(function () { return "sub-" + react_bus_core_1.context.subId++; }, []);
react_1.useEffect(function () {
stateBus.subscribe(subId, function () {
var nextValue = selector(stateBus.getState());
if (value.current !== nextValue) {
value.current = nextValue;
forceUpdate({});
}
});
return function () { return stateBus.unsubscribe(subId); };
}, [stateBus, subId, value]);
return value.current;
}
exports.useStateBusSelector = useStateBusSelector;