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: refactor database update event logic
Abdullah Atta335021090d2c
diff --git a/packages/core/src/api/sync/auto-sync.ts b/packages/core/src/api/sync/auto-sync.tsindex e4da19ef6..e87783fd8 100644--- a/packages/core/src/api/sync/auto-sync.ts+++ b/packages/core/src/api/sync/auto-sync.ts@@ -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));diff --git a/packages/core/src/database/index.ts b/packages/core/src/database/index.tsindex 6dbff40dc..62df0ad48 100644--- a/packages/core/src/database/index.ts+++ b/packages/core/src/database/index.ts@@ -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;diff --git a/packages/core/src/database/sql-collection.ts b/packages/core/src/database/sql-collection.tsindex 3bbbf364f..f8aa257fd 100644--- a/packages/core/src/database/sql-collection.ts+++ b/packages/core/src/database/sql-collection.ts@@ -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});}}diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex f6ec7caea..351e6a417 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -31,3 +31,4 @@ export {highlightInternalLinks,type TextSlice} from "./utils/content-block";+export { type DatabaseUpdatedEvent } from "./database";diff --git a/packages/core/src/types.ts b/packages/core/src/types.tsindex 6d54185f6..9ab215cce 100644--- a/packages/core/src/types.ts+++ b/packages/core/src/types.ts@@ -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>>;-};