@just-in/core
Version:
A TypeScript-first framework for building adaptive digital health interventions.
89 lines • 4.11 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.JustInLite = void 0;
const JustInWrapper_1 = require("./JustInWrapper");
const data_manager_1 = __importDefault(require("./data-manager/data-manager"));
const change_listener_manager_1 = require("./data-manager/change-listener.manager");
const logger_manager_1 = require("./logger/logger-manager");
const event_queue_1 = require("./event/event-queue");
const data_manager_constants_1 = require("./data-manager/data-manager.constants");
/**
* JustinLite is a lightweight version of the core JustInWrapper singleton,
* designed specifically for stateless environments like Google Cloud Functions or Cloud Run.
*
* It extends the core functionality with lifecycle controls such as singleton reset
* and event engine queue waiting.
*/
class JustinLiteWrapper extends JustInWrapper_1.JustInWrapper {
/**
* Private constructor ensures use through getInstance().
*/
constructor() {
super();
}
/**
* Retrieves the singleton instance of JustinLite.
* @returns {JustinLite} The singleton instance.
*/
static getInstance() {
if (!JustinLiteWrapper.instance) {
JustinLiteWrapper.instance = new JustinLiteWrapper();
}
return JustinLiteWrapper.instance;
}
/**
* Resets all singleton instances used by the framework.
*
* This should be called at the start of a stateless execution context,
* such as in Google Cloud Functions or tests.
*/
static reset() {
var _a, _b, _c;
(_a = JustInWrapper_1.JustInWrapper['killInstance']) === null || _a === void 0 ? void 0 : _a.call(JustInWrapper_1.JustInWrapper);
(_b = data_manager_1.default['killInstance']) === null || _b === void 0 ? void 0 : _b.call(data_manager_1.default);
(_c = change_listener_manager_1.ChangeListenerManager['killInstance']) === null || _c === void 0 ? void 0 : _c.call(change_listener_manager_1.ChangeListenerManager);
JustinLiteWrapper.instance = null;
}
/**
* Cleanses the database by clearing the EVENTS_QUEUE, EVENTS, and USERS collections.
* This is useful for ensuring a clean state before starting a new execution.
*
* @returns {Promise<void>} A promise that resolves when the database is cleansed.
*/
async cleanseDB() {
await data_manager_1.default.getInstance().clearCollection(data_manager_constants_1.EVENT_QUEUE);
await data_manager_1.default.getInstance().clearCollection(data_manager_constants_1.USERS);
logger_manager_1.Log.info('DB cleansed');
}
/**
* Waits until the event engine queue is empty before proceeding.
* Useful for ensuring all events are processed before shutdown in serverless environments.
*
* @param timeout - Maximum wait time in milliseconds (default: 10 seconds)
* @throws Error if the queue is not empty within the timeout period
*/
async waitUntilQueueIsEmpty(timeout = JustinLiteWrapper.EVENT_QUEUE_WAIT_TIME) {
const start = Date.now();
logger_manager_1.Log.info('Waiting for event queue to empty, is it empty?', await (0, event_queue_1.queueIsEmpty)(), 'isRunning', (0, event_queue_1.isRunning)());
while ((0, event_queue_1.isRunning)()) {
if (await (0, event_queue_1.queueIsEmpty)())
return;
if (Date.now() - start > timeout) {
throw new Error('Timeout while waiting for event queue to empty ' + timeout);
}
await new Promise(resolve => setTimeout(resolve, 200));
}
}
}
JustinLiteWrapper.instance = null;
JustinLiteWrapper.EVENT_QUEUE_WAIT_TIME = 60 * 1000;
exports.JustInLite = Object.assign(() => JustinLiteWrapper.getInstance(), {
reset: () => {
logger_manager_1.Log.info('Calling JustInLite.reset()');
JustinLiteWrapper.reset();
},
});
//# sourceMappingURL=JustInLite.js.map