diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client/Server.ts | 83 | ||||
-rw-r--r-- | src/client/SocketStub.ts | 7 | ||||
-rw-r--r-- | src/client/documents/Documents.ts | 2 | ||||
-rw-r--r-- | src/client/util/Scripting.ts | 2 | ||||
-rw-r--r-- | src/client/views/Main.tsx | 2 | ||||
-rw-r--r-- | src/client/views/collections/CollectionDockingView.tsx | 2 | ||||
-rw-r--r-- | src/client/views/collections/CollectionFreeFormView.tsx | 2 | ||||
-rw-r--r-- | src/client/views/collections/CollectionSchemaView.tsx | 2 | ||||
-rw-r--r-- | src/client/views/collections/CollectionViewBase.tsx | 2 | ||||
-rw-r--r-- | src/client/views/nodes/CollectionFreeFormDocumentView.tsx | 2 | ||||
-rw-r--r-- | src/client/views/nodes/DocumentView.tsx | 3 | ||||
-rw-r--r-- | src/debug/Viewer.tsx | 192 | ||||
-rw-r--r-- | src/fields/BasicField.ts | 2 | ||||
-rw-r--r-- | src/fields/Document.ts | 3 | ||||
-rw-r--r-- | src/fields/Key.ts | 23 | ||||
-rw-r--r-- | src/fields/KeyStore.ts | 24 | ||||
-rw-r--r-- | src/fields/ListField.ts | 2 | ||||
-rw-r--r-- | src/fields/NumberField.ts | 2 | ||||
-rw-r--r-- | src/server/database.ts | 61 | ||||
-rw-r--r-- | src/server/index.ts | 1 |
20 files changed, 290 insertions, 129 deletions
diff --git a/src/client/Server.ts b/src/client/Server.ts index ed69e70b7..2077c0c57 100644 --- a/src/client/Server.ts +++ b/src/client/Server.ts @@ -1,12 +1,12 @@ import { Field, FieldWaiting, FIELD_ID, FIELD_WAITING, FieldValue, Opt } from "../fields/Field" -import { Key, KeyStore } from "../fields/Key" -import { ObservableMap, action } from "mobx"; +import { Key } from "../fields/Key" +import { KeyStore } from "../fields/KeyStore" +import { ObservableMap, action, reaction, when } from "mobx"; import { Document } from "../fields/Document" import { SocketStub } from "./SocketStub"; import * as OpenSocket from 'socket.io-client'; import { Utils } from "./../Utils"; import { MessageStore, Types } from "./../server/Message"; -import { ListField } from "../fields/ListField"; export class Server { public static ClientFieldsCached: ObservableMap<FIELD_ID, Field | FIELD_WAITING> = new ObservableMap(); @@ -17,36 +17,45 @@ export class Server { // Retrieves the cached value of the field and sends a request to the server for the real value (if it's not cached). // Call this is from within a reaction and test whether the return value is FieldWaiting. // 'hackTimeout' is here temporarily for simplicity when debugging things. - public static GetField(fieldid: FIELD_ID, callback: (field: Opt<Field>) => void = (f) => { }, doc: Document, key: Key, hackTimeout: number = -1) { - if (!this.ClientFieldsCached.get(fieldid)) { - var ft = this.times++; - var title = (!doc.fields.has(KeyStore.Title.Id) ? "???" : doc.Title) + "(" + doc.Id + ")"; - var mesg = " Query> field(" + ft + ") " + title + " " + key.Name; - console.log(mesg); + public static GetField(fieldid: FIELD_ID, callback: (field: Opt<Field>) => void = (f) => { }, doc?: Document, key?: Key, hackTimeout: number = -1) { + let cached = this.ClientFieldsCached.get(fieldid); + if (!cached) { this.ClientFieldsCached.set(fieldid, FieldWaiting); - //simulating a server call with a registered callback action SocketStub.SEND_FIELD_REQUEST(fieldid, action((field: Field | undefined) => { - console.log(" Reply> field(" + ft + ") " + title + " " + key.Name + " = " + (field ? field.GetValue() : "<undefined>")); - - if (this.ClientFieldsCached.has(fieldid) && this.ClientFieldsCached.get(fieldid) != FieldWaiting) - callback(this.ClientFieldsCached.get(fieldid) as Field); + let cached = this.ClientFieldsCached.get(fieldid); + if (cached != FieldWaiting) + callback(cached); else { if (field) { this.ClientFieldsCached.set(fieldid, field); + } else { + this.ClientFieldsCached.delete(fieldid) } callback(field) } })); - } else if (this.ClientFieldsCached.get(fieldid) != FieldWaiting) { - callback(this.ClientFieldsCached.get(fieldid) as Field); + } else if (cached != FieldWaiting) { + callback(cached); + } else { + reaction(() => { + return this.ClientFieldsCached.get(fieldid); + }, (field, reaction) => { + if (field !== "<Waiting>") { + callback(field) + reaction.dispose() + } + }) } - return this.ClientFieldsCached.get(fieldid); + return cached; } public static GetFields(fieldIds: FIELD_ID[], callback: (fields: { [id: string]: Field }) => any) { SocketStub.SEND_FIELDS_REQUEST(fieldIds, (fields) => { for (let key in fields) { let field = fields[key]; + if (!this.ClientFieldsCached.has(field.Id)) { + this.ClientFieldsCached.set(field.Id, field) + } } callback(fields) }); @@ -86,44 +95,11 @@ export class Server { SocketStub.SEND_DELETE_DOCUMENT_FIELD(doc, key); } - private static lock: boolean = false; - - static printfield(field: Field) { - if (field instanceof Key) { - return field.Name; - } - else if (field instanceof Document) { - var title = (field._proxies.has(KeyStore.Title.Id) ? field.Title : "???") - return title; - } else if (field instanceof ListField) { - var str = "["; - (field as ListField<Field>).Data.map(d => str += this.printfield(d)); - str += "]"; - return str; - } - return field.GetValue() - } - public static UpdateField(field: Field) { - if (this.lock) { - // setTimeout(this.UpdateField, 1000, field) - } - this.lock = true - var type = "field" - if (field instanceof Key) { - type = "Key"; + if (!this.ClientFieldsCached.has(field.Id)) { + this.ClientFieldsCached.set(field.Id, field) } - else if (field instanceof Document) { - type = "Doc"; - } else if (field instanceof ListField) { - type = "List" - } - console.log("Set: " + type + "(" + field.Id + ") =" + this.printfield(field)); - SocketStub.SEND_SET_FIELD(field, (args: any) => { - if (this.lock) { - this.lock = false - } - }); + SocketStub.SEND_SET_FIELD(field); } static connected(message: string) { @@ -146,7 +122,6 @@ export class Server { if (Server.ClientFieldsCached.has(field._id)) { var f = Server.ClientFieldsCached.get(field._id); if (f && f != FieldWaiting) { - console.log("Update from server:" + Server.printfield(f)); f.UpdateFromServer(field.data); f.init(() => { }); } diff --git a/src/client/SocketStub.ts b/src/client/SocketStub.ts index 36818b1eb..2bddf4e97 100644 --- a/src/client/SocketStub.ts +++ b/src/client/SocketStub.ts @@ -1,5 +1,6 @@ import { Field, FIELD_ID, Opt } from "../fields/Field" -import { Key, KeyStore } from "../fields/Key" +import { Key } from "../fields/Key" +import { KeyStore } from "../fields/KeyStore" import { ObservableMap, action } from "mobx"; import { Document } from "../fields/Document" import { MessageStore, SetFieldArgs, GetFieldArgs, DocumentTransfer, Types } from "../server/Message"; @@ -84,12 +85,12 @@ export class SocketStub { document._proxies.delete(key.Id); } - public static SEND_SET_FIELD(field: Field, fn: (args: any) => void) { + public static SEND_SET_FIELD(field: Field) { // 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.EmitCallback(Server.Socket, MessageStore.SetField, field.ToJson(), fn) + Utils.Emit(Server.Socket, MessageStore.SetField, field.ToJson()) } } diff --git a/src/client/documents/Documents.ts b/src/client/documents/Documents.ts index 118f0d83f..f779dcd03 100644 --- a/src/client/documents/Documents.ts +++ b/src/client/documents/Documents.ts @@ -1,6 +1,6 @@ import { Document } from "../../fields/Document"; import { Server } from "../Server"; -import { KeyStore } from "../../fields/Key"; +import { KeyStore } from "../../fields/KeyStore"; import { TextField } from "../../fields/TextField"; import { NumberField } from "../../fields/NumberField"; import { ListField } from "../../fields/ListField"; diff --git a/src/client/util/Scripting.ts b/src/client/util/Scripting.ts index 6bc5fa412..befb9df4c 100644 --- a/src/client/util/Scripting.ts +++ b/src/client/util/Scripting.ts @@ -6,7 +6,7 @@ import { NumberField as NumberFieldImport, NumberField } from "../../fields/Numb import { ImageField as ImageFieldImport } from "../../fields/ImageField"; import { TextField as TextFieldImport, TextField } from "../../fields/TextField"; import { RichTextField as RichTextFieldImport } from "../../fields/RichTextField"; -import { KeyStore as KeyStoreImport } from "../../fields/Key"; +import { KeyStore as KeyStoreImport } from "../../fields/KeyStore"; export interface ExecutableScript { (): any; diff --git a/src/client/views/Main.tsx b/src/client/views/Main.tsx index bdefecf19..858c02eb4 100644 --- a/src/client/views/Main.tsx +++ b/src/client/views/Main.tsx @@ -5,7 +5,7 @@ import * as ReactDOM from 'react-dom'; import { DocumentDecorations } from './DocumentDecorations'; import { Documents } from '../documents/Documents'; import { Document } from '../../fields/Document'; -import { KeyStore, KeyStore as KS } from '../../fields/Key'; +import { KeyStore } from '../../fields/KeyStore'; import { ListField } from '../../fields/ListField'; import { NumberField } from '../../fields/NumberField'; import { TextField } from '../../fields/TextField'; diff --git a/src/client/views/collections/CollectionDockingView.tsx b/src/client/views/collections/CollectionDockingView.tsx index 9aee9c10f..b8e040388 100644 --- a/src/client/views/collections/CollectionDockingView.tsx +++ b/src/client/views/collections/CollectionDockingView.tsx @@ -1,5 +1,5 @@ import { observer } from "mobx-react"; -import { KeyStore } from "../../../fields/Key"; +import { KeyStore } from "../../../fields/KeyStore"; import React = require("react"); import FlexLayout from "flexlayout-react"; import { action, observable, computed } from "mobx"; diff --git a/src/client/views/collections/CollectionFreeFormView.tsx b/src/client/views/collections/CollectionFreeFormView.tsx index c7ead2f2f..e3a43aa5b 100644 --- a/src/client/views/collections/CollectionFreeFormView.tsx +++ b/src/client/views/collections/CollectionFreeFormView.tsx @@ -7,7 +7,7 @@ import "./CollectionFreeFormView.scss"; import { Utils } from "../../../Utils"; import { CollectionViewBase, CollectionViewProps, COLLECTION_BORDER_WIDTH } from "./CollectionViewBase"; import { SelectionManager } from "../../util/SelectionManager"; -import { Key, KeyStore } from "../../../fields/Key"; +import { KeyStore } from "../../../fields/KeyStore"; import { Document } from "../../../fields/Document"; import { ListField } from "../../../fields/ListField"; import { NumberField } from "../../../fields/NumberField"; diff --git a/src/client/views/collections/CollectionSchemaView.tsx b/src/client/views/collections/CollectionSchemaView.tsx index 2d5bd6c99..8a1e21847 100644 --- a/src/client/views/collections/CollectionSchemaView.tsx +++ b/src/client/views/collections/CollectionSchemaView.tsx @@ -11,7 +11,7 @@ import { CollectionViewBase } from "./CollectionViewBase"; import { DocumentView } from "../nodes/DocumentView"; import { EditableView } from "../EditableView"; import { CompileScript, ToField } from "../../util/Scripting"; -import { KeyStore as KS, Key } from "../../../fields/Key"; +import { KeyStore as KS } from "../../../fields/KeyStore"; import { Document } from "../../../fields/Document"; import { Field } from "../../../fields/Field"; diff --git a/src/client/views/collections/CollectionViewBase.tsx b/src/client/views/collections/CollectionViewBase.tsx index 09e8ec729..8bb10b743 100644 --- a/src/client/views/collections/CollectionViewBase.tsx +++ b/src/client/views/collections/CollectionViewBase.tsx @@ -2,7 +2,7 @@ import { action, computed } from "mobx"; import { observer } from "mobx-react"; import { Document } from "../../../fields/Document"; import { Opt } from "../../../fields/Field"; -import { Key, KeyStore } from "../../../fields/Key"; +import { Key } from "../../../fields/Key"; import { ListField } from "../../../fields/ListField"; import { SelectionManager } from "../../util/SelectionManager"; import { ContextMenu } from "../ContextMenu"; diff --git a/src/client/views/nodes/CollectionFreeFormDocumentView.tsx b/src/client/views/nodes/CollectionFreeFormDocumentView.tsx index 616ccea65..9e6407768 100644 --- a/src/client/views/nodes/CollectionFreeFormDocumentView.tsx +++ b/src/client/views/nodes/CollectionFreeFormDocumentView.tsx @@ -1,6 +1,6 @@ import { action, computed } from "mobx"; import { observer } from "mobx-react"; -import { Key, KeyStore } from "../../../fields/Key"; +import { KeyStore } from "../../../fields/KeyStore"; import { NumberField } from "../../../fields/NumberField"; import { DragManager } from "../../util/DragManager"; import { SelectionManager } from "../../util/SelectionManager"; diff --git a/src/client/views/nodes/DocumentView.tsx b/src/client/views/nodes/DocumentView.tsx index 3df351c6c..b219f2279 100644 --- a/src/client/views/nodes/DocumentView.tsx +++ b/src/client/views/nodes/DocumentView.tsx @@ -2,7 +2,8 @@ import { action, computed } from "mobx"; import { observer } from "mobx-react"; import { Document } from "../../../fields/Document"; import { Opt, FieldWaiting } from "../../../fields/Field"; -import { Key, KeyStore } from "../../../fields/Key"; +import { Key } from "../../../fields/Key"; +import { KeyStore } from "../../../fields/KeyStore"; import { ListField } from "../../../fields/ListField"; import { NumberField } from "../../../fields/NumberField"; import { TextField } from "../../../fields/TextField"; diff --git a/src/debug/Viewer.tsx b/src/debug/Viewer.tsx new file mode 100644 index 000000000..ddfe884ed --- /dev/null +++ b/src/debug/Viewer.tsx @@ -0,0 +1,192 @@ +import { action, configure, observable, ObservableMap, Lambda } from 'mobx'; +import "normalize.css"; +import * as React from 'react'; +import * as ReactDOM from 'react-dom'; +import { observer } from 'mobx-react'; +import { Document } from '../fields/Document'; +import { BasicField } from '../fields/BasicField'; +import { ListField } from '../fields/ListField'; +import { Key } from '../fields/Key'; +import { Field } from '../fields/Field'; +import { Server } from '../client/Server'; + +configure({ + enforceActions: "observed" +}); + +@observer +class FieldViewer extends React.Component<{ field: BasicField<any> }> { + render() { + return <span>{this.props.field.Data} ({this.props.field.Id})</span>; + } +} + +@observer +class KeyViewer extends React.Component<{ field: Key }> { + render() { + return this.props.field.Name; + } +} + +@observer +class ListViewer extends React.Component<{ field: ListField<Field> }>{ + @observable + expanded = false; + + render() { + let content; + if (this.expanded) { + content = ( + <div> + {this.props.field.Data.map(field => <DebugViewer fieldId={field.Id} key={field.Id} />)} + </div> + ) + } else { + content = <>[...] ({this.props.field.Id})</> + } + return ( + <div> + <button onClick={action(() => this.expanded = !this.expanded)}>Toggle</button> + {content} + </div > + ) + } +} + +@observer +class DocumentViewer extends React.Component<{ field: Document }> { + private keyMap: ObservableMap<string, Key> = new ObservableMap + + private disposer?: Lambda; + + componentDidMount() { + let f = () => { + Array.from(this.props.field._proxies.keys()).forEach(id => { + if (!this.keyMap.has(id)) { + Server.GetField(id, (field) => { + if (field && field instanceof Key) { + this.keyMap.set(id, field); + } + }) + } + }); + } + this.disposer = this.props.field._proxies.observe(f) + f() + } + + componentWillUnmount() { + if (this.disposer) { + this.disposer(); + } + } + + render() { + let fields = Array.from(this.props.field._proxies.entries()).map(kv => { + let key = this.keyMap.get(kv[0]); + return ( + <div key={kv[0]}> + <b>({key ? key.Name : kv[0]}): </b> + <DebugViewer fieldId={kv[1]!}></DebugViewer> + </div> + ) + }) + return ( + <div> + Document ({this.props.field.Id}) + <div style={{ paddingLeft: "25px" }}> + {fields} + </div> + </div> + ) + } +} + +@observer +class DebugViewer extends React.Component<{ fieldId: string }> { + @observable + private field?: Field; + + @observable + private error?: string; + + constructor(props: { fieldId: string }) { + super(props) + this.update() + } + + update() { + Server.GetField(this.props.fieldId, (field => { + this.field = field; + if (!field) { + this.error = `Field with id ${this.props.fieldId} not found` + } + })); + + } + + render() { + let content; + if (this.field) { + // content = this.field.ToJson(); + if (this.field instanceof ListField) { + content = (<ListViewer field={this.field} />) + } else if (this.field instanceof Document) { + content = (<DocumentViewer field={this.field} />) + } else if (this.field instanceof BasicField) { + content = (<FieldViewer field={this.field} />) + } else if (this.field instanceof Key) { + content = (<KeyViewer field={this.field} />) + } + } else if (this.error) { + content = <span>Field <b>{this.props.fieldId}</b> not found <button onClick={() => this.update()}>Refresh</button></span> + } else { + content = <>Field loading</> + } + return content; + } +} + +@observer +class Viewer extends React.Component { + @observable + private idToAdd: string = ''; + + @observable + private ids: string[] = []; + + @action + inputOnChange = (e: React.ChangeEvent<HTMLInputElement>) => { + this.idToAdd = e.target.value; + } + + @action + onKeyPress = (e: React.KeyboardEvent<HTMLDivElement>) => { + if (e.key === "Enter") { + this.ids.push(this.idToAdd) + this.idToAdd = "" + } + } + + render() { + return ( + <> + <input value={this.idToAdd} + onChange={this.inputOnChange} + onKeyDown={this.onKeyPress} /> + <div> + {this.ids.map(id => { + return <DebugViewer fieldId={id} key={id}></DebugViewer> + })} + </div> + </> + ) + } +} + +ReactDOM.render(( + <div style={{ position: "absolute", width: "100%", height: "100%" }}> + <Viewer /> + </div>), + document.getElementById('root') +);
\ No newline at end of file diff --git a/src/fields/BasicField.ts b/src/fields/BasicField.ts index 6326a90ad..15eb067a0 100644 --- a/src/fields/BasicField.ts +++ b/src/fields/BasicField.ts @@ -19,7 +19,7 @@ export abstract class BasicField<T> extends Field { } @observable - private data: T; + protected data: T; @computed get Data(): T { diff --git a/src/fields/Document.ts b/src/fields/Document.ts index 0d233c295..fce316b17 100644 --- a/src/fields/Document.ts +++ b/src/fields/Document.ts @@ -1,5 +1,6 @@ import { Field, Cast, Opt, FieldWaiting, FIELD_ID, FieldValue } from "./Field" -import { Key, KeyStore } from "./Key" +import { Key } from "./Key" +import { KeyStore } from "./KeyStore"; import { NumberField } from "./NumberField"; import { ObservableMap, computed, action, observable } from "mobx"; import { TextField } from "./TextField"; diff --git a/src/fields/Key.ts b/src/fields/Key.ts index 51d8e093c..d3958df2d 100644 --- a/src/fields/Key.ts +++ b/src/fields/Key.ts @@ -48,27 +48,4 @@ export class Key extends Field { _id: this.Id } } -} - -export namespace KeyStore { - export const Prototype = new Key("Prototype"); - export const X = new Key("X"); - export const Y = new Key("Y"); - export const Title = new Key("Title"); - export const Author = new Key("Author"); - export const PanX = new Key("PanX"); - export const PanY = new Key("PanY"); - export const Scale = new Key("Scale"); - export const Width = new Key("Width"); - export const Height = new Key("Height"); - export const ZIndex = new Key("ZIndex"); - export const Data = new Key("Data"); - export const Layout = new Key("Layout"); - export const LayoutKeys = new Key("LayoutKeys"); - export const LayoutFields = new Key("LayoutFields"); - export const ColumnsKey = new Key("SchemaColumns"); - - export function Get(name: string): Key { - return new Key(name) - } }
\ No newline at end of file diff --git a/src/fields/KeyStore.ts b/src/fields/KeyStore.ts new file mode 100644 index 000000000..1e0b12729 --- /dev/null +++ b/src/fields/KeyStore.ts @@ -0,0 +1,24 @@ +import { Key } from "./Key"; + +export namespace KeyStore { + export const Prototype = new Key("Prototype"); + export const X = new Key("X"); + export const Y = new Key("Y"); + export const Title = new Key("Title"); + export const Author = new Key("Author"); + export const PanX = new Key("PanX"); + export const PanY = new Key("PanY"); + export const Scale = new Key("Scale"); + export const Width = new Key("Width"); + export const Height = new Key("Height"); + export const ZIndex = new Key("ZIndex"); + export const Data = new Key("Data"); + export const Layout = new Key("Layout"); + export const LayoutKeys = new Key("LayoutKeys"); + export const LayoutFields = new Key("LayoutFields"); + export const ColumnsKey = new Key("SchemaColumns"); + + export function Get(name: string): Key { + return new Key(name) + } +} diff --git a/src/fields/ListField.ts b/src/fields/ListField.ts index a46ce813d..1357dc445 100644 --- a/src/fields/ListField.ts +++ b/src/fields/ListField.ts @@ -45,7 +45,7 @@ export class ListField<T extends Field> extends BasicField<T[]> { init(callback: (field: Field) => any) { Server.GetFields(this._proxies, action((fields: { [index: string]: Field }) => { if (!this.arraysEqual(this._proxies, this.Data.map(field => field.Id))) { - this.Data = this._proxies.map(id => fields[id] as T) + this.data = this._proxies.map(id => fields[id] as T) observe(this.Data, () => { this.updateProxies() Server.UpdateField(this); diff --git a/src/fields/NumberField.ts b/src/fields/NumberField.ts index 6571695de..29e285201 100644 --- a/src/fields/NumberField.ts +++ b/src/fields/NumberField.ts @@ -4,7 +4,7 @@ import { FIELD_ID } from "./Field"; export class NumberField extends BasicField<number> { constructor(data: number = 0, id: FIELD_ID = undefined, save: boolean = true) { - super(data, false, id); + super(data, save, id); } ToScriptString(): string { diff --git a/src/server/database.ts b/src/server/database.ts index 51103520e..07c5819ab 100644 --- a/src/server/database.ts +++ b/src/server/database.ts @@ -8,55 +8,53 @@ export class Database { public static Instance = new Database() private MongoClient = mongodb.MongoClient; private url = 'mongodb://localhost:27017/Dash'; + private db?: mongodb.Db; + + constructor() { + this.MongoClient.connect(this.url, (err, client) => { + this.db = client.db() + }) + } public update(id: string, value: any) { - this.MongoClient.connect(this.url, { bufferMaxEntries: 1 }, (err, db) => { - let collection = db.db().collection('documents'); + if (this.db) { + let collection = this.db.collection('documents'); collection.update({ _id: id }, { $set: value }, { upsert: true }); - db.close(); - }); + } } public delete(id: string) { - this.MongoClient.connect(this.url, { bufferMaxEntries: 1 }, (err, db) => { - let collection = db.db().collection('documents'); + if (this.db) { + let collection = this.db.collection('documents'); collection.remove({ _id: id }); - db.close(); - }); + } } public deleteAll() { - this.MongoClient.connect(this.url, (err, db) => { - let collection = db.db().collection('documents'); + if (this.db) { + let collection = this.db.collection('documents'); collection.deleteMany({}); - }) + } } public insert(kvpairs: any) { - this.MongoClient.connect(this.url, { bufferMaxEntries: 1 }, (err, db) => { - let collection = db.db().collection('documents'); + if (this.db) { + let collection = this.db.collection('documents'); collection.insertOne(kvpairs, (err: any, res: any) => { if (err) { // console.log(err) return } }); - db.close(); - }); + } } public getDocument(id: string, fn: (res: any) => void) { var result: JSON; - this.MongoClient.connect(this.url, { - bufferMaxEntries: 1 - }, (err, db) => { - if (err) { - console.log(err) - return undefined - } - let collection = db.db().collection('documents'); + if (this.db) { + let collection = this.db.collection('documents'); collection.findOne({ _id: id }, (err: any, res: any) => { result = res if (!result) { @@ -64,26 +62,17 @@ export class Database { } fn(result) }) - db.close(); - }); + }; } public getDocuments(ids: string[], fn: (res: any) => void) { - var result: JSON; - this.MongoClient.connect(this.url, { - bufferMaxEntries: 1 - }, (err, db) => { - if (err) { - console.log(err) - return undefined - } - let collection = db.db().collection('documents'); + if (this.db) { + let collection = this.db.collection('documents'); let cursor = collection.find({ _id: { "$in": ids } }) cursor.toArray((err, docs) => { fn(docs); }) - db.close(); - }); + }; } public print() { diff --git a/src/server/index.ts b/src/server/index.ts index eb26aa233..d05e1fca6 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -30,6 +30,7 @@ import c = require("crypto"); const MongoStore = require('connect-mongo')(session); const mongoose = require('mongoose'); const bluebird = require('bluebird'); +import { performance } from 'perf_hooks' const mongoUrl = 'mongodb://localhost:27017/Dash'; // mongoose.Promise = bluebird; |