Skip to content

Commit 075bf74

Browse files
committed
feat: add setAttributes(), make update() public
1 parent e7144a6 commit 075bf74

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/lib/marker.ts

+23-15
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,11 @@ export class Marker<TUserData = unknown> {
116116
this.bindMarkerEvents();
117117

118118
// set all remaining parameters as attributes
119-
for (const [key, value] of Object.entries(attributes)) {
120-
this.setAttribute_(key as AttributeKey, value);
121-
}
119+
this.setAttributes(attributes);
122120

123121
if (map) {
124122
this.map = map;
125-
this.scheduleUpdate();
123+
this.update();
126124
}
127125
}
128126

@@ -188,7 +186,7 @@ export class Marker<TUserData = unknown> {
188186
);
189187

190188
this.onMapBoundsChange(map);
191-
this.scheduleUpdate();
189+
this.update();
192190
}
193191
}
194192

@@ -199,7 +197,18 @@ export class Marker<TUserData = unknown> {
199197
setData(data: TUserData) {
200198
this.data_ = data;
201199

202-
this.scheduleUpdate();
200+
this.update();
201+
}
202+
203+
/**
204+
* Sets multiple attributes at once.
205+
* @param attributes
206+
*/
207+
setAttributes(attributes: Partial<Attributes<TUserData>>) {
208+
// set all remaining parameters as attributes
209+
for (const [key, value] of Object.entries(attributes)) {
210+
this.setAttribute_(key as AttributeKey, value);
211+
}
203212
}
204213

205214
/**
@@ -213,7 +222,7 @@ export class Marker<TUserData = unknown> {
213222
TValue extends Attributes<TUserData>[TKey]
214223
>(name: TKey, value: TValue) {
215224
// update the marker when we're done
216-
this.scheduleUpdate();
225+
this.update();
217226

218227
if (typeof value === 'function') {
219228
this.dynamicAttributes_[name] =
@@ -242,15 +251,14 @@ export class Marker<TUserData = unknown> {
242251
/**
243252
* Schedules an update via microtask. This makes sure that we won't
244253
* run multiple updates when multiple attributes are changed sequentially.
245-
* @internal
246254
*/
247-
private scheduleUpdate() {
255+
update() {
248256
if (this.updateScheduled_) return;
249257

250258
this.updateScheduled_ = true;
251259
queueMicrotask(() => {
252260
this.updateScheduled_ = false;
253-
this.update();
261+
this.performUpdate();
254262
});
255263
}
256264

@@ -264,7 +272,7 @@ export class Marker<TUserData = unknown> {
264272
* setAttribute_ or the ComputedMarkerAttributes class
265273
* @internal
266274
*/
267-
private update() {
275+
private performUpdate() {
268276
if (!this.map || !this.mapState_) {
269277
console.warn('marker update skipped: missing map or mapState');
270278
return;
@@ -359,12 +367,12 @@ export class Marker<TUserData = unknown> {
359367

360368
this.addListener('pointerenter', () => {
361369
this.markerState_.hovered = true;
362-
this.scheduleUpdate();
370+
this.update();
363371
});
364372

365373
this.addListener('pointerleave', () => {
366374
this.markerState_.hovered = false;
367-
this.scheduleUpdate();
375+
this.update();
368376
});
369377
};
370378

@@ -393,7 +401,7 @@ export class Marker<TUserData = unknown> {
393401
tilt: map.getTilt() || 0
394402
};
395403

396-
this.scheduleUpdate();
404+
this.update();
397405
};
398406

399407
/**
@@ -572,7 +580,7 @@ export type DynamicAttributes<TUserData> = {
572580

573581
// These are the attribute-types as specified to the constructor and
574582
// individual attribute setters
575-
export type Attributes<TUserData> = {
583+
export type Attributes<TUserData = unknown> = {
576584
[key in AttributeKey]: AttributeValue<TUserData, StaticAttributes[key]>;
577585
};
578586

0 commit comments

Comments
 (0)