Quantumbraid's picture
download
raw
4.39 kB
Object.defineProperty(exports, '__esModule', { value: true });
var react = require('react');
var objCopy = function (obj) {
return JSON.parse(JSON.stringify(obj));
};
var GlobalState = /** @class */ (function () {
function GlobalState(initialValue) {
var _this = this;
this._hooks = [];
this._setValues = [];
this._addHook = function (hook) {
_this._hooks.push(hook);
};
this.get = function (clone) {
if (clone === void 0) { clone = false; }
// if clone is true, use object copy to return a new instance of the object
if (clone) {
try {
return objCopy(_this._value);
}
catch (e) {
// objCopy failed for some reason, just return normal value
return _this._value;
}
}
else {
return _this._value;
}
};
this.set = function (newVal) {
_this._value = newVal;
// keep an array of hooks to remove that no longer exist
var toRemoveHooks = [];
var toRemoveSetValues = [];
// trigger the state update for each registered hook
for (var i = 0; i < _this._hooks.length; i++) {
if (_this._hooks[i]) {
try {
_this._hooks[i](newVal);
}
catch (e) {
// threw an error, component muse have unmounted, add to remove list
toRemoveHooks.push(_this._hooks[i]);
}
}
else {
// hook no longer exist, add to remove list
toRemoveHooks.push(_this._hooks[i]);
}
}
// trigger set values
for (var i = 0; i < _this._setValues.length; i++) {
if (_this._setValues[i]) {
try {
_this._setValues[i](newVal);
}
catch (e) {
// threw an error, component muse have unmounted, add to remove list
toRemoveSetValues.push(_this._setValues[i]);
}
}
else {
// hook no longer exist, add to remove list
toRemoveSetValues.push(_this._setValues[i]);
}
}
// remove any that do not exist or failed
for (var i = 0; i < toRemoveHooks.length; i++) {
_this._hooks.splice(_this._hooks.indexOf(toRemoveHooks[i]), 1);
}
// remove any that do not exist or failed
for (var i = 0; i < toRemoveSetValues.length; i++) {
_this._setValues.splice(_this._setValues.indexOf(toRemoveSetValues[i]), 1);
}
};
this.use = function () {
return useGlobalState(_this);
};
this.useValue = function () {
return useGlobalState(_this)[0];
};
this.onChange = function (callback) {
if (!(_this._hooks.includes(callback))) {
_this._addHook(callback);
}
};
this._addSetValueHook = function (setValue) {
if (!(_this._setValues.includes(setValue))) {
_this._setValues.push(setValue);
}
};
this._value = initialValue;
}
return GlobalState;
}());
var createGlobalState = function (initialValue) {
// return a new instance of State
return new GlobalState(initialValue);
};
var useGlobalState = function (state) {
// piggyback on the react hook useState
var _a = react.useState(state.get()), value = _a[0], setValue = _a[1];
// add use state set hook to the state hooks
// state._addHook((newVal:S) => {
// setValue(newVal);
// });
state._addSetValueHook(setValue);
// return in a same use format as react useState hook
return [value, state.set];
};
exports.GlobalState = GlobalState;
exports.createGlobalState = createGlobalState;
exports.useGlobalState = useGlobalState;
//# sourceMappingURL=index.js.map

Xet Storage Details

Size:
4.39 kB
·
Xet hash:
214f25e9e3c1830363b2423e792554308891640c9f46af94ae2e11d83e1640aa

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.