From 6473fdcbc01fecfa5bc8dc46cdcd7841c1ef35ae Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Wed, 20 Mar 2019 05:04:01 -0400 Subject: Started adding support for promises --- src/fields/Document.ts | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) (limited to 'src/fields') diff --git a/src/fields/Document.ts b/src/fields/Document.ts index b6439364a..66d3d3aef 100644 --- a/src/fields/Document.ts +++ b/src/fields/Document.ts @@ -124,22 +124,31 @@ export class Document extends Field { * Note: The callback will not be called if there is no associated field. * @returns `true` if the field exists on the document and `callback` will be called, and `false` otherwise */ - GetAsync(key: Key, callback: (field: Field) => void): boolean { + GetAsync(key: Key, callback: (field: Opt) => void): void { //TODO: This currently doesn't deal with prototypes let field = this.fields.get(key.Id); if (field && field.field) { callback(field.field); } else if (this._proxies.has(key.Id)) { Server.GetDocumentField(this, key, callback); - return true; + } else { + callback(undefined); } - return false; } - GetTAsync(key: Key, ctor: { new(): T }, callback: (field: Opt) => void): boolean { - return this.GetAsync(key, (field) => { - callback(Cast(field, ctor)); - }) + GetTAsync(key: Key, ctor: { new(): T }): Promise>; + GetTAsync(key: Key, ctor: { new(): T }, callback: (field: Opt) => void): void; + GetTAsync(key: Key, ctor: { new(): T }, callback?: (field: Opt) => void): Promise> | void { + let fn = (cb: (field: Opt) => void) => { + return this.GetAsync(key, (field) => { + cb(Cast(field, ctor)); + }); + } + if (callback) { + fn(callback); + } else { + return new Promise(res => fn(res)); + } } /** @@ -214,13 +223,6 @@ export class Document extends Field { return this.GetData>(key, ListField, defaultVal) } - @action - SetOnPrototype(key: Key, field: Field | undefined): void { - this.GetAsync(KeyStore.Prototype, (f: Field) => { - (f as Document).Set(key, field) - }) - } - @action Set(key: Key, field: Field | undefined, setOnPrototype = false): void { let old = this.fields.get(key.Id); @@ -248,16 +250,22 @@ export class Document extends Field { } } + @action + SetOnPrototype(key: Key, field: Field | undefined): void { + this.GetTAsync(KeyStore.Prototype, Document, (f: Opt) => { + f && f.Set(key, field) + }) + } + @action SetDataOnPrototype(key: Key, value: T, ctor: { new(): U }, replaceWrongType = true) { - this.GetAsync(KeyStore.Prototype, (f: Field) => { - (f as Document).SetData(key, value, ctor) + this.GetTAsync(KeyStore.Prototype, Document, (f: Opt) => { + f && f.SetData(key, value, ctor) }) } @action SetData(key: Key, value: T, ctor: { new(): U }, replaceWrongType = true) { - let field = this.Get(key, true); if (field instanceof ctor) { field.Data = value; @@ -294,8 +302,8 @@ export class Document extends Field { CreateAlias(id?: string): Document { let alias = new Document(id) - this.GetAsync(KeyStore.Prototype, (f: Field) => { - alias.Set(KeyStore.Prototype, f) + this.GetTAsync(KeyStore.Prototype, Document, (f: Opt) => { + f && alias.Set(KeyStore.Prototype, f) }) return alias -- cgit v1.2.3-70-g09d2