Skip to content

Commit a4cea19

Browse files
committed
fix: resolve options deep copy issue in deepMerge function
1 parent 77f6f2f commit a4cea19

File tree

9 files changed

+84
-9
lines changed

9 files changed

+84
-9
lines changed

example/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# example
22

3+
## 0.4.3
4+
5+
### Patch Changes
6+
7+
- Updated dependencies
8+
- cesium-wind-layer@0.7.2
9+
310
## 0.4.2
411

512
### Patch Changes

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "example",
33
"private": true,
4-
"version": "0.4.2",
4+
"version": "0.4.3",
55
"type": "module",
66
"scripts": {
77
"dev": "vite",

packages/cesium-wind-layer/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# cesium-wind-layer
22

3+
## 0.7.2
4+
5+
### Patch Changes
6+
7+
- fix: resolve options deep copy issue in deepMerge function
8+
39
## 0.7.1
410

511
### Patch Changes

packages/cesium-wind-layer/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cesium-wind-layer",
3-
"version": "0.7.1",
3+
"version": "0.7.2",
44
"publishConfig": {
55
"access": "public"
66
},

packages/cesium-wind-layer/src/index.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99

1010
import { WindLayerOptions, WindData, WindDataAtLonLat } from './types';
1111
import { WindParticleSystem } from './windParticleSystem';
12+
import { deepMerge } from './utils';
1213

1314
export * from './types';
1415

@@ -308,13 +309,17 @@ export class WindLayer {
308309
*/
309310
updateOptions(options: Partial<WindLayerOptions>): void {
310311
if (this._isDestroyed) return;
311-
this.options = { ...this.options, ...options };
312+
this.options = deepMerge(options, this.options);
312313
this.particleSystem.changeOptions(options);
313314
this.viewer.scene.requestRender();
314315
// Dispatch options change event
315316
this.dispatchEvent('optionsChange', this.options);
316317
}
317318

319+
/**
320+
* Zoom to the wind data bounds.
321+
* @param {number} [duration=0] - The duration of the zoom animation.
322+
*/
318323
zoomTo(duration: number = 0): void {
319324
if (this.windData.bounds) {
320325
const rectangle = Rectangle.fromDegrees(
@@ -330,24 +335,37 @@ export class WindLayer {
330335
}
331336
}
332337

338+
/**
339+
* Add the wind layer to the scene.
340+
*/
333341
add(): void {
334342
this.primitives = this.particleSystem.getPrimitives();
335343
this.primitives.forEach(primitive => {
336344
this.scene.primitives.add(primitive);
337345
});
338346
}
339347

348+
/**
349+
* Remove the wind layer from the scene.
350+
*/
340351
remove(): void {
341352
this.primitives.forEach(primitive => {
342353
this.scene.primitives.remove(primitive);
343354
});
344355
this.primitives = [];
345356
}
346357

358+
/**
359+
* Check if the wind layer is destroyed.
360+
* @returns {boolean} - True if the wind layer is destroyed, otherwise false.
361+
*/
347362
isDestroyed(): boolean {
348363
return this._isDestroyed;
349364
}
350365

366+
/**
367+
* Destroy the wind layer and release all resources.
368+
*/
351369
destroy(): void {
352370
this.remove();
353371
this.removeEventListeners();
@@ -364,13 +382,23 @@ export class WindLayer {
364382
});
365383
}
366384

385+
/**
386+
* Add an event listener for the specified event type.
387+
* @param {WindLayerEventType} type - The type of event to listen for.
388+
* @param {WindLayerEventCallback} callback - The callback function to execute when the event occurs.
389+
*/
367390
addEventListener(type: WindLayerEventType, callback: WindLayerEventCallback) {
368391
if (!this.eventListeners.has(type)) {
369392
this.eventListeners.set(type, new Set());
370393
}
371394
this.eventListeners.get(type)?.add(callback);
372395
}
373396

397+
/**
398+
* Remove an event listener for the specified event type.
399+
* @param {WindLayerEventType} type - The type of event to remove.
400+
* @param {WindLayerEventCallback} callback - The callback function to remove.
401+
*/
374402
removeEventListener(type: WindLayerEventType, callback: WindLayerEventCallback) {
375403
this.eventListeners.get(type)?.delete(callback);
376404
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export function deepMerge<T extends Record<string, any>>(from: Partial<T>, to: T): T {
2+
// Handle null or undefined cases
3+
if (!from) return to;
4+
if (!to) return from as T;
5+
6+
// Create a new object to avoid modifying the original
7+
const result = { ...to };
8+
9+
for (const key in from) {
10+
if (Object.prototype.hasOwnProperty.call(from, key)) {
11+
const fromValue = from[key];
12+
const toValue = to[key];
13+
14+
// Handle array case
15+
if (Array.isArray(fromValue)) {
16+
result[key] = fromValue.slice();
17+
continue;
18+
}
19+
20+
// Handle object case
21+
if (fromValue && typeof fromValue === 'object') {
22+
result[key] = deepMerge(fromValue, toValue || {}) as T[Extract<keyof T, string>];
23+
continue;
24+
}
25+
26+
// Handle primitive values
27+
if (fromValue !== undefined) {
28+
result[key] = fromValue;
29+
}
30+
}
31+
}
32+
33+
return result;
34+
}

packages/cesium-wind-layer/src/windParticleSystem.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { WindParticlesComputing } from './windParticlesComputing';
33
import { WindParticlesRendering } from './windParticlesRendering';
44
import CustomPrimitive from './customPrimitive';
55
import { ClearCommand, Color, Pass } from 'cesium';
6+
import { deepMerge } from './utils';
67

78
export class WindParticleSystem {
89
computing: WindParticlesComputing;
@@ -50,10 +51,7 @@ export class WindParticleSystem {
5051
maxParticlesChanged = true;
5152
}
5253

53-
const newOptions = {
54-
...this.options,
55-
...options
56-
}
54+
const newOptions = deepMerge(options, this.options);
5755
if (newOptions.particlesTextureSize < 1) {
5856
throw new Error('particlesTextureSize must be greater than 0');
5957
}

packages/cesium-wind-layer/src/windParticlesComputing.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { PixelDatatype, PixelFormat, Sampler, Texture, TextureMagnificationFilte
22
import { WindLayerOptions, WindData } from './types';
33
import { ShaderManager } from './shaderManager';
44
import CustomPrimitive from './customPrimitive'
5+
import { deepMerge } from './utils';
56

67
export class WindParticlesComputing {
78
context: any;
@@ -221,7 +222,7 @@ export class WindParticlesComputing {
221222

222223
updateOptions(options: Partial<WindLayerOptions>) {
223224
const needUpdateWindTextures = options.flipY !== this.options.flipY;
224-
this.options = { ...this.options, ...options };
225+
this.options = deepMerge(options, this.options);
225226
if (needUpdateWindTextures) {
226227
this.reCreateWindTextures();
227228
}

packages/cesium-wind-layer/src/windParticlesRendering.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { WindLayerOptions } from './types';
33
import { WindParticlesComputing } from './windParticlesComputing';
44
import CustomPrimitive from './customPrimitive';
55
import { ShaderManager } from './shaderManager';
6+
import { deepMerge } from './utils';
67

78
export class WindParticlesRendering {
89
private context: any;
@@ -251,7 +252,7 @@ export class WindParticlesRendering {
251252
JSON.stringify(options.colors) !== JSON.stringify(this.options.colors);
252253

253254
// Update options first
254-
this.options = { ...this.options, ...options };
255+
this.options = deepMerge(options, this.options);
255256

256257
// Then update color table if needed
257258
if (needUpdateColorTable) {

0 commit comments

Comments
 (0)