{"version":3,"file":"SetInterval.mjs","sources":["../../../../src/components/SetInterval/SetInterval.tsx"],"sourcesContent":["import { isEqual } from 'lodash';\nimport { PureComponent } from 'react';\nimport { interval, Subscription, Subject, of, NEVER } from 'rxjs';\nimport { tap, switchMap } from 'rxjs/operators';\n\nimport { stringToMs, SelectableValue } from '@grafana/data';\n\nimport { RefreshPicker } from '../RefreshPicker/RefreshPicker';\n\nexport function getIntervalFromString(strInterval: string): SelectableValue<number> {\n  return {\n    label: strInterval,\n    value: stringToMs(strInterval),\n  };\n}\n\ninterface Props {\n  func: () => unknown;\n  loading: boolean;\n  interval: string;\n}\n\nexport class SetInterval extends PureComponent<Props> {\n  private propsSubject: Subject<Props>;\n  private subscription: Subscription | null;\n\n  constructor(props: Props) {\n    super(props);\n    this.propsSubject = new Subject<Props>();\n    this.subscription = null;\n  }\n\n  componentDidMount() {\n    // Creating a subscription to propsSubject. This subject pushes values every time\n    // SetInterval's props change\n    this.subscription = this.propsSubject\n      .pipe(\n        // switchMap creates a new observables based on the input stream,\n        // which becomes part of the propsSubject stream\n        switchMap((props) => {\n          // If the query is live, empty value is emitted. `of` creates single value,\n          // which is merged to propsSubject stream\n          if (RefreshPicker.isLive(props.interval)) {\n            return of({});\n          }\n\n          // When query is loading, a new stream is merged. But it's a stream that emits no values(NEVER),\n          // hence next call of this function will happen when query changes, and new props are passed into this component\n          // When query is NOT loading, a new value is emitted, this time it's an interval value,\n          // which makes tap function below execute on that interval basis.\n          return props.loading ? NEVER : interval(stringToMs(props.interval));\n        }),\n        // tap will execute function passed via func prop\n        // * on value from `of` stream merged if query is live\n        // * on specified interval (triggered by values emitted by interval)\n        tap(() => this.props.func())\n      )\n      .subscribe();\n\n    // When component has mounted, propsSubject emits its first value\n    this.propsSubject.next(this.props);\n  }\n\n  componentDidUpdate(prevProps: Props) {\n    if (\n      (RefreshPicker.isLive(prevProps.interval) && RefreshPicker.isLive(this.props.interval)) ||\n      isEqual(prevProps, this.props)\n    ) {\n      return;\n    }\n    // if props changed, a new value is emitted from propsSubject\n    this.propsSubject.next(this.props);\n  }\n\n  componentWillUnmount() {\n    if (this.subscription) {\n      this.subscription.unsubscribe();\n    }\n\n    this.propsSubject.unsubscribe();\n  }\n\n  render(): null {\n    return null;\n  }\n}\n"],"names":[],"mappings":";;;;;;;AAsBO,MAAM,oBAAoB,aAAqB,CAAA;AAAA,EAIpD,YAAY,KAAc,EAAA;AACxB,IAAA,KAAA,CAAM,KAAK,CAAA;AACX,IAAK,IAAA,CAAA,YAAA,GAAe,IAAI,OAAe,EAAA;AACvC,IAAA,IAAA,CAAK,YAAe,GAAA,IAAA;AAAA;AACtB,EAEA,iBAAoB,GAAA;AAGlB,IAAK,IAAA,CAAA,YAAA,GAAe,KAAK,YACtB,CAAA,IAAA;AAAA;AAAA;AAAA,MAGC,SAAA,CAAU,CAAC,KAAU,KAAA;AAGnB,QAAA,IAAI,aAAc,CAAA,MAAA,CAAO,KAAM,CAAA,QAAQ,CAAG,EAAA;AACxC,UAAO,OAAA,EAAA,CAAG,EAAE,CAAA;AAAA;AAOd,QAAA,OAAO,MAAM,OAAU,GAAA,KAAA,GAAQ,SAAS,UAAW,CAAA,KAAA,CAAM,QAAQ,CAAC,CAAA;AAAA,OACnE,CAAA;AAAA;AAAA;AAAA;AAAA,MAID,GAAI,CAAA,MAAM,IAAK,CAAA,KAAA,CAAM,MAAM;AAAA,MAE5B,SAAU,EAAA;AAGb,IAAK,IAAA,CAAA,YAAA,CAAa,IAAK,CAAA,IAAA,CAAK,KAAK,CAAA;AAAA;AACnC,EAEA,mBAAmB,SAAkB,EAAA;AACnC,IAAA,IACG,aAAc,CAAA,MAAA,CAAO,SAAU,CAAA,QAAQ,KAAK,aAAc,CAAA,MAAA,CAAO,IAAK,CAAA,KAAA,CAAM,QAAQ,CACrF,IAAA,OAAA,CAAQ,SAAW,EAAA,IAAA,CAAK,KAAK,CAC7B,EAAA;AACA,MAAA;AAAA;AAGF,IAAK,IAAA,CAAA,YAAA,CAAa,IAAK,CAAA,IAAA,CAAK,KAAK,CAAA;AAAA;AACnC,EAEA,oBAAuB,GAAA;AACrB,IAAA,IAAI,KAAK,YAAc,EAAA;AACrB,MAAA,IAAA,CAAK,aAAa,WAAY,EAAA;AAAA;AAGhC,IAAA,IAAA,CAAK,aAAa,WAAY,EAAA;AAAA;AAChC,EAEA,MAAe,GAAA;AACb,IAAO,OAAA,IAAA;AAAA;AAEX;;;;"}