@mlightcad/data-model
Version:
The data-model package provides the core classes for interacting with AutoCAD's database and entities. This package mimics AutoCAD ObjectARX's AcDb (Database) classes and implements the drawing database structure that AutoCAD developers are familiar with.
272 lines • 11.3 kB
JavaScript
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 };
}
};
/**
* Class used to break up work into smaller chunks that are executed asynchronously.
*
* This is often referred to as "batch processing" or "cooperative multitasking," where the
* time-consuming task is broken into smaller pieces and executed in small intervals to allow
* the UI to remain responsive.
*
* @example
* ```typescript
* const batchProcessor = new AcDbBatchProcessing(1000, 10, 50);
* await batchProcessor.processChunk(async (start, end) => {
* // Process items from start to end
* for (let i = start; i < end; i++) {
* // Process item i
* }
* });
* ```
*/
var AcDbBatchProcessing = /** @class */ (function () {
/**
* Creates a new AcDbBatchProcessing instance.
*
* @param count - The total number of items to process
* @param numerOfChunk - The number of chunks to process
* @param minimumChunkSize - The minimum number of items in one chunk. If it is greater
* than the total number of items to process, the total number is used.
*
* @example
* ```typescript
* const batchProcessor = new AcDbBatchProcessing(1000, 10, 50);
* ```
*/
function AcDbBatchProcessing(count, numerOfChunk, minimumChunkSize) {
/** Number of items in one chunk */
this._chunkSize = -1;
/** Minimum number of items in one chunk */
this._minimumChunkSize = 50;
this._count = count;
this._numerOfChunk = numerOfChunk < 1 ? 1 : numerOfChunk;
this._minimumChunkSize = minimumChunkSize;
this.calculateChunkSize();
}
Object.defineProperty(AcDbBatchProcessing.prototype, "count", {
/**
* Gets the total number of items to process.
*
* @returns The total number of items to process
*
* @example
* ```typescript
* const totalItems = batchProcessor.count;
* ```
*/
get: function () {
return this._count;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbBatchProcessing.prototype, "numerOfChunk", {
/**
* Gets the number of chunks to process.
*
* @returns The number of chunks to process
*
* @example
* ```typescript
* const numberOfChunks = batchProcessor.numerOfChunk;
* ```
*/
get: function () {
return this._numerOfChunk;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbBatchProcessing.prototype, "minimumChunkSize", {
/**
* Gets the minimum number of items in one chunk.
*
* @returns The minimum number of items in one chunk
*
* @example
* ```typescript
* const minChunkSize = batchProcessor.minimumChunkSize;
* ```
*/
get: function () {
return this._minimumChunkSize;
},
/**
* Sets the minimum number of items in one chunk.
*
* @param value - The new minimum chunk size
*
* @example
* ```typescript
* batchProcessor.minimumChunkSize = 100;
* ```
*/
set: function (value) {
this._minimumChunkSize = value;
this.calculateChunkSize();
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbBatchProcessing.prototype, "chunkSize", {
/**
* Gets the number of items in one chunk.
*
* @returns The number of items in one chunk
*
* @example
* ```typescript
* const chunkSize = batchProcessor.chunkSize;
* ```
*/
get: function () {
return this._chunkSize;
},
enumerable: false,
configurable: true
});
/**
* Calculates the chunk size based on the total count, number of chunks, and minimum chunk size.
*
* @example
* ```typescript
* batchProcessor.calculateChunkSize();
* ```
*/
AcDbBatchProcessing.prototype.calculateChunkSize = function () {
var demicalChunkSize = this._count / this._numerOfChunk;
if (demicalChunkSize < this._minimumChunkSize) {
demicalChunkSize = Math.min(this._minimumChunkSize, this._count);
}
this._chunkSize =
demicalChunkSize < 1 ? this._count : Math.floor(demicalChunkSize);
};
/**
* Schedules a task to be executed asynchronously.
*
* This method uses requestAnimationFrame in browser environments or setTimeout
* in Node.js environments to schedule the task.
*
* @param callback - The callback function to schedule
* @returns Promise that resolves when the task completes
*
* @example
* ```typescript
* await batchProcessor.scheduleTask(async () => {
* // Task to be executed asynchronously
* });
* ```
*/
AcDbBatchProcessing.prototype.scheduleTask = function (callback) {
return new Promise(function (resolve, reject) {
var executeCallback = function () {
// Execute the callback and handle the result
Promise.resolve(callback()).then(resolve).catch(reject);
};
if (typeof window !== 'undefined' &&
typeof window.requestAnimationFrame === 'function') {
// Browser environment with requestAnimationFrame
window.requestAnimationFrame(executeCallback);
}
else {
// Node.js or fallback to setTimeout
setTimeout(executeCallback, 0);
}
});
};
/**
* Processes items in chunks using the provided callback function.
*
* This method breaks up the work into chunks and processes each chunk
* asynchronously, allowing the UI to remain responsive.
*
* @param callback - The callback function to execute for each chunk
* @returns Promise that resolves when all chunks have been processed
*
* @example
* ```typescript
* await batchProcessor.processChunk(async (start, end) => {
* for (let i = start; i < end; i++) {
* // Process item i
* await processItem(i);
* }
* });
* ```
*/
AcDbBatchProcessing.prototype.processChunk = function (callback) {
return __awaiter(this, void 0, void 0, function () {
var currentIndex, processNextChunk;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
currentIndex = 0;
processNextChunk = function () { return __awaiter(_this, void 0, void 0, function () {
var start, end;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
start = currentIndex;
end = Math.min(currentIndex + this._chunkSize, this._count);
// Call the provided callback with the chunk's range
return [4 /*yield*/, callback(start, end)];
case 1:
// Call the provided callback with the chunk's range
_a.sent();
currentIndex = end;
if (!(currentIndex < this._count)) return [3 /*break*/, 3];
return [4 /*yield*/, this.scheduleTask(processNextChunk)]; // Schedule the next chunk to be processed asynchronously
case 2:
_a.sent(); // Schedule the next chunk to be processed asynchronously
_a.label = 3;
case 3: return [2 /*return*/];
}
});
}); };
// Start processing the first chunk and wait for all chunks to complete
return [4 /*yield*/, processNextChunk()];
case 1:
// Start processing the first chunk and wait for all chunks to complete
_a.sent();
return [2 /*return*/];
}
});
});
};
return AcDbBatchProcessing;
}());
export { AcDbBatchProcessing };
//# sourceMappingURL=AcDbBatchProcessing.js.map