aboutsummaryrefslogtreecommitdiff
path: root/src/new_fields/Doc.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/new_fields/Doc.ts')
-rw-r--r--src/new_fields/Doc.ts21
1 files changed, 13 insertions, 8 deletions
diff --git a/src/new_fields/Doc.ts b/src/new_fields/Doc.ts
index f4514c33e..b0237d04d 100644
--- a/src/new_fields/Doc.ts
+++ b/src/new_fields/Doc.ts
@@ -19,12 +19,15 @@ export namespace Field {
return field[ToScriptString]();
}
}
- export function IsField(field: any): field is Field {
+ export function IsField(field: any): field is Field;
+ export function IsField(field: any, includeUndefined: true): field is Field | undefined;
+ export function IsField(field: any, includeUndefined: boolean = false): field is Field | undefined {
return (typeof field === "string")
|| (typeof field === "number")
|| (typeof field === "boolean")
|| (field instanceof ObjectField)
- || (field instanceof RefField);
+ || (field instanceof RefField)
+ || (includeUndefined && field === undefined);
}
}
export type Field = number | string | boolean | ObjectField | RefField;
@@ -116,7 +119,6 @@ export class Doc extends RefField {
}
public [HandleUpdate](diff: any) {
- console.log(diff);
const set = diff.$set;
if (set) {
for (const key in set) {
@@ -149,6 +151,9 @@ export namespace Doc {
export function GetT<T extends Field>(doc: Doc, key: string, ctor: ToConstructor<T>, ignoreProto: boolean = false): FieldResult<T> {
return Cast(Get(doc, key, ignoreProto), ctor) as FieldResult<T>;
}
+ export function IsPrototype(doc: Doc) {
+ return GetT(doc, "isPrototype", "boolean", true);
+ }
export async function SetOnPrototype(doc: Doc, key: string, value: Field) {
const proto = Object.getOwnPropertyNames(doc).indexOf("isPrototype") === -1 ? doc.proto : doc;
@@ -180,11 +185,11 @@ export namespace Doc {
// compare whether documents or their protos match
export function AreProtosEqual(doc: Doc, other: Doc) {
- let r = (doc[Id] === other[Id]);
- let r2 = (doc.proto && doc.proto.Id === other[Id]);
- let r3 = (other.proto && other.proto.Id === doc[Id]);
- let r4 = (doc.proto && other.proto && doc.proto[Id] === other.proto[Id]);
- return r || r2 || r3 || r4 ? true : false;
+ let r = (doc === other);
+ let r2 = (doc.proto === other);
+ let r3 = (other.proto === doc);
+ let r4 = (doc.proto === other.proto);
+ return r || r2 || r3 || r4;
}
// gets the document's prototype or returns the document if it is a prototype