wip: derive the dirty status from the undoManager

This should pave the way for removing the SaveManager.

Co-authored-by: David Larlet <david@larlet.fr>
This commit is contained in:
Yohan Boniface 2025-03-20 16:51:30 +01:00
parent 4fd066387d
commit dfdfae0080
7 changed files with 123 additions and 80 deletions

View file

@ -19,7 +19,7 @@
.leaflet-container .edit-disable, .leaflet-container .edit-disable,
.leaflet-container .connected-peers .leaflet-container .connected-peers
{ {
display: block; display: inline-block;
height: 32px; height: 32px;
line-height: 30px; line-height: 30px;
padding: 0 20px; padding: 0 20px;
@ -78,21 +78,13 @@
background: rgba(66, 236, 230, 0.10); background: rgba(66, 236, 230, 0.10);
} }
.leaflet-container .edit-save, .leaflet-container .edit-save,
.leaflet-container .edit-undo,
.leaflet-container .edit-redo,
.leaflet-container .edit-disable,
.umap-edit-enabled .edit-enable { .umap-edit-enabled .edit-enable {
display: none; display: none;
} }
.umap-edit-enabled .edit-save, .umap-edit-enabled .edit-save,
.umap-edit-enabled .edit-disable, .umap-edit-enabled .edit-disable {
.umap-edit-enabled.umap-is-dirty .edit-undo,
.umap-edit-enabled.umap-is-dirty .edit-redo {
display: inline-block; display: inline-block;
} }
.umap-is-dirty .edit-disable {
display: none;
}
.umap-caption-bar { .umap-caption-bar {
display: none; display: none;
} }

View file

@ -31,7 +31,7 @@ function has(obj) {
function onUpdate() { function onUpdate() {
isDirty = Boolean(_queue.size) isDirty = Boolean(_queue.size)
document.body.classList.toggle('umap-is-dirty', isDirty) // document.body.classList.toggle('umap-is-dirty', isDirty)
} }
export class ServerStored { export class ServerStored {

View file

@ -138,69 +138,90 @@ export class SyncEngine {
verb: 'batch', verb: 'batch',
operations: this._batch, operations: this._batch,
}) })
const syncOperations = this._batch.map((operation) => const operations = this._batch.map((stage) => stage.operation)
this.convertToSyncOperation(operation) this._send({ verb: 'batch', operations: operations, subject: 'batch' })
)
this._send({ verb: 'batch', operations: syncOperations, subject: 'batch' })
this._batch = null this._batch = null
} }
convertToSyncOperation(undoOperation) {
const syncOperation = { ...undoOperation, value: undoOperation.newValue }
delete syncOperation.oldValue
delete syncOperation.newValue
return syncOperation
}
upsert(subject, metadata, value, oldValue) { upsert(subject, metadata, value, oldValue) {
const undoOperation = { const operation = {
verb: 'upsert', verb: 'upsert',
subject, subject,
metadata, metadata,
value,
}
const stage = {
operation,
newValue: value, newValue: value,
oldValue: oldValue, oldValue: oldValue,
} }
if (this._batch) { if (this._batch) {
this._batch.push(undoOperation) this._batch.push(stage)
return return
} }
this._undoManager.add(undoOperation) this._undoManager.add(stage)
const syncOperation = this.convertToSyncOperation(undoOperation) this._send(operation)
this._send(syncOperation)
} }
update(subject, metadata, key, value, oldValue) { update(subject, metadata, key, value, oldValue) {
const undoOperation = { const operation = {
verb: 'update', verb: 'update',
subject, subject,
metadata, metadata,
key, key,
value,
}
const stage = {
operation,
oldValue: oldValue, oldValue: oldValue,
newValue: value, newValue: value,
} }
if (this._batch) { if (this._batch) {
this._batch.push(undoOperation) this._batch.push(stage)
return return
} }
this._undoManager.add(undoOperation) this._undoManager.add(stage)
const syncOperation = this.convertToSyncOperation(undoOperation) this._send(operation)
this._send(syncOperation)
} }
delete(subject, metadata, oldValue) { delete(subject, metadata, oldValue) {
const undoOperation = { const operation = {
verb: 'delete', verb: 'delete',
subject, subject,
metadata, metadata,
}
const stage = {
operation,
oldValue: oldValue, oldValue: oldValue,
} }
if (this._batch) { if (this._batch) {
this._batch.push(undoOperation) this._batch.push(stage)
return return
} }
this._undoManager.add(undoOperation) this._undoManager.add(stage)
const syncOperation = this.convertToSyncOperation(undoOperation) this._send(operation)
this._send(syncOperation) }
async save() {
const needSave = new Map()
for (const operation of this._operations.sorted()) {
if (operation.dirty) {
const updater = this._getUpdater(operation.subject)
const obj = updater.getStoredObject(operation.metadata)
if (!needSave.has(obj)) {
needSave.set(obj, [])
}
needSave.get(obj).push(operation)
}
}
for (const [obj, operations] of needSave.entries()) {
await obj.save()
for (const operation of operations) {
operation.dirty = false
}
}
this.saved()
this._undoManager.toggleState()
} }
saved() { saved() {
@ -213,8 +234,8 @@ export class SyncEngine {
} }
} }
_send(inputMessage) { _send(operation) {
const message = this._operations.addLocal(inputMessage) const message = this._operations.addLocal(operation)
if (this.offline) return if (this.offline) return
if (this.transport) { if (this.transport) {
@ -377,9 +398,7 @@ export class SyncEngine {
onSavedMessage({ sender, lastKnownHLC }) { onSavedMessage({ sender, lastKnownHLC }) {
debug(`received saved message from peer ${sender}`, lastKnownHLC) debug(`received saved message from peer ${sender}`, lastKnownHLC)
if (lastKnownHLC === this._operations.getLastKnownHLC() && SaveManager.isDirty) { this._operations.saved(lastKnownHLC)
SaveManager.clear()
}
} }
/** /**
@ -451,16 +470,22 @@ export class Operations {
this._operations = new Array() this._operations = new Array()
} }
saved(hlc) {
for (const operation of this.getOperationsSince(hlc)) {
operation.dirty = false
}
}
/** /**
* Tick the clock and store the passed message in the operations list. * Tick the clock and store the passed message in the operations list.
* *
* @param {*} inputMessage * @param {*} inputMessage
* @returns {*} clock-aware message * @returns {*} clock-aware message
*/ */
addLocal(inputMessage) { addLocal(operation) {
const message = { ...inputMessage, hlc: this._hlc.tick() } operation.hlc = this._hlc.tick()
this._operations.push(message) this._operations.push(operation)
return message return operation
} }
/** /**

View file

@ -14,35 +14,53 @@ export class UndoManager {
const redoButton = document.querySelector('.edit-redo') const redoButton = document.querySelector('.edit-redo')
if (undoButton) undoButton.disabled = !this._undoStack.length if (undoButton) undoButton.disabled = !this._undoStack.length
if (redoButton) redoButton.disabled = !this._redoStack.length if (redoButton) redoButton.disabled = !this._redoStack.length
const dirty = this.isDirty()
document.body.classList.toggle('umap-is-dirty', dirty)
for (const button of document.querySelectorAll('.disabled-on-dirty')) {
button.disabled = dirty
}
} }
add(operation) { isDirty() {
this._redoStack = [] for (const stage of this._undoStack) {
this._undoStack.push(operation) if (stage.operation.dirty) return true
this.toggleState() }
for (const stage of this._redoStack) {
if (stage.operation.dirty) return true
}
return false
} }
cleanOperation(operation, redo) { add(stage) {
const syncOperation = Utils.CopyJSON(operation) // FIXME make it more generic
delete syncOperation.oldValue if (stage.operation.key !== '_referenceVersion') {
delete syncOperation.newValue stage.operation.dirty = true
syncOperation.value = redo ? operation.newValue : operation.oldValue this._redoStack = []
return syncOperation this._undoStack.push(stage)
this.toggleState()
}
}
copyOperation(stage, redo) {
const operation = Utils.CopyJSON(stage.operation)
operation.value = redo ? stage.newValue : stage.oldValue
return operation
} }
undo(redo = false) { undo(redo = false) {
const fromStack = redo ? this._redoStack : this._undoStack const fromStack = redo ? this._redoStack : this._undoStack
const toStack = redo ? this._undoStack : this._redoStack const toStack = redo ? this._undoStack : this._redoStack
const operation = fromStack.pop() const stage = fromStack.pop()
if (!operation) return if (!stage) return
if (operation.verb === 'batch') { stage.operation.dirty = !stage.operation.dirty
for (const op of operation.operations) { if (stage.operation.verb === 'batch') {
this.applyOperation(this.cleanOperation(op, redo)) for (const op of stage.operations) {
this.applyOperation(this.copyOperation(op, redo))
} }
} else { } else {
this.applyOperation(this.cleanOperation(operation, redo)) this.applyOperation(this.copyOperation(stage, redo))
} }
toStack.push(operation) toStack.push(stage)
this.toggleState() this.toggleState()
} }
@ -50,21 +68,21 @@ export class UndoManager {
this.undo(true) this.undo(true)
} }
applyOperation(syncOperation) { applyOperation(operation) {
const updater = this._getUpdater(syncOperation.subject, syncOperation.metadata) const updater = this._getUpdater(operation.subject, operation.metadata)
switch (syncOperation.verb) { switch (operation.verb) {
case 'update': case 'update':
updater.update(syncOperation) updater.update(operation)
this._syncEngine._send(syncOperation) this._syncEngine._send(operation)
break break
case 'delete': case 'delete':
case 'upsert': case 'upsert':
if (syncOperation.value === null || syncOperation.value === undefined) { if (operation.value === null || operation.value === undefined) {
updater.delete(syncOperation) updater.delete(operation)
} else { } else {
updater.upsert(syncOperation) updater.upsert(operation)
} }
this._syncEngine._send(syncOperation) this._syncEngine._send(operation)
break break
} }
} }

View file

@ -51,6 +51,10 @@ export class MapUpdater extends BaseUpdater {
this._umap.onPropertiesUpdated([key]) this._umap.onPropertiesUpdated([key])
this._umap.render([key]) this._umap.render([key])
} }
getStoredObject() {
return this._umap
}
} }
export class DataLayerUpdater extends BaseUpdater { export class DataLayerUpdater extends BaseUpdater {
@ -91,6 +95,10 @@ export class DataLayerUpdater extends BaseUpdater {
datalayer.commitDelete() datalayer.commitDelete()
} }
} }
getStoredObject(metadata) {
return this.getDataLayerFromID(metadata.id)
}
} }
export class FeatureUpdater extends BaseUpdater { export class FeatureUpdater extends BaseUpdater {
@ -101,14 +109,11 @@ export class FeatureUpdater extends BaseUpdater {
// Create or update an object at a specific position // Create or update an object at a specific position
upsert({ metadata, value }) { upsert({ metadata, value }) {
console.log('updater.upsert for', metadata, value)
const { id, layerId } = metadata const { id, layerId } = metadata
const datalayer = this.getDataLayerFromID(layerId) const datalayer = this.getDataLayerFromID(layerId)
const feature = this.getFeatureFromMetadata(metadata) const feature = this.getFeatureFromMetadata(metadata)
console.log('feature', feature)
if (feature) { if (feature) {
console.log('changing feature geometry')
feature.geometry = value.geometry feature.geometry = value.geometry
} else { } else {
datalayer.makeFeature(value, false) datalayer.makeFeature(value, false)
@ -139,4 +144,8 @@ export class FeatureUpdater extends BaseUpdater {
const feature = this.getFeatureFromMetadata(metadata) const feature = this.getFeatureFromMetadata(metadata)
if (feature) feature.del(false) if (feature) feature.del(false)
} }
getStoredObject(metadata) {
return this.getDataLayerFromID(metadata.layerId)
}
} }

View file

@ -30,7 +30,7 @@ const TOP_BAR_TEMPLATE = `
<i class="icon icon-16 icon-redo"></i> <i class="icon icon-16 icon-redo"></i>
<span class="">${translate('Redo')}</span> <span class="">${translate('Redo')}</span>
</button> </button>
<button class="edit-disable round" type="button" data-ref="view"> <button class="edit-disable round disabled-on-dirty" type="button" data-ref="view">
<i class="icon icon-16 icon-eye"></i> <i class="icon icon-16 icon-eye"></i>
<span class="">${translate('View')}</span> <span class="">${translate('View')}</span>
</button> </button>
@ -159,7 +159,7 @@ export class TopBar extends WithTemplate {
redraw() { redraw() {
const syncEnabled = this._umap.getProperty('syncEnabled') const syncEnabled = this._umap.getProperty('syncEnabled')
this.elements.peers.hidden = !syncEnabled this.elements.peers.hidden = !syncEnabled
// this.elements.cancel.hidden = syncEnabled this.elements.view.disabled = this._umap.sync._undoManager.isDirty()
this.elements.saveLabel.hidden = this._umap.permissions.isDraft() this.elements.saveLabel.hidden = this._umap.permissions.isDraft()
this.elements.saveDraftLabel.hidden = !this._umap.permissions.isDraft() this.elements.saveDraftLabel.hidden = !this._umap.permissions.isDraft()
} }

View file

@ -670,10 +670,10 @@ export default class Umap extends ServerStored {
} }
async saveAll() { async saveAll() {
if (!SAVEMANAGER.isDirty) return // if (!SAVEMANAGER.isDirty) return
if (this._defaultExtent) this._setCenterAndZoom() if (this._defaultExtent) this._setCenterAndZoom()
this.backup() this.backup()
await SAVEMANAGER.save() await this.sync.save()
// Do a blind render for now, as we are not sure what could // Do a blind render for now, as we are not sure what could
// have changed, we'll be more subtil when we'll remove the // have changed, we'll be more subtil when we'll remove the
// save action // save action
@ -684,7 +684,6 @@ export default class Umap extends ServerStored {
Alert.success(translate('Map has been saved!')) Alert.success(translate('Map has been saved!'))
}) })
} }
this.sync.saved()
this.fire('saved') this.fire('saved')
} }