Files
wekan/models/lib/cardsLoading.js
T
Lauri Ojansivu 8d1fae4f7d Convert client-reachable models/lib helpers from CommonJS to ES modules (1/2).
The rspack client build treats a `module.exports` file imported into the
client bundle as an ES module and throws
"ES Modules may not assign module.exports" at evaluation time, aborting
the whole client module chain (No such template: swimlane / notifications
/ boardButtons, blank board, missing top bar). This is not an rspack
version regression (the lockfile has pinned ~1.7.x since v8.40); it is
that WeKan added CommonJS helpers to the client bundle.

Convert the 42 client-reachable pure-logic models/lib helpers from
`module.exports = { ... }` to `export { ... }`, and calendarFilter's
internal require('./swimlaneFilter') to an import with a .js extension
(node ESM needs it; rspack/reify tolerate it). Server-only helpers that
use node built-ins (fs/path/child_process) stay CommonJS. The plain-Node
.cjs unit tests for these helpers now use `await import(...js)` (wrapped
in an async IIFE); a source-guard in importVirtualMembers was updated
from module.exports to export. Verified: the full tests/*.test.cjs suite
has the SAME 7 pre-existing failures as before this change and no new
ones.
2026-07-23 18:55:30 +03:00

73 lines
3.1 KiB
JavaScript

'use strict';
// Pure, Meteor-free helpers for the CARDS_LOADING / lazy card-loading feature.
// Unit-tested in tests/cardsLoading.test.cjs. Used by server/cards-loading.js,
// server/models/settings.js (env seeding), server/publications/boards.js and
// client/lib/lazyCards.js.
//
// Modes:
// all — the board publication ships every card into minimongo (simple, fully
// featured; heavy for very large boards).
// lazy — each list loads only its visible window; the board ships no cards.
// auto — (DEFAULT) decide PER BOARD by size: a board with more than the
// threshold of cards loads lazily, smaller boards load eagerly ('all').
// This keeps small boards on the simple, full-featured path and only
// pays the windowed-loading overhead where it actually helps (#6480).
const DEFAULT_LAZY_THRESHOLD = 500;
// Effective GLOBAL card-loading mode from any input (env var, setting): one of
// 'all' | 'lazy' | 'auto'. Anything unrecognised / unset defaults to 'auto'.
// Never throws.
function resolveCardsLoadingMode(value) {
const v = String(value == null ? '' : value).trim().toLowerCase();
if (v === 'all') return 'all';
if (v === 'lazy') return 'lazy';
return 'auto';
}
// Parse the CARDS_LOADING env var for the "env is authoritative only when
// explicitly set" case: returns 'all' | 'lazy' | 'auto' when set to one of those,
// otherwise undefined (leave the stored setting alone).
function parseCardsLoadingEnv(value) {
const v = String(value == null ? '' : value).trim().toLowerCase();
return v === 'all' || v === 'lazy' || v === 'auto' ? v : undefined;
}
// The card-count threshold above which an 'auto' board loads lazily. Parses the
// CARDS_LOADING_LAZY_THRESHOLD env var; falls back to the default for unset /
// invalid / negative values. A threshold of 0 makes every 'auto' board lazy.
function cardsLoadingLazyThreshold(value) {
const n = parseInt(value, 10);
return Number.isFinite(n) && n >= 0 ? n : DEFAULT_LAZY_THRESHOLD;
}
// Resolve the EFFECTIVE per-board mode ('all' | 'lazy') from the global mode and a
// board's card count. 'all'/'lazy' are honoured as-is; 'auto' becomes 'lazy' when
// the board has MORE than `threshold` cards, otherwise 'all'. Pure + unit-tested.
function effectiveBoardCardsMode(mode, cardCount, threshold) {
const m = resolveCardsLoadingMode(mode);
if (m === 'all') return 'all';
if (m === 'lazy') return 'lazy';
const count = Number.isFinite(cardCount) ? cardCount : 0;
const limit = Number.isFinite(threshold) ? threshold : DEFAULT_LAZY_THRESHOLD;
return count > limit ? 'lazy' : 'all';
}
// Stable id for one (list, swimlane) window's reactive count doc. A falsy
// swimlaneId (undefined, null, '') MUST collapse to the same id, because the
// list body subscribes with `undefined` (list view) while the list header reads
// with '' — both must reference the same count.
function windowCountId(listId, swimlaneId) {
return `${listId}::${swimlaneId || ''}`;
}
export {
DEFAULT_LAZY_THRESHOLD,
resolveCardsLoadingMode,
parseCardsLoadingEnv,
cardsLoadingLazyThreshold,
effectiveBoardCardsMode,
windowCountId,
};