@cssfn/cssfn
Advanced tools
Comparing version 2.0.13 to 2.0.14
@@ -16,3 +16,4 @@ import { generateRulesFromFactory, renderStyleSheet, } from './renderStyleSheets.js'; | ||
worker: newWorkerInstance, | ||
busyLevel: 0, | ||
unfinishedWorks: 0, | ||
totalWorks: 0, | ||
}; | ||
@@ -26,5 +27,5 @@ workerList.push(newWorkerEntry); // store the worker to re-use later | ||
}; | ||
const isNotBusyWorker = (workerEntry) => (workerEntry.busyLevel === 0); | ||
const isNotBusyWorker = (workerEntry) => (workerEntry.unfinishedWorks === 0); | ||
const sortBusiestWorker = (a, b) => { | ||
return a.busyLevel - b.busyLevel; // sort from the least busy to the most busy | ||
return a.unfinishedWorks - b.unfinishedWorks; // sort from the least busy to the most busy | ||
}; | ||
@@ -64,5 +65,11 @@ const bookingWorker = () => { | ||
currentWorker.removeEventListener('error', handleError); | ||
currentWorkerEntry.busyLevel--; | ||
if ((--currentWorkerEntry.unfinishedWorks) === 0) { // free | ||
currentWorkerEntry.totalWorks = 0; // reset the counter | ||
} // if | ||
}; | ||
const handleProcessed = (event) => { | ||
// conditions: | ||
const currentQueueId = currentWorkerEntry.totalWorks - currentWorkerEntry.unfinishedWorks; | ||
if (queueId !== currentQueueId) | ||
return; // not my queue_id => ignore | ||
handleDone(); | ||
@@ -72,2 +79,6 @@ resolve(event.data); | ||
const handleError = (event) => { | ||
// conditions: | ||
const currentQueueId = currentWorkerEntry.totalWorks - currentWorkerEntry.unfinishedWorks; | ||
if (queueId !== currentQueueId) | ||
return; // not my queue_id => ignore | ||
handleDone(); | ||
@@ -77,3 +88,4 @@ reject(event); | ||
// actions: | ||
currentWorkerEntry.busyLevel++; | ||
currentWorkerEntry.unfinishedWorks++; // count how many work is in progress | ||
const queueId = currentWorkerEntry.totalWorks++; // a queue_id to distinguish between current data and previously posted data | ||
currentWorker.addEventListener('message', handleProcessed); | ||
@@ -80,0 +92,0 @@ currentWorker.addEventListener('error', handleError); |
{ | ||
"name": "@cssfn/cssfn", | ||
"version": "2.0.13", | ||
"version": "2.0.14", | ||
"description": "Writes, imports, and exports css stylesheets as javascript modules.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -24,4 +24,5 @@ // cssfn: | ||
type WorkerEntry = { | ||
worker : Worker // holds web worker instance | ||
busyLevel : number // the busy level: 0 = free, 1 = a bit busy, 99 = very busy | ||
worker : Worker // holds web worker instance | ||
unfinishedWorks : number // the busy level: 0 = free, 1 = a bit busy, 99 = very busy | ||
totalWorks : number // the total works | ||
} | ||
@@ -42,5 +43,6 @@ const workerList : WorkerEntry[] = []; // holds the workers | ||
const newWorkerEntry = { | ||
worker : newWorkerInstance, | ||
busyLevel : 0, | ||
const newWorkerEntry : WorkerEntry = { | ||
worker : newWorkerInstance, | ||
unfinishedWorks : 0, | ||
totalWorks : 0, | ||
}; | ||
@@ -55,5 +57,5 @@ workerList.push(newWorkerEntry); // store the worker to re-use later | ||
const isNotBusyWorker = (workerEntry: WorkerEntry) => (workerEntry.busyLevel === 0); | ||
const isNotBusyWorker = (workerEntry: WorkerEntry) => (workerEntry.unfinishedWorks === 0); | ||
const sortBusiestWorker = (a: WorkerEntry, b: WorkerEntry): number => { | ||
return a.busyLevel - b.busyLevel; // sort from the least busy to the most busy | ||
return a.unfinishedWorks - b.unfinishedWorks; // sort from the least busy to the most busy | ||
} | ||
@@ -107,5 +109,13 @@ const bookingWorker = (): WorkerEntry|null => { | ||
currentWorkerEntry.busyLevel--; | ||
if ((--currentWorkerEntry.unfinishedWorks) === 0) { // free | ||
currentWorkerEntry.totalWorks = 0; // reset the counter | ||
} // if | ||
}; | ||
const handleProcessed = (event: MessageEvent<string|null>) => { | ||
// conditions: | ||
const currentQueueId = currentWorkerEntry.totalWorks - currentWorkerEntry.unfinishedWorks; | ||
if (queueId !== currentQueueId) return; // not my queue_id => ignore | ||
handleDone(); | ||
@@ -115,2 +125,8 @@ resolve(event.data); | ||
const handleError = (event: Event) => { | ||
// conditions: | ||
const currentQueueId = currentWorkerEntry.totalWorks - currentWorkerEntry.unfinishedWorks; | ||
if (queueId !== currentQueueId) return; // not my queue_id => ignore | ||
handleDone(); | ||
@@ -123,3 +139,4 @@ reject(event); | ||
// actions: | ||
currentWorkerEntry.busyLevel++; | ||
currentWorkerEntry.unfinishedWorks++; // count how many work is in progress | ||
const queueId = currentWorkerEntry.totalWorks++; // a queue_id to distinguish between current data and previously posted data | ||
@@ -126,0 +143,0 @@ currentWorker.addEventListener('message', handleProcessed); |
608770
16687