emberflow 1.3.3 → 1.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/utils/forms.d.ts +1 -1
- package/lib/utils/forms.js +45 -14
- package/lib/utils/forms.js.map +1 -1
- package/package.json +1 -1
package/lib/utils/forms.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { FormData } from "emberflow-admin-client/lib/types";
|
|
2
2
|
export declare function waitForSubmitFormQueueToEmpty(): Promise<void>;
|
|
3
|
-
export declare function queueSubmitForm(formData: FormData): void
|
|
3
|
+
export declare function queueSubmitForm(formData: FormData): Promise<void>;
|
package/lib/utils/forms.js
CHANGED
|
@@ -2,16 +2,20 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.queueSubmitForm = exports.waitForSubmitFormQueueToEmpty = void 0;
|
|
4
4
|
const lib_1 = require("emberflow-admin-client/lib");
|
|
5
|
-
const
|
|
6
|
-
let activeCount = 0;
|
|
7
|
-
const MAX_CONCURRENT_SUBMISSIONS = 10;
|
|
5
|
+
const index_1 = require("../index");
|
|
8
6
|
let resolveQueueEmpty;
|
|
9
7
|
let queueEmptyPromise = new Promise((resolve) => {
|
|
10
8
|
resolveQueueEmpty = resolve;
|
|
11
9
|
});
|
|
10
|
+
let maxConcurrentSubmit = 10;
|
|
11
|
+
let running = false;
|
|
12
|
+
let activeCount = 0;
|
|
13
|
+
const queue = [];
|
|
14
|
+
const execRef = index_1.db.doc("@internal/submitFormExecutor");
|
|
15
|
+
const queueRef = index_1.db.collection("@internal/submitFormExecutor/queue");
|
|
12
16
|
function waitForSubmitFormQueueToEmpty() {
|
|
13
17
|
// If the queue is already empty and there are no active submissions, resolve immediately
|
|
14
|
-
if (
|
|
18
|
+
if (!running) {
|
|
15
19
|
return Promise.resolve();
|
|
16
20
|
}
|
|
17
21
|
// Otherwise, return the existing promise or create a new one if necessary
|
|
@@ -27,36 +31,64 @@ function waitForSubmitFormQueueToEmpty() {
|
|
|
27
31
|
return queueEmptyPromise;
|
|
28
32
|
}
|
|
29
33
|
exports.waitForSubmitFormQueueToEmpty = waitForSubmitFormQueueToEmpty;
|
|
30
|
-
function queueSubmitForm(formData) {
|
|
34
|
+
async function queueSubmitForm(formData) {
|
|
31
35
|
// Add the formData and the current timestamp to the queue
|
|
32
36
|
console.log("Queueing form submission", formData);
|
|
33
|
-
|
|
37
|
+
await index_1.db.runTransaction(async (transaction) => {
|
|
38
|
+
const docRef = queueRef.doc();
|
|
39
|
+
transaction.set(docRef, { formData, timestamp: new Date() });
|
|
40
|
+
});
|
|
34
41
|
// Process the queue
|
|
35
42
|
processQueue();
|
|
36
43
|
}
|
|
37
44
|
exports.queueSubmitForm = queueSubmitForm;
|
|
38
|
-
function
|
|
45
|
+
function updateQueueInfo() {
|
|
46
|
+
return index_1.db.runTransaction(async (transaction) => {
|
|
47
|
+
const queueDoc = await transaction.get(execRef);
|
|
48
|
+
({ activeCount, maxConcurrentSubmit } = queueDoc.data() || { activeCount: 0, maxConcurrentSubmit: 10 });
|
|
49
|
+
if (queue.length <= 1) {
|
|
50
|
+
const queryRef = queueRef.orderBy("timestamp", "asc")
|
|
51
|
+
.limit(2 * maxConcurrentSubmit);
|
|
52
|
+
const queueSnapshot = await transaction.get(queryRef);
|
|
53
|
+
queueSnapshot.forEach((doc) => {
|
|
54
|
+
queue.push(Object.assign(Object.assign({}, doc.data()), { "@id": doc.id }));
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
async function processQueue() {
|
|
60
|
+
await updateQueueInfo();
|
|
61
|
+
console.debug("maxConcurrentSubmit", maxConcurrentSubmit);
|
|
62
|
+
if (!running && activeCount > 0) {
|
|
63
|
+
console.debug("Another executor is already running");
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
39
66
|
// While there are items in the queue and we haven't hit the max concurrency
|
|
40
|
-
while (queue.length > 0 && activeCount <
|
|
67
|
+
while (queue.length > 0 && activeCount < maxConcurrentSubmit) {
|
|
68
|
+
running = true;
|
|
41
69
|
// Get the next item from the queue
|
|
42
70
|
const queueItem = queue.shift();
|
|
43
71
|
if (!queueItem) {
|
|
44
72
|
continue;
|
|
45
73
|
}
|
|
74
|
+
await index_1.db.runTransaction(async (transaction) => {
|
|
75
|
+
transaction.delete(queueRef.doc(queueItem["@id"]));
|
|
76
|
+
transaction.update(execRef, { activeCount: index_1.admin.firestore.FieldValue.increment(1) });
|
|
77
|
+
});
|
|
46
78
|
const { formData, timestamp } = queueItem;
|
|
47
|
-
// Increment the active count
|
|
48
|
-
activeCount++;
|
|
49
79
|
// Submit the form
|
|
50
|
-
_submitForm(formData).then(() => {
|
|
80
|
+
_submitForm(formData).then(async () => {
|
|
51
81
|
// Decrement the active count
|
|
52
|
-
activeCount
|
|
82
|
+
await execRef.update({ activeCount: index_1.admin.firestore.FieldValue.increment(-1) });
|
|
83
|
+
await updateQueueInfo();
|
|
53
84
|
// Check if the queue is empty and all submissions have been processed
|
|
54
85
|
if (queue.length === 0 && activeCount === 0) {
|
|
55
86
|
resolveQueueEmpty(); // Resolve the waitForQueueToEmpty promise
|
|
56
87
|
queueEmptyPromise = undefined;
|
|
88
|
+
running = false;
|
|
57
89
|
}
|
|
58
90
|
// Calculate the time spent in the queue
|
|
59
|
-
const timeSpent = (Date.now() - timestamp) / 1000; // time in seconds
|
|
91
|
+
const timeSpent = (Date.now() - timestamp.getTime()) / 1000; // time in seconds
|
|
60
92
|
// Log the time spent in the queue
|
|
61
93
|
console.log(`formData spent ${timeSpent.toFixed(2)} seconds in the queue.`);
|
|
62
94
|
// Process the next item in the queue
|
|
@@ -66,7 +98,6 @@ function processQueue() {
|
|
|
66
98
|
}
|
|
67
99
|
function _submitForm(formData) {
|
|
68
100
|
return new Promise((resolve) => {
|
|
69
|
-
// Assuming submitFormRequest is the function actually handling the form submission.
|
|
70
101
|
(0, lib_1.submitForm)(formData, (status, data, isLastUpdate) => {
|
|
71
102
|
console.log("status", status, "data", data, "isLastUpdate", isLastUpdate);
|
|
72
103
|
if (isLastUpdate) {
|
package/lib/utils/forms.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"forms.js","sourceRoot":"","sources":["../../src/utils/forms.ts"],"names":[],"mappings":";;;AACA,oDAAsD;
|
|
1
|
+
{"version":3,"file":"forms.js","sourceRoot":"","sources":["../../src/utils/forms.ts"],"names":[],"mappings":";;;AACA,oDAAsD;AACtD,oCAAmC;AAInC,IAAI,iBAA6B,CAAC;AAClC,IAAI,iBAAiB,GAA8B,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;IACzE,iBAAiB,GAAG,OAAO,CAAC;AAC9B,CAAC,CAAC,CAAC;AAEH,IAAI,mBAAmB,GAAG,EAAE,CAAC;AAC7B,IAAI,OAAO,GAAG,KAAK,CAAC;AACpB,IAAI,WAAW,GAAG,CAAC,CAAC;AACpB,MAAM,KAAK,GAAgB,EAAE,CAAC;AAE9B,MAAM,OAAO,GAAG,UAAE,CAAC,GAAG,CAAC,8BAA8B,CAAC,CAAC;AACvD,MAAM,QAAQ,GAAG,UAAE,CAAC,UAAU,CAAC,oCAAoC,CAAC,CAAC;AAErE,SAAgB,6BAA6B;IAC3C,yFAAyF;IACzF,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;KAC1B;IAED,0EAA0E;IAC1E,IAAI,CAAC,iBAAiB,EAAE;QACtB,iBAAiB,GAAG,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC1C,iBAAiB,GAAG,OAAO,CAAC;QAC9B,CAAC,CAAC,CAAC;KACJ;IAED,uEAAuE;IACvE,iBAAiB,CAAC,IAAI,CAAC,GAAG,EAAE;QAC1B,iBAAiB,GAAG,SAAS,CAAC;IAChC,CAAC,CAAC,CAAC;IAEH,OAAO,iBAAiB,CAAC;AAC3B,CAAC;AAnBD,sEAmB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