diff options
author | Tyler Schicke <tyler_schicke@brown.edu> | 2019-08-07 21:37:14 -0400 |
---|---|---|
committer | Tyler Schicke <tyler_schicke@brown.edu> | 2019-08-07 21:37:14 -0400 |
commit | 35cb61f0d93983464b29152f159d09ea6bd4edf9 (patch) | |
tree | 85f9ffadb021d85074b6dd9606a9e7551ece4f19 /src/new_fields/Doc.ts | |
parent | 221acd0cfb4831435d1d1b61b86c2cc5e3d3b413 (diff) |
Mostly have field level read only working
Diffstat (limited to 'src/new_fields/Doc.ts')
-rw-r--r-- | src/new_fields/Doc.ts | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/src/new_fields/Doc.ts b/src/new_fields/Doc.ts index c01f4e8cf..ebe3a5ba8 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 { |