diff options
Diffstat (limited to 'src/fields/Document.ts')
-rw-r--r-- | src/fields/Document.ts | 37 |
1 files changed, 23 insertions, 14 deletions
diff --git a/src/fields/Document.ts b/src/fields/Document.ts index cb4f6f25c..4bab1299d 100644 --- a/src/fields/Document.ts +++ b/src/fields/Document.ts @@ -10,13 +10,22 @@ import { Types } from "../server/Message"; import { ObjectID } from "bson"; export class Document extends Field { - public fields: ObservableMap<Key, Opt<Field>> = new ObservableMap(); + public fields: ObservableMap<string, { key: Key, field: Opt<Field> }> = new ObservableMap(); public _proxies: ObservableMap<string, FIELD_ID> = new ObservableMap(); - constructor(id?: string) { + constructor(id?: string, save: boolean = true) { super(id) - Server.UpdateField(this) + if (save) { + Server.UpdateField(this) + } + } + + UpdateFromServer(data: [string, string][]) { + for (const key in data) { + const element = data[key]; + this._proxies.set(element[0], element[1]); + } } @computed @@ -27,25 +36,25 @@ export class Document extends Field { Get(key: Key, ignoreProto: boolean = false): FieldValue<Field> { let field: FieldValue<Field>; if (ignoreProto) { - if (this.fields.has(key)) { - field = this.fields.get(key); + if (this.fields.has(key.Id)) { + field = this.fields.get(key.Id)!.field; } else if (this._proxies.has(key.Id)) { field = Server.GetDocumentField(this, key); } } else { let doc: FieldValue<Document> = this; while (doc && doc != FieldWaiting && field != FieldWaiting) { - if (!doc.fields.has(key)) { + if (!doc.fields.has(key.Id)) { if (doc._proxies.has(key.Id)) { field = Server.GetDocumentField(doc, key); break; } - if ((doc.fields.has(KeyStore.Prototype) || doc._proxies.has(KeyStore.Prototype.Id))) { + if ((doc.fields.has(KeyStore.Prototype.Id) || doc._proxies.has(KeyStore.Prototype.Id))) { doc = doc.GetPrototype(); } else break; } else { - field = doc.fields.get(key); + field = doc.fields.get(key.Id)!.field; break; } } @@ -93,11 +102,11 @@ export class Document extends Field { @action Set(key: Key, field: Field | undefined): void { if (field) { - this.fields.set(key, field); + this.fields.set(key.Id, { key, field }); this._proxies.set(key.Id, field.Id) // Server.AddDocumentField(this, key, field); } else { - this.fields.delete(key); + this.fields.delete(key.Id); this._proxies.delete(key.Id) // Server.DeleteDocumentField(this, key); } @@ -144,8 +153,8 @@ export class Document extends Field { return protos; } - MakeDelegate(): Document { - let delegate = new Document(); + MakeDelegate(id?: string): Document { + let delegate = new Document(id); delegate.Set(KeyStore.Prototype, this); @@ -167,14 +176,14 @@ export class Document extends Field { } ToJson(): { type: Types, data: [string, string][], _id: string } { - console.log(this.fields) + // console.log(this.fields) let fields: [string, string][] = [] this._proxies.forEach((field, key) => { if (field) { fields.push([key, field as string]) } }); - console.log(fields) + // console.log(fields) return { type: Types.Document, |