Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: refresh plugins interval #3663

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {createContext, useEffect, useState} from 'react';
import semver from 'semver';
import islandHttpClient, {APIEndpoint} from '../../IslandHttpClient';
import {useInterval} from '../../ui-components/utils/useInterval';


// Types returned from the API
Expand Down Expand Up @@ -85,15 +86,22 @@ export const PluginState = () :PluginsContextType => {
const [installedPlugins, setInstalledPlugins] = useState([]);
const [numberOfPluginsThatRequiresUpdate, setNumberOfPluginsThatRequiresUpdate] = useState(0);

useInterval(() => {
refreshAvailablePluginsAndNumberOfUpgradablePlugins(true);
}, 1000 * 60 * 60)

useEffect(() => {
refreshInstalledPlugins();
}, []);

useEffect(() => {
refreshAvailablePlugins().then(() => refreshNumberOfUpgradablePlugins());
refreshAvailablePluginsAndNumberOfUpgradablePlugins();
}, [installedPlugins]);

const refreshAvailablePluginsAndNumberOfUpgradablePlugins = (forceRefresh = false) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general this is not a good name. If a name is "DoSomething1andDoSomething2" it means that there are 2 functions: "DoSomething1" and "DoSomething2". So we should just call "DoSomething1" and "DoSomething2" in a row instead of creating another function. But since there's a temporal coupling, I don't feel strongly about this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can change it if you have a better one

refreshAvailablePlugins(forceRefresh).then(() => refreshNumberOfUpgradablePlugins());
}

const parsePluginMetadataResponse = (response: PluginMetadataResponse) :AvailablePlugin[] => {
let plugins :AvailablePlugin[] = [];
for (const pluginType in response) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {useEffect, useRef} from 'react'

export const useInterval = (callback: () => void, delay: number | null) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should or need to override this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you mean?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why we need to wrap the useInterval and what this wrapping achieves

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a general hook to use interval with self-cleaning.

const savedCallback = useRef(callback);

// Remember the latest callback if it changes
useEffect(() => {
savedCallback.current = callback;
}, [callback])
Comment on lines +6 to +9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the practice


// Set up the interval
useEffect(() => {
// Don't schedule if no delay is specified
// Note: 0 is a valid value for delay
if (!delay && delay !== 0) {
return;
}

const id = setInterval(() => savedCallback.current(), delay);

return () => clearInterval(id);
}, [delay])
}