mirror of
https://github.com/outline/outline.git
synced 2026-12-01 01:14:11 +03:00
* feat: Persist policies to IndexedDB so abilities survive reload Adds a StorePersistence layer that caches a store's models in IndexedDB and hydrates them back at boot as stale data, revalidated by normal API traffic. Enabled for the policies store, scoped per team, so cached abilities are available immediately after a reload instead of every policy needing a forced refetch. - Persistence hooks in the base Store add/remove/clear methods, since add() updates existing models in place and map observation would miss those changes - Enabled from RootStore construction (team read from localStorage) and again after auth.info succeeds to cover the first session after login - Store.clear() wipes the persisted records, covering both logout paths - deleteAllDatabases() fallback removes the policies database on browsers without indexedDB.databases() Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XisV6sRdexfnPSiehurKoV * refactor: Opt stores into persistence via a persistable property Stores now declare persistence with `persistable = true` and RootStore sweeps all persistable stores, deriving each database name from the store's apiEndpoint, instead of RootStore hard-coding the policies store. The deleteAllDatabases fallback uses the same sweep. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XisV6sRdexfnPSiehurKoV * refactor: Simplify store persistence enablement and clearing - Move the persistable guard into Store.enablePersistence so RootStore can call it unconditionally on every store - StorePersistence derives its own database name from the store and teamId rather than receiving a prebuilt name - StorePersistence.clear returns a promise, making the test deterministic instead of sleeping Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XisV6sRdexfnPSiehurKoV * fix: Avoid re-persisting hydrated records, await clear transaction Hydration called store.add() for every cached record, which now triggers persist() and scheduled a flush that wrote the just-loaded data straight back to IndexedDB at boot. Guard persist() with a hydrating flag. clear() awaited the objectStore.clear() request rather than the enclosing transaction, so it could resolve before commit and miss abort errors. * lock * iterate --------- Co-authored-by: Claude <noreply@anthropic.com>
200 lines
6.6 KiB
TypeScript
200 lines
6.6 KiB
TypeScript
import invariant from "invariant";
|
|
import { lowerFirst } from "es-toolkit/compat";
|
|
import pluralize from "pluralize";
|
|
import ApiKeysStore from "./ApiKeysStore";
|
|
import AuthStore from "./AuthStore";
|
|
import AuthenticationProvidersStore from "./AuthenticationProvidersStore";
|
|
import CollectionsStore from "./CollectionsStore";
|
|
import CommentsStore from "./CommentsStore";
|
|
import DialogsStore from "./DialogsStore";
|
|
import DocumentPresenceStore from "./DocumentPresenceStore";
|
|
import DocumentsStore from "./DocumentsStore";
|
|
import EventsStore from "./EventsStore";
|
|
import EmojisStore from "./EmojiStore";
|
|
import FileOperationsStore from "./FileOperationsStore";
|
|
import GroupMembershipsStore from "./GroupMembershipsStore";
|
|
import GroupUsersStore from "./GroupUsersStore";
|
|
import GroupsStore from "./GroupsStore";
|
|
import ImportsStore from "./ImportsStore";
|
|
import IntegrationsStore from "./IntegrationsStore";
|
|
import MembershipsStore from "./MembershipsStore";
|
|
import NotificationsStore from "./NotificationsStore";
|
|
import OAuthAuthenticationsStore from "./OAuthAuthenticationsStore";
|
|
import OAuthClientsStore from "./OAuthClientsStore";
|
|
import PinsStore from "./PinsStore";
|
|
import PoliciesStore from "./PoliciesStore";
|
|
import RevisionsStore from "./RevisionsStore";
|
|
import SearchesStore from "./SearchesStore";
|
|
import SharesStore from "./SharesStore";
|
|
import StarsStore from "./StarsStore";
|
|
import SubscriptionsStore from "./SubscriptionsStore";
|
|
import TemplatesStore from "./TemplatesStore";
|
|
import UiStore from "./UiStore";
|
|
import UnfurlsStore from "./UnfurlsStore";
|
|
import UserMembershipsStore from "./UserMembershipsStore";
|
|
import UsersStore from "./UsersStore";
|
|
import ViewsStore from "./ViewsStore";
|
|
import WebhookSubscriptionsStore from "./WebhookSubscriptionStore";
|
|
import Store from "./base/Store";
|
|
|
|
export default class RootStore {
|
|
apiKeys: ApiKeysStore;
|
|
auth: AuthStore;
|
|
authenticationProviders: AuthenticationProvidersStore;
|
|
collections: CollectionsStore;
|
|
groupMemberships: GroupMembershipsStore;
|
|
comments: CommentsStore;
|
|
dialogs: DialogsStore;
|
|
documents: DocumentsStore;
|
|
emojis: EmojisStore;
|
|
events: EventsStore;
|
|
groups: GroupsStore;
|
|
groupUsers: GroupUsersStore;
|
|
imports: ImportsStore;
|
|
integrations: IntegrationsStore;
|
|
memberships: MembershipsStore;
|
|
notifications: NotificationsStore;
|
|
oauthAuthentications: OAuthAuthenticationsStore;
|
|
oauthClients: OAuthClientsStore;
|
|
presence: DocumentPresenceStore;
|
|
pins: PinsStore;
|
|
policies: PoliciesStore;
|
|
revisions: RevisionsStore;
|
|
searches: SearchesStore;
|
|
shares: SharesStore;
|
|
ui: UiStore;
|
|
unfurls: UnfurlsStore;
|
|
stars: StarsStore;
|
|
subscriptions: SubscriptionsStore;
|
|
templates: TemplatesStore;
|
|
users: UsersStore;
|
|
views: ViewsStore;
|
|
fileOperations: FileOperationsStore;
|
|
webhookSubscriptions: WebhookSubscriptionsStore;
|
|
userMemberships: UserMembershipsStore;
|
|
|
|
constructor() {
|
|
// Models
|
|
this.registerStore(ApiKeysStore);
|
|
this.registerStore(AuthenticationProvidersStore);
|
|
this.registerStore(CollectionsStore);
|
|
this.registerStore(GroupMembershipsStore);
|
|
this.registerStore(CommentsStore);
|
|
this.registerStore(DocumentsStore);
|
|
this.registerStore(EmojisStore);
|
|
this.registerStore(EventsStore);
|
|
this.registerStore(GroupsStore);
|
|
this.registerStore(GroupUsersStore);
|
|
this.registerStore(ImportsStore);
|
|
this.registerStore(IntegrationsStore);
|
|
this.registerStore(MembershipsStore);
|
|
this.registerStore(NotificationsStore);
|
|
this.registerStore(OAuthAuthenticationsStore, "oauthAuthentications");
|
|
this.registerStore(OAuthClientsStore, "oauthClients");
|
|
this.registerStore(PinsStore);
|
|
this.registerStore(PoliciesStore);
|
|
this.registerStore(RevisionsStore);
|
|
this.registerStore(SearchesStore);
|
|
this.registerStore(SharesStore);
|
|
this.registerStore(StarsStore);
|
|
this.registerStore(SubscriptionsStore);
|
|
this.registerStore(TemplatesStore);
|
|
this.registerStore(UnfurlsStore);
|
|
this.registerStore(UsersStore);
|
|
this.registerStore(ViewsStore);
|
|
this.registerStore(FileOperationsStore);
|
|
this.registerStore(WebhookSubscriptionsStore);
|
|
this.registerStore(UserMembershipsStore);
|
|
|
|
// Non-models
|
|
this.registerStore(DocumentPresenceStore, "presence");
|
|
this.registerStore(DialogsStore, "dialogs");
|
|
this.registerStore(UiStore, "ui");
|
|
|
|
// AuthStore must be initialized last as it makes use of the other stores.
|
|
this.registerStore(AuthStore, "auth");
|
|
|
|
// AuthStore will have synchronously rehydrated the current team from
|
|
// localStorage at this point, if a session exists.
|
|
this.enablePersistence();
|
|
}
|
|
|
|
/**
|
|
* Enable IndexedDB persistence for all stores marked as persistable, scoped
|
|
* to the current team. A no-op when there is no authenticated team or
|
|
* persistence is already enabled.
|
|
*/
|
|
public enablePersistence() {
|
|
const teamId = this.auth.currentTeamId;
|
|
if (!teamId) {
|
|
return;
|
|
}
|
|
|
|
Object.values(this).forEach((store) => {
|
|
if (store instanceof Store) {
|
|
void store.enablePersistence(teamId);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get a store by model name.
|
|
*
|
|
* @param modelName
|
|
*/
|
|
public getStoreForModelName<K extends keyof RootStore>(modelName: string) {
|
|
const storeName = this.getStoreNameForModelName(modelName);
|
|
invariant(storeName, `No store found for model name "${modelName}"`);
|
|
|
|
const store = this[storeName];
|
|
return store as RootStore[K];
|
|
}
|
|
|
|
/**
|
|
* Clear all data from the stores except for auth and ui.
|
|
*/
|
|
public clear() {
|
|
Object.getOwnPropertyNames(this)
|
|
.filter((key) => ["auth", "ui"].includes(key) === false)
|
|
.forEach((key: keyof RootStore) => {
|
|
if ("clear" in this[key]) {
|
|
// @ts-expect-error clear exists on all stores
|
|
this[key].clear();
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Register a store with the root store.
|
|
*
|
|
* @param StoreClass
|
|
*/
|
|
private registerStore<T = typeof Store>(
|
|
StoreClass: T,
|
|
name?: keyof RootStore
|
|
) {
|
|
// @ts-expect-error TS thinks we are instantiating an abstract class.
|
|
const store = new StoreClass(this);
|
|
const storeName = name ?? this.getStoreNameForModelName(store.modelName);
|
|
invariant(storeName, `No store found for model name "${store.modelName}"`);
|
|
|
|
this[storeName] = store;
|
|
}
|
|
|
|
private getStoreNameForModelName(modelName: string) {
|
|
for (const key of Object.keys(this)) {
|
|
const store = this[key as keyof RootStore];
|
|
if (store && "modelName" in store && store.modelName === modelName) {
|
|
return key as keyof RootStore;
|
|
}
|
|
}
|
|
|
|
const storeName = pluralize(lowerFirst(modelName)) as keyof RootStore;
|
|
if (storeName) {
|
|
return storeName;
|
|
}
|
|
|
|
return undefined;
|
|
}
|
|
}
|