@grafana/ui
Version:
Grafana Components Library
79 lines (72 loc) • 2.75 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var React = require('react');
var rxjs = require('rxjs');
var operators = require('rxjs/operators');
var data = require('@grafana/data');
var RefreshPicker = require('../RefreshPicker/RefreshPicker.cjs');
function _interopNamespaceCompat(e) {
if (e && typeof e === 'object' && 'default' in e) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n.default = e;
return Object.freeze(n);
}
var React__namespace = /*#__PURE__*/_interopNamespaceCompat(React);
;
const SetInterval = React__namespace.memo(({ func, loading, interval: intervalStr }) => {
const propsSubjectRef = React.useRef(null);
const subscriptionRef = React.useRef(null);
const prevPropsRef = React.useRef({ func, loading, interval: intervalStr });
React.useEffect(() => {
propsSubjectRef.current = new rxjs.Subject();
subscriptionRef.current = propsSubjectRef.current.pipe(
// switchMap creates a new observables based on the input stream,
// which becomes part of the propsSubject stream
operators.switchMap((props) => {
if (RefreshPicker.RefreshPicker.isLive(props.interval)) {
return rxjs.of({});
}
return props.loading ? rxjs.NEVER : rxjs.interval(data.stringToMs(props.interval));
}),
// tap will execute function passed by props
// * on value from `of` stream merged if query is live
// * on specified interval (triggered by values emitted by interval)
operators.tap(() => {
propsSubjectRef.current && func();
})
).subscribe();
propsSubjectRef.current.next({ func, loading, interval: intervalStr });
return () => {
if (subscriptionRef.current) {
subscriptionRef.current.unsubscribe();
}
if (propsSubjectRef.current) {
propsSubjectRef.current.complete();
}
};
}, []);
React.useEffect(() => {
const prev = prevPropsRef.current;
const currentProps = { func, loading, interval: intervalStr };
if (RefreshPicker.RefreshPicker.isLive(prev.interval) && RefreshPicker.RefreshPicker.isLive(currentProps.interval)) {
return;
}
propsSubjectRef.current && propsSubjectRef.current.next(currentProps);
prevPropsRef.current = currentProps;
}, [func, loading, intervalStr]);
return null;
});
SetInterval.displayName = "SetInterval";
exports.SetInterval = SetInterval;
//# sourceMappingURL=SetInterval.cjs.map