Bug 1552424 - Go to file is slow when searching for a file. r=davidwalsh

Differential Revision: https://phabricator.services.mozilla.com/D32328

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Jason Laster 2019-05-28 13:23:39 +00:00
Родитель 3c96684a0f
Коммит 050fe9e3d6
4 изменённых файлов: 68 добавлений и 2 удалений

Просмотреть файл

@ -22,6 +22,7 @@ import {
isSymbolsLoading,
getContext,
} from "../selectors";
import { memoizeLast } from "../utils/memoizeLast";
import { scrollList } from "../utils/result-list";
import {
formatSymbols,
@ -131,11 +132,15 @@ export class QuickOpenModal extends Component<Props, State> {
return query.split(":")[0];
};
formatSources = memoizeLast((displayedSources, tabs) => {
const tabUrls = new Set(tabs.map((tab: Tab) => tab.url));
return formatSources(displayedSources, tabUrls);
});
searchSources = (query: string) => {
const { displayedSources, tabs } = this.props;
const tabUrls = new Set(tabs.map((tab: Tab) => tab.url));
const sources = formatSources(displayedSources, tabUrls);
const sources = this.formatSources(displayedSources, tabs);
const results =
query == "" ? sources : filter(sources, this.dropGoto(query));
return this.setResults(results);

Просмотреть файл

@ -0,0 +1,27 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
export function memoizeLast(fn) {
let lastArgs: any[];
let lastResult: any;
const memoized = (...args) => {
if (
lastArgs &&
args.length === lastArgs.length &&
args.every((arg, i) => arg === lastArgs[i])
) {
return lastResult;
}
lastArgs = args;
lastResult = fn(...args);
return lastResult;
};
return memoized;
}
export default memoizeLast;

Просмотреть файл

@ -33,6 +33,7 @@ CompiledModules(
'log.js',
'makeRecord.js',
'memoize.js',
'memoizeLast.js',
'memoizableAction.js',
'path.js',
'prefs.js',

Просмотреть файл

@ -0,0 +1,33 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// @flow
import { memoizeLast } from "../memoizeLast";
const a = { number: 3 };
const b = { number: 4 };
function add(...numberObjects) {
return numberObjects.reduce((prev, cur) => prev + cur.number, 0);
}
describe("memozie", () => {
it("should re-calculate when a value changes", () => {
const mAdd = memoizeLast(add);
mAdd(a);
expect(mAdd(a)).toEqual(3);
mAdd(b);
expect(mAdd(b)).toEqual(4);
});
it("should only run once", () => {
const mockAdd = jest.fn(add);
const mAdd = memoizeLast(mockAdd);
mAdd(a);
mAdd(a);
expect(mockAdd.mock.calls[0]).toEqual([{ number: 3 }]);
});
});