Files
wekan/models/lib/userFieldsProjection.js
T
Lauri Ojansivu 6d5f24ee27 Large board: read only the user fields each card and list needs.
Opening a card writes the user document (profile.cardLastViews, #3078). The
board view (Utils.storedBoardView and allowBoardView, which every list's card
loop reads) and the helpers every minicard runs read the WHOLE user document,
so that one write re-ran every list's card loop, and Blaze then re-evaluated
every minicard: 37,452 Tracker invalidations for one card open on a 40-card
board. Measured in the running app, it is now 390.

client/lib/currentUserWith.js reads the current user with only the named
fields; minimongo re-runs a field-limited query only when a projected field
changes. The board view, feature preview, unread comments, folds, drag
handles, label text, dependency layers, date format, week number, mobile mode
and the admin-only custom field check now use it. setCardLastViewed $sets one
entry instead of rewriting the whole map, which is also capped at 5,000.

Thanks to markusst1982 and xet7 !

Fixes #6745.
2026-10-07 03:08:48 +03:00

37 lines
1.7 KiB
JavaScript

'use strict';
// #6745: the field projection a client helper uses to read only the parts of
// the current user it actually needs. Pure, so tests/userFieldsProjection.test.cjs
// runs it without Meteor.
//
// Minimongo re-runs a field-limited query only when one of the PROJECTED fields
// changed (LocalCollection._updateInResults diffs projectionFn(doc) against
// projectionFn(oldDoc)). A minicard helper that reads the whole user document
// (Meteor.user(), ReactiveCache.getCurrentUser()) instead re-runs on EVERY
// write to that document - and opening a card writes one
// (profile.cardLastViews, #3078). On a large board that was every helper of
// every minicard, several seconds per card open.
//
// A path is dot-separated, and each segment must be a key the profile maps
// already accept (models/lib/safeMapKey.js): no '.', no leading '$', not a
// prototype name. A card or board id interpolated into a path therefore cannot
// turn the projection into something else.
const { isSafeMapKey } = require('./safeMapKey');
function isUserFieldPath(path) {
return typeof path === 'string' && path.length > 0 && path.length <= 512 &&
path.split('.').every(isSafeMapKey);
}
// ['profile.a', 'profile.b.<id>'] -> { 'profile.a': 1, 'profile.b.<id>': 1 },
// or null when any path is not one - the caller then reads the whole document,
// which is slower but never wrong, instead of failing inside a template helper.
function userFieldsProjection(paths) {
const list = Array.isArray(paths) ? paths : [paths];
if (list.length === 0 || !list.every(isUserFieldPath)) return null;
const fields = {};
list.forEach(path => { fields[path] = 1; });
return fields;
}
module.exports = { isUserFieldPath, userFieldsProjection };