Skip to content

Commit

Permalink
use wrapper's setRefreshInterval subscriber method
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarezmelissa87 committed Jun 20, 2019
1 parent 0c385e9 commit fb462ee
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import React, { useEffect } from 'react';

import { timefilter } from 'ui/timefilter';
import { timefilter } from '../../../../../../common/timefilter';

import {
DEFAULT_REFRESH_INTERVAL_MS,
Expand All @@ -21,6 +21,7 @@ export const useRefreshInterval = (
) => {
useEffect(() => {
let jobsRefreshInterval: null | number = null;
let timefilterSubscriber: { unsubscribe: () => void };

timefilter.disableTimeRangeSelector();
timefilter.enableAutoRefreshSelector();
Expand All @@ -44,7 +45,7 @@ export const useRefreshInterval = (

function initAutoRefreshUpdate() {
// update the interval if it changes
timefilter.on('refreshIntervalUpdate', () => {
timefilterSubscriber = timefilter.subscribeToRefreshIntervalUpdate(function triggerUpdate() {
setAutoRefresh();
});
}
Expand Down Expand Up @@ -80,6 +81,8 @@ export const useRefreshInterval = (
// useEffect cleanup
return () => {
clearRefreshInterval();
timefilterSubscriber.unsubscribe();
timefilter.off('refreshIntervalUpdate');
};
}, []); // [] as comparator makes sure this only runs once
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/


import { timefilter } from 'ui/timefilter';
import { timefilter } from '../../../../../common/timefilter';

import { ml } from 'plugins/ml/services/ml_api_service';
import { loadFullJob, filterJobs, checkForAutoStartDatafeed } from '../utils';
Expand Down Expand Up @@ -68,6 +68,7 @@ export class JobsListView extends Component {
this.showCreateWatchFlyout = () => {};

this.blockRefresh = false;
this.timefilterSubscriber = null;
}

componentDidMount() {
Expand Down Expand Up @@ -95,6 +96,9 @@ export class JobsListView extends Component {

componentWillUnmount() {
timefilter.off('refreshIntervalUpdate');
if (this.timefilterSubscriber !== null) {
this.timefilterSubscriber.unsubscribe();
}
deletingJobsRefreshTimeout = null;
this.clearRefreshInterval();
}
Expand All @@ -115,8 +119,9 @@ export class JobsListView extends Component {

initAutoRefreshUpdate() {
// update the interval if it changes
timefilter.on('refreshIntervalUpdate', () => {
this.setAutoRefresh();
const self = this;
this.timefilterSubscriber = timefilter.subscribeToRefreshIntervalUpdate(function triggerUpdate() {
self.setAutoRefresh();
});
}

Expand Down
8 changes: 6 additions & 2 deletions x-pack/plugins/ml/public/util/refresh_interval_watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { timefilter } from 'ui/timefilter';
import { timefilter } from '../../common/timefilter';

/*
* Watches for changes to the refresh interval of the page time filter,
Expand All @@ -15,6 +15,7 @@ export function refreshIntervalWatcher($timeout) {

let refresher;
let listener;
let timefilterSubscriber;

const onRefreshIntervalChange = () => {
if (refresher) {
Expand All @@ -34,11 +35,14 @@ export function refreshIntervalWatcher($timeout) {

function init(listenerCallback) {
listener = listenerCallback;
timefilter.on('refreshIntervalUpdate', onRefreshIntervalChange);
timefilterSubscriber = timefilter.subscribeToRefreshIntervalUpdate(function triggerUpdate() {
onRefreshIntervalChange();
});
}

function cancel() {
$timeout.cancel(refresher);
timefilterSubscriber.unsubscribe();
timefilter.off('refreshIntervalUpdate', onRefreshIntervalChange);
}

Expand Down

0 comments on commit fb462ee

Please sign in to comment.