Skip to content

Commit

Permalink
Fixed multiple saves on one thing
Browse files Browse the repository at this point in the history
  • Loading branch information
andev0 committed Sep 2, 2024
1 parent b782357 commit 70bf386
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 28 deletions.
14 changes: 7 additions & 7 deletions dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@
<div class="button hidden" id="cancel-linking" title="Cancel connecting nodes">
<svg class="unlink_icon-placeholder"></svg>
</div>
<div class="button" id="clear-canvas" title="Clear canvas">
<svg class="clear_icon-placeholder"></svg>
</div>
<div class="button" id="open-help" title="Help">
<svg class="question-mark_icon-placeholder"></svg>
</div>
<div class="button" id="clear-canvas" title="Clear canvas">
<svg class="clear_icon-placeholder"></svg>
</div>
<div class="button toggle off" id="grid-toggle" title="Toggle grid">
<svg class="off grid_icon-placeholder"></svg>
<svg class="on disable-grid_icon-placeholder"></svg>
Expand Down Expand Up @@ -480,10 +480,6 @@ <h2 class="title">Help <span class="background">&nbsp;&nbsp;&nbsp;&nbsp;(⩾﹏
<svg class="unlink_icon-placeholder"></svg>
<div class="text">Cancel connecting nodes</div>
</div>
<div class="option" id="clear-canvas-option">
<svg class="clear_icon-placeholder"></svg>
<div class="text">Clear canvas</div>
</div>
<div class="option" id="create-node-option">
<svg class="plus_icon-placeholder"></svg>
<div class="text">Create node</div>
Expand All @@ -508,6 +504,10 @@ <h2 class="title">Help <span class="background">&nbsp;&nbsp;&nbsp;&nbsp;(⩾﹏
<div class="text">Enable grid</div>
</div>
</div>
<div class="option" id="clear-canvas-option">
<svg class="clear_icon-placeholder"></svg>
<div class="text">Clear canvas</div>
</div>
<div class="option" id="show-help-option">
<svg class="question-mark_icon-placeholder"></svg>
<div class="text">Show help</div>
Expand Down
18 changes: 8 additions & 10 deletions src/AppData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ export class AppData extends EventTarget

let savedData = atob(decodeURI(dataEncoded));

this.isSavingEnabled = false;
this.lockSaving();

AppData.deserialize(savedData);

this.isSavingEnabled = true;
this.unlockSaving();
}

public static saveToUrl()
{
if (this.isSavingEnabled)
if (this._savingLocks === 0)
{
if (AppData.nodes.length === 0)
{
Expand All @@ -74,14 +74,14 @@ export class AppData extends EventTarget
}
}

public static get isSavingEnabled(): boolean
public static lockSaving()
{
return this._isSavingEnabled;
++this._savingLocks;
}

public static set isSavingEnabled(value: boolean)
public static unlockSaving()
{
this._isSavingEnabled = value;
--this._savingLocks;
}

private static dataFromArray(array: any[]): AppData.SerializableData
Expand Down Expand Up @@ -139,8 +139,6 @@ export class AppData extends EventTarget
{
this._nodes.at(-1)!.delete();
}

console.log("Deleting complete");
}

public static addNode(node: SankeyNode)
Expand Down Expand Up @@ -194,7 +192,7 @@ export class AppData extends EventTarget

private static _nodes: SankeyNode[] = [];

private static _isSavingEnabled = true;
private static _savingLocks = 0;
}

// Don't change positions and types of existing properties!
Expand Down
8 changes: 3 additions & 5 deletions src/Sankey/SankeyNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,23 @@ export class SankeyNode extends EventTarget

public delete()
{
AppData.lockSaving();

for (const slotsGroup of this._inputSlotGroups)
{
AppData.isSavingEnabled = false;

slotsGroup.delete();
}

for (const slotsGroup of this._outputSlotGroups)
{
AppData.isSavingEnabled = false;

slotsGroup.delete();
}

this.nodeSvgGroup.remove();

this.dispatchEvent(new Event(SankeyNode.deletionEvent));

AppData.isSavingEnabled = true;
AppData.unlockSaving();

AppData.saveToUrl();
}
Expand Down
6 changes: 3 additions & 3 deletions src/Sankey/Slots/SankeySlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ export abstract class SankeySlot extends EventTarget

public delete(): void
{
AppData.isSavingEnabled = false;
AppData.lockSaving();

this.dispatchEvent(new Event(SankeySlot.deletionEvent));

this.slotSvgRect.remove();

AppData.saveToUrl();
AppData.unlockSaving();

AppData.isSavingEnabled = true;
AppData.saveToUrl();
}

public get resourcesAmount()
Expand Down
8 changes: 5 additions & 3 deletions src/Sankey/SlotsGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,19 @@ export class SlotsGroup extends EventTarget

public delete()
{
AppData.lockSaving();

// Don't use for/for-of because of iterator invalidation after array is spliced by event.
while (this._slots.length !== 0)
{
AppData.isSavingEnabled = false;

this._slots[0].delete();
}

this._groupSvg.remove();

AppData.isSavingEnabled = true;
AppData.unlockSaving();

AppData.saveToUrl();
}

public toSerializable(): AppData.SerializableSlotsGroup
Expand Down
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ async function main()
+ "This will delete all nodes and connections.\n"
+ "Save the page link if you don't want to lose the data."))
{
AppData.lockSaving();
AppData.deleteAllNodes();
AppData.unlockSaving();

AppData.saveToUrl();
}
};
Expand Down

0 comments on commit 70bf386

Please sign in to comment.