@clerk/clerk-react
Version:
Clerk.dev React library
33 lines • 1.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.withMaxAllowedInstancesGuard = exports.useMaxAllowedInstancesGuard = void 0;
const tslib_1 = require("tslib");
const react_1 = (0, tslib_1.__importDefault)(require("react"));
const counts = new Map();
function useMaxAllowedInstancesGuard(name, error, maxCount = 1) {
react_1.default.useEffect(() => {
const count = counts.get(name) || 0;
if (count == maxCount) {
throw new Error(error);
}
counts.set(name, count + 1);
return () => {
counts.set(name, (counts.get(name) || 1) - 1);
};
}, []);
}
exports.useMaxAllowedInstancesGuard = useMaxAllowedInstancesGuard;
function withMaxAllowedInstancesGuard(WrappedComponent, name, error) {
const displayName = WrappedComponent.displayName ||
WrappedComponent.name ||
name ||
'Component';
const Hoc = (props) => {
useMaxAllowedInstancesGuard(name, error);
return react_1.default.createElement(WrappedComponent, Object.assign({}, props));
};
Hoc.displayName = `withMaxAllowedInstancesGuard(${displayName})`;
return Hoc;
}
exports.withMaxAllowedInstancesGuard = withMaxAllowedInstancesGuard;
//# sourceMappingURL=useMaxAllowedInstancesGuard.js.map