From 2a2303d1dccc120dd7aa5f6b6ade0f89e0064850 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Tue, 25 Nov 2025 07:42:23 +0000
Subject: [PATCH] =Feed block mostly good! Referrals look good to go. Ready for Madi and Heidi to approve
---
assets/js/concise/Queue.js | 290 ++++++++++++++++++++++++----------------------------------
1 files changed, 120 insertions(+), 170 deletions(-)
diff --git a/assets/js/concise/Queue.js b/assets/js/concise/Queue.js
index b817cbb..297885d 100644
--- a/assets/js/concise/Queue.js
+++ b/assets/js/concise/Queue.js
@@ -5,7 +5,6 @@
class QueueManager {
constructor(config = {}) {
this.canUpdateUI = true;
- console.log('jvbSettings', jvbSettings);
this.config = {
apiBase: jvbSettings.api,
maxRetries: 3,
@@ -27,20 +26,19 @@
this.errors = window.jvbError;
// Initialize DataStore for queue persistence
- this.store = new window.jvbStore({
- name: 'queue',
- storeName: 'operations',
+ const store = window.jvbStore.register('queue', {
+ storeName: 'queue',
keyPath: 'id',
endpoint: this.config.endpoint,
- TTL: Infinity, //Queue data doesn't expire,
+ TTL: Infinity,
indexes: [
{name: 'status', keyPath: 'status'},
{name: 'type', keyPath: 'type'},
],
showLoading: false,
+ delayFetch: false, // Queue should fetch immediately
});
-
- this.queue = new Map();
+ this.store = store.queue;
this.classes = [
'offline',
@@ -68,7 +66,6 @@
// Initialize
this.initUI();
this.initListeners();
- console.log(this.ui);
if (this.ui.panel) {
this.popup = new window.jvbPopup({
popup: this.ui.panel,
@@ -96,23 +93,55 @@
this.store.subscribe((event, data) => {
switch (event) {
- case 'data-fetched':
- case 'data-cached':
- this.updateOperationsFromServer(data.data.items);
+ case 'data-loaded':
+ case 'items-saved':
+ // Initial load from IndexedDB
+ const incomplete = this.getOperationsByStatus(['completed', 'failed_permanent'], false);
+ if (incomplete.length > 0) {
+ this.startPolling();
+ }
+ this.updateUI();
break;
- case 'items-updated':
- this.updateOperationsFromServer(data.items);
+ case 'item-saved':
+ // Check for status changes
+ if (data.item) {
+ const oldItem = this.store.data.get(data.item.id);
+ if (oldItem && oldItem.status !== data.item.status) {
+ this.handleOperationStatusChange(data.item, oldItem.status);
+ }
+ }
+ if (this.hasQueuedOperations()) {
+ this.startPolling();
+ }
break;
- case 'item-stored':
- this.updateOperationsFromServer([data])
+ default:
+ this.updateUI();
break;
}
});
-
- this.store.fetch();
this.notify('queue-initialized', {operations: incomplete});
}
+
+ /**
+ * Handle operation status changes and notify subscribers
+ */
+ handleOperationStatusChange(operation, oldStatus) {
+ if (!operation || oldStatus === operation.status) return;
+
+ // Notify based on new status
+ switch(operation.status) {
+ case 'completed':
+ this.notify('operation-completed', operation);
+ break;
+ case 'failed':
+ this.notify('operation-failed', operation);
+ break;
+ case 'failed_permanent':
+ this.notify('operation-failed-permanent', operation);
+ break;
+ }
+ }
/**
*
* @param {object} operation
@@ -152,7 +181,7 @@
return null;
}
- const existingOps = Array.from(this.queue.values()).filter(op=>
+ const existingOps = Array.from(this.store.data.values()).filter(op=>
op.status === 'queued' &&
op.endpoint === item.endpoint &&
op.canMerge
@@ -171,6 +200,7 @@
}
console.log('Added to Queue: ', item);
+ this.store.clearCache();
//Add new operation to DataStore
this.setQueue(item);
@@ -185,32 +215,26 @@
}
setQueue(item) {
- this.queue.set(item.id, item);
- this.store.save(item.id, item);
+ this.store.save(item); // Remove first parameter
}
updateOperationStatus(itemID, status) {
- let item = this.queue.get(itemID);
+ let item = this.store.get(itemID);
if (!item){
return;
}
item.status = status;
+
this.notify('operation-status', item);
this.updateOperationUI(item);
}
getQueue(itemID) {
- if (this.queue.has(itemID)) {
- return this.queue.get(itemID);
- }
- return this.store.getItem(itemID);
+ return this.store.get(itemID);
}
clearQueue(itemID) {
- if (this.queue.has(itemID)) {
- this.queue.delete(itemID);
- }
- this.store.clearItem(itemID);
+ this.store.delete(itemID);
}
startActivityTracking() {
@@ -284,10 +308,12 @@
async processOperation(operation) {
try {
- //update to uploading
this.updateOperationStatus(operation.id, 'uploading');
- //build request
+ if (operation.data?._isFormData) {
+ operation.data = await this.store.objectToFormData(operation.data);
+ }
+
const url = `${this.config.apiBase}${operation.endpoint}`;
let requestBody;
@@ -295,21 +321,12 @@
operation.data.append('id', operation.id);
operation.data.append('user', this.user);
requestBody = operation.data;
- // console.log('Sending formData: ');
- // for (const pair of requestBody.entries()) {
- // console.log(pair[0], pair[1]);
- // }
} else {
requestBody = JSON.stringify({
...operation.data,
id: operation.id,
user: this.user
});
- // console.log('Sending data: ', {
- // ...operation.data,
- // id: operation.id,
- // user: this.user
- // });
operation.headers['Content-Type'] = 'application/json';
}
@@ -384,74 +401,24 @@
startPolling() {
if (this.isPolling) return;
+
this.isPolling = true;
- this.pollServer();
- this.pollTimer = setInterval(() => {
- this.pollServer();
- }, this.config.pollInterval);
-
- this.updateCountdown();
- }
-
- pollServer(force = false) {
- const operations = this.getOperationsByStatus(['pending', 'processing', 'uploading']);
-
- if (operations.length === 0 && !force) {
- this.stopPolling();
- return;
- }
this.updateStatusPanel('pending');
- try {
- // const operationIds = operations.map(op => op.id);
- // this.store.setFilter('operation_ids', operationIds.join(','));
- this.store.fetch();
- } catch (error) {
- console.error('Polling error:', error);
- } finally {
- this.updateStatusPanel();
- }
- }
+ this.pollTimer = setInterval(async () => {
+ try {
+ this.store.clearCache();
+ await this.store.fetch(); // Fetches from server, updates store.data
- async updateOperationsFromServer(serverOperations) {
- let hasChanges = false;
- const processedIds = new Set();
- for (const serverOp of serverOperations) {
- let operation = (this.queue.has(serverOp.id)) ? this.queue.get(serverOp.id) : {};
- processedIds.add(serverOp.id);
- if (serverOp.status !== operation.status) {
- operation = {
- ... operation,
- ... serverOp
- };
- // Update in DataStore
- this.queue.set(operation.id, operation);
-
- // Update UI for this operation
- this.updateOperationStatus(operation.id, operation.status);
+ const incomplete = this.getOperationsByStatus(['completed', 'failed_permanent'], false);
+ if (incomplete.length === 0) {
+ this.stopPolling();
+ this.updateStatusPanel('synced');
+ }
+ } catch (error) {
+ console.error('Polling error:', error);
}
- }
-
- // Clean up operations that were completed/dismissed on server
- const localOps = this.getOperationsByStatus(['pending', 'processing', 'uploading']);
- for (const localOp of localOps) {
- if (!processedIds.has(localOp.id)) {
- localOp.status = 'completed';
- localOp.completedAt = Date.now();
- this.setQueue(localOp);
- hasChanges = true;
- this.updateOperationStatus(localOp.id, localOp.status);
- }
- }
-
- // Check if all operations are completed
- const pendingOps = this.getOperationsByStatus(['pending', 'processing', 'uploading']);
-
- if (pendingOps.length === 0) {
- this.stopPolling();
- }
-
- this.updateUI();
+ }, this.config.pollInterval);
}
stopPolling() {
@@ -506,7 +473,7 @@
'Content-Type': 'application/json',
...this.headers
},
- body: JSON.stringify({ids,action})
+ body: JSON.stringify({ids,action, user: jvbSettings.currentUser})
}
);
@@ -602,8 +569,13 @@
window.addEventListener('beforeunload', this.handleBeforeUnload);
}
handleClick(e) {
+ if (!e.target.closest(this.selectors.panel, this.selectors.toggle)) {
+ return;
+ }
if (e.target.closest(this.selectors.refreshButton)) {
- this.pollServer(true);
+ this.store.clearCache();
+ this.store.clearHttpHeaders(); // Clear cached headers first
+ this.store.fetch();
} else if (e.target.closest(this.selectors.clearButton)) {
const completedOps = this.getOperationsByStatus('completed');
if (completedOps.length > 0) {
@@ -637,9 +609,9 @@
*********************************************/
initUI() {
this.icons = {
- queued: 'refresh', localProcessing: 'refresh', uploading: 'syncing',
- pending: 'cloud', processing: 'syncing', completed: 'synced',
- failed: 'error', failed_permanent: 'error'
+ queued: 'arrows-clockwise', localProcessing: 'arrows-clockwise', uploading: 'syncing',
+ pending: 'cloud', processing: 'syncing', completed: 'cloud-check',
+ failed: 'cloud-warning', failed_permanent: 'cloud-warning'
};
this.selectors = {
@@ -695,13 +667,27 @@
if (!this.canUpdateUI) {
return;
}
- const stats = this.getQueueStats();
+
+ // Get current operations from store
+ const operations = Array.from(this.store.data.values());
+
+ // Get stats from last fetch response (server-provided)
+ const stats = this.store.lastResponse?.queue_stats || {
+ queued: 0,
+ localProcessing: 0,
+ uploading: 0,
+ pending: 0,
+ processing: 0,
+ completed: 0,
+ failed: 0,
+ failed_permanent: 0
+ };
// Update count badge
if (this.ui.count) {
- const total = stats.total - stats.completed;
- this.ui.count.textContent = total > 0 ? total : '';
- this.ui.count.style.display = total > 0 ? '' : 'none';
+ const activeCount = operations.length - stats.completed;
+ this.ui.count.textContent = activeCount > 0 ? activeCount : '';
+ this.ui.count.style.display = activeCount > 0 ? '' : 'none';
}
// Update indicator
@@ -710,14 +696,16 @@
stats.pending > 0 || stats.processing > 0;
this.ui.indicator.classList.toggle('active', hasActive);
}
- let failed = this.getOperationsByStatus('failed');
- let completed = this.getOperationsByStatus('completed');
- this.ui.clearButton.disabled = completed.length === 0;
- this.ui.retryButton.disabled = failed.length === 0;
- // Update filter counts
+ // Update button states
+ this.ui.clearButton.disabled = this.getOperationsByStatus('completed').length === 0;
+ this.ui.retryButton.disabled = this.getOperationsByStatus('failed').length === 0 && this.getOperationsByStatus('failed_permanent').length === 0;
+
+ // Update filter counts (from server stats)
Object.entries(this.ui.filters).forEach(([status, button]) => {
- const count = status === 'all' ? stats.total : stats[status] || 0;
+ const count = status === 'all'
+ ? operations.length
+ : stats[status] || 0;
const countEl = button.querySelector('.count');
if (countEl) {
countEl.textContent = count > 0 ? count : '';
@@ -725,7 +713,7 @@
button.setAttribute('data-count', count);
});
- // Update operation list
+ // Render current operations
this.renderOperations();
}
@@ -784,46 +772,24 @@
return statusProgress[item.status] || 0;
}
- getQueueStats() {
- const stats = {};
- this.statuses.forEach(status => {
- stats[status] = 0;
- });
-
- Array.from(this.store.items.values())
- .forEach(op => {
- if (stats.hasOwnProperty(op.status)) {
- stats[op.status]++;
- }
- });
-
- stats.total = Array.from(this.store.items.values()).length;
-
- return stats;
- }
renderOperations() {
if (!this.ui.itemsContainer) return;
- const activeFilter = this.getActiveFilter();
- const operations = this.getFilteredOperations(activeFilter);
+ const operations = this.store.getFiltered();
// Clear container
window.removeChildren(this.ui.itemsContainer);
- // Render each operation
+ // Render operations or empty state
if (operations.length === 0) {
let empty = window.getTemplate('emptyQueue');
this.ui.itemsContainer.append(empty);
this.a11y.announce('Nothing queued.');
} else {
- let empty = this.ui.itemsContainer.querySelector('.emptyQueue');
- if (empty) {
- empty.remove();
- }
operations.forEach(op => {
const element = this.createOperationUI(op);
- this.ui.itemsContainer.appendChild(element);
+ this.ui.itemsContainer.append(element);
});
}
}
@@ -970,29 +936,18 @@
FILTERS
**************************************************/
setFilter(filter) {
+ // Update active button
Object.values(this.ui.filters).forEach(button => {
if (button) {
button.classList.toggle('active', button.dataset.filter === filter);
}
});
- this.activeFilter = filter;
- this.renderOperations();
- }
-
- getActiveFilter() {
- const activeButton = this.ui.panel?.querySelector('.filter.active');
- return activeButton?.dataset.filter || 'all';
- }
-
- getFilteredOperations(filter) {
- const operations = Array.from(this.store.items.values());
-
if (filter === 'all') {
- return operations;
+ this.store.clearFilters();
+ } else {
+ this.store.setFilter('status', filter);
}
-
- return operations.filter(op => op.status === filter);
}
/**************************************************************************
@@ -1017,20 +972,15 @@
**************************************************************************/
getOperationsByStatus(status, include = true) {
- status = Array.isArray(status) ? status : ((status.includes(',')) ? status.split(',') : [status]);
- if (include) {
- return Array.from(this.queue.values()).filter(op =>
- status.includes(op.status)
- );
+ if (!Array.isArray(status) && typeof status === 'string') {
+ status = [status];
}
- return Array.from(this.queue.values()).filter(op =>
- !status.includes(op.status)
- );
+ return (include)
+ ? Array.from(this.store.data.values()).filter((item) => status.includes(item.status))
+ : Array.from(this.store.data.values()).filter((item) => !status.includes(item.status));
}
hasQueuedOperations() {
- return this.queue.some(op =>
- op.status === 'queued'
- );
+ return this.getOperationsByStatus('queued').length > 0;
}
subscribe(callback) {
this.subscribers.add(callback);
--
Gitblit v1.10.0