diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Utils.ts | 2 | ||||
-rw-r--r-- | src/client/Server.ts | 35 | ||||
-rw-r--r-- | src/client/SocketStub.ts | 7 | ||||
-rw-r--r-- | src/client/documents/Documents.ts | 8 | ||||
-rw-r--r-- | src/client/views/Main.tsx | 122 | ||||
-rw-r--r-- | src/fields/BasicField.ts | 1 | ||||
-rw-r--r-- | src/fields/Document.ts | 14 | ||||
-rw-r--r-- | src/fields/DocumentReference.ts | 4 | ||||
-rw-r--r-- | src/fields/Field.ts | 3 | ||||
-rw-r--r-- | src/fields/ImageField.ts | 4 | ||||
-rw-r--r-- | src/fields/Key.ts | 6 | ||||
-rw-r--r-- | src/fields/ListField.ts | 5 | ||||
-rw-r--r-- | src/fields/NumberField.ts | 5 | ||||
-rw-r--r-- | src/fields/RichTextField.ts | 5 | ||||
-rw-r--r-- | src/fields/TextField.ts | 5 | ||||
-rw-r--r-- | src/server/Message.ts | 29 | ||||
-rw-r--r-- | src/server/ServerUtil.ts | 18 | ||||
-rw-r--r-- | src/server/database.ts | 40 | ||||
-rw-r--r-- | src/server/index.ts | 33 |
19 files changed, 198 insertions, 148 deletions
diff --git a/src/Utils.ts b/src/Utils.ts index f07c644b7..ce4f7ac3e 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -16,12 +16,10 @@ export class Utils { public static GenerateGuid(): string { return v4() - // return new Buffer(v4()).toString("hex").substr(0, 24); } public static GenerateDeterministicGuid(seed: string): string { return v5(seed, v5.URL) - // return new Buffer(v5(seed, v5.URL)).toString("hex").substr(0, 24); } public static GetScreenTransform(ele: HTMLElement): { scale: number, translateX: number, translateY: number } { diff --git a/src/client/Server.ts b/src/client/Server.ts index a08fc2e87..fab51ca9c 100644 --- a/src/client/Server.ts +++ b/src/client/Server.ts @@ -20,7 +20,9 @@ export class Server { this.ClientFieldsCached.set(fieldid, FieldWaiting); //simulating a server call with a registered callback action SocketStub.SEND_FIELD_REQUEST(fieldid, - action((field: Field) => callback(Server.cacheField(field)))); + action((field: Field) => { + callback(Server.cacheField(field)) + })); } else if (this.ClientFieldsCached.get(fieldid) != FieldWaiting) { callback(this.ClientFieldsCached.get(fieldid) as Field); } @@ -29,14 +31,24 @@ export class Server { static times = 0; // hack for testing public static GetDocumentField(doc: Document, key: Key) { - var hackTimeout: number = key == KeyStore.Data ? (this.times++ == 0 ? 5000 : 1000) : key == KeyStore.X ? 2500 : 500; + // let keyId: string = element[0] + // let valueId: string = element[1] + // Server.GetField(keyId, (key: Field) => { + // if (key instanceof Key) { + // Server.GetField(valueId, (field: Field) => { + // console.log(field) + // doc.Set(key as Key, field) + // }) + // } + // else { + // console.log("how did you get a key that isnt a key wtf") + // } + // }) return this.GetField(doc._proxies.get(key.Id), action((fieldfromserver: Field) => { - doc._proxies.delete(key.Id); doc.fields.set(key, fieldfromserver); - }) - , hackTimeout); + })); } public static AddDocument(document: Document) { @@ -48,8 +60,19 @@ export class Server { public static DeleteDocumentField(doc: Document, key: Key) { SocketStub.SEND_DELETE_DOCUMENT_FIELD(doc, key); } + + private static lock: boolean = false; + public static UpdateField(field: Field) { - SocketStub.SEND_SET_FIELD(field); + if (this.lock) { + setTimeout(this.UpdateField, 1000, field) + } + this.lock = true + SocketStub.SEND_SET_FIELD(field, (args: any) => { + if (this.lock) { + this.lock = false + } + }); } static connected(message: string) { diff --git a/src/client/SocketStub.ts b/src/client/SocketStub.ts index 03c0131ba..136c69668 100644 --- a/src/client/SocketStub.ts +++ b/src/client/SocketStub.ts @@ -34,8 +34,7 @@ export class SocketStub { public static SEND_FIELD_REQUEST(fieldid: FIELD_ID, callback: (field: Field) => void) { if (fieldid) { - let args: GetFieldArgs = new GetFieldArgs(fieldid) - Utils.EmitCallback(Server.Socket, MessageStore.GetField, args, (field: Field) => callback(field)) + Utils.EmitCallback(Server.Socket, MessageStore.GetField, fieldid, (field: Field) => callback(field)) } } @@ -67,12 +66,12 @@ export class SocketStub { document._proxies.delete(key.Id); } - public static SEND_SET_FIELD(field: Field) { + public static SEND_SET_FIELD(field: Field, fn: (args: any) => void) { // Send a request to set the value of a field // ...SOCKET(SET_FIELD, field id, serialized field value) // Server updates the value of the field in its fieldstore - Utils.Emit(Server.Socket, MessageStore.SetField, field.ToJson()) + Utils.EmitCallback(Server.Socket, MessageStore.SetField, field.ToJson(), fn) } } diff --git a/src/client/documents/Documents.ts b/src/client/documents/Documents.ts index 460bf9b23..f362af392 100644 --- a/src/client/documents/Documents.ts +++ b/src/client/documents/Documents.ts @@ -137,9 +137,9 @@ export namespace Documents { } let collectionProto: Document; - function GetCollectionPrototype(): Document { + function GetCollectionPrototype(isMainDoc: boolean): Document { if (!collectionProto) { - collectionProto = new Document(); + collectionProto = new Document(isMainDoc ? "dash" : undefined); collectionProto.Set(KeyStore.X, new NumberField(0)); collectionProto.Set(KeyStore.Y, new NumberField(0)); collectionProto.Set(KeyStore.Scale, new NumberField(1)); @@ -153,8 +153,8 @@ export namespace Documents { return collectionProto; } - export function CollectionDocument(documents: Array<Document>, options: DocumentOptions = {}): Document { - let doc = GetCollectionPrototype().MakeDelegate(); + export function CollectionDocument(documents: Array<Document>, options: DocumentOptions = {}, isMainDoc: boolean = false): Document { + let doc = GetCollectionPrototype(isMainDoc).MakeDelegate(); setupOptions(doc, options); doc.Set(KeyStore.Data, new ListField(documents)); return doc; diff --git a/src/client/views/Main.tsx b/src/client/views/Main.tsx index 197529c91..0f06f1278 100644 --- a/src/client/views/Main.tsx +++ b/src/client/views/Main.tsx @@ -14,16 +14,21 @@ import { ContextMenu } from './ContextMenu'; import { DocumentView } from './nodes/DocumentView'; import { ImageField } from '../../fields/ImageField'; import { CompileScript } from './../util/Scripting'; +import { Server } from '../Server'; +import { Utils } from '../../Utils'; +import { ServerUtils } from '../../server/ServerUtil'; +import { MessageStore, DocumentTransfer } from '../../server/Message'; +import { Database } from '../../server/database'; configure({ enforceActions: "observed" }); -const mainNodeCollection = new Array<Document>(); -let mainContainer = Documents.DockDocument(mainNodeCollection, { - x: 0, y: 0, title: "main container" -}) +// const mainNodeCollection = new Array<Document>(); +// let mainContainer = Documents.DockDocument(mainNodeCollection, { +// x: 0, y: 0, title: "main container" +// }) window.addEventListener("drop", function (e) { e.preventDefault(); @@ -54,11 +59,68 @@ document.addEventListener("pointerdown", action(function (e: PointerEvent) { // schemaDocs[4].SetData(KS.Author, "Bob", TextField); // schemaDocs.push(doc2); // const doc7 = Documents.SchemaDocument(schemaDocs) -const docset: Document[] = []; -let doc4 = Documents.CollectionDocument(docset, { - x: 0, y: 400, title: "mini collection" -}); -mainContainer = doc4; + +Utils.EmitCallback(Server.Socket, MessageStore.GetField, "dash", (res: any) => { + console.log("HELLO WORLD") + console.log("RESPONSE: " + res) + let mainContainer: Document = new Document(); + if (res) { + let obj = ServerUtils.FromJson(res) as Document + mainContainer = obj + console.log(mainContainer) + } + else { + const docset: Document[] = []; + let doc4 = Documents.CollectionDocument(docset, { + x: 0, y: 400, title: "mini collection" + }, true); + mainContainer = doc4; + let args = new DocumentTransfer(mainContainer.ToJson()) + Utils.Emit(Server.Socket, MessageStore.AddDocument, args) + } + + let addImageNode = action(() => { + mainContainer.GetList<Document>(KeyStore.Data, []).push(Documents.ImageDocument("https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg", { + x: 0, y: 300, width: 200, height: 200, title: "added note" + })); + }) + let addTextNode = action(() => { + mainContainer.GetList<Document>(KeyStore.Data, []).push(Documents.TextDocument({ + x: 0, y: 300, width: 200, height: 200, title: "added note" + })); + }) + let addColNode = action(() => { + mainContainer.GetList<Document>(KeyStore.Data, []).push(Documents.CollectionDocument([], { + x: 0, y: 300, width: 200, height: 200, title: "added note" + })); + }) + + ReactDOM.render(( + <div style={{ position: "absolute", width: "100%", height: "100%" }}> + <DocumentView Document={mainContainer} ContainingCollectionView={undefined} DocumentView={undefined} /> + <DocumentDecorations /> + <ContextMenu /> + <button style={{ + position: 'absolute', + bottom: '0px', + left: '0px', + width: '150px' + }} onClick={addImageNode}>Add Image</button> + <button style={{ + position: 'absolute', + bottom: '25px', + left: '0px', + width: '150px' + }} onClick={addTextNode}>Add Text</button> + <button style={{ + position: 'absolute', + bottom: '50px', + left: '0px', + width: '150px' + }} onClick={addColNode}>Add Collection</button> + </div>), + document.getElementById('root')); +}) // let doc5 = Documents.ImageDocument("https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg", { // x: 650, y: 500, width: 600, height: 600, title: "cat 2" // }); @@ -78,45 +140,3 @@ mainContainer = doc4; // mainContainer.Set(KeyStore.Data, mainNodes); //} //); - -let addImageNode = action(() => { - doc4.GetList<Document>(KeyStore.Data, []).push(Documents.ImageDocument("https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg", { - x: 0, y: 300, width: 200, height: 200, title: "added note" - })); -}) -let addTextNode = action(() => { - doc4.GetList<Document>(KeyStore.Data, []).push(Documents.TextDocument({ - x: 0, y: 300, width: 200, height: 200, title: "added note" - })); -}) -let addColNode = action(() => { - doc4.GetList<Document>(KeyStore.Data, []).push(Documents.CollectionDocument([], { - x: 0, y: 300, width: 200, height: 200, title: "added note" - })); -}) - -ReactDOM.render(( - <div style={{ position: "absolute", width: "100%", height: "100%" }}> - <DocumentView Document={mainContainer} ContainingCollectionView={undefined} DocumentView={undefined} /> - <DocumentDecorations /> - <ContextMenu /> - <button style={{ - position: 'absolute', - bottom: '0px', - left: '0px', - width: '150px' - }} onClick={addImageNode}>Add Image</button> - <button style={{ - position: 'absolute', - bottom: '25px', - left: '0px', - width: '150px' - }} onClick={addTextNode}>Add Text</button> - <button style={{ - position: 'absolute', - bottom: '50px', - left: '0px', - width: '150px' - }} onClick={addColNode}>Add Collection</button> - </div>), - document.getElementById('root'));
\ No newline at end of file diff --git a/src/fields/BasicField.ts b/src/fields/BasicField.ts index 9476f5d21..4b68ba01f 100644 --- a/src/fields/BasicField.ts +++ b/src/fields/BasicField.ts @@ -7,6 +7,7 @@ export abstract class BasicField<T> extends Field { super(id); this.data = data; + Server.UpdateField(this) } @observable diff --git a/src/fields/Document.ts b/src/fields/Document.ts index 56ac3c471..cb4f6f25c 100644 --- a/src/fields/Document.ts +++ b/src/fields/Document.ts @@ -8,12 +8,17 @@ import { findDOMNode } from "react-dom"; import { Server } from "../client/Server"; import { Types } from "../server/Message"; import { ObjectID } from "bson"; -import { Utils } from "../Utils"; export class Document extends Field { public fields: ObservableMap<Key, Opt<Field>> = new ObservableMap(); public _proxies: ObservableMap<string, FIELD_ID> = new ObservableMap(); + constructor(id?: string) { + super(id) + + Server.UpdateField(this) + } + @computed public get Title() { return this.GetText(KeyStore.Title, "<untitled>"); @@ -89,9 +94,11 @@ export class Document extends Field { Set(key: Key, field: Field | undefined): void { if (field) { this.fields.set(key, field); + this._proxies.set(key.Id, field.Id) // Server.AddDocumentField(this, key, field); } else { this.fields.delete(key); + this._proxies.delete(key.Id) // Server.DeleteDocumentField(this, key); } Server.UpdateField(this); @@ -159,13 +166,15 @@ export class Document extends Field { throw new Error("Method not implemented."); } - ToJson(): { type: Types, data: [string, string][], _id: ObjectID } { + ToJson(): { type: Types, data: [string, string][], _id: string } { + console.log(this.fields) let fields: [string, string][] = [] this._proxies.forEach((field, key) => { if (field) { fields.push([key, field as string]) } }); + console.log(fields) return { type: Types.Document, @@ -173,5 +182,4 @@ export class Document extends Field { _id: this.Id } } - }
\ No newline at end of file diff --git a/src/fields/DocumentReference.ts b/src/fields/DocumentReference.ts index b3d8e059d..b1edd1dff 100644 --- a/src/fields/DocumentReference.ts +++ b/src/fields/DocumentReference.ts @@ -43,11 +43,11 @@ export class DocumentReference extends Field { return ""; } - ToJson(): { type: Types, data: FIELD_ID, _id: ObjectID } { + ToJson(): { type: Types, data: FIELD_ID, _id: string } { return { type: Types.DocumentReference, data: this.document.Id, - _id: new ObjectID(this.Id) + _id: this.Id } } }
\ No newline at end of file diff --git a/src/fields/Field.ts b/src/fields/Field.ts index 4b9d996ac..5a65e35b9 100644 --- a/src/fields/Field.ts +++ b/src/fields/Field.ts @@ -9,7 +9,6 @@ import { ImageField } from "./ImageField"; import { ListField } from "./ListField"; import { Document } from "./Document"; import { Server } from "../client/Server"; -import { ObjectID } from "bson"; export function Cast<T extends Field>(field: FieldValue<Field>, ctor: { new(): T }): Opt<T> { if (field) { @@ -65,5 +64,5 @@ export abstract class Field { abstract Copy(): Field; - abstract ToJson(): { _id: ObjectID, type: Types, data: any } + abstract ToJson(): { _id: string, type: Types, data: any } }
\ No newline at end of file diff --git a/src/fields/ImageField.ts b/src/fields/ImageField.ts index 30fe0970b..12503b2ec 100644 --- a/src/fields/ImageField.ts +++ b/src/fields/ImageField.ts @@ -20,11 +20,11 @@ export class ImageField extends BasicField<URL> { return new ImageField(this.Data); } - ToJson(): { type: Types, data: URL, _id: ObjectID } { + ToJson(): { type: Types, data: URL, _id: string } { return { type: Types.Image, data: this.Data, - _id: new ObjectID(this.Id) + _id: this.Id } } }
\ No newline at end of file diff --git a/src/fields/Key.ts b/src/fields/Key.ts index ffcbfc8b4..1e878a361 100644 --- a/src/fields/Key.ts +++ b/src/fields/Key.ts @@ -3,6 +3,7 @@ import { Utils } from "../Utils"; import { observable } from "mobx"; import { Types } from "../server/Message"; import { ObjectID } from "bson"; +import { Server } from "../client/Server"; export class Key extends Field { private name: string; @@ -15,6 +16,7 @@ export class Key extends Field { super(id || Utils.GenerateDeterministicGuid(name)); this.name = name; + Server.UpdateField(this) } TrySetValue(value: any): boolean { @@ -33,11 +35,11 @@ export class Key extends Field { return name; } - ToJson(): { type: Types, data: string, _id: ObjectID } { + ToJson(): { type: Types, data: string, _id: string } { return { type: Types.Key, data: this.name, - _id: new ObjectID(this.Id) + _id: this.Id } } } diff --git a/src/fields/ListField.ts b/src/fields/ListField.ts index e98ced902..cf8a1ba8b 100644 --- a/src/fields/ListField.ts +++ b/src/fields/ListField.ts @@ -1,7 +1,6 @@ import { Field, FIELD_ID } from "./Field"; import { BasicField } from "./BasicField"; import { Types } from "../server/Message"; -import { ObjectId } from "bson"; export class ListField<T extends Field> extends BasicField<T[]> { constructor(data: T[] = [], id: FIELD_ID = undefined) { @@ -16,11 +15,11 @@ export class ListField<T extends Field> extends BasicField<T[]> { return new ListField<T>(this.Data); } - ToJson(): { type: Types, data: T[], _id: ObjectId } { + ToJson(): { type: Types, data: T[], _id: string } { return { type: Types.List, data: this.Data, - _id: new ObjectId(this.Id) + _id: this.Id } } }
\ No newline at end of file diff --git a/src/fields/NumberField.ts b/src/fields/NumberField.ts index ce07a18b8..7fa9ec2e4 100644 --- a/src/fields/NumberField.ts +++ b/src/fields/NumberField.ts @@ -1,7 +1,6 @@ import { BasicField } from "./BasicField" import { Types } from "../server/Message"; import { FIELD_ID } from "./Field"; -import { ObjectID } from "bson"; export class NumberField extends BasicField<number> { constructor(data: number = 0, id: FIELD_ID = undefined) { @@ -16,9 +15,9 @@ export class NumberField extends BasicField<number> { return new NumberField(this.Data); } - ToJson(): { _id: ObjectID, type: Types, data: number } { + ToJson(): { _id: string, type: Types, data: number } { return { - _id: new ObjectID(this.Id), + _id: this.Id, type: Types.Number, data: this.Data } diff --git a/src/fields/RichTextField.ts b/src/fields/RichTextField.ts index a7ea1f2ce..3c6151009 100644 --- a/src/fields/RichTextField.ts +++ b/src/fields/RichTextField.ts @@ -1,7 +1,6 @@ import { BasicField } from "./BasicField"; import { Types } from "../server/Message"; import { FIELD_ID } from "./Field"; -import { ObjectID } from "bson"; export class RichTextField extends BasicField<string> { constructor(data: string = "", id: FIELD_ID = undefined) { @@ -16,11 +15,11 @@ export class RichTextField extends BasicField<string> { return new RichTextField(this.Data); } - ToJson(): { type: Types, data: string, _id: ObjectID } { + ToJson(): { type: Types, data: string, _id: string } { return { type: Types.RichText, data: this.Data, - _id: new ObjectID(this.Id) + _id: this.Id } } diff --git a/src/fields/TextField.ts b/src/fields/TextField.ts index 692b746d1..f2b277298 100644 --- a/src/fields/TextField.ts +++ b/src/fields/TextField.ts @@ -1,7 +1,6 @@ import { BasicField } from "./BasicField" import { FIELD_ID } from "./Field"; import { Types } from "../server/Message"; -import { ObjectID } from "bson"; export class TextField extends BasicField<string> { constructor(data: string = "", id: FIELD_ID = undefined) { @@ -16,11 +15,11 @@ export class TextField extends BasicField<string> { return new TextField(this.Data); } - ToJson(): { type: Types, data: string, _id: ObjectID } { + ToJson(): { type: Types, data: string, _id: string } { return { type: Types.Text, data: this.Data, - _id: new ObjectID(this.Id) + _id: this.Id } } } diff --git a/src/server/Message.ts b/src/server/Message.ts index 0391b6671..f63168223 100644 --- a/src/server/Message.ts +++ b/src/server/Message.ts @@ -52,9 +52,9 @@ export enum Types { export class DocumentTransfer implements Transferable { readonly type = Types.Document - _id: ObjectId; + _id: string - constructor(readonly obj: { type: Types, data: [string, string][], _id: ObjectId }) { + constructor(readonly obj: { type: Types, data: [string, string][], _id: string }) { this._id = obj._id } } @@ -62,56 +62,56 @@ export class DocumentTransfer implements Transferable { export class ImageTransfer implements Transferable { readonly type = Types.Image - constructor(readonly _id: ObjectId) { } + constructor(readonly _id: string) { } } export class KeyTransfer implements Transferable { name: string - readonly _id: ObjectId + readonly _id: string readonly type = Types.Key constructor(i: string, n: string) { this.name = n - this._id = new ObjectId(i) + this._id = i } } export class ListTransfer implements Transferable { type = Types.List; - constructor(readonly _id: ObjectId) { } + constructor(readonly _id: string) { } } export class NumberTransfer implements Transferable { readonly type = Types.Number - constructor(readonly value: number, readonly _id: ObjectId) { } + constructor(readonly value: number, readonly _id: string) { } } export class TextTransfer implements Transferable { value: string - readonly _id: ObjectId + readonly _id: string readonly type = Types.Text constructor(t: string, i: string) { this.value = t - this._id = new ObjectId(i) + this._id = i } } export class RichTextTransfer implements Transferable { value: string - readonly _id: ObjectId + readonly _id: string readonly type = Types.Text constructor(t: string, i: string) { this.value = t - this._id = new ObjectId(i) + this._id = i } } export interface Transferable { - readonly _id: ObjectId + readonly _id: string readonly type: Types } @@ -119,6 +119,7 @@ export namespace MessageStore { export const Foo = new Message<string>("Foo"); export const Bar = new Message<string>("Bar"); export const AddDocument = new Message<DocumentTransfer>("Add Document"); - export const SetField = new Message<{ _id: ObjectId, data: any, type: Types }>("Set Field") - export const GetField = new Message<GetFieldArgs>("Get Field") + export const SetField = new Message<{ _id: string, data: any, type: Types }>("Set Field") + export const GetField = new Message<string>("Get Field") + export const GetDocument = new Message<string>("Get Document"); }
\ No newline at end of file diff --git a/src/server/ServerUtil.ts b/src/server/ServerUtil.ts index 6757615fb..d1de71dbe 100644 --- a/src/server/ServerUtil.ts +++ b/src/server/ServerUtil.ts @@ -12,9 +12,10 @@ import { Utils } from '../Utils'; export class ServerUtils { public static FromJson(json: string): Field { - let obj = JSON.parse(json) + let obj = JSON.parse(JSON.stringify(json)) + console.log(obj) let data: any = obj.data - let id: string = obj.id + let id: string = obj._id let type: Types = obj.type if (!(data && id && type != undefined)) { @@ -40,19 +41,8 @@ export class ServerUtils { let fields: [string, string][] = data as [string, string][] fields.forEach(element => { doc._proxies.set(element[0], element[1]); - let keyId: string = element[0] - let valueId: string = element[1] - Server.GetField(keyId, (key: Field) => { - if (key instanceof Key) { - Server.GetField(valueId, (field: Field) => { - doc.Set(key as Key, field) - }) - } - else { - console.log("how did you get a key that isnt a key wtf") - } - }) }); + console.log(doc._proxies) return doc } return new TextField(data, id) diff --git a/src/server/database.ts b/src/server/database.ts index 72ddbc82c..5844a88a2 100644 --- a/src/server/database.ts +++ b/src/server/database.ts @@ -9,8 +9,8 @@ export class Database { private MongoClient = mongodb.MongoClient; private url = 'mongodb://localhost:27017/Dash'; - public update(id: mongodb.ObjectID, value: any) { - this.MongoClient.connect(this.url, (err, db) => { + public update(id: string, value: any) { + this.MongoClient.connect(this.url, { bufferMaxEntries: 1 }, (err, db) => { let collection = db.db().collection('documents'); collection.update({ _id: id }, { $set: value }); db.close(); @@ -18,7 +18,7 @@ export class Database { } public delete(id: string) { - this.MongoClient.connect(this.url, (err, db) => { + this.MongoClient.connect(this.url, { bufferMaxEntries: 1 }, (err, db) => { let collection = db.db().collection('documents'); collection.remove({ _id: id }); db.close(); @@ -26,32 +26,40 @@ export class Database { } public insert(kvpairs: any) { - this.MongoClient.connect(this.url, (err, db) => { + this.MongoClient.connect(this.url, { bufferMaxEntries: 1 }, (err, db) => { + // console.log(kvpairs) let collection = db.db().collection('documents'); - collection.insertOne(kvpairs, () => { }); + collection.insertOne(kvpairs, (err: any, res: any) => { + if (err) { + // console.log(err) + return + } + // console.log(kvpairs) + // console.log("1 document inserted") + }); db.close(); }); } - public getDocument(id: mongodb.ObjectID): string | undefined { + public getDocument(id: string, fn: (res: any) => void) { var result: JSON; - this.MongoClient.connect(this.url, (err, db) => { + this.MongoClient.connect(this.url, { + bufferMaxEntries: 1 + }, (err, db) => { if (err) { console.log(err) return undefined } let collection = db.db().collection('documents'); - collection.findOne({ _id: Utils.GenerateDeterministicGuid(id.toHexString()) }, (err: any, res: any) => result = res) - console.log(result) + collection.findOne({ _id: id }, (err: any, res: any) => { + result = res + if (!result) { + fn(undefined) + } + fn(result) + }) db.close(); - if (!result) { - console.log("not found") - return undefined - } - console.log("found") - return result; }); - return undefined } public print() { diff --git a/src/server/index.ts b/src/server/index.ts index 98d897d2f..b256b6a82 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -13,6 +13,7 @@ import { FIELD_ID, Field } from '../fields/Field'; import { Database } from './database'; import { ServerUtils } from './ServerUtil'; import { ObjectID } from 'mongodb'; +import { Document } from '../fields/Document'; const config = require('../../webpack.config') const compiler = webpack(config) const port = 1050; // default port to listen @@ -65,22 +66,26 @@ function addDocument(document: Document) { } -function setField(newValue: Transferable) { - console.log(newValue._id) - if (Database.Instance.getDocument(newValue._id)) { - Database.Instance.update(newValue._id, newValue) - } - else { - Database.Instance.insert(newValue) - } +function getField([id, callback]: [string, (result: any) => void]) { + Database.Instance.getDocument(id, (result: any) => { + if (result) { + callback(result) + } + else { + callback(undefined) + } + }) } -function getField([fieldRequest, callback]: [GetFieldArgs, (field: Field) => void]) { - let fieldid: string = fieldRequest.field - let result: string | undefined = Database.Instance.getDocument(new ObjectID(fieldid)) - if (result) { - let fromJson: Field = ServerUtils.FromJson(result) - } +function setField(newValue: Transferable) { + Database.Instance.getDocument(newValue._id, (res: any) => { + if (res) { + Database.Instance.update(newValue._id, newValue) + } + else { + Database.Instance.insert(newValue) + } + }) } server.listen(serverPort); |