Skip to content

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

core: implement event manager

thecodrr2deec9b6754f
diff --git a/packages/core/api/index.js b/packages/core/api/index.js
index 642d348cd..70ba616f3 100644
--- a/packages/core/api/index.js
+++ b/packages/core/api/index.js
@@ -9,6 +9,7 @@ import Vault from "./vault";
import Lookup from "./lookup";
import Content from "../collections/content";
import Conflicts from "./sync/conflicts";
+import EventManager from "../utils/event-manager";
class Database {
constructor(context) {
@@ -21,6 +22,7 @@ class Database {
this.vault = new Vault(this);
this.conflicts = new Conflicts(this);
this.lookup = new Lookup(this);
+ this.ev = new EventManager();
// collections
/** @type {Notes} */
diff --git a/packages/core/utils/event-manager.js b/packages/core/utils/event-manager.js
new file mode 100644
index 000000000..3dec63454
--- /dev/null
+++ b/packages/core/utils/event-manager.js
@@ -0,0 +1,27 @@
+class EventManager {
+ constructor() {
+ this._registry = {};
+ }
+
+ subscribe(name, handler) {
+ if (!name || !handler) throw new Error("name and handler are required.");
+ if (!this._registry[name]) this._registry.name = [];
+ this._registry[name].push(handler);
+ }
+
+ unsubscribe(name, handler) {
+ if (!this._registry[name]) return;
+ const index = this._registry[name].indexOf(handler);
+ if (index <= -1) return;
+ this._registry[name].splice(index, 1);
+ }
+
+ publish(name, ...args) {
+ if (!this._registry[name]) return;
+ const handlers = this._registry[name];
+ handlers.forEach((handler) => {
+ handler(...args);
+ });
+ }
+}
+export default EventManager;