@trap_stevo/ventry
Version:
The universal engine for creating, tracking, and evolving interactive content — from posts, comments, and likes to offers, auctions, events, and beyond. Define, extend, and analyze content objects in real time. Turn anything into user-driven content.
139 lines (138 loc) • 5.06 kB
JavaScript
"use strict";
function _classPrivateMethodInitSpec(e, a) { _checkPrivateRedeclaration(e, a), a.add(e); }
function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); }
function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }
function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); }
function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; }
function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
const {
EventEmitter
} = require("events");
const {
now
} = require("../HUDComponents/Time.cjs");
var _prefix = /*#__PURE__*/new WeakMap();
var _sched = /*#__PURE__*/new WeakMap();
var _config = /*#__PURE__*/new WeakMap();
var _bus = /*#__PURE__*/new WeakMap();
var _EventsManager_brand = /*#__PURE__*/new WeakSet();
class EventsManager {
constructor(vaultManager, options = {}) {
_classPrivateMethodInitSpec(this, _EventsManager_brand);
_classPrivateFieldInitSpec(this, _prefix, "ventry");
_classPrivateFieldInitSpec(this, _sched, null);
_classPrivateFieldInitSpec(this, _config, {});
_classPrivateFieldInitSpec(this, _bus, null);
this.vault = vaultManager;
_classPrivateFieldSet(_prefix, this, options.eventPrefix || "ventry");
_classPrivateFieldSet(_bus, this, new EventEmitter());
_classPrivateFieldSet(_sched, this, null);
_classPrivateFieldSet(_config, this, {
expiringSoonBeforeMs: 24 * 60 * 60 * 1000,
lookaheadMs: 30_000,
intervalMs: 30_000,
actionInfo: options.actionInfo || null,
...(options ?? {})
});
}
setEventPrefix(prefix = "ventry") {
_classPrivateFieldSet(_prefix, this, prefix || "ventry");
}
configureScheduler(config = {}) {
Object.assign(_classPrivateFieldGet(_config, this), config);
}
on(event, fn) {
_classPrivateFieldGet(_bus, this).on(_assertClassBrand(_EventsManager_brand, this, _name).call(this, event), fn);
return () => this.off(event, fn);
}
once(event, fn) {
_classPrivateFieldGet(_bus, this).once(_assertClassBrand(_EventsManager_brand, this, _name).call(this, event), fn);
}
off(event, fn) {
_classPrivateFieldGet(_bus, this).off(_assertClassBrand(_EventsManager_brand, this, _name).call(this, event), fn);
}
emit(event, payload) {
_classPrivateFieldGet(_bus, this).emit(_assertClassBrand(_EventsManager_brand, this, _name).call(this, event), payload);
}
startScheduler() {
if (!_classPrivateFieldGet(_sched, this)) {
_classPrivateFieldSet(_sched, this, setInterval(() => _assertClassBrand(_EventsManager_brand, this, _tick).call(this), _classPrivateFieldGet(_config, this).intervalMs));
_assertClassBrand(_EventsManager_brand, this, _tick).call(this).catch(() => {});
}
}
stopScheduler() {
if (_classPrivateFieldGet(_sched, this)) {
clearInterval(_classPrivateFieldGet(_sched, this));
_classPrivateFieldSet(_sched, this, null);
}
}
}
function _name(ev) {
return ev.startsWith(_classPrivateFieldGet(_prefix, this) + ".") ? ev : `${_classPrivateFieldGet(_prefix, this)}.${ev}`;
}
async function _tick() {
const nowVal = now();
const soonCutoff = nowVal + _classPrivateFieldGet(_config, this).expiringSoonBeforeMs;
const dueCutoff = nowVal + _classPrivateFieldGet(_config, this).lookaheadMs;
const qb = this.vault.queryItems(undefined, null, [{
path: "timestamp",
dir: "desc"
}], {
limit: 1000
}, undefined, _classPrivateFieldGet(_config, this).actionInfo || undefined);
const items = qb.execute(false);
for (const item of items) {
const it = item.data;
const exp = it.expiration;
if (!exp) {
continue;
}
let expiresAt = exp.expiresAt;
if (!expiresAt && exp.ttlMs) {
expiresAt = (item.timestamp || nowVal) + exp.ttlMs;
}
if (!expiresAt) {
continue;
}
if (!it.expSoon && expiresAt > nowVal && expiresAt <= soonCutoff) {
this.emit("item.expire_soon", {
ownerID: it.ownerID,
id: item.id,
expiresAt,
item: it
});
try {
this.vault.updateItem(item.id, {
expSoon: true
});
} catch {}
}
if (expiresAt <= dueCutoff && expiresAt <= nowVal) {
const action = exp.onExpire || "hide";
try {
if (action === "hide") {
this.vault.updateItem(item.id, {
status: "hidden"
});
} else if (action === "archive") {
this.vault.updateItem(item.id, {
status: "archived"
});
} else if (action === "delete") {
this.vault.deleteRecord(item.id);
}
} catch {}
this.emit("item.expired", {
ownerID: it.ownerID,
id: item.id,
expiresAt,
item: it,
action
});
}
}
}
;
module.exports = {
EventsManager
};