core: standarize database updated event

This commit is contained in:
Abdullah Atta
2024-03-13 11:01:50 +05:00
parent 47ae03ba8a
commit cec7c5f3b1
3 changed files with 55 additions and 13 deletions

View File

@@ -20,7 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import Database from "..";
import { EVENTS } from "../../common";
import { logger } from "../../logger";
import { Item } from "../../types";
import { DatabaseUpdatedEvent } from "../../types";
export class AutoSync {
timeout = 0;
@@ -54,13 +54,13 @@ export class AutoSync {
this.logger.info(`Auto sync stopped`);
}
private schedule(id: string | string[], item?: Item) {
private schedule(event: DatabaseUpdatedEvent) {
if (
item &&
(item.remote ||
("localOnly" in item && item.localOnly) ||
("failed" in item && item.failed) ||
("dateUploaded" in item && item.dateUploaded))
(event.type === "upsert" || event.type === "update") &&
(event.item.remote ||
("localOnly" in event.item && event.item.localOnly) ||
("failed" in event.item && event.item.failed) ||
("dateUploaded" in event.item && event.item.dateUploaded))
)
return;
@@ -71,10 +71,14 @@ export class AutoSync {
// be a few milliseconds less than Date.now(). Setting sync
// interval to 0 causes a conflict where Date.now() & dateModified
// are equal causing the item to not be synced.
const interval = item && item.type === "tiptap" ? 100 : this.interval;
const interval =
(event.type === "update" || event.type === "upsert") &&
event.collection === "content"
? 100
: this.interval;
this.timeout = setTimeout(() => {
this.logger.info(
`Sync requested by: ${Array.isArray(id) ? id.join(", ") : id}`
`Sync requested (type=${event.type} collection=${event.collection})`
);
this.db.eventManager.publish(EVENTS.databaseSyncRequested, false, false);
}, interval) as unknown as number;

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { EVENTS } from "../common";
import {
DatabaseUpdatedEvent,
GroupOptions,
Item,
MaybeDeletedItem,
@@ -101,7 +102,11 @@ export class SQLCollection<
.values(item)
.execute();
this.eventManager.publish(EVENTS.databaseUpdated, item.id, item);
this.eventManager.publish(EVENTS.databaseUpdated, <DatabaseUpdatedEvent>{
collection: this.type,
type: "upsert",
item
});
}
async softDelete(ids: string[]) {
@@ -116,7 +121,11 @@ export class SQLCollection<
}))
)
.execute();
this.eventManager.publish(EVENTS.databaseUpdated, ids);
this.eventManager.publish(EVENTS.databaseUpdated, <DatabaseUpdatedEvent>{
collection: this.type,
type: "softDelete",
ids
});
}
async delete(ids: string[]) {
@@ -125,7 +134,12 @@ export class SQLCollection<
.deleteFrom<keyof DatabaseSchema>(this.type)
.where("id", "in", ids)
.execute();
this.eventManager.publish(EVENTS.databaseUpdated, ids);
this.eventManager.publish(EVENTS.databaseUpdated, <DatabaseUpdatedEvent>{
collection: this.type,
type: "delete",
ids
});
}
async exists(id: string) {
@@ -208,7 +222,12 @@ export class SQLCollection<
})
.execute();
if (options.sendEvent) {
this.eventManager.publish(EVENTS.databaseUpdated, ids);
this.eventManager.publish(EVENTS.databaseUpdated, <DatabaseUpdatedEvent>{
collection: this.type,
ids,
item: partial,
type: "update"
});
}
}

View File

@@ -19,6 +19,7 @@ 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:
@@ -555,3 +556,21 @@ 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>>;
};