@geospatial-sdk/core 0.0.5-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +28 -0
- package/README.md +11 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/model/index.d.ts +3 -0
- package/dist/model/index.d.ts.map +1 -0
- package/dist/model/index.js +2 -0
- package/dist/model/map-context-diff.d.ts +35 -0
- package/dist/model/map-context-diff.d.ts.map +1 -0
- package/dist/model/map-context-diff.js +1 -0
- package/dist/model/map-context.d.ts +78 -0
- package/dist/model/map-context.d.ts.map +1 -0
- package/dist/model/map-context.js +1 -0
- package/dist/utils/freeze.d.ts +2 -0
- package/dist/utils/freeze.d.ts.map +1 -0
- package/dist/utils/freeze.js +15 -0
- package/dist/utils/freeze.test.d.ts +2 -0
- package/dist/utils/freeze.test.d.ts.map +1 -0
- package/dist/utils/freeze.test.js +51 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +3 -0
- package/dist/utils/map-context-diff.d.ts +25 -0
- package/dist/utils/map-context-diff.d.ts.map +1 -0
- package/dist/utils/map-context-diff.js +111 -0
- package/dist/utils/map-context-diff.test.d.ts +2 -0
- package/dist/utils/map-context-diff.test.d.ts.map +1 -0
- package/dist/utils/map-context-diff.test.js +370 -0
- package/dist/utils/url.d.ts +7 -0
- package/dist/utils/url.d.ts.map +1 -0
- package/dist/utils/url.js +17 -0
- package/dist/utils/url.test.d.ts +2 -0
- package/dist/utils/url.test.d.ts.map +1 -0
- package/dist/utils/url.test.js +8 -0
- package/lib/index.ts +3 -0
- package/lib/model/index.ts +2 -0
- package/lib/model/map-context-diff.ts +37 -0
- package/lib/model/map-context.ts +96 -0
- package/lib/utils/freeze.test.ts +52 -0
- package/lib/utils/freeze.ts +14 -0
- package/lib/utils/index.ts +3 -0
- package/lib/utils/map-context-diff.test.ts +577 -0
- package/lib/utils/map-context-diff.ts +144 -0
- package/lib/utils/url.test.ts +14 -0
- package/lib/utils/url.ts +20 -0
- package/package.json +26 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023, Camptocamp
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AACA,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/model/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { MapContextLayer, MapContextView } from "./map-context";
|
|
2
|
+
/**
|
|
3
|
+
* Associates a position to a layer; the position is the index of
|
|
4
|
+
* the layer in the layers array
|
|
5
|
+
*/
|
|
6
|
+
export interface MapContextLayerPositioned {
|
|
7
|
+
layer: MapContextLayer;
|
|
8
|
+
position: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Describes a layer being moved to a different position
|
|
12
|
+
*/
|
|
13
|
+
export interface MapContextLayerReordered {
|
|
14
|
+
layer: MapContextLayer;
|
|
15
|
+
newPosition: number;
|
|
16
|
+
previousPosition: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Describes a delta between two contexts, in order to be
|
|
20
|
+
* applied to an existing map.
|
|
21
|
+
*
|
|
22
|
+
* For positions to be correct the order of operations should be:
|
|
23
|
+
* 1. change layers
|
|
24
|
+
* 2. remove layers
|
|
25
|
+
* 3. add layers
|
|
26
|
+
* 4. move layers
|
|
27
|
+
*/
|
|
28
|
+
export interface MapContextDiff {
|
|
29
|
+
layersChanged: MapContextLayerPositioned[];
|
|
30
|
+
layersReordered: MapContextLayerReordered[];
|
|
31
|
+
layersRemoved: MapContextLayerPositioned[];
|
|
32
|
+
layersAdded: MapContextLayerPositioned[];
|
|
33
|
+
viewChanges: MapContextView;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=map-context-diff.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"map-context-diff.d.ts","sourceRoot":"","sources":["../../lib/model/map-context-diff.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEhE;;;GAGG;AACH,MAAM,WAAW,yBAAyB;IACxC,KAAK,EAAE,eAAe,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,KAAK,EAAE,eAAe,CAAC;IACvB,WAAW,EAAE,MAAM,CAAC;IACpB,gBAAgB,EAAE,MAAM,CAAC;CAC1B;AAED;;;;;;;;;GASG;AACH,MAAM,WAAW,cAAc;IAC7B,aAAa,EAAE,yBAAyB,EAAE,CAAC;IAC3C,eAAe,EAAE,wBAAwB,EAAE,CAAC;IAC5C,aAAa,EAAE,yBAAyB,EAAE,CAAC;IAC3C,WAAW,EAAE,yBAAyB,EAAE,CAAC;IACzC,WAAW,EAAE,cAAc,CAAC;CAC7B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { FeatureCollection, Geometry } from "geojson";
|
|
2
|
+
export type LayerDimensions = Record<string, string>;
|
|
3
|
+
export type LayerExtras = Record<string, unknown>;
|
|
4
|
+
export interface MapContextBaseLayer {
|
|
5
|
+
id?: string | number;
|
|
6
|
+
version?: number;
|
|
7
|
+
/**
|
|
8
|
+
* This property can be used to store anything application-specific on layers; as its content may occasionally
|
|
9
|
+
* be serialized to JSON for change detection purposes, it is not recommended to store Functions or other
|
|
10
|
+
* non-serializable entities
|
|
11
|
+
*/
|
|
12
|
+
extras?: LayerExtras;
|
|
13
|
+
}
|
|
14
|
+
export interface MapContextLayerWms extends MapContextBaseLayer {
|
|
15
|
+
type: "wms";
|
|
16
|
+
url: string;
|
|
17
|
+
name: string;
|
|
18
|
+
dimensions?: LayerDimensions;
|
|
19
|
+
style?: string;
|
|
20
|
+
}
|
|
21
|
+
export interface MapContextLayerWmts extends MapContextBaseLayer {
|
|
22
|
+
type: "wmts";
|
|
23
|
+
url: string;
|
|
24
|
+
name: string;
|
|
25
|
+
dimensions?: LayerDimensions;
|
|
26
|
+
style?: string;
|
|
27
|
+
}
|
|
28
|
+
export interface MapContextLayerWfs extends MapContextBaseLayer {
|
|
29
|
+
type: "wfs";
|
|
30
|
+
url: string;
|
|
31
|
+
featureType: string;
|
|
32
|
+
}
|
|
33
|
+
export interface MapContextLayerXyz extends MapContextBaseLayer {
|
|
34
|
+
type: "xyz";
|
|
35
|
+
url: string;
|
|
36
|
+
}
|
|
37
|
+
interface LayerGeojson extends MapContextBaseLayer {
|
|
38
|
+
type: "geojson";
|
|
39
|
+
}
|
|
40
|
+
interface LayerGeojsonWithUrl extends LayerGeojson {
|
|
41
|
+
url: string;
|
|
42
|
+
data?: never;
|
|
43
|
+
}
|
|
44
|
+
interface LayerGeojsonWithData extends LayerGeojson {
|
|
45
|
+
data: FeatureCollection<Geometry | null> | string;
|
|
46
|
+
url?: never;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* @interface
|
|
50
|
+
*/
|
|
51
|
+
export type MapContextLayerGeojson = LayerGeojsonWithUrl | LayerGeojsonWithData;
|
|
52
|
+
/**
|
|
53
|
+
* @interface
|
|
54
|
+
*/
|
|
55
|
+
export type MapContextLayer = MapContextLayerWms | MapContextLayerWmts | MapContextLayerWfs | MapContextLayerXyz | MapContextLayerGeojson;
|
|
56
|
+
export type Coordinate = [number, number];
|
|
57
|
+
/**
|
|
58
|
+
* Min X, min Y, max X, max Y
|
|
59
|
+
*/
|
|
60
|
+
export type Extent = [number, number, number, number];
|
|
61
|
+
/**
|
|
62
|
+
* @property center Expressed in longitude/latitude
|
|
63
|
+
* @property extent Expressed in longitude/latitude
|
|
64
|
+
* @property maxExtent Expressed in longitude/latitude
|
|
65
|
+
*/
|
|
66
|
+
export interface MapContextView {
|
|
67
|
+
center?: Coordinate;
|
|
68
|
+
zoom?: number;
|
|
69
|
+
extent?: Extent;
|
|
70
|
+
maxZoom?: number;
|
|
71
|
+
maxExtent?: Extent;
|
|
72
|
+
}
|
|
73
|
+
export interface MapContext {
|
|
74
|
+
layers: MapContextLayer[];
|
|
75
|
+
view: MapContextView;
|
|
76
|
+
}
|
|
77
|
+
export {};
|
|
78
|
+
//# sourceMappingURL=map-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"map-context.d.ts","sourceRoot":"","sources":["../../lib/model/map-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEtD,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAErD,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAElD,MAAM,WAAW,mBAAmB;IAClC,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;;;OAIG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,MAAM,WAAW,kBAAmB,SAAQ,mBAAmB;IAC7D,IAAI,EAAE,KAAK,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE,eAAe,CAAC;IAC7B,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,mBAAoB,SAAQ,mBAAmB;IAC9D,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE,eAAe,CAAC;IAC7B,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,kBAAmB,SAAQ,mBAAmB;IAC7D,IAAI,EAAE,KAAK,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,kBAAmB,SAAQ,mBAAmB;IAC7D,IAAI,EAAE,KAAK,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;CACb;AAED,UAAU,YAAa,SAAQ,mBAAmB;IAChD,IAAI,EAAE,SAAS,CAAC;CACjB;AACD,UAAU,mBAAoB,SAAQ,YAAY;IAChD,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,KAAK,CAAC;CACd;AACD,UAAU,oBAAqB,SAAQ,YAAY;IACjD,IAAI,EAAE,iBAAiB,CAAC,QAAQ,GAAG,IAAI,CAAC,GAAG,MAAM,CAAC;IAClD,GAAG,CAAC,EAAE,KAAK,CAAC;CACb;AAED;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG,mBAAmB,GAAG,oBAAoB,CAAC;AAEhF;;GAEG;AACH,MAAM,MAAM,eAAe,GACvB,kBAAkB,GAClB,mBAAmB,GACnB,kBAAkB,GAClB,kBAAkB,GAClB,sBAAsB,CAAC;AAE3B,MAAM,MAAM,UAAU,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAE1C;;GAEG;AACH,MAAM,MAAM,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAEtD;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAC7B,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,eAAe,EAAE,CAAC;IAC1B,IAAI,EAAE,cAAc,CAAC;CACtB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"freeze.d.ts","sourceRoot":"","sources":["../../lib/utils/freeze.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAavC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function deepFreeze(obj) {
|
|
2
|
+
if (Array.isArray(obj)) {
|
|
3
|
+
for (const elt of obj) {
|
|
4
|
+
deepFreeze(elt);
|
|
5
|
+
}
|
|
6
|
+
return obj;
|
|
7
|
+
}
|
|
8
|
+
else if (obj instanceof Object) {
|
|
9
|
+
for (const prop in obj) {
|
|
10
|
+
deepFreeze(obj[prop]);
|
|
11
|
+
}
|
|
12
|
+
return Object.freeze(obj);
|
|
13
|
+
}
|
|
14
|
+
return obj;
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"freeze.test.d.ts","sourceRoot":"","sources":["../../lib/utils/freeze.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { deepFreeze } from "./freeze";
|
|
2
|
+
describe("freeze util", () => {
|
|
3
|
+
describe("deepFreeze", () => {
|
|
4
|
+
let obj;
|
|
5
|
+
beforeEach(() => {
|
|
6
|
+
obj = deepFreeze({
|
|
7
|
+
ab: "cde",
|
|
8
|
+
fg: {
|
|
9
|
+
h: true,
|
|
10
|
+
ij: 12,
|
|
11
|
+
},
|
|
12
|
+
k: [
|
|
13
|
+
{
|
|
14
|
+
l: 456,
|
|
15
|
+
mn: ["opqr"],
|
|
16
|
+
},
|
|
17
|
+
],
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
it("prevents mutation of root properties", () => {
|
|
21
|
+
expect(() => {
|
|
22
|
+
obj.ab = "AA";
|
|
23
|
+
}).toThrow();
|
|
24
|
+
});
|
|
25
|
+
it("prevents mutation of nested properties", () => {
|
|
26
|
+
expect(() => {
|
|
27
|
+
obj.fg.h = "AA";
|
|
28
|
+
}).toThrow();
|
|
29
|
+
});
|
|
30
|
+
it("prevents mutation of objects inside nested arrays", () => {
|
|
31
|
+
expect(() => {
|
|
32
|
+
obj.k[0].l = "AA";
|
|
33
|
+
}).toThrow();
|
|
34
|
+
});
|
|
35
|
+
describe("starting with array", () => {
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
obj = deepFreeze([
|
|
38
|
+
{
|
|
39
|
+
l: 456,
|
|
40
|
+
mn: ["opqr"],
|
|
41
|
+
},
|
|
42
|
+
]);
|
|
43
|
+
});
|
|
44
|
+
it("prevents mutation of nested objects", () => {
|
|
45
|
+
expect(() => {
|
|
46
|
+
obj[0].l = "AA";
|
|
47
|
+
}).toThrow();
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,UAAU,CAAC;AACzB,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { MapContext, MapContextDiff, MapContextLayer } from "../model";
|
|
2
|
+
export declare function getLayerHash(layer: MapContextLayer, includeExtras?: boolean): string;
|
|
3
|
+
export declare function isLayerSame(layerA: MapContextLayer, layerB: MapContextLayer): boolean;
|
|
4
|
+
export declare function isLayerSameAndUnchanged(layerA: MapContextLayer, layerB: MapContextLayer): boolean;
|
|
5
|
+
/**
|
|
6
|
+
* The following logic is produced by identifying layers in both context
|
|
7
|
+
* and determining whether they have been added, removed, changed or reordered.
|
|
8
|
+
*
|
|
9
|
+
* Identifying layers to determine if they have been added/removed/reordered is done like so:
|
|
10
|
+
* 1. For layers with an `id` property, use non-strict equality on it (e.g. '2' and 2 are equivalent);
|
|
11
|
+
* 2. For layers without `id`, compute a hash of their base properties _excluding the `extras` property_
|
|
12
|
+
*
|
|
13
|
+
* Determining whether layers have changed is done like so:
|
|
14
|
+
* 1. For layers with an `id` property, the value of the `version` field is compared;
|
|
15
|
+
* if values are different (using non-strict equality), then the layer is considered to have changed; otherwise
|
|
16
|
+
* it is considered to have remained the same
|
|
17
|
+
* 2. For layers without `id`, a full hash is computed _including the `extras` property_;
|
|
18
|
+
* this means that a layer which only had changes in its `extras` object will not be considered added/removed,
|
|
19
|
+
* but only changed
|
|
20
|
+
*
|
|
21
|
+
* @param nextContext
|
|
22
|
+
* @param previousContext
|
|
23
|
+
*/
|
|
24
|
+
export declare function computeMapContextDiff(nextContext: MapContext, previousContext: MapContext): MapContextDiff;
|
|
25
|
+
//# sourceMappingURL=map-context-diff.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"map-context-diff.d.ts","sourceRoot":"","sources":["../../lib/utils/map-context-diff.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,cAAc,EACd,eAAe,EAIhB,MAAM,UAAU,CAAC;AAElB,wBAAgB,YAAY,CAC1B,KAAK,EAAE,eAAe,EACtB,aAAa,UAAQ,GACpB,MAAM,CAkBR;AAED,wBAAgB,WAAW,CACzB,MAAM,EAAE,eAAe,EACvB,MAAM,EAAE,eAAe,GACtB,OAAO,CAKT;AAED,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,eAAe,EACvB,MAAM,EAAE,eAAe,GACtB,OAAO,CAKT;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,qBAAqB,CACnC,WAAW,EAAE,UAAU,EACvB,eAAe,EAAE,UAAU,GAC1B,cAAc,CAqEhB"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
export function getLayerHash(layer, includeExtras = false) {
|
|
2
|
+
function getHash(input) {
|
|
3
|
+
if (input instanceof Object) {
|
|
4
|
+
const obj = {};
|
|
5
|
+
const keys = Object.keys(input).sort();
|
|
6
|
+
for (const key of keys) {
|
|
7
|
+
if (!includeExtras && key === "extras")
|
|
8
|
+
continue;
|
|
9
|
+
obj[key] = getHash(input[key]);
|
|
10
|
+
}
|
|
11
|
+
const hash = JSON.stringify(obj)
|
|
12
|
+
.split("")
|
|
13
|
+
.reduce((prev, curr) => (prev << 5) - prev + curr.charCodeAt(0), 0);
|
|
14
|
+
return (hash >>> 0).toString();
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
return JSON.stringify(input);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return getHash(layer);
|
|
21
|
+
}
|
|
22
|
+
export function isLayerSame(layerA, layerB) {
|
|
23
|
+
if ("id" in layerA && "id" in layerB) {
|
|
24
|
+
return layerA.id == layerB.id;
|
|
25
|
+
}
|
|
26
|
+
return getLayerHash(layerA) === getLayerHash(layerB);
|
|
27
|
+
}
|
|
28
|
+
export function isLayerSameAndUnchanged(layerA, layerB) {
|
|
29
|
+
if ("id" in layerA && "id" in layerB) {
|
|
30
|
+
return layerA.id == layerB.id && layerA.version == layerB.version;
|
|
31
|
+
}
|
|
32
|
+
return getLayerHash(layerA, true) === getLayerHash(layerB, true);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The following logic is produced by identifying layers in both context
|
|
36
|
+
* and determining whether they have been added, removed, changed or reordered.
|
|
37
|
+
*
|
|
38
|
+
* Identifying layers to determine if they have been added/removed/reordered is done like so:
|
|
39
|
+
* 1. For layers with an `id` property, use non-strict equality on it (e.g. '2' and 2 are equivalent);
|
|
40
|
+
* 2. For layers without `id`, compute a hash of their base properties _excluding the `extras` property_
|
|
41
|
+
*
|
|
42
|
+
* Determining whether layers have changed is done like so:
|
|
43
|
+
* 1. For layers with an `id` property, the value of the `version` field is compared;
|
|
44
|
+
* if values are different (using non-strict equality), then the layer is considered to have changed; otherwise
|
|
45
|
+
* it is considered to have remained the same
|
|
46
|
+
* 2. For layers without `id`, a full hash is computed _including the `extras` property_;
|
|
47
|
+
* this means that a layer which only had changes in its `extras` object will not be considered added/removed,
|
|
48
|
+
* but only changed
|
|
49
|
+
*
|
|
50
|
+
* @param nextContext
|
|
51
|
+
* @param previousContext
|
|
52
|
+
*/
|
|
53
|
+
export function computeMapContextDiff(nextContext, previousContext) {
|
|
54
|
+
function getLayerPosition(layer, layers) {
|
|
55
|
+
for (let i = 0; i < layers.length; i++) {
|
|
56
|
+
if (isLayerSame(layers[i], layer)) {
|
|
57
|
+
return i;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return -1;
|
|
61
|
+
}
|
|
62
|
+
const layersChanged = [];
|
|
63
|
+
const layersReordered = [];
|
|
64
|
+
const layersRemoved = [];
|
|
65
|
+
const layersAdded = [];
|
|
66
|
+
const viewChanges = {};
|
|
67
|
+
// loop on prev context layers (for removed layers)
|
|
68
|
+
for (let i = 0; i < previousContext.layers.length; i++) {
|
|
69
|
+
const layer = previousContext.layers[i];
|
|
70
|
+
const nextPosition = getLayerPosition(layer, nextContext.layers);
|
|
71
|
+
const prevPosition = getLayerPosition(layer, previousContext.layers);
|
|
72
|
+
if (nextPosition === -1) {
|
|
73
|
+
layersRemoved.push({ layer, position: prevPosition });
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// loop on next context layers (for added & updated)
|
|
77
|
+
for (let i = 0; i < nextContext.layers.length; i++) {
|
|
78
|
+
const layer = nextContext.layers[i];
|
|
79
|
+
const prevPosition = getLayerPosition(layer, previousContext.layers);
|
|
80
|
+
if (prevPosition === -1) {
|
|
81
|
+
layersAdded.push({ layer, position: i });
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
const prevLayer = previousContext.layers[prevPosition];
|
|
85
|
+
if (!isLayerSameAndUnchanged(layer, prevLayer)) {
|
|
86
|
+
layersChanged.push({ layer, position: i });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// look for moved layers
|
|
91
|
+
const prevLayersFiltered = previousContext.layers.filter((l) => !layersRemoved.find(({ layer }) => l === layer));
|
|
92
|
+
const nextLayersFiltered = nextContext.layers.filter((l) => !layersAdded.find(({ layer }) => l === layer));
|
|
93
|
+
for (let i = 0; i < nextLayersFiltered.length; i++) {
|
|
94
|
+
const layer = nextLayersFiltered[i];
|
|
95
|
+
const prevPosition = getLayerPosition(layer, prevLayersFiltered);
|
|
96
|
+
if (i !== prevPosition) {
|
|
97
|
+
layersReordered.push({
|
|
98
|
+
layer,
|
|
99
|
+
newPosition: getLayerPosition(layer, nextContext.layers),
|
|
100
|
+
previousPosition: getLayerPosition(layer, previousContext.layers),
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
layersAdded,
|
|
106
|
+
layersChanged,
|
|
107
|
+
layersRemoved,
|
|
108
|
+
layersReordered,
|
|
109
|
+
viewChanges,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"map-context-diff.test.d.ts","sourceRoot":"","sources":["../../lib/utils/map-context-diff.test.ts"],"names":[],"mappings":""}
|