Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

refactor: change all event strings to constants

thecodrr4e262e615e0b
diff --git a/packages/core/api/index.js b/packages/core/api/index.js
index 004149aad..83d6d22c5 100644
--- a/packages/core/api/index.js
+++ b/packages/core/api/index.js
@@ -11,7 +11,7 @@ import Backup from "../database/backup";
import Conflicts from "./sync/conflicts";
import Session from "./session";
import Constants from "../utils/constants";
-import { EV } from "../common";
+import { EV, EVENTS } from "../common";
import Settings from "./settings";
import Migrations from "./migrations";
import Outbox from "./outbox";
@@ -119,7 +119,7 @@ class Database {
const user = await this.user.getUser();
user.subscription = data;
await this.user.setUser(user);
- EV.publish("user:upgraded", data);
+ EV.publish(EVENTS.userSubscriptionUpdated, data);
break;
case "userDeleted":
await this.user.logout(false, "Account Deleted");
@@ -131,7 +131,7 @@ class Database {
const token = await this.context.read("token");
await this.user.tokenManager._refreshToken(token);
await this.user.fetchUser(true);
- EV.publish("user:emailConfirmed");
+ EV.publish(EVENTS.userEmailConfirmed);
break;
case "sync":
await this.syncer.eventMerge(data);
@@ -150,7 +150,7 @@ class Database {
}
clearTimeout(this._syncTimeout);
this._syncTimeout = setTimeout(() => {
- EV.publish("db:sync");
+ EV.publish(EVENTS.databaseSyncRequested);
}, 15 * 1000);
}
diff --git a/packages/core/api/migrations.js b/packages/core/api/migrations.js
index 687d5144f..9715d425d 100644
--- a/packages/core/api/migrations.js
+++ b/packages/core/api/migrations.js
@@ -1,4 +1,4 @@
-import { CURRENT_DATABASE_VERSION, EV } from "../common";
+import { CURRENT_DATABASE_VERSION, EV, EVENTS } from "../common";
import Migrator from "../database/migrator";
class Migrations {
@@ -54,7 +54,7 @@ class Migrations {
await this._migrator.migrate(collections, (item) => item, this.dbVersion);
await this._db.context.write("v", CURRENT_DATABASE_VERSION);
- EV.publish("db:onMigrationDone", {
+ EV.publish(EVENTS.databaseMigrated, {
prev: this.dbVersion,
current: CURRENT_DATABASE_VERSION,
});
diff --git a/packages/core/api/sync/index.js b/packages/core/api/sync/index.js
index 1a02dcb73..49624b52a 100644
--- a/packages/core/api/sync/index.js
+++ b/packages/core/api/sync/index.js
@@ -25,7 +25,7 @@
* Syncing should pause until all the conflicts have been resolved
* And then it should continue.
*/
-import { EV } from "../../common";
+import { EV, EVENTS } from "../../common";
import Constants from "../../utils/constants";
import http from "../../utils/http";
import TokenManager from "../token-manager";
@@ -98,7 +98,7 @@ export default class Sync {
// merge the server response
await this._merger.merge(serverResponse, lastSynced);
- EV.publish("db:refresh");
+ EV.publish(EVENTS.appRefreshRequested);
// check for conflicts and throw
// await this._db.conflicts.check();
diff --git a/packages/core/api/user-manager.js b/packages/core/api/user-manager.js
index e93ffefff..754ab52bb 100644
--- a/packages/core/api/user-manager.js
+++ b/packages/core/api/user-manager.js
@@ -1,7 +1,7 @@
import http from "../utils/http";
import constants from "../utils/constants";
import TokenManager from "./token-manager";
-import { EV, NOTESNOOK_CORE_VERSION } from "../common";
+import { EV, EVENTS, NOTESNOOK_CORE_VERSION } from "../common";
const ENDPOINTS = {
signup: "/users",
@@ -86,7 +86,7 @@ class UserManager {
salt: user.salt,
});
- EV.publish("user:loggedIn", user);
+ EV.publish(EVENTS.userLoggedIn, user);
}
async getSessions() {
@@ -102,7 +102,7 @@ class UserManager {
console.error(e);
} finally {
await this._db.context.clear();
- EV.publish("user:loggedOut", reason);
+ EV.publish(EVENTS.userLoggedOut, reason);
}
}
@@ -145,7 +145,7 @@ class UserManager {
}
await this.setUser({ ...user, remember });
- EV.publish("user:fetched", user);
+ EV.publish(EVENTS.userFetched, user);
return user;
} else {
return await this.getUser();
diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.js
index 994ddf2d4..7cbef57c2 100644
--- a/packages/core/collections/notes.js
+++ b/packages/core/collections/notes.js
@@ -8,7 +8,7 @@ import {
} from "../utils/date";
import Note from "../models/note";
import getId from "../utils/id";
-import { EV } from "../common";
+import { EV, EVENTS } from "../common";
import { getContentFromData } from "../content-types";
import qclone from "qclone/src/qclone";
var tfun = require("transfun/transfun.js").tfun;
@@ -65,7 +65,7 @@ export default class Notes extends Collection {
if (isNoteEmpty(note, content)) {
if (oldNote) {
- EV.publish("notes:removeEmptyNote", id);
+ EV.publish(EVENTS.noteRemoved, id);
await this.remove(id);
}
return;
diff --git a/packages/core/common.js b/packages/core/common.js
index 25ae132fe..8ff452358 100644
--- a/packages/core/common.js
+++ b/packages/core/common.js
@@ -3,7 +3,7 @@ import EventManager from "./utils/event-manager";
export const EV = new EventManager();
export async function sendCheckUserStatusEvent(type) {
- const results = await EV.publishWithResult("user:checkStatus", type);
+ const results = await EV.publishWithResult(EVENTS.userCheckStatus, type);
if (typeof results === "boolean") return results;
return results.some((r) => r.type === type && r.result === true);
}
@@ -17,4 +17,18 @@ export const CHECK_IDS = {
backupEncrypt: "backup:encrypt",
};
+export const EVENTS = {
+ userCheckStatus: "user:checkStatus",
+ userSubscriptionUpdated: "user:subscriptionUpdated",
+ userEmailConfirmed: "user:emailConfirmed",
+ userLoggedIn: "user:loggedIn",
+ userLoggedOut: "user:loggedOut",
+ userFetched: "user:fetched",
+ databaseSyncRequested: "db:syncRequested",
+ databaseMigrated: "db:migrated",
+ databaseUpdated: "db:updated",
+ appRefreshRequested: "app:refreshRequested",
+ noteRemoved: "note:removed",
+};
+
export const CURRENT_DATABASE_VERSION = 4.3;
diff --git a/packages/core/database/cached-collection.js b/packages/core/database/cached-collection.js
index 48fb91e40..17d90cd7c 100644
--- a/packages/core/database/cached-collection.js
+++ b/packages/core/database/cached-collection.js
@@ -1,6 +1,6 @@
import Indexer from "./indexer";
import sort from "fast-sort";
-import { EV } from "../common";
+import { EV, EVENTS } from "../common";
import IndexedCollection from "./indexed-collection";
export default class CachedCollection extends IndexedCollection {
@@ -21,7 +21,7 @@ export default class CachedCollection extends IndexedCollection {
}
async updateItem(item) {
- EV.publish("db:write", item);
+ EV.publish(EVENTS.databaseUpdated, item);
await super.updateItem(item);
this.map.set(item.id, item);
}
diff --git a/packages/core/package.json b/packages/core/package.json
index 1d278067e..3c9f104ff 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -1,6 +1,6 @@
{
"name": "notes-core",
- "version": "5.4.1",
+ "version": "5.5.0",
"private": true,
"main": "./api/index.js",
"devDependencies": {