syncflow-engine
Version:
A flexible and robust data synchronization library for JavaScript applications
376 lines (375 loc) • 17.3 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SyncEngine = void 0;
var logger_1 = require("../utils/logger");
var DEFAULT_CONFIG = {
retryLimit: 3,
retryDelay: 1000,
batchSize: 10,
autoStart: true,
debug: false,
};
var SyncEngine = /** @class */ (function () {
function SyncEngine(store, config, entityTypes) {
if (config === void 0) { config = DEFAULT_CONFIG; }
if (entityTypes === void 0) { entityTypes = []; }
var _this = this;
this.store = store;
this.config = config;
this.status = "idle";
this.listeners = new Map();
this.entityListeners = new Map();
this.syncInterval = null;
this.entities = new Map();
this.isOffline = false;
entityTypes.forEach(function (entityType) {
_this.entities.set(entityType, []);
_this.entityListeners.set(entityType, new Set());
});
}
SyncEngine.prototype.init = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!this.config.autoStart) return [3 /*break*/, 2];
return [4 /*yield*/, this.start()];
case 1:
_a.sent();
_a.label = 2;
case 2: return [2 /*return*/];
}
});
});
};
SyncEngine.prototype.start = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
if (this.syncInterval)
return [2 /*return*/];
this.isOffline = false;
this.setStatus("idle");
this.syncInterval = setInterval(function () { return _this.sync(); }, 5000);
logger_1.logger.debug("Sync engine started");
return [2 /*return*/];
});
});
};
SyncEngine.prototype.stop = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
if (this.syncInterval) {
clearInterval(this.syncInterval);
this.syncInterval = null;
}
this.isOffline = true;
this.setStatus("offline");
logger_1.logger.debug("Sync engine stopped");
return [2 /*return*/];
});
});
};
SyncEngine.prototype.sync = function () {
return __awaiter(this, void 0, void 0, function () {
var operations, pendingOps, _i, pendingOps_1, op, error_1, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this.status === "syncing")
return [2 /*return*/];
_a.label = 1;
case 1:
_a.trys.push([1, 10, , 11]);
this.setStatus("syncing");
this.emit("syncStart");
return [4 /*yield*/, this.store.getOperations()];
case 2:
operations = _a.sent();
pendingOps = operations
.filter(function (op) { return op.status === "pending"; })
.slice(0, this.config.batchSize);
_i = 0, pendingOps_1 = pendingOps;
_a.label = 3;
case 3:
if (!(_i < pendingOps_1.length)) return [3 /*break*/, 9];
op = pendingOps_1[_i];
_a.label = 4;
case 4:
_a.trys.push([4, 6, , 8]);
return [4 /*yield*/, this.processOperation(op)];
case 5:
_a.sent();
this.emit("operationComplete", op);
return [3 /*break*/, 8];
case 6:
error_1 = _a.sent();
logger_1.logger.error("Operation failed: ".concat(op.id), error_1);
return [4 /*yield*/, this.handleOperationError(op)];
case 7:
_a.sent();
return [3 /*break*/, 8];
case 8:
_i++;
return [3 /*break*/, 3];
case 9:
this.setStatus("idle");
this.emit("syncComplete");
return [3 /*break*/, 11];
case 10:
error_2 = _a.sent();
this.setStatus("error");
this.emit("syncError", error_2);
logger_1.logger.error("Sync failed:", error_2);
return [3 /*break*/, 11];
case 11: return [2 /*return*/];
}
});
});
};
SyncEngine.prototype.cleanupCompletedOperations = function () {
return __awaiter(this, void 0, void 0, function () {
var operations, completedOps, _i, completedOps_1, op;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.store.getOperations()];
case 1:
operations = _a.sent();
completedOps = operations.filter(function (op) { return op.status === "completed"; });
_i = 0, completedOps_1 = completedOps;
_a.label = 2;
case 2:
if (!(_i < completedOps_1.length)) return [3 /*break*/, 5];
op = completedOps_1[_i];
return [4 /*yield*/, this.store.removeOperation(op.id)];
case 3:
_a.sent();
_a.label = 4;
case 4:
_i++;
return [3 /*break*/, 2];
case 5: return [2 /*return*/];
}
});
});
};
SyncEngine.prototype.removePendingOperations = function () {
return __awaiter(this, void 0, void 0, function () {
var operations, pendingOps, _i, pendingOps_2, op;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.store.getOperations()];
case 1:
operations = _a.sent();
pendingOps = operations.filter(function (op) { return op.status === "pending"; });
_i = 0, pendingOps_2 = pendingOps;
_a.label = 2;
case 2:
if (!(_i < pendingOps_2.length)) return [3 /*break*/, 5];
op = pendingOps_2[_i];
return [4 /*yield*/, this.store.removeOperation(op.id)];
case 3:
_a.sent();
_a.label = 4;
case 4:
_i++;
return [3 /*break*/, 2];
case 5: return [2 /*return*/, pendingOps];
}
});
});
};
SyncEngine.prototype.getStatus = function () {
return this.status;
};
SyncEngine.prototype.addListener = function (event, callback) {
var _a;
if (!this.listeners.has(event)) {
this.listeners.set(event, new Set());
}
(_a = this.listeners.get(event)) === null || _a === void 0 ? void 0 : _a.add(callback);
};
SyncEngine.prototype.removeListener = function (event, callback) {
var _a;
(_a = this.listeners.get(event)) === null || _a === void 0 ? void 0 : _a.delete(callback);
};
SyncEngine.prototype.setStatus = function (status) {
this.status = status;
this.emit("statusChange", status);
};
SyncEngine.prototype.emit = function (event, data) {
var _a, _b;
if (this.listeners.has(event)) {
(_a = this.listeners.get(event)) === null || _a === void 0 ? void 0 : _a.forEach(function (callback) { return callback(data); });
}
if (this.entityListeners.has(event)) {
var entities_1 = this.entities.get(event) || [];
(_b = this.entityListeners
.get(event)) === null || _b === void 0 ? void 0 : _b.forEach(function (callback) { return callback(entities_1); });
}
};
SyncEngine.prototype.getEntities = function (entityType) {
return __awaiter(this, void 0, void 0, function () {
var entities;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.store.getEntities(entityType)];
case 1:
entities = _a.sent();
this.entities.set(entityType, entities);
return [2 /*return*/, entities];
}
});
});
};
SyncEngine.prototype.getEntityTypes = function () {
return Array.from(this.entities.keys());
};
SyncEngine.prototype.addEntityListener = function (entityType, callback) {
var _a;
if (!this.entityListeners.has(entityType)) {
throw new Error("Entity type \"".concat(entityType, "\" not registered"));
}
(_a = this.entityListeners.get(entityType)) === null || _a === void 0 ? void 0 : _a.add(callback);
};
SyncEngine.prototype.removeEntityListener = function (entityType, callback) {
var _a;
(_a = this.entityListeners.get(entityType)) === null || _a === void 0 ? void 0 : _a.delete(callback);
};
SyncEngine.prototype.processOperation = function (operation) {
return __awaiter(this, void 0, void 0, function () {
var type, entity, data, entities, updatedEntities;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
type = operation.type, entity = operation.entity, data = operation.data;
return [4 /*yield*/, this.getEntities(entity)];
case 1:
entities = _a.sent();
updatedEntities = [];
switch (type) {
case "create":
updatedEntities = __spreadArray(__spreadArray([], entities, true), [data], false);
break;
case "update":
updatedEntities = entities.map(function (e) {
return e.id === data.id ? __assign(__assign({}, e), data) : e;
});
break;
case "delete":
updatedEntities = entities.filter(function (e) { return e.id !== data.id; });
break;
}
return [4 /*yield*/, this.store.saveEntities(entity, updatedEntities)];
case 2:
_a.sent();
this.entities.set(entity, updatedEntities);
this.emit(entity, updatedEntities);
if (!(process.env.NODE_ENV === "test" || !this.isOffline)) return [3 /*break*/, 4];
return [4 /*yield*/, this.store.updateOperation(operation.id, { status: "completed" })];
case 3:
_a.sent();
_a.label = 4;
case 4: return [2 /*return*/];
}
});
});
};
SyncEngine.prototype.handleOperationError = function (operation) {
return __awaiter(this, void 0, void 0, function () {
var newRetryCount;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
newRetryCount = (operation.retryCount || 0) + 1;
if (!(newRetryCount >= this.config.retryLimit)) return [3 /*break*/, 2];
return [4 /*yield*/, this.store.updateOperation(operation.id, {
status: "error",
retryCount: newRetryCount,
})];
case 1:
_a.sent();
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, this.store.updateOperation(operation.id, {
retryCount: newRetryCount,
})];
case 3:
_a.sent();
_a.label = 4;
case 4: return [2 /*return*/];
}
});
});
};
SyncEngine.prototype.getPendingOperations = function () {
return __awaiter(this, void 0, void 0, function () {
var operations;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.store.getOperations()];
case 1:
operations = _a.sent();
return [2 /*return*/, operations.filter(function (op) { return op.status === "pending"; })];
}
});
});
};
return SyncEngine;
}());
exports.SyncEngine = SyncEngine;