A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
core: standarize database updated event
Abdullah Attacec7c5f3b160
diff --git a/packages/core/src/api/sync/auto-sync.ts b/packages/core/src/api/sync/auto-sync.tsindex ddeac4b83..e4da19ef6 100644--- a/packages/core/src/api/sync/auto-sync.ts+++ b/packages/core/src/api/sync/auto-sync.ts@@ -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;diff --git a/packages/core/src/database/sql-collection.ts b/packages/core/src/database/sql-collection.tsindex 833084d40..3bbbf364f 100644--- a/packages/core/src/database/sql-collection.ts+++ b/packages/core/src/database/sql-collection.ts@@ -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"+ });}}diff --git a/packages/core/src/types.ts b/packages/core/src/types.tsindex 926e4113f..6d54185f6 100644--- a/packages/core/src/types.ts+++ b/packages/core/src/types.ts@@ -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>>;+};