Skip to content

Commit

Permalink
feature(code/frontend): cancel file blob and directory commits reques…
Browse files Browse the repository at this point in the history
…t if outdated (#43348)
  • Loading branch information
WangQianliang authored Aug 18, 2019
1 parent d28690d commit 6c28612
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
12 changes: 6 additions & 6 deletions x-pack/legacy/plugins/code/public/sagas/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,12 @@ function* handleMainRouteChange(action: Action<Match>) {
} else {
yield put(closePanel(false));
}
}

yield call(handleFile, repoUri, file, revision);
const commits = yield select((state: RootState) => state.revision.treeCommits[file]);
if (commits === undefined) {
yield put(fetchTreeCommits({ revision, uri: repoUri, path: file }));
yield call(handleFile, repoUri, file, revision);
} else {
const commits = yield select((state: RootState) => state.revision.treeCommits[file]);
if (commits === undefined) {
yield put(fetchTreeCommits({ revision, uri: repoUri, path: file }));
}
}
}
const lastRequestPath = yield select(lastRequestPathSelector);
Expand Down
29 changes: 19 additions & 10 deletions x-pack/legacy/plugins/code/public/sagas/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { Action } from 'redux-actions';
import { npStart } from 'ui/new_platform';
import Url from 'url';
import { call, put, select, takeEvery, takeLatest } from 'redux-saga/effects';
import { call, put, select, takeEvery, takeLatest, cancel } from 'redux-saga/effects';

import {
fetchDirectory,
Expand Down Expand Up @@ -45,6 +45,7 @@ import {
import { treeCommitsSelector, currentPathSelector } from '../selectors';
import { repoRoutePattern } from './patterns';
import { FileTree } from '../../model';
import { singletonRequestSaga } from '../utils/saga_utils';

function* handleFetchRepoTree(action: Action<FetchRepoTreePayload>) {
try {
Expand Down Expand Up @@ -154,21 +155,24 @@ function* handleFetchMoreCommits(action: Action<string>) {
}
}

function* handleFetchTreeCommits(action: Action<FetchFilePayload>) {
function* handleFetchTreeCommits(action: Action<FetchFilePayload>, signal: AbortSignal, task: any) {
try {
const path = action.payload!.path;
const commits = yield call(requestCommits, action.payload!, path);
const commits = yield call(requestCommits, action.payload!, path, undefined, undefined, signal);
yield put(fetchTreeCommitsSuccess({ path, commits }));
} catch (err) {
yield put(fetchTreeCommitsFailed(err));
} finally {
yield cancel(task);
}
}

function requestCommits(
{ uri, revision }: FetchRepoPayloadWithRevision,
path?: string,
loadMore?: boolean,
count?: number
count?: number,
signal?: AbortSignal
) {
const pathStr = path ? `/${path}` : '';
let query: any = {};
Expand All @@ -182,13 +186,15 @@ function requestCommits(
`/api/code/repo/${uri}/history/${encodeURIComponent(revision)}${pathStr}`,
{
query,
signal,
}
);
}

export async function requestFile(
payload: FetchFilePayload,
line?: string
line?: string,
signal?: AbortSignal
): Promise<FetchFileResponse> {
const { uri, revision, path } = payload;
const url = `/api/code/repo/${uri}/blob/${encodeURIComponent(revision)}/${path}`;
Expand All @@ -197,7 +203,8 @@ export async function requestFile(
query.line = line;
}
const response: Response = await fetch(
npStart.core.http.basePath.prepend(Url.format({ pathname: url, query }))
npStart.core.http.basePath.prepend(Url.format({ pathname: url, query })),
{ signal }
);

if (response.status >= 200 && response.status < 300) {
Expand Down Expand Up @@ -244,9 +251,9 @@ export async function requestFile(
throw new Error('invalid file type');
}

function* handleFetchFile(action: Action<FetchFilePayload>) {
function* handleFetchFile(action: Action<FetchFilePayload>, signal: AbortSignal, task: any) {
try {
const results = yield call(requestFile, action.payload!);
const results = yield call(requestFile, action.payload!, undefined, signal);
if (results.isNotFound) {
yield put(setNotFound(true));
yield put(fetchFileFailed(new Error('file not found')));
Expand All @@ -258,6 +265,8 @@ function* handleFetchFile(action: Action<FetchFilePayload>) {
}
} catch (err) {
yield put(fetchFileFailed(err));
} finally {
yield cancel(task);
}
}

Expand All @@ -273,9 +282,9 @@ function* handleFetchDirs(action: Action<FetchRepoTreePayload>) {
export function* watchFetchBranchesAndCommits() {
yield takeEvery(String(fetchRepoBranches), handleFetchBranches);
yield takeEvery(String(fetchRepoCommits), handleFetchCommits);
yield takeLatest(String(fetchFile), handleFetchFile);
yield takeLatest(String(fetchFile), singletonRequestSaga(handleFetchFile));
yield takeEvery(String(fetchDirectory), handleFetchDirs);
yield takeLatest(String(fetchTreeCommits), handleFetchTreeCommits);
yield takeLatest(String(fetchTreeCommits), singletonRequestSaga(handleFetchTreeCommits));
yield takeLatest(String(fetchMoreCommits), handleFetchMoreCommits);
}

Expand Down
20 changes: 20 additions & 0 deletions x-pack/legacy/plugins/code/public/utils/saga_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { take, spawn } from 'redux-saga/effects';
import { Action } from 'redux-actions';

function* cancelRequest(action: Action<any>, abortController: AbortController) {
yield take(action.type);
abortController.abort();
}

export const singletonRequestSaga = (saga: any) =>
function*(action: Action<any>) {
const abortController = new AbortController();
const task = yield spawn(cancelRequest, action, abortController);
yield spawn(saga, action, abortController.signal, task);
};

0 comments on commit 6c28612

Please sign in to comment.