From 6cee1e69703463cd6410d24a96c1f9eb33e996a5 Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Tue, 19 Mar 2019 20:29:51 -0400 Subject: Refactored undo stuff --- src/client/util/UndoManager.ts | 56 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 10 deletions(-) (limited to 'src/client/util/UndoManager.ts') diff --git a/src/client/util/UndoManager.ts b/src/client/util/UndoManager.ts index 46ad558f3..3cbb994b7 100644 --- a/src/client/util/UndoManager.ts +++ b/src/client/util/UndoManager.ts @@ -1,4 +1,13 @@ import { observable, action } from "mobx"; +import 'source-map-support/register' + +function getBatchName(target: any, key: string | symbol): string { + let keyName = key.toString(); + if (target && target.constructor && target.constructor.name) { + return `${target.constructor.name}.${keyName}`; + } + return keyName; +} function propertyDecorator(target: any, key: string | symbol) { Object.defineProperty(target, key, { @@ -13,11 +22,11 @@ function propertyDecorator(target: any, key: string | symbol) { writable: true, configurable: true, value: function (...args: any[]) { + let batch = UndoManager.StartBatch(getBatchName(target, key)); try { - UndoManager.StartBatch(); return value.apply(this, args); } finally { - UndoManager.EndBatch(); + batch.end(); } } }) @@ -32,11 +41,11 @@ export function undoBatch(target: any, key: string | symbol, descriptor?: TypedP const oldFunction = descriptor.value; descriptor.value = function (...args: any[]) { + let batch = UndoManager.StartBatch(getBatchName(target, key)); try { - UndoManager.StartBatch() return oldFunction.apply(this, args) } finally { - UndoManager.EndBatch() + batch.end(); } } @@ -70,26 +79,53 @@ export namespace UndoManager { return redoStack.length > 0; } - export function StartBatch(): void { + let openBatches: Batch[] = []; + export function GetOpenBatches(): { batchName: string, cancel: () => void }[] { + return openBatches; + } + export class Batch { + private disposed: boolean = false; + + constructor(readonly batchName: string) { + openBatches.push(this); + } + + private dispose = (cancel: boolean) => { + if (this.disposed) { + throw new Error("Cannot dispose an already disposed batch"); + } + this.disposed = true; + openBatches.splice(openBatches.indexOf(this)); + EndBatch(cancel); + } + + end = () => { this.dispose(false); } + cancel = () => { this.dispose(true); } + } + + export function StartBatch(batchName: string): Batch { batchCounter++; if (batchCounter > 0) { currentBatch = []; } + return new Batch(batchName); } - export const EndBatch = action(() => { + const EndBatch = action((cancel: boolean = false) => { batchCounter--; if (batchCounter === 0 && currentBatch && currentBatch.length) { - undoStack.push(currentBatch); + if (!cancel) { + undoStack.push(currentBatch); + } redoStack.length = 0; currentBatch = undefined; } }) - export function RunInBatch(fn: () => void) { - StartBatch(); + export function RunInBatch(fn: () => void, batchName: string) { + let batch = StartBatch(batchName); fn(); - EndBatch(); + batch.end(); } export const Undo = action(() => { -- cgit v1.2.3-70-g09d2 From e418b10b4e515c6ebbe0b9be64e7bfd451ddd11f Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Wed, 20 Mar 2019 05:01:42 -0400 Subject: Small tweak to UndoManager --- src/Utils.ts | 4 +++- src/client/util/UndoManager.ts | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'src/client/util/UndoManager.ts') diff --git a/src/Utils.ts b/src/Utils.ts index d4b7da52c..a4db94809 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -52,4 +52,6 @@ export class Utils { public static AddServerHandlerCallback(socket: Socket, message: Message, handler: (args: [T, (res: any) => any]) => any) { socket.on(message.Message, (arg: T, fn: (res: any) => any) => handler([arg, fn])); } -} \ No newline at end of file +} + +export type Without = Pick>; \ No newline at end of file diff --git a/src/client/util/UndoManager.ts b/src/client/util/UndoManager.ts index 3cbb994b7..6d1b2f1b8 100644 --- a/src/client/util/UndoManager.ts +++ b/src/client/util/UndoManager.ts @@ -1,5 +1,6 @@ import { observable, action } from "mobx"; import 'source-map-support/register' +import { Without } from "../../Utils"; function getBatchName(target: any, key: string | symbol): string { let keyName = key.toString(); @@ -80,7 +81,7 @@ export namespace UndoManager { } let openBatches: Batch[] = []; - export function GetOpenBatches(): { batchName: string, cancel: () => void }[] { + export function GetOpenBatches(): Without[] { return openBatches; } export class Batch { -- cgit v1.2.3-70-g09d2