@shined/reactive
Version:
⚛️ Proxy-driven state library for JavaScript application, Intuitive, Flexible and Written in TypeScript.
187 lines • 7.22 kB
JavaScript
;
function _array_like_to_array(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
return arr2;
}
function _array_without_holes(arr) {
if (Array.isArray(arr)) return _array_like_to_array(arr);
}
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _iterable_to_array(iter) {
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _non_iterable_spread() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _object_spread(target) {
for(var i = 1; i < arguments.length; i++){
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === "function") {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
ownKeys.forEach(function(key) {
_define_property(target, key, source[key]);
});
}
return target;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) {
symbols = symbols.filter(function(sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
}
keys.push.apply(keys, symbols);
}
return keys;
}
function _object_spread_props(target, source) {
source = source != null ? source : {};
if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function(key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
return target;
}
function _to_consumable_array(arr) {
return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
function _unsupported_iterable_to_array(o, minLen) {
if (!o) return;
if (typeof o === "string") return _array_like_to_array(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(n);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
Object.defineProperty(exports, "__esModule", {
value: true
});
function _nullishCoalesce(lhs, rhsFn) {
if (lhs != null) {
return lhs;
} else {
return rhsFn();
}
}
var _chunk7Y3UI2QQcjs = require('./chunk-7Y3UI2QQ.cjs');
// src/react/use-snapshot.ts
var _react = require('react');
var _withselectorjs = require('use-sync-external-store/shim/with-selector.js');
function useSnapshot(proxyState, selectorOrOption, maybeOptions) {
var isUnmountedRef = _react.useRef.call(void 0, false);
_react.useEffect.call(void 0, function() {
return function() {
isUnmountedRef.current = true;
};
}, []);
var options;
var selector;
if (selectorOrOption && !_chunk7Y3UI2QQcjs.isFunction.call(void 0, selectorOrOption)) {
options = selectorOrOption;
selector = void 0;
} else {
options = maybeOptions;
selector = selectorOrOption;
}
var _$_nullishCoalesce = _nullishCoalesce(options, function() {
return {};
}), updateInSync = _$_nullishCoalesce.sync, _nullishCoalesce_isEqual = _$_nullishCoalesce.isEqual, isEqual = _nullishCoalesce_isEqual === void 0 ? _chunk7Y3UI2QQcjs.shallowEqual : _nullishCoalesce_isEqual;
var _subscribe = _react.useCallback.call(void 0, function(callback) {
return _chunk7Y3UI2QQcjs.subscribe.call(void 0, proxyState, callback, updateInSync);
}, [
proxyState,
updateInSync
]);
var _getSnapshot = _react.useCallback.call(void 0, function() {
return _chunk7Y3UI2QQcjs.snapshot.call(void 0, proxyState);
}, [
proxyState
]);
var _selector = selector || function(s) {
return s;
};
var _snapshot = _withselectorjs.useSyncExternalStoreWithSelector.call(void 0, _subscribe, _getSnapshot, _getSnapshot, _selector, function(a, b) {
if (isUnmountedRef.current) return true;
return isEqual(a, b);
});
return _snapshot;
}
// src/enhancers/react/with-use-snapshot.ts
function withUseSnapshot(store) {
var boundUseSnapshot = function(selectorOrOption, maybeOptions) {
var options;
var selector;
if (selectorOrOption && !_chunk7Y3UI2QQcjs.isFunction.call(void 0, selectorOrOption)) {
options = selectorOrOption;
selector = void 0;
} else {
options = maybeOptions;
selector = selectorOrOption;
}
return useSnapshot(store.mutate, selector, options);
};
return _object_spread_props(_object_spread({}, store), {
useSnapshot: boundUseSnapshot
});
}
// src/react/use-subscribe.ts
function useSubscribe(proxyState, listener, notifyInSync) {
var listenerRef = _react.useRef.call(void 0, listener);
listenerRef.current = listener;
_react.useEffect.call(void 0, function() {
var _listenerRef;
var unsubscribe = _chunk7Y3UI2QQcjs.subscribe.call(void 0, proxyState, function() {
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
args[_key] = arguments[_key];
}
return (_listenerRef = listenerRef).current.apply(_listenerRef, _to_consumable_array(args));
}, notifyInSync);
return unsubscribe;
}, [
proxyState,
notifyInSync
]);
}
// src/enhancers/react/with-use-subscribe.ts
function withUseSubscribe(store) {
var boundUseSubscribe = function(listener, notifyInSync) {
return useSubscribe(store.mutate, listener, notifyInSync);
};
return _object_spread_props(_object_spread({}, store), {
useSubscribe: boundUseSubscribe
});
}
// src/react/create-with-hooks.ts
function createWithHooks(initialState) {
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
return withUseSnapshot(withUseSubscribe(_chunk7Y3UI2QQcjs.createVanilla.call(void 0, initialState, options)));
}
exports.useSnapshot = useSnapshot;
exports.withUseSnapshot = withUseSnapshot;
exports.useSubscribe = useSubscribe;
exports.withUseSubscribe = withUseSubscribe;
exports.createWithHooks = createWithHooks;
//# sourceMappingURL=chunk-VDKMANM7.cjs.map