Implementing persisting action result mutations
This commit is contained in:
55
app/imports/api/engine/shared/bulkWrite.ts
Normal file
55
app/imports/api/engine/shared/bulkWrite.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
// This is more efficient on the database, but significantly less efficient
|
||||
// in the UI because of incompatibility with latency compensation. If the
|
||||
// duplicate redraws can be fixed, this is a strictly better way of processing
|
||||
// writes
|
||||
export default function bulkWrite(bulkWriteOps, collection): void | Promise<any> {
|
||||
if (!bulkWriteOps.length) return;
|
||||
// bulkWrite is only available on the server
|
||||
if (!Meteor.isServer) {
|
||||
return writePropertiesSequentially(bulkWriteOps, collection);
|
||||
}
|
||||
return collection.rawCollection().bulkWrite(
|
||||
bulkWriteOps,
|
||||
{ ordered: false }
|
||||
);
|
||||
}
|
||||
|
||||
// If we re-enable client-side sheet recalculation, this needs to be run on
|
||||
// both client and server to preserve latency compensation. Bulkwrite breaks
|
||||
// latency compensation and causes flickering
|
||||
function writePropertiesSequentially(bulkWriteOps: any[], collection: Mongo.Collection<any>) {
|
||||
bulkWriteOps.forEach(op => {
|
||||
const updateOneOrMany = op.updateOne || op.updateMany;
|
||||
collection.update(updateOneOrMany.filter, updateOneOrMany.update, {
|
||||
// The bulk code is bypassing validation, so do the same here
|
||||
// @ts-expect-error Collection 2 has no typescript support
|
||||
bypassCollection2: true,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function newOperation(_id) {
|
||||
const newOp = {
|
||||
updateOne: {
|
||||
filter: { _id },
|
||||
update: {},
|
||||
}
|
||||
};
|
||||
return newOp;
|
||||
}
|
||||
|
||||
export function addSetOp(op, key, value) {
|
||||
if (op.updateOne.update.$set) {
|
||||
op.updateOne.update.$set[key] = value;
|
||||
} else {
|
||||
op.updateOne.update.$set = { [key]: value };
|
||||
}
|
||||
}
|
||||
|
||||
export function addUnsetOp(op, key) {
|
||||
if (op.updateOne.update.$unset) {
|
||||
op.updateOne.update.$unset[key] = 1;
|
||||
} else {
|
||||
op.updateOne.update.$unset = { [key]: 1 };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user