@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
128 lines (127 loc) • 4.3 kB
JavaScript
;
"use client";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.GlobalStatusRemove = exports.GlobalStatusInterceptor = void 0;
var _react = 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 }; }
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 = (0, _react.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 = (0, _react.memo)(GlobalStatusRemove);
MemoizedGlobalStatusController.Remove = MemoizedGlobalStatusRemove;
MemoizedGlobalStatusController.Update = MemoizedGlobalStatusController;
(0, _withComponentMarkers.default)(MemoizedGlobalStatusController, {
_supportsSpacingProps: true
});
var _default = exports.default = MemoizedGlobalStatusController;
//# sourceMappingURL=GlobalStatusController.js.map