Skip to content

Commit

Permalink
feat: add clear to the public API
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesLMilner committed Jan 8, 2023
1 parent a7a2bcf commit 6a8fa72
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 23 deletions.
38 changes: 38 additions & 0 deletions src/store/store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,44 @@ describe("GeoJSONStore", () => {
});
});

describe("size", () => {

it("gets zero size on initialisation", () => {
const store = new GeoJSONStore();
expect(store.size()).toBe(0);
});

it("gets size one after feature added", () => {
const store = new GeoJSONStore();

store.create([
{ geometry: { type: "Point", coordinates: [0, 0] } },
]);

expect(store.size()).toBe(1);
});

});

describe("clear", () => {
it("removes all data from store", () => {
const store = new GeoJSONStore();

const ids = store.create([
{ geometry: { type: "Point", coordinates: [0, 0] } },
]);

store.clear();

expect(store.size()).toBe(0);

expect(() => {
store.getGeometryCopy(ids[0]);
}).toThrowError();
});

});

describe("getGeometryCopy", () => {
it("copy existing geometry", () => {
const store = new GeoJSONStore();
Expand Down
49 changes: 29 additions & 20 deletions src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SpatialIndex } from "./spatial-index/spatial-index";
type JSON = string | number | boolean | null | JSONArray | JSONObject;

export interface JSONObject {
[member: string]: JSON;
[member: string]: JSON;
}
type JSONArray = Array<JSON>;

Expand All @@ -16,21 +16,21 @@ export type GeoJSONStoreGeometries = Polygon | LineString | Point;
export type BBoxPolygon = Feature<Polygon, DefinedProperties>;

export type GeoJSONStoreFeatures = Feature<
GeoJSONStoreGeometries,
DefinedProperties
GeoJSONStoreGeometries,
DefinedProperties
>;

export type StoreChangeEvents = "delete" | "create" | "update";

export type StoreChangeHandler = (
ids: string[],
change: StoreChangeEvents
ids: string[],
change: StoreChangeEvents
) => void;

export type GeoJSONStoreConfig = {
data?: GeoJSONStoreFeatures[];
tracked?: boolean;
validateFeature?: (feature: unknown, tracked?: boolean) => void;
data?: GeoJSONStoreFeatures[];
tracked?: boolean;
validateFeature?: (feature: unknown, tracked?: boolean) => void;
};

export class GeoJSONStore {
Expand All @@ -52,11 +52,11 @@ export class GeoJSONStore {
private spatialIndex: SpatialIndex;

private store: {
[key: string]: GeoJSONStoreFeatures;
};
[key: string]: GeoJSONStoreFeatures;
};

// Default to no-op
private _onChange: StoreChangeHandler = () => {};
private _onChange: StoreChangeHandler = () => { };

private getId(): string {
return uuid4();
Expand Down Expand Up @@ -209,9 +209,9 @@ export class GeoJSONStore {

create(
features: {
geometry: GeoJSONStoreGeometries;
properties?: JSONObject;
}[]
geometry: GeoJSONStoreGeometries;
properties?: JSONObject;
}[]
): string[] {
const ids: string[] = [];
features.forEach(({ geometry, properties }) => {
Expand All @@ -223,13 +223,13 @@ export class GeoJSONStore {

if (properties) {
createdProperties.createdAt =
typeof properties.createdAt === "number"
? properties.createdAt
: createdAt;
typeof properties.createdAt === "number"
? properties.createdAt
: createdAt;
createdProperties.updatedAt =
typeof properties.updatedAt === "number"
? properties.updatedAt
: createdAt;
typeof properties.updatedAt === "number"
? properties.updatedAt
: createdAt;
} else {
createdProperties = { createdAt, updatedAt: createdAt };
}
Expand Down Expand Up @@ -274,4 +274,13 @@ export class GeoJSONStore {
copyAll(): GeoJSONStoreFeatures[] {
return this.clone(Object.keys(this.store).map((id) => this.store[id]));
}

clear(): void {
this.store = {};
this.spatialIndex.clear();
}

size(): number {
return Object.keys(this.store).length;
}
}
24 changes: 21 additions & 3 deletions src/terra-draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ class TerraDraw {
);
}
}

private checkEnabled() {
if (!this._enabled) {
throw new Error("Terra Draw is not enabled");
}
}


private getModeStyles() {
const modeStyles: { [key: string]: (feature: GeoJSONStoreFeatures) => TerraDrawAdapterStyling } = {};
Object.keys(this._modes).forEach((mode) => {
Expand All @@ -217,6 +225,7 @@ class TerraDraw {
* @alpha
*/
setModeStyles(mode: string, styles: TerraDrawAdapterStyling) {
this.checkEnabled()
this._modes[mode].styles = styles;
}

Expand All @@ -228,9 +237,19 @@ class TerraDraw {
* @alpha
*/
getSnapshot() {
// This is a read only method so we do not need to check if enabled
return this._store.copyAll();
}

/**
* Removes all data from the current store
*
* @alpha
*/
clear() {
this.checkEnabled()
this._store.clear();
}

/**
* A property used to determine whether the instance is active or not. You
Expand Down Expand Up @@ -262,6 +281,7 @@ class TerraDraw {
* @alpha
*/
getMode(): string {
// This is a read only method so we do not need to check if enabled
return this._mode.mode;
}

Expand All @@ -273,9 +293,7 @@ class TerraDraw {
* @alpha
*/
setMode(mode: string) {
if (!this._enabled) {
throw new Error("Terra Draw is not started");
}
this.checkEnabled()

if (this._modes[mode]) {
// Before we swap modes we want to
Expand Down

0 comments on commit 6a8fa72

Please sign in to comment.