From 3de39e2608e62a278b9c8cc37b53333f0877fa59 Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Tue, 15 Jan 2019 07:57:16 -0500 Subject: Have most of document stuff working --- src/controllers/DocumentController.ts | 73 +++++------------------------------ 1 file changed, 9 insertions(+), 64 deletions(-) (limited to 'src/controllers/DocumentController.ts') diff --git a/src/controllers/DocumentController.ts b/src/controllers/DocumentController.ts index 02ef66417..0627f9717 100644 --- a/src/controllers/DocumentController.ts +++ b/src/controllers/DocumentController.ts @@ -2,25 +2,25 @@ import { FieldController, Cast, Opt } from "./FieldController" import { KeyController, KeyStore } from "./KeyController" import { TypedEvent, Listener, Disposable } from "../util/TypedEvent"; import { DocumentUpdatedArgs, FieldUpdatedAction } from "./FieldUpdatedArgs"; +import { ObservableMap } from "mobx"; export class DocumentController extends FieldController { - private fields: { [key: string]: { key: KeyController, field: FieldController, disposer: Disposable } } = {}; - private fieldUpdateHandlers: { [key: string]: TypedEvent } = {}; + private fields: ObservableMap = new ObservableMap(); GetField(key: KeyController, ignoreProto?: boolean): Opt { let field: Opt; if (ignoreProto) { - if (key.Id in this.fields) { - field = this.fields[key.Id].field; + if (this.fields.has(key)) { + field = this.fields.get(key); } } else { let doc: Opt = this; - while (doc && !(key.Id in doc.fields)) { + while (doc && !(doc.fields.has(key))) { doc = doc.GetPrototype(); } if (doc) { - field = doc.fields[key.Id].field; + field = doc.fields.get(key); } } @@ -32,45 +32,11 @@ export class DocumentController extends FieldController { } SetField(key: KeyController, field: Opt): void { - let oldField: Opt; - if (key.Id in this.fields) { - let old = this.fields[key.Id]; - oldField = old.field; - old.disposer.dispose(); - } - - if (oldField === field) { - return; - } - - if (field == null) { - delete this.fields[key.Id]; + if (field) { + this.fields.set(key, field); } else { - this.fields[key.Id] = { - key: key, - field: field, - disposer: field.FieldUpdated.on((args) => this.DocumentFieldUpdated({ - action: FieldUpdatedAction.Update, - oldValue: undefined, - newValue: field, - field: this, - fieldArgs: args, - key: key - })) - } + this.fields.delete(key); } - - let action = oldField === null ? FieldUpdatedAction.Add : - (field === null ? FieldUpdatedAction.Remove : - FieldUpdatedAction.Replace); - - this.DocumentFieldUpdated({ - field: this, - key: key, - oldValue: oldField, - newValue: field, - action: action - }) } SetFieldValue(key: KeyController, value: any, ctor: { new(): T }): boolean { @@ -110,27 +76,6 @@ export class DocumentController extends FieldController { return delegate; } - private DocumentFieldUpdated(args: DocumentUpdatedArgs) { - if (args.key.Id in this.fieldUpdateHandlers) { - this.fieldUpdateHandlers[args.key.Id].emit(args); - } - this.FieldUpdated.emit(args); - } - - AddFieldUpdatedHandler(key: KeyController, listener: Listener): Disposable { - if (!(key.Id in this.fieldUpdateHandlers)) { - this.fieldUpdateHandlers[key.Id] = new TypedEvent(); - } - - return this.fieldUpdateHandlers[key.Id].on(listener); - } - - RemoveFieldUpdatedHandler(key: KeyController, listener: Listener) { - if (key.Id in this.fieldUpdateHandlers) { - this.fieldUpdateHandlers[key.Id].off(listener); - } - } - TrySetValue(value: any): boolean { throw new Error("Method not implemented."); } -- cgit v1.2.3-70-g09d2