diff options
author | laurawilsonri <laura_wilson@brown.edu> | 2019-04-08 19:10:03 -0400 |
---|---|---|
committer | laurawilsonri <laura_wilson@brown.edu> | 2019-04-08 19:10:03 -0400 |
commit | 74411165f21b6c616fa98e0676c6f4568c2d4564 (patch) | |
tree | 38079f721f8cdeba25e7bc0439aa5d6523927f1c /src/server/database.ts | |
parent | 5d4414e8d2a17c75c808bce9343dba85fbb32440 (diff) | |
parent | a72fcdd0ebc06a3c851007c6ed89ab13a9a0d835 (diff) |
Merge branch 'master' of https://github.com/browngraphicslab/Dash-Web into richTextEditor
Diffstat (limited to 'src/server/database.ts')
-rw-r--r-- | src/server/database.ts | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/src/server/database.ts b/src/server/database.ts index 87a0b3c70..415acc09a 100644 --- a/src/server/database.ts +++ b/src/server/database.ts @@ -1,8 +1,4 @@ -import { action, configure } from 'mobx'; import * as mongodb from 'mongodb'; -import { ObjectID } from 'mongodb'; -import { Transferable } from './Message'; -import { Utils } from '../Utils'; export class Database { public static Instance = new Database() @@ -16,21 +12,39 @@ export class Database { }) } + private currentWrites: { [_id: string]: Promise<void> } = {}; + public update(id: string, value: any, callback: () => void) { if (this.db) { let collection = this.db.collection('documents'); - collection.updateOne({ _id: id }, { $set: value }, { - upsert: true - }, (err, res) => { - if (err) { - console.log(err.message); - console.log(err.errmsg); - } - if (res) { - // console.log(JSON.stringify(res.result)); - } - callback() - }); + const prom = this.currentWrites[id]; + const run = (promise: Promise<void>, resolve?: () => void) => { + collection.updateOne({ _id: id }, { $set: value }, { + upsert: true + }, (err, res) => { + if (err) { + console.log(err.message); + console.log(err.errmsg); + } + // if (res) { + // console.log(JSON.stringify(res.result)); + // } + if (this.currentWrites[id] === promise) { + delete this.currentWrites[id] + } + if (resolve) { + resolve(); + } + callback(); + }); + } + if (prom) { + const newProm: Promise<void> = prom.then(() => run(newProm)); + this.currentWrites[id] = newProm; + } else { + const newProm: Promise<void> = new Promise<void>(res => run(newProm, res)) + this.currentWrites[id] = newProm; + } } } |