aboutsummaryrefslogtreecommitdiff
path: root/src/new_fields/Doc.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/new_fields/Doc.ts')
-rw-r--r--src/new_fields/Doc.ts38
1 files changed, 31 insertions, 7 deletions
diff --git a/src/new_fields/Doc.ts b/src/new_fields/Doc.ts
index b3d1dc109..736e8e69d 100644
--- a/src/new_fields/Doc.ts
+++ b/src/new_fields/Doc.ts
@@ -68,6 +68,7 @@ export function DocListCast(field: FieldResult): Doc[] {
export const WidthSym = Symbol("Width");
export const HeightSym = Symbol("Height");
+const CachedUpdates = Symbol("Cached updates");
function fetchProto(doc: Doc) {
const proto = doc.proto;
@@ -147,6 +148,8 @@ export class Doc extends RefField {
return "invalid";
}
+ private [CachedUpdates]: { [key: string]: () => Promise<any> } = {};
+
public async [HandleUpdate](diff: any) {
const set = diff.$set;
if (set) {
@@ -154,11 +157,18 @@ export class Doc extends RefField {
if (!key.startsWith("fields.")) {
continue;
}
- const value = await SerializationHelper.Deserialize(set[key]);
const fKey = key.substring(7);
- updatingFromServer = true;
- this[fKey] = value;
- updatingFromServer = false;
+ const fn = async () => {
+ const value = await SerializationHelper.Deserialize(set[key]);
+ updatingFromServer = true;
+ this[fKey] = value;
+ updatingFromServer = false;
+ };
+ if (DocServer.getFieldWriteMode(fKey)) {
+ this[CachedUpdates][fKey] = fn;
+ } else {
+ await fn();
+ }
}
}
const unset = diff.$unset;
@@ -168,9 +178,16 @@ export class Doc extends RefField {
continue;
}
const fKey = key.substring(7);
- updatingFromServer = true;
- delete this[fKey];
- updatingFromServer = false;
+ const fn = async () => {
+ updatingFromServer = true;
+ delete this[fKey];
+ updatingFromServer = false;
+ };
+ if (DocServer.getFieldWriteMode(fKey)) {
+ this[CachedUpdates][fKey] = fn;
+ } else {
+ await fn();
+ }
}
}
}
@@ -187,6 +204,13 @@ export namespace Doc {
// return Cast(field, ctor);
// });
// }
+ export function RunCachedUpdate(doc: Doc, field: string) {
+ const update = doc[CachedUpdates][field];
+ if (update) {
+ update();
+ delete doc[CachedUpdates][field];
+ }
+ }
export function MakeReadOnly(): { end(): void } {
makeReadOnly();
return {