This commit is contained in:
seb
2026-10-28 23:25:54 +01:00
parent 6851e7c16f
commit 5fcc5670d9
4 changed files with 153 additions and 33 deletions

View File

@@ -21,6 +21,7 @@
import { Automat } from 'react-automat';
import authAutomat from './authAutomat';
import { articleHistoryAutomat, articleSupplierAutomat } from './articleDetailAutomat';
/**
* @typedef {Object} SseState
@@ -202,6 +203,24 @@ function dispatchSseEvent(event) {
void Automat.get('company')?.actions?.reloadWarehouses?.().catch(() => {});
Automat.get('category')?.actions?.invalidateAllCategoryViews?.();
void Automat.get('articleIndex')?.actions?.reload?.().catch(() => {});
} else if (eventType === 'supplier') {
void Automat.get('company')?.actions?.reloadSuppliers?.().catch(() => {});
} else if (eventType === 'brand') {
void Automat.get('company')?.actions?.loadBrands?.(true).catch(() => {});
} else if (eventType === 'unit') {
void Automat.get('company')?.actions?.loadUnits?.().catch(() => {});
} else if (eventType === 'carrier') {
void Automat.get('company')?.actions?.reloadCarriers?.().catch(() => {});
} else if (eventType === 'posterminal') {
void Automat.get('company')?.actions?.loadTerminals?.().catch(() => {});
} else if (eventType === 'customergroup') {
void Automat.get('company')?.actions?.reloadCustomergroups?.().catch(() => {});
} else if (eventType === 'propertyname') {
void Automat.get('company')?.actions?.loadProperties?.().catch(() => {});
} else if (eventType === 'attributename') {
void Automat.get('company')?.actions?.loadAttributes?.().catch(() => {});
} else if (eventType === 'account') {
void Automat.get('company')?.actions?.reloadAccounts?.().catch(() => {});
} else if (eventType === 'order') {
void Automat.get('order')?.actions?.reload?.().catch(() => {});
void Automat.get('shipment')?.actions?.reload?.().catch(() => {});
@@ -219,9 +238,10 @@ function dispatchSseEvent(event) {
// 3. Dispatch to article-specific handlers if it starts with article-
if (eventType.startsWith(ARTICLE_EVENT_PREFIX)) {
const sku = eventType.slice(ARTICLE_EVENT_PREFIX.length);
const lowerSku = sku.trim().toLowerCase();
// Watchers registered via watchArticle(sku, handler)
const watchers = articleWatchers.get(sku);
const watchers = articleWatchers.get(lowerSku) || articleWatchers.get(sku);
if (watchers) {
for (const handler of watchers) {
try {
@@ -522,20 +542,21 @@ const actions = {
* @returns {() => void} Unsubscribe callback.
*/
watchArticle(sku, handler) {
const key = String(sku);
let set = articleWatchers.get(key);
const rawKey = String(sku).trim();
const lowerKey = rawKey.toLowerCase();
let set = articleWatchers.get(lowerKey);
if (!set) {
set = new Set();
articleWatchers.set(key, set);
attachArticleListener(key);
articleWatchers.set(lowerKey, set);
attachArticleListener(rawKey);
}
set.add(handler);
return () => {
const current = articleWatchers.get(key);
const current = articleWatchers.get(lowerKey);
if (!current) return;
current.delete(handler);
if (current.size === 0) articleWatchers.delete(key);
if (current.size === 0) articleWatchers.delete(lowerKey);
};
},
@@ -575,14 +596,11 @@ export function invalidateArticle(sku) {
if (art?.actions?.reloadArticle) {
void art.actions.reloadArticle(key).catch(() => {});
}
const hist = Automat.get('articleHistory');
if (hist?.has?.(key)) {
void hist.reload(key).catch(() => {});
}
const sup = Automat.get('articleSupplier');
if (sup?.has?.(key)) {
void sup.reload(key).catch(() => {});
if (articleHistoryAutomat.has(key)) {
void articleHistoryAutomat.reload(key).catch(() => {});
}
// Reload assigned suppliers and sync with companyAutomat
void articleSupplierAutomat.reload(key).catch(() => {});
}
authAutomat.subscribe(handleAuthSync);