diff --git a/umap/static/umap/js/modules/saving.js b/umap/static/umap/js/modules/saving.js index c478c494..501fe19a 100644 --- a/umap/static/umap/js/modules/saving.js +++ b/umap/static/umap/js/modules/saving.js @@ -10,6 +10,11 @@ export async function save() { } } +export function clear() { + _queue.clear() + onUpdate() +} + function add(obj) { _queue.add(obj) onUpdate() diff --git a/umap/static/umap/js/modules/sync/engine.js b/umap/static/umap/js/modules/sync/engine.js index 62106352..fcb05b1d 100644 --- a/umap/static/umap/js/modules/sync/engine.js +++ b/umap/static/umap/js/modules/sync/engine.js @@ -2,6 +2,7 @@ import * as Utils from '../utils.js' import { HybridLogicalClock } from './hlc.js' import { DataLayerUpdater, FeatureUpdater, MapUpdater } from './updaters.js' import { WebSocketTransport } from './websocket.js' +import * as SaveManager from '../saving.js' // Start reconnecting after 2 seconds, then double the delay each time // maxing out at 32 seconds. @@ -125,6 +126,13 @@ export class SyncEngine { this._send({ verb: 'delete', subject, metadata, key }) } + saved() { + this.transport.send('SavedMessage', { + sender: this.peerId, + lastKnownHLC: this._operations.getLastKnownHLC(), + }) + } + _send(inputMessage) { const message = this._operations.addLocal(inputMessage) @@ -168,6 +176,8 @@ export class SyncEngine { } else if (payload.message.verb === 'ListOperationsResponse') { this.onListOperationsResponse(payload) } + } else if (kind === 'SavedMessage') { + this.onSavedMessage(payload) } else { throw new Error(`Received unknown message from the websocket server: ${kind}`) } @@ -280,6 +290,13 @@ export class SyncEngine { // Else: apply } + onSavedMessage({ sender, lastKnownHLC }) { + debug(`received saved message from peer ${sender}`, lastKnownHLC) + if (lastKnownHLC === this._operations.getLastKnownHLC() && SaveManager.isDirty) { + SaveManager.clear() + } + } + /** * Send a message to another peer (via the transport layer) * @@ -350,7 +367,7 @@ export class Operations { } /** - * Tick the clock and add store the passed message in the operations list. + * Tick the clock and store the passed message in the operations list. * * @param {*} inputMessage * @returns {*} clock-aware message diff --git a/umap/static/umap/js/modules/umap.js b/umap/static/umap/js/modules/umap.js index 740cc9b9..234409e1 100644 --- a/umap/static/umap/js/modules/umap.js +++ b/umap/static/umap/js/modules/umap.js @@ -684,6 +684,7 @@ export default class Umap extends ServerStored { Alert.success(translate('Map has been saved!')) }) } + this.sync.saved() this.fire('saved') } diff --git a/umap/sync/app.py b/umap/sync/app.py index 6ef0d72f..1d66b648 100644 --- a/umap/sync/app.py +++ b/umap/sync/app.py @@ -14,6 +14,7 @@ from .payloads import ( OperationMessage, PeerMessage, Request, + SavedMessage, ) @@ -165,6 +166,10 @@ class Peer: case OperationMessage(): await self.broadcast(text_data) + # Broadcast the new map state to connected peers + case SavedMessage(): + await self.broadcast(text_data) + # Send peer messages to the proper peer case PeerMessage(): await self.send_to(incoming.root.recipient, text_data) diff --git a/umap/sync/payloads.py b/umap/sync/payloads.py index 9ab2bf1a..f5e167e4 100644 --- a/umap/sync/payloads.py +++ b/umap/sync/payloads.py @@ -30,10 +30,17 @@ class PeerMessage(BaseModel): message: dict +class SavedMessage(BaseModel): + kind: Literal["SavedMessage"] = "SavedMessage" + lastKnownHLC: str + + class Request(RootModel): """Any message coming from the websocket should be one of these, and will be rejected otherwise.""" - root: Union[PeerMessage, OperationMessage] = Field(discriminator="kind") + root: Union[PeerMessage, OperationMessage, SavedMessage] = Field( + discriminator="kind" + ) class JoinResponse(BaseModel): diff --git a/umap/tests/integration/test_websocket_sync.py b/umap/tests/integration/test_websocket_sync.py index 13b647b0..3c42a6e1 100644 --- a/umap/tests/integration/test_websocket_sync.py +++ b/umap/tests/integration/test_websocket_sync.py @@ -557,3 +557,46 @@ def test_create_and_sync_map(new_page, asgi_live_server, tilelayer, login, user) peerA.get_by_role("button", name="Edit").click() expect(markersA).to_have_count(2) expect(markersB).to_have_count(2) + + +@pytest.mark.xdist_group(name="websockets") +def test_should_sync_saved_status(new_page, asgi_live_server, tilelayer): + map = MapFactory(name="sync", edit_status=Map.ANONYMOUS) + map.settings["properties"]["syncEnabled"] = True + map.save() + + # Create two tabs + peerA = new_page("Page A") + peerA.goto(f"{asgi_live_server.url}{map.get_absolute_url()}?edit") + peerB = new_page("Page B") + peerB.goto(f"{asgi_live_server.url}{map.get_absolute_url()}?edit") + + # Create a new marker from peerA + peerA.get_by_title("Draw a marker").click() + peerA.locator("#map").click(position={"x": 220, "y": 220}) + + # Peer A should be in dirty state + expect(peerA.locator("body")).to_have_class(re.compile(".*umap-is-dirty.*")) + + # Peer B should not be in dirty state + expect(peerB.locator("body")).not_to_have_class(re.compile(".*umap-is-dirty.*")) + + # Create a new marker from peerB + peerB.get_by_title("Draw a marker").click() + peerB.locator("#map").click(position={"x": 200, "y": 250}) + + # Peer B should be in dirty state + expect(peerB.locator("body")).to_have_class(re.compile(".*umap-is-dirty.*")) + + # Peer A should still be in dirty state + expect(peerA.locator("body")).to_have_class(re.compile(".*umap-is-dirty.*")) + + # Save layer to the server from peerA + with peerA.expect_response(re.compile(".*/datalayer/create/.*")): + peerA.get_by_role("button", name="Save").click() + + # Peer B should not be in dirty state + expect(peerB.locator("body")).not_to_have_class(re.compile(".*umap-is-dirty.*")) + + # Peer A should not be in dirty state + expect(peerA.locator("body")).not_to_have_class(re.compile(".*umap-is-dirty.*"))