Skip to content

Commit

Permalink
chore(utils): use native events instead of array of callbacks for Wit…
Browse files Browse the repository at this point in the history
…hEvents

Co-authored-by: David Larlet <[email protected]>
  • Loading branch information
yohanboniface and davidbgk committed Jan 10, 2025
1 parent 0ba69e4 commit 07c29ab
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 15 deletions.
4 changes: 2 additions & 2 deletions umap/static/umap/js/modules/data/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,9 +668,9 @@ export class DataLayer extends ServerStored {
]
DomUtil.createTitle(container, translate('Layer properties'), 'icon-layers')
let builder = new MutatingForm(this, metadataFields)
builder.on('set', (helper) => {
builder.on('set', ({ detail }) => {
this._umap.onDataLayersChanged()
if (helper.field === 'options.type') {
if (detail.helper.field === 'options.type') {
this.edit()
}
})
Expand Down
2 changes: 1 addition & 1 deletion umap/static/umap/js/modules/form/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class BaseElement {

sync() {
this.set()
this.builder.fire('set', this)
this.builder.fire('set', { helper: this })
}

set() {
Expand Down
17 changes: 5 additions & 12 deletions umap/static/umap/js/modules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,24 +451,17 @@ export function eachElement(selector, callback) {

export class WithEvents {
constructor() {
this._callbacks = {}
this._target = new EventTarget()
}

on(eventType, callback) {
if (typeof callback !== 'function') return
if (this._callbacks[eventType] === undefined) {
this._callbacks[eventType] = []
}

this._callbacks[eventType].push(callback)
this._target.addEventListener(eventType, callback)
}

fire(eventType, ...args) {
if (this._callbacks[eventType] === undefined) return

for (const callback of this._callbacks[eventType]) {
callback(...args)
}
fire(eventType, detail) {
const event = new CustomEvent(eventType, { detail })
this._target.dispatchEvent(event)
}
}

Expand Down

0 comments on commit 07c29ab

Please sign in to comment.