mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 23:19:40 +01:00
core: refactor database update event logic
This commit is contained in:
@@ -36,9 +36,10 @@ export class AutoSync {
|
|||||||
async start() {
|
async start() {
|
||||||
this.logger.info(`Auto sync requested`);
|
this.logger.info(`Auto sync requested`);
|
||||||
if (this.isAutoSyncing) return;
|
if (this.isAutoSyncing) return;
|
||||||
|
if (this.databaseUpdatedEvent) this.databaseUpdatedEvent.unsubscribe();
|
||||||
|
|
||||||
this.isAutoSyncing = true;
|
this.isAutoSyncing = true;
|
||||||
this.databaseUpdatedEvent = this.db.eventManager.subscribeSingle(
|
this.databaseUpdatedEvent = this.db.eventManager.subscribe(
|
||||||
EVENTS.databaseUpdated,
|
EVENTS.databaseUpdated,
|
||||||
this.schedule.bind(this)
|
this.schedule.bind(this)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -41,6 +41,8 @@ import {
|
|||||||
Color,
|
Color,
|
||||||
ContentItem,
|
ContentItem,
|
||||||
HistorySession,
|
HistorySession,
|
||||||
|
ItemReference,
|
||||||
|
ItemReferences,
|
||||||
ItemType,
|
ItemType,
|
||||||
MaybeDeletedItem,
|
MaybeDeletedItem,
|
||||||
Note,
|
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
|
type AsyncOrSyncResult<Async extends boolean, Response> = Async extends true
|
||||||
? Promise<Response>
|
? Promise<Response>
|
||||||
: Response;
|
: Response;
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
|
|
||||||
import { EVENTS } from "../common";
|
import { EVENTS } from "../common";
|
||||||
import {
|
import {
|
||||||
DatabaseUpdatedEvent,
|
|
||||||
GroupOptions,
|
GroupOptions,
|
||||||
Item,
|
Item,
|
||||||
MaybeDeletedItem,
|
MaybeDeletedItem,
|
||||||
@@ -31,7 +30,10 @@ import {
|
|||||||
DatabaseAccessor,
|
DatabaseAccessor,
|
||||||
DatabaseCollection,
|
DatabaseCollection,
|
||||||
DatabaseSchema,
|
DatabaseSchema,
|
||||||
|
DeleteEvent,
|
||||||
SQLiteItem,
|
SQLiteItem,
|
||||||
|
UpdateEvent,
|
||||||
|
UpsertEvent,
|
||||||
isFalse
|
isFalse
|
||||||
} from ".";
|
} from ".";
|
||||||
import {
|
import {
|
||||||
@@ -102,9 +104,9 @@ export class SQLCollection<
|
|||||||
.values(item)
|
.values(item)
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
this.eventManager.publish(EVENTS.databaseUpdated, <DatabaseUpdatedEvent>{
|
this.eventManager.publish(EVENTS.databaseUpdated, <UpsertEvent>{
|
||||||
collection: this.type,
|
|
||||||
type: "upsert",
|
type: "upsert",
|
||||||
|
collection: this.type,
|
||||||
item
|
item
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -121,9 +123,10 @@ export class SQLCollection<
|
|||||||
}))
|
}))
|
||||||
)
|
)
|
||||||
.execute();
|
.execute();
|
||||||
this.eventManager.publish(EVENTS.databaseUpdated, <DatabaseUpdatedEvent>{
|
|
||||||
collection: this.type,
|
this.eventManager.publish(EVENTS.databaseUpdated, <DeleteEvent>{
|
||||||
type: "softDelete",
|
type: "softDelete",
|
||||||
|
collection: this.type,
|
||||||
ids
|
ids
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -135,9 +138,9 @@ export class SQLCollection<
|
|||||||
.where("id", "in", ids)
|
.where("id", "in", ids)
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
this.eventManager.publish(EVENTS.databaseUpdated, <DatabaseUpdatedEvent>{
|
this.eventManager.publish(EVENTS.databaseUpdated, <DeleteEvent>{
|
||||||
collection: this.type,
|
|
||||||
type: "delete",
|
type: "delete",
|
||||||
|
collection: this.type,
|
||||||
ids
|
ids
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -222,11 +225,11 @@ export class SQLCollection<
|
|||||||
})
|
})
|
||||||
.execute();
|
.execute();
|
||||||
if (options.sendEvent) {
|
if (options.sendEvent) {
|
||||||
this.eventManager.publish(EVENTS.databaseUpdated, <DatabaseUpdatedEvent>{
|
this.eventManager.publish(EVENTS.databaseUpdated, <UpdateEvent>{
|
||||||
|
type: "update",
|
||||||
collection: this.type,
|
collection: this.type,
|
||||||
ids,
|
ids,
|
||||||
item: partial,
|
item: partial
|
||||||
type: "update"
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,3 +31,4 @@ export {
|
|||||||
highlightInternalLinks,
|
highlightInternalLinks,
|
||||||
type TextSlice
|
type TextSlice
|
||||||
} from "./utils/content-block";
|
} from "./utils/content-block";
|
||||||
|
export { type DatabaseUpdatedEvent } from "./database";
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
|
|
||||||
import { Cipher } from "@notesnook/crypto";
|
import { Cipher } from "@notesnook/crypto";
|
||||||
import { TimeFormat } from "./utils/date";
|
import { TimeFormat } from "./utils/date";
|
||||||
import { SQLiteItem } from "./database";
|
|
||||||
|
|
||||||
export type SortOptions = {
|
export type SortOptions = {
|
||||||
sortBy:
|
sortBy:
|
||||||
@@ -304,6 +303,8 @@ export type ItemReference = {
|
|||||||
type: keyof ItemMap;
|
type: keyof ItemMap;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type ItemReferences = { type: ItemType; ids: string[] };
|
||||||
|
|
||||||
export interface Relation extends BaseItem<"relation"> {
|
export interface Relation extends BaseItem<"relation"> {
|
||||||
fromId: string;
|
fromId: string;
|
||||||
fromType: keyof ItemMap;
|
fromType: keyof ItemMap;
|
||||||
@@ -556,21 +557,3 @@ export type ContentBlock = {
|
|||||||
type: string;
|
type: string;
|
||||||
id: 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>>;
|
|
||||||
};
|
|
||||||
|
|||||||
Reference in New Issue
Block a user