Skip to content

Commit

Permalink
Implemented base of EventEmitters
Browse files Browse the repository at this point in the history
  • Loading branch information
QSmally committed Aug 11, 2020
1 parent dfa2583 commit ef3ff81
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/Connections/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit ef3ff81

Please sign in to comment.