@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
129 lines (128 loc) • 4.95 kB
JavaScript
;
"use client";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.GlobalStatusRemove = exports.GlobalStatusInterceptor = void 0;
var _react = _interopRequireWildcard(require("react"));
var _withComponentMarkers = _interopRequireDefault(require("../../shared/helpers/withComponentMarkers.js"));
var _useMountEffect = _interopRequireDefault(require("../../shared/helpers/useMountEffect.js"));
var _GlobalStatusProvider = _interopRequireDefault(require("./GlobalStatusProvider.js"));
var _componentHelper = require("../../shared/component-helper.js");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
class GlobalStatusInterceptor {
constructor(props) {
let GSP = null;
try {
GSP = _GlobalStatusProvider.default;
} catch (e) {}
if (!GSP && typeof window !== 'undefined') {
GSP = window.GlobalStatusProvider;
}
this.provider = GSP.init(props.id || 'main', provider => {
const {
statusId
} = provider.add(props);
this.statusId = statusId;
});
return this;
}
add(props) {
return this.provider.add({
statusId: this.statusId,
...props
});
}
update(props) {
return this.provider.update(this.statusId, props);
}
remove() {
return this.provider.remove(this.statusId);
}
}
exports.GlobalStatusInterceptor = GlobalStatusInterceptor;
function initProvider(id) {
let GSP = null;
try {
GSP = _GlobalStatusProvider.default;
} catch (e) {}
if (!GSP && typeof window !== 'undefined') {
GSP = window.GlobalStatusProvider;
}
return GSP.init(id);
}
const globalStatusControllerDefaultProps = {
id: 'main',
statusId: null,
removeOnUnmount: false
};
function GlobalStatusController(ownProps) {
const props = {
...globalStatusControllerDefaultProps,
...(0, _componentHelper.removeUndefinedProps)({
...ownProps
})
};
const providerRef = (0, _react.useRef)(undefined);
const statusIdRef = (0, _react.useRef)(undefined);
const prevPropsRef = (0, _react.useRef)(undefined);
const removeOnUnmountRef = (0, _react.useRef)(props.removeOnUnmount);
removeOnUnmountRef.current = props.removeOnUnmount;
if (!providerRef.current) {
providerRef.current = initProvider(props.id);
prevPropsRef.current = ownProps;
}
if (prevPropsRef.current !== ownProps) {
var _providerRef$current;
(_providerRef$current = providerRef.current) === null || _providerRef$current === void 0 || _providerRef$current.update(statusIdRef.current, props);
prevPropsRef.current = ownProps;
}
(0, _useMountEffect.default)(() => {
const {
statusId
} = providerRef.current.add(props);
statusIdRef.current = statusId;
return () => {
if (providerRef.current && removeOnUnmountRef.current) {
providerRef.current.remove(statusIdRef.current);
}
};
});
return null;
}
const MemoizedGlobalStatusController = _react.default.memo(GlobalStatusController);
const globalStatusRemoveDefaultProps = {
id: 'main'
};
function GlobalStatusRemove(ownProps) {
const props = {
...globalStatusRemoveDefaultProps,
...(0, _componentHelper.removeUndefinedProps)({
...ownProps
})
};
const providerRef = (0, _react.useRef)(undefined);
const prevPropsRef = (0, _react.useRef)(undefined);
if (!providerRef.current) {
providerRef.current = initProvider(props.id);
prevPropsRef.current = ownProps;
}
if (prevPropsRef.current !== ownProps) {
var _providerRef$current2;
(_providerRef$current2 = providerRef.current) === null || _providerRef$current2 === void 0 || _providerRef$current2.update(props.statusId, props);
prevPropsRef.current = ownProps;
}
(0, _useMountEffect.default)(() => {
providerRef.current.remove(props.statusId, props);
});
return null;
}
const MemoizedGlobalStatusRemove = exports.GlobalStatusRemove = _react.default.memo(GlobalStatusRemove);
MemoizedGlobalStatusController.Remove = MemoizedGlobalStatusRemove;
MemoizedGlobalStatusController.Update = MemoizedGlobalStatusController;
(0, _withComponentMarkers.default)(MemoizedGlobalStatusController, {
_supportsSpacingProps: true
});
var _default = exports.default = MemoizedGlobalStatusController;
//# sourceMappingURL=GlobalStatusController.js.map