diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client/util/UndoManager.ts | 4 | ||||
-rw-r--r-- | src/fields/BooleanField.ts | 2 | ||||
-rw-r--r-- | src/fields/Document.ts | 22 | ||||
-rw-r--r-- | src/server/Message.ts | 186 | ||||
-rw-r--r-- | src/server/ServerUtil.ts | 2 | ||||
-rw-r--r-- | src/server/database.ts | 42 |
6 files changed, 133 insertions, 125 deletions
diff --git a/src/client/util/UndoManager.ts b/src/client/util/UndoManager.ts index 1e5028375..bdc77f1ba 100644 --- a/src/client/util/UndoManager.ts +++ b/src/client/util/UndoManager.ts @@ -1,6 +1,7 @@ import { observable, action } from "mobx"; import 'source-map-support/register' import { Without } from "../../Utils"; +import { string } from "prop-types"; function getBatchName(target: any, key: string | symbol): string { let keyName = key.toString(); @@ -84,6 +85,9 @@ export namespace UndoManager { export function GetOpenBatches(): Without<Batch, 'end'>[] { return openBatches; } + export function TraceOpenBatches() { + console.log(`Open batches:\n\t${openBatches.map(batch => batch.batchName).join("\n\t")}\n`); + } export class Batch { private disposed: boolean = false; diff --git a/src/fields/BooleanField.ts b/src/fields/BooleanField.ts index 7378b30a1..d319b4021 100644 --- a/src/fields/BooleanField.ts +++ b/src/fields/BooleanField.ts @@ -17,7 +17,7 @@ export class BooleanField extends BasicField<boolean> { ToJson(): { type: Types; data: boolean; _id: string } { return { - type: Types.Minimized, + type: Types.Boolean, data: this.Data, _id: this.Id }; diff --git a/src/fields/Document.ts b/src/fields/Document.ts index 8eca5e30c..3f26abbda 100644 --- a/src/fields/Document.ts +++ b/src/fields/Document.ts @@ -308,24 +308,14 @@ export class Document extends Field { } @action - SetDataOnPrototype<T, U extends Field & { Data: T }>( - key: Key, - value: T, - ctor: { new(): U }, - replaceWrongType = true - ) { + SetDataOnPrototype<T, U extends Field & { Data: T }>(key: Key, value: T, ctor: { new(): U }, replaceWrongType = true) { this.GetTAsync(KeyStore.Prototype, Document, (f: Opt<Document>) => { - f && f.SetData(key, value, ctor); + f && f.SetData(key, value, ctor, replaceWrongType); }); } @action - SetData<T, U extends Field & { Data: T }>( - key: Key, - value: T, - ctor: { new(data: T): U }, - replaceWrongType = true - ) { + SetData<T, U extends Field & { Data: T }>(key: Key, value: T, ctor: { new(data: T): U }, replaceWrongType = true) { let field = this.Get(key, true); if (field instanceof ctor) { field.Data = value; @@ -386,11 +376,7 @@ export class Document extends Field { } GetValue() { return this.Title; - var title = - (this._proxies.has(KeyStore.Title.Id) ? "???" : this.Title) + - "(" + - this.Id + - ")"; + var title = (this._proxies.has(KeyStore.Title.Id) ? "???" : this.Title) + "(" + this.Id + ")"; return title; //throw new Error("Method not implemented."); } diff --git a/src/server/Message.ts b/src/server/Message.ts index 29df57419..0274609bb 100644 --- a/src/server/Message.ts +++ b/src/server/Message.ts @@ -1,145 +1,145 @@ import { Utils } from "../Utils"; export class Message<T> { - private name: string; - private guid: string; + private name: string; + private guid: string; - get Name(): string { - return this.name; - } + get Name(): string { + return this.name; + } - get Message(): string { - return this.guid; - } + get Message(): string { + return this.guid; + } - constructor(name: string) { - this.name = name; - this.guid = Utils.GenerateDeterministicGuid(name); - } + constructor(name: string) { + this.name = name; + this.guid = Utils.GenerateDeterministicGuid(name); + } - GetValue() { - return this.Name; - } + GetValue() { + return this.Name; + } } class TestMessageArgs { - hello: string = ""; + hello: string = ""; } export class SetFieldArgs { - field: string; - value: any; + field: string; + value: any; - constructor(f: string, v: any) { - this.field = f; - this.value = v; - } + constructor(f: string, v: any) { + this.field = f; + this.value = v; + } } export class GetFieldArgs { - field: string; + field: string; - constructor(f: string) { - this.field = f; - } + constructor(f: string) { + this.field = f; + } } export enum Types { - Number, - List, - Key, - Image, - Web, - Document, - Text, - RichText, - DocumentReference, - Html, - Video, - Audio, - Ink, - PDF, - Tuple, - HistogramOp, - Minimized + Number, + List, + Key, + Image, + Web, + Document, + Text, + RichText, + DocumentReference, + Html, + Video, + Audio, + Ink, + PDF, + Tuple, + HistogramOp, + Boolean } export class DocumentTransfer implements Transferable { - readonly type = Types.Document; - _id: string; - - constructor( - readonly obj: { type: Types; data: [string, string][]; _id: string } - ) { - this._id = obj._id; - } + readonly type = Types.Document; + _id: string; + + constructor( + readonly obj: { type: Types; data: [string, string][]; _id: string } + ) { + this._id = obj._id; + } } export class ImageTransfer implements Transferable { - readonly type = Types.Image; + readonly type = Types.Image; - constructor(readonly _id: string) {} + constructor(readonly _id: string) { } } export class KeyTransfer implements Transferable { - name: string; - readonly _id: string; - readonly type = Types.Key; - - constructor(i: string, n: string) { - this.name = n; - this._id = i; - } + name: string; + readonly _id: string; + readonly type = Types.Key; + + constructor(i: string, n: string) { + this.name = n; + this._id = i; + } } export class ListTransfer implements Transferable { - type = Types.List; + type = Types.List; - constructor(readonly _id: string) {} + constructor(readonly _id: string) { } } export class NumberTransfer implements Transferable { - readonly type = Types.Number; + readonly type = Types.Number; - constructor(readonly value: number, readonly _id: string) {} + constructor(readonly value: number, readonly _id: string) { } } export class TextTransfer implements Transferable { - value: string; - readonly _id: string; - readonly type = Types.Text; - - constructor(t: string, i: string) { - this.value = t; - this._id = i; - } + value: string; + readonly _id: string; + readonly type = Types.Text; + + constructor(t: string, i: string) { + this.value = t; + this._id = i; + } } export class RichTextTransfer implements Transferable { - value: string; - readonly _id: string; - readonly type = Types.Text; - - constructor(t: string, i: string) { - this.value = t; - this._id = i; - } + value: string; + readonly _id: string; + readonly type = Types.Text; + + constructor(t: string, i: string) { + this.value = t; + this._id = i; + } } export interface Transferable { - readonly _id: string; - readonly type: Types; + readonly _id: string; + readonly type: Types; } 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: string; data: any; type: Types }>( - "Set Field" - ); - export const GetField = new Message<string>("Get Field"); - export const GetFields = new Message<string[]>("Get Fields"); - export const GetDocument = new Message<string>("Get Document"); - export const DeleteAll = new Message<any>("Delete All"); + 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: string; data: any; type: Types }>( + "Set Field" + ); + export const GetField = new Message<string>("Get Field"); + export const GetFields = new Message<string[]>("Get Fields"); + export const GetDocument = new Message<string>("Get Document"); + export const DeleteAll = new Message<any>("Delete All"); } diff --git a/src/server/ServerUtil.ts b/src/server/ServerUtil.ts index d3409abf4..2c2bfd0c9 100644 --- a/src/server/ServerUtil.ts +++ b/src/server/ServerUtil.ts @@ -38,7 +38,7 @@ export class ServerUtils { } switch (type) { - case Types.Minimized: + case Types.Boolean: return new BooleanField(data, id, false); case Types.Number: return new NumberField(data, id, false); diff --git a/src/server/database.ts b/src/server/database.ts index 616251c72..415acc09a 100644 --- a/src/server/database.ts +++ b/src/server/database.ts @@ -12,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; + } } } |