Skip to content

Commit

Permalink
feat(sync): sync layer's properties.
Browse files Browse the repository at this point in the history
This also changes the interface between the synced classes and the sync
engine.

The sync engines only requires two methods now:

- `getSyncMetadata()` which returns all the metadata needed, including
the `engine`.
- `render()` which renders the object, updating the needed parts.
  • Loading branch information
almet committed Apr 29, 2024
1 parent 75b2b5b commit e1629ba
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 24 deletions.
5 changes: 2 additions & 3 deletions umap/static/umap/js/modules/sync/updaters.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class MapUpdater extends BaseUpdater {
update({ key, value }) {
console.log(key, value)
this.updateObjectValue(this.map, key, value)
this.map.render([key.replace('options.', '')])
this.map.render([key])
}
}

Expand All @@ -50,8 +50,7 @@ export class DatalayerUpdater extends BaseUpdater {
const datalayer = this.getLayerFromID(metadata.id)
console.log(datalayer, key, value)
this.updateObjectValue(datalayer, key, value)
const property = key.replace('options.', '')
datalayer.render([property])
datalayer.render([key])
}
}

Expand Down
6 changes: 3 additions & 3 deletions umap/static/umap/js/modules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ export function template(str, data) {
}

export function parseNaiveDate(value) {
const naive = new Date(value)
// Let's pretend naive date are UTC, and remove time…
return new Date(Date.UTC(naive.getFullYear(), naive.getMonth(), naive.getDate()))
const naive = new Date(value)
// Let's pretend naive date are UTC, and remove time…
return new Date(Date.UTC(naive.getFullYear(), naive.getMonth(), naive.getDate()))
}
18 changes: 7 additions & 11 deletions umap/static/umap/js/umap.features.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ U.FeatureMixin = {

getSyncMetadata: function () {
return {
engine: this.map.sync,
subject: 'feature',
metadata: {
id: this.id,
Expand All @@ -12,14 +13,9 @@ U.FeatureMixin = {
}
},

getSyncEngine: function () {
// FIXME use a get property / defineProperty
return this.map.sync
},

onCommit: function () {
const { subject, metadata } = this.getSyncMetadata()
this.map.sync.upsert(subject, metadata, {
const { subject, metadata, engine } = this.getSyncMetadata()
engine.upsert(subject, metadata, {
geometry: this.getGeometry(),
})
},
Expand All @@ -30,14 +26,14 @@ U.FeatureMixin = {

syncUpdatedProperties: function (properties) {
if ('latlng'.includes(properties)) {
const { subject, metadata } = this.getSyncMetadata()
this.map.sync.update(subject, metadata, 'geometry', this.getGeometry())
const { subject, metadata, engine } = this.getSyncMetadata()
engine.update(subject, metadata, 'geometry', this.getGeometry())
}
},

syncDelete: function () {
let { subject, metadata } = this.getSyncMetadata()
this.map.sync.delete(subject, metadata)
let { subject, metadata, engine } = this.getSyncMetadata()
engine.delete(subject, metadata)
},

initialize: function (map, latlng, options, id) {
Expand Down
7 changes: 4 additions & 3 deletions umap/static/umap/js/umap.forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -1118,9 +1118,10 @@ U.FormBuilder = L.FormBuilder.extend({
L.FormBuilder.prototype.setter.call(this, field, value)
this.obj.isDirty = true
if ('render' in this.obj) this.obj.render([field], this)
const syncEngine = this.obj.getSyncEngine()
const { subject, metadata } = this.obj.getSyncMetadata()
syncEngine.update(subject, metadata, field, value)
console.log('setter', field, value)
const { subject, metadata, engine } = this.obj.getSyncMetadata()
console.log('metadata', metadata)
engine.update(subject, metadata, field, value)
},

finish: function () {
Expand Down
10 changes: 10 additions & 0 deletions umap/static/umap/js/umap.layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,16 @@ U.DataLayer = L.Evented.extend({
this.on('datachanged', this.map.onDataLayersChanged, this.map)
},

getSyncMetadata: function () {
return {
engine: this.map.sync,
subject: 'datalayer',
metadata: {
id: this.umap_id, // XXX: should we specify it's the layerID here?
},
}
},

render: function (fields, builder) {
let impacts = U.Utils.getImpactsFromSchema(fields)

Expand Down
9 changes: 5 additions & 4 deletions umap/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class JoinMessage(BaseModel):


class Geometry(BaseModel):
type: Literal["Point",]
type: Literal["Point", "Polygon"]
coordinates: list


Expand All @@ -43,13 +43,15 @@ class GeometryValue(BaseModel):

# FIXME better define the different messages
# to ensure only relying valid ones.
# This would mean having different kind of validation types
# based on the kind and verb.
class OperationMessage(BaseModel):
kind: str = "operation"
verb: str = Literal["upsert", "update", "delete"]
subject: str = Literal["map", "layer", "feature"]
metadata: Optional[dict] = None
key: Optional[str] = None
value: Optional[str | bool | int | GeometryValue]
value: Optional[str | bool | int | GeometryValue] = None


async def join_and_listen(
Expand Down Expand Up @@ -90,8 +92,7 @@ async def handler(websocket):
signed = TimestampSigner().unsign_object(message.token, max_age=30)
user, map_id, permissions = signed.values()

# We trust the signed info from the server to give access
# If the user can edit the map, let her in
# Check if permissions for this map have been granted by the server
if "edit" in signed["permissions"]:
await join_and_listen(map_id, permissions, user, websocket)

Expand Down

0 comments on commit e1629ba

Please sign in to comment.