mirror of
https://github.com/outline/outline.git
synced 2026-11-22 22:02:35 +03:00
* fix: Run forced document tree fetches that arrive while a fetch is in flight * fix: Run forced store fetches that arrive while a fetch is in flight * test
62 lines
2.1 KiB
TypeScript
62 lines
2.1 KiB
TypeScript
import { runInAction } from "mobx";
|
|
import { type JSONObject } from "@shared/types";
|
|
import { RevisionHelper } from "@shared/utils/RevisionHelper";
|
|
import type RootStore from "~/stores/RootStore";
|
|
import Store from "~/stores/base/Store";
|
|
import Revision from "~/models/Revision";
|
|
import { client } from "~/utils/ApiClient";
|
|
|
|
export default class RevisionsStore extends Store<Revision> {
|
|
constructor(rootStore: RootStore) {
|
|
super(rootStore, Revision);
|
|
}
|
|
|
|
/**
|
|
* Fetches a single revision by ID, including its full document content.
|
|
*
|
|
* @param id - The ID of the revision to fetch.
|
|
* @param options - Options to pass through to the underlying fetch.
|
|
* @returns A promise that resolves to the fetched revision.
|
|
*/
|
|
async fetch(id: string, options: JSONObject = {}): Promise<Revision> {
|
|
const item = this.get(id);
|
|
const force = Boolean(options.force) || (!!item && !item.data);
|
|
|
|
const documentId = RevisionHelper.documentIdFromLatestId(id);
|
|
if (documentId) {
|
|
return item && !force ? item : this.fetchLatest(documentId);
|
|
}
|
|
|
|
return super.fetch(id, { ...options, force });
|
|
}
|
|
|
|
/**
|
|
* Retrieves all revisions for a given document ID
|
|
*
|
|
* @param documentId - The ID of the document to retrieve revisions for
|
|
* @returns An array of revisions for the specified document ID
|
|
*/
|
|
getByDocumentId = (documentId: string): Revision[] =>
|
|
this.orderedData.filter((revision) => revision.documentId === documentId);
|
|
|
|
/**
|
|
* Fetches the latest revision for the given document.
|
|
*
|
|
* @param documentId - the id of the document to fetch the latest revision for.
|
|
* @returns A promise that resolves to the latest revision for the given document.
|
|
*/
|
|
fetchLatest = async (documentId: string): Promise<Revision> => {
|
|
const id = RevisionHelper.latestId(documentId);
|
|
return this.fetches.run(id, () => this.requestLatest(documentId));
|
|
};
|
|
|
|
private async requestLatest(documentId: string): Promise<Revision> {
|
|
const res = await client.post(`/revisions.info`, { documentId });
|
|
|
|
return runInAction(() => {
|
|
this.addPolicies(res.policies);
|
|
return this.add(res.data);
|
|
});
|
|
}
|
|
}
|