diff --git a/lib/Connections/Connection.js b/lib/Connections/Connection.js index 07442af..f0654b9 100644 --- a/lib/Connections/Connection.js +++ b/lib/Connections/Connection.js @@ -174,6 +174,21 @@ class Connection extends PartialConnection { .all().map(Row => Row.Key); } + /** + * + * @param {String} Event String to represent which event you're applying to the function. + * @param {Function} Fn Function to execute when this event triggers. + * @returns {Boolean} Whether this event was registered. + */ + On (Event, Fn) { + if (typeof Fn !== "function") return false; + if (typeof Event !== "string") return false; + if (!["Set", "Fetch", "Erase"].includes(Event)) return false; + + this._EventHandler.on(Event, Fn); + return true; + } + /** * Disconnects from this Connection, clears in-memory rows. * Only run this method when you are exiting the program, @@ -327,6 +342,8 @@ class Connection extends PartialConnection { this.API.prepare(`INSERT OR REPLACE INTO '${this.Table}' ('Key', 'Val') VALUES (?, ?);`) .run(Key, JSON.stringify(Value)); + this._EventEmmitter.emit("Set", {KeyOrPath, Key, Path}, Value); + return this; } @@ -353,6 +370,8 @@ class Connection extends PartialConnection { if (typeof Path !== "undefined") Fecthed = this._CastPath(Fetched, Path); if (typeof Fetched === "object") delete Fetched._Timestamp; + this._EventEmmitter.emit("Fetch", {KeyOrPath, Key, Path}, Fetched, Cache); + return Fetched; } @@ -380,6 +399,7 @@ class Connection extends PartialConnection { if (this._Ready && Keys.length) { for (const Key of Keys) this.API.prepare(`DELETE FROM '${this.Table}' WHERE Key = ?;`).run(Key); + this._EventEmmitter.emit("Set", Keys); this.Evict(...Keys); }