core: refactor database update event logic

This commit is contained in:
Abdullah Atta
2024-03-14 09:58:36 +05:00
parent b001107135
commit 335021090d
5 changed files with 63 additions and 30 deletions

View File

@@ -36,9 +36,10 @@ export class AutoSync {
async start() {
this.logger.info(`Auto sync requested`);
if (this.isAutoSyncing) return;
if (this.databaseUpdatedEvent) this.databaseUpdatedEvent.unsubscribe();
this.isAutoSyncing = true;
this.databaseUpdatedEvent = this.db.eventManager.subscribeSingle(
this.databaseUpdatedEvent = this.db.eventManager.subscribe(
EVENTS.databaseUpdated,
this.schedule.bind(this)
);

View File

@@ -41,6 +41,8 @@ import {
Color,
ContentItem,
HistorySession,
ItemReference,
ItemReferences,
ItemType,
MaybeDeletedItem,
Note,
@@ -109,6 +111,49 @@ export type RawDatabaseSchema = DatabaseSchema & {
}>;
};
export type DatabaseUpdatedEvent<
TCollectionType extends keyof DatabaseSchema = keyof DatabaseSchema
> =
| UpsertEvent<TCollectionType>
| DeleteEvent
| UpdateEvent<TCollectionType>
| UnlinkEvent;
export type UpsertEvent<
TCollectionType extends keyof DatabaseSchema = keyof DatabaseSchema
> = TCollectionType extends keyof DatabaseSchema
? {
type: "upsert";
collection: TCollectionType;
item: DatabaseSchema[TCollectionType];
}
: never;
export type UnlinkEvent = {
collection: "relations";
type: "unlink";
reference: ItemReference | ItemReferences;
types: ItemType[];
direction: "from" | "to";
};
export type DeleteEvent = {
collection: keyof DatabaseSchema;
type: "softDelete" | "delete";
ids: string[];
};
export type UpdateEvent<
TCollectionType extends keyof DatabaseSchema = keyof DatabaseSchema
> = TCollectionType extends keyof DatabaseSchema
? {
type: "update";
ids: string[];
collection: TCollectionType;
item: Partial<DatabaseSchema[TCollectionType]>;
}
: never;
type AsyncOrSyncResult<Async extends boolean, Response> = Async extends true
? Promise<Response>
: Response;

View File

@@ -19,7 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { EVENTS } from "../common";
import {
DatabaseUpdatedEvent,
GroupOptions,
Item,
MaybeDeletedItem,
@@ -31,7 +30,10 @@ import {
DatabaseAccessor,
DatabaseCollection,
DatabaseSchema,
DeleteEvent,
SQLiteItem,
UpdateEvent,
UpsertEvent,
isFalse
} from ".";
import {
@@ -102,9 +104,9 @@ export class SQLCollection<
.values(item)
.execute();
this.eventManager.publish(EVENTS.databaseUpdated, <DatabaseUpdatedEvent>{
collection: this.type,
this.eventManager.publish(EVENTS.databaseUpdated, <UpsertEvent>{
type: "upsert",
collection: this.type,
item
});
}
@@ -121,9 +123,10 @@ export class SQLCollection<
}))
)
.execute();
this.eventManager.publish(EVENTS.databaseUpdated, <DatabaseUpdatedEvent>{
collection: this.type,
this.eventManager.publish(EVENTS.databaseUpdated, <DeleteEvent>{
type: "softDelete",
collection: this.type,
ids
});
}
@@ -135,9 +138,9 @@ export class SQLCollection<
.where("id", "in", ids)
.execute();
this.eventManager.publish(EVENTS.databaseUpdated, <DatabaseUpdatedEvent>{
collection: this.type,
this.eventManager.publish(EVENTS.databaseUpdated, <DeleteEvent>{
type: "delete",
collection: this.type,
ids
});
}
@@ -222,11 +225,11 @@ export class SQLCollection<
})
.execute();
if (options.sendEvent) {
this.eventManager.publish(EVENTS.databaseUpdated, <DatabaseUpdatedEvent>{
this.eventManager.publish(EVENTS.databaseUpdated, <UpdateEvent>{
type: "update",
collection: this.type,
ids,
item: partial,
type: "update"
item: partial
});
}
}

View File

@@ -31,3 +31,4 @@ export {
highlightInternalLinks,
type TextSlice
} from "./utils/content-block";
export { type DatabaseUpdatedEvent } from "./database";

View File

@@ -19,7 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { Cipher } from "@notesnook/crypto";
import { TimeFormat } from "./utils/date";
import { SQLiteItem } from "./database";
export type SortOptions = {
sortBy:
@@ -304,6 +303,8 @@ export type ItemReference = {
type: keyof ItemMap;
};
export type ItemReferences = { type: ItemType; ids: string[] };
export interface Relation extends BaseItem<"relation"> {
fromId: string;
fromType: keyof ItemMap;
@@ -556,21 +557,3 @@ export type ContentBlock = {
type: string;
id: string;
};
export type DatabaseUpdatedEvent = UpsertEvent | DeleteEvent | UpdateEvent;
type BaseEvent = {
collection: CollectionType;
};
type UpsertEvent = BaseEvent & {
type: "upsert";
item: SQLiteItem<Item>;
};
type DeleteEvent = BaseEvent & {
type: "softDelete" | "delete";
ids: string[];
};
type UpdateEvent = BaseEvent & {
type: "update";
ids: string[];
item: Partial<SQLiteItem<Item>>;
};