fix: excessive autorun calls

This commit is contained in:
Baptiste Grob
2021-04-08 18:48:49 +02:00
parent 87076addad
commit e12b9ecac8
4 changed files with 41 additions and 45 deletions

View File

@@ -5,7 +5,9 @@ import { AppState } from '@/ui_models/app_state';
type Props = { appState: AppState };
function NoAccountWarning({ appState }: Props) {
const canShow = useAutorunValue(() => appState.noAccountWarning.show);
const canShow = useAutorunValue(() => appState.noAccountWarning.show, [
appState,
]);
if (!canShow) {
return null;
}

View File

@@ -24,11 +24,14 @@ function SearchOptions({ appState }: Props) {
includeProtectedContents,
includeArchived,
includeTrashed,
} = useAutorunValue(() => ({
includeProtectedContents: searchOptions.includeProtectedContents,
includeArchived: searchOptions.includeArchived,
includeTrashed: searchOptions.includeTrashed,
}));
} = useAutorunValue(
() => ({
includeProtectedContents: searchOptions.includeProtectedContents,
includeArchived: searchOptions.includeArchived,
includeTrashed: searchOptions.includeTrashed,
}),
[searchOptions]
);
const [
togglingIncludeProtectedContents,

View File

@@ -15,7 +15,7 @@ import {
AlertDialogDescription,
AlertDialogLabel,
} from '@reach/alert-dialog';
import { toDirective, useAutorun } from './utils';
import { toDirective, useAutorunValue } from './utils';
import { WebApplication } from '@/ui_models/application';
type Session = RemoteSession & {
@@ -211,22 +211,22 @@ const SessionsModal: FunctionComponent<{
<p>{SessionStrings.RevokeText}</p>
</AlertDialogDescription>
<div className="flex my-1 gap-2">
<button
className="sn-button neutral sk-label"
ref={cancelRevokeRef}
onClick={closeRevokeSessionAlert}
>
<span>{SessionStrings.RevokeCancelButton}</span>
</button>
<button
className="sn-button danger sk-label"
onClick={() => {
closeRevokeSessionAlert();
revokeSession(confirmRevokingSessionUuid);
}}
>
<span>{SessionStrings.RevokeConfirmButton}</span>
</button>
<button
className="sn-button neutral sk-label"
ref={cancelRevokeRef}
onClick={closeRevokeSessionAlert}
>
<span>{SessionStrings.RevokeCancelButton}</span>
</button>
<button
className="sn-button danger sk-label"
onClick={() => {
closeRevokeSessionAlert();
revokeSession(confirmRevokingSessionUuid);
}}
>
<span>{SessionStrings.RevokeConfirmButton}</span>
</button>
</div>
</div>
</div>
@@ -243,8 +243,9 @@ const Sessions: FunctionComponent<{
appState: AppState;
application: WebApplication;
}> = ({ appState, application }) => {
const [showModal, setShowModal] = useState(false);
useAutorun(() => setShowModal(appState.isSessionsModalVisible));
const showModal = useAutorunValue(() => appState.isSessionsModalVisible, [
appState,
]);
if (showModal) {
return <SessionsModal application={application} appState={appState} />;

View File

@@ -1,25 +1,18 @@
import { WebApplication } from '@/ui_models/application';
import { AppState } from '@/ui_models/app_state';
import { autorun, IAutorunOptions, IReactionPublic } from 'mobx';
import { autorun } from 'mobx';
import { FunctionComponent, h, render } from 'preact';
import { useEffect } from 'preact/hooks';
import { useState } from 'react';
import { Inputs, useEffect, useState } from 'preact/hooks';
export function useAutorunValue<T>(query: () => T): T {
export function useAutorunValue<T>(query: () => T, inputs: Inputs): T {
const [value, setValue] = useState(query);
useAutorun(() => {
setValue(query());
});
useEffect(() => {
return autorun(() => {
setValue(query());
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, inputs);
return value;
}
export function useAutorun(
view: (r: IReactionPublic) => unknown,
opts?: IAutorunOptions
): void {
useEffect(() => autorun(view, opts), [view, opts]);
}
export function toDirective<Props>(
component: FunctionComponent<Props>,
scope: Record<string, '=' | '&'> = {}
@@ -37,10 +30,7 @@ export function toDirective<Props>(
}
return {
$onChanges() {
render(
h(component, $scope),
$element[0]
);
render(h(component, $scope), $element[0]);
},
};
},