aboutsummaryrefslogtreecommitdiff
path: root/src/new_fields/Doc.ts
diff options
context:
space:
mode:
authorBob Zeleznik <zzzman@gmail.com>2019-08-10 08:31:45 -0400
committerBob Zeleznik <zzzman@gmail.com>2019-08-10 08:31:45 -0400
commitb465486c571b2e4b44a3b87c2a85df23d936d3ab (patch)
tree93db00dd9d9b35252b6709247ab7574250a8a7a7 /src/new_fields/Doc.ts
parente291730c407932506a80f96457a84dce1820521d (diff)
parent3aea955ae56c1aa0611de09c4a013e9dc5c86c42 (diff)
Merge branch 'master' into dragBoxes
Diffstat (limited to 'src/new_fields/Doc.ts')
-rw-r--r--src/new_fields/Doc.ts43
1 files changed, 28 insertions, 15 deletions
diff --git a/src/new_fields/Doc.ts b/src/new_fields/Doc.ts
index fc4411d93..4cfd7929b 100644
--- a/src/new_fields/Doc.ts
+++ b/src/new_fields/Doc.ts
@@ -13,6 +13,7 @@ import { List } from "./List";
import { DocumentType } from "../client/documents/Documents";
import { ComputedField, ScriptField } from "./ScriptField";
import { PrefetchProxy, ProxyField } from "./Proxy";
+import { CurrentUserUtils } from "../server/authentication/models/current_user_utils";
export namespace Field {
export function toKeyValueString(doc: Doc, key: string): string {
@@ -68,6 +69,7 @@ export function DocListCast(field: FieldResult): Doc[] {
export const WidthSym = Symbol("Width");
export const HeightSym = Symbol("Height");
+export const UpdatingFromServer = Symbol("UpdatingFromServer");
const CachedUpdates = Symbol("Cached updates");
function fetchProto(doc: Doc) {
@@ -77,8 +79,6 @@ function fetchProto(doc: Doc) {
}
}
-let updatingFromServer = false;
-
@scriptingGlobal
@Deserializable("Doc", fetchProto).withFields(["id"])
export class Doc extends RefField {
@@ -132,8 +132,10 @@ export class Doc extends RefField {
//{ [key: string]: Field | FieldWaiting | undefined }
private ___fields: any = {};
+ private [UpdatingFromServer]: boolean = false;
+
private [Update] = (diff: any) => {
- if (updatingFromServer) {
+ if (this[UpdatingFromServer]) {
return;
}
DocServer.UpdateField(this[Id], diff);
@@ -148,10 +150,11 @@ export class Doc extends RefField {
return "invalid";
}
- private [CachedUpdates]: { [key: string]: () => Promise<any> } = {};
+ private [CachedUpdates]: { [key: string]: () => void | Promise<any> } = {};
public async [HandleUpdate](diff: any) {
const set = diff.$set;
+ const sameAuthor = this.author === CurrentUserUtils.email;
if (set) {
for (const key in set) {
if (!key.startsWith("fields.")) {
@@ -160,14 +163,15 @@ export class Doc extends RefField {
const fKey = key.substring(7);
const fn = async () => {
const value = await SerializationHelper.Deserialize(set[key]);
- updatingFromServer = true;
+ this[UpdatingFromServer] = true;
this[fKey] = value;
- updatingFromServer = false;
+ this[UpdatingFromServer] = false;
};
- if (DocServer.getFieldWriteMode(fKey)) {
- this[CachedUpdates][fKey] = fn;
- } else {
+ if (sameAuthor || DocServer.getFieldWriteMode(fKey) !== DocServer.WriteMode.Playground) {
+ delete this[CachedUpdates][fKey];
await fn();
+ } else {
+ this[CachedUpdates][fKey] = fn;
}
}
}
@@ -178,15 +182,16 @@ export class Doc extends RefField {
continue;
}
const fKey = key.substring(7);
- const fn = async () => {
- updatingFromServer = true;
+ const fn = () => {
+ this[UpdatingFromServer] = true;
delete this[fKey];
- updatingFromServer = false;
+ this[UpdatingFromServer] = false;
};
- if (DocServer.getFieldWriteMode(fKey)) {
- this[CachedUpdates][fKey] = fn;
- } else {
+ if (sameAuthor || DocServer.getFieldWriteMode(fKey) !== DocServer.WriteMode.Playground) {
+ delete this[CachedUpdates][fKey];
await fn();
+ } else {
+ this[CachedUpdates][fKey] = fn;
}
}
}
@@ -211,6 +216,14 @@ export namespace Doc {
delete doc[CachedUpdates][field];
}
}
+ export function AddCachedUpdate(doc: Doc, field: string, oldValue: any) {
+ const val = oldValue;
+ doc[CachedUpdates][field] = () => {
+ doc[UpdatingFromServer] = true;
+ doc[field] = val;
+ doc[UpdatingFromServer] = false;
+ };
+ }
export function MakeReadOnly(): { end(): void } {
makeReadOnly();
return {