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

[Code] cancel source view page outdated request #43348

Merged
merged 1 commit into from
Aug 18, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
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();
Copy link
Contributor

Choose a reason for hiding this comment

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

Correct me if I'm wrong. Is this abortController created every time? Shouldn't we cancel the one last created when the request is sent?

Copy link
Contributor

Choose a reason for hiding this comment

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

My understanding here is the next action with the same type will trigger the abort() (line 12) of a previous created abortController. Then thus signal via line 19. So essentially this code block is just hooking up all the cancellation logic, which the actual cancellation is triggered until the next action with the same type is detected. It does take a moment to understand the code here. @WangQianliang if you reference any stackoverflow answer, please comment here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's created every time to detect if there's new action dispatched so that it will cancel this request. In my latest commit, it would cancel the abort request task if api request succeeds or fails.

const task = yield spawn(cancelRequest, action, abortController);
yield spawn(saga, action, abortController.signal, task);
};