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

[Files] Dispose of subscriptions on unmount #141597

Merged
merged 3 commits into from
Sep 26, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ export const UploadFile = <Kind extends string = string>({
return () => subs.forEach((sub) => sub.unsubscribe());
}, [uploadState, onDone, onError]);

useEffect(() => uploadState.dispose, [uploadState]);

return (
<context.Provider value={uploadState}>
<Component
Expand Down
60 changes: 34 additions & 26 deletions x-pack/plugins/files/public/components/upload_file/upload_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
type Observable,
combineLatest,
distinctUntilChanged,
Subscription,
} from 'rxjs';
import type { FileKind, FileJSON } from '../../../common/types';
import type { FilesClient } from '../../types';
Expand Down Expand Up @@ -62,38 +63,41 @@ export class UploadState {
public readonly uploading$ = new BehaviorSubject(false);
public readonly done$ = new Subject<undefined | DoneNotification[]>();

private subscriptions: Subscription[];

constructor(
private readonly fileKind: FileKind,
private readonly client: FilesClient,
private readonly opts: UploadOptions = { allowRepeatedUploads: false }
) {
const latestFiles$ = this.files$$.pipe(switchMap((files$) => combineLatest(files$)));

latestFiles$
.pipe(
map((files) => files.some((file) => file.status === 'uploading')),
distinctUntilChanged()
)
.subscribe(this.uploading$);

latestFiles$
.pipe(
map((files) => {
const errorFile = files.find((file) => Boolean(file.error));
return errorFile ? errorFile.error : undefined;
}),
filter(Boolean)
)
.subscribe(this.error$);

latestFiles$
.pipe(
filter(
(files) => Boolean(files.length) && files.every((file) => file.status === 'uploaded')
),
map((files) => files.map((file) => ({ id: file.id!, kind: this.fileKind.id })))
)
.subscribe(this.done$);
this.subscriptions = [
latestFiles$
.pipe(
map((files) => files.some((file) => file.status === 'uploading')),
distinctUntilChanged()
)
.subscribe(this.uploading$),

latestFiles$
.pipe(
map((files) => {
const errorFile = files.find((file) => Boolean(file.error));
return errorFile ? errorFile.error : undefined;
}),
filter(Boolean)
)
.subscribe(this.error$),

latestFiles$
.pipe(
filter(
(files) => Boolean(files.length) && files.every((file) => file.status === 'uploaded')
),
map((files) => files.map((file) => ({ id: file.id!, kind: this.fileKind.id })))
)
.subscribe(this.done$),
];
}

public isUploading(): boolean {
Expand Down Expand Up @@ -226,6 +230,10 @@ export class UploadState {

return upload$;
};

public dispose = (): void => {
for (const sub of this.subscriptions) sub.unsubscribe();
};
}

export const createUploadState = ({
Expand Down