Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove whole Instances #153

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ yarn.lock
# code coverage
__coverage__

src/tests/**
test*.*

test/unit/coverage/**
Expand Down
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"no-prototype-builtins": "off",
"no-restricted-globals": "off",
"no-underscore-dangle": "off",
"prettier/prettier": "error",
"prettier/prettier": ["error", { "endOfLine": "auto" }],
"semi": "off",
"standard/no-callback-literal": "off"
}
Expand Down
3 changes: 2 additions & 1 deletion jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
"testEnvironment": "jsdom",
"transform": {
"^.+\\.glsl": "jest-raw-loader"
}
},
"testMatch": ["**/tests/**/*.test.ts"]
}
8 changes: 7 additions & 1 deletion src/base-gl-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ export type EventCallback = (
feature: any
) => boolean | void;

export type SetupHoverCallback = (map: Map, hoverWait?: number, immediate?: false) => void;
export type SetupHoverCallback = (
map: Map,
hoverWait?: number,
immediate?: false
) => void;

export interface IBaseGlLayerSettings {
data: any;
Expand Down Expand Up @@ -77,6 +81,7 @@ export abstract class BaseGlLayer<
static defaults = defaults;

abstract render(): this;
abstract removeInstance(this: any): this;

get data(): any {
if (!this.settings.data) {
Expand Down Expand Up @@ -312,6 +317,7 @@ export abstract class BaseGlLayer<

remove(indices?: number | number[]): this {
if (indices === undefined) {
this.removeInstance();
this.map.removeLayer(this.layer);
this.active = false;
} else {
Expand Down
1 change: 1 addition & 0 deletions src/canvas-overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class CanvasOverlay extends Layer {
_pane: string;

_frame?: number | null;
_leaflet_id?: number;
options?: LayerOptions;

constructor(userDrawFunc: IUserDrawFunc, pane: string) {
Expand Down
11 changes: 11 additions & 0 deletions src/lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ICanvasOverlayDrawEvent } from "./canvas-overlay";
import * as color from "./color";
import { LineFeatureVertices } from "./line-feature-vertices";
import { latLngDistance, inBounds } from "./utils";
import glify from "./index";

export type WeightCallback = (i: number, feature: any) => number;

Expand Down Expand Up @@ -223,6 +224,16 @@ export class Lines extends BaseGlLayer<ILinesSettings> {
return this;
}

removeInstance(): this {
const index = glify.linesInstances.findIndex(
(element) => element.layer._leaflet_id === this.layer._leaflet_id
);
if (index !== -1) {
glify.linesInstances.splice(index, 1);
}
return this;
}

drawOnCanvas(e: ICanvasOverlayDrawEvent): this {
if (!this.gl) return this;

Expand Down
11 changes: 11 additions & 0 deletions src/points.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as Color from "./color";
import { LeafletMouseEvent, Map, Point, LatLng } from "leaflet";
import { IPixel } from "./pixel";
import { locationDistance, pixelInCircle } from "./utils";
import glify from "./index";

export interface IPointsSettings extends IBaseGlLayerSettings {
data: number[][] | FeatureCollection<GeoPoint>;
Expand Down Expand Up @@ -287,6 +288,16 @@ export class Points extends BaseGlLayer<IPointsSettings> {
return this;
}

removeInstance(): this {
const index = glify.pointsInstances.findIndex(
(element) => element.layer._leaflet_id === this.layer._leaflet_id
);
if (index !== -1) {
glify.pointsInstances.splice(index, 1);
}
return this;
}

// TODO: remove?
pointSize(pointIndex: number): number {
const { map, size } = this;
Expand Down
11 changes: 11 additions & 0 deletions src/shapes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as Color from "./color";
import { latLonToPixel } from "./utils";

import { notProperlyDefined } from "./errors";
import glify from "./index";

export interface IShapesSettings extends IBaseGlLayerSettings {
border?: boolean;
Expand Down Expand Up @@ -257,6 +258,16 @@ export class Shapes extends BaseGlLayer {
return this;
}

removeInstance(): this {
const index = glify.shapesInstances.findIndex(
(element) => element.layer._leaflet_id === this.layer._leaflet_id
);
if (index !== -1) {
glify.shapesInstances.splice(index, 1);
}
return this;
}

drawOnCanvas(e: ICanvasOverlayDrawEvent): this {
if (!this.gl) return this;

Expand Down
10 changes: 7 additions & 3 deletions src/base-gl-layer.test.ts → src/tests/base-gl-layer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {
defaultPane,
EventCallback,
IBaseGlLayerSettings,
} from "./base-gl-layer";
import { ICanvasOverlayDrawEvent } from "./canvas-overlay";
} from "../base-gl-layer";
import { ICanvasOverlayDrawEvent } from "../canvas-overlay";
import { LatLng, LatLngBounds, LeafletMouseEvent, Map, Point } from "leaflet";

jest.mock("./canvas-overlay");
jest.mock("../canvas-overlay");

describe("BaseGlLayer", () => {
interface ITestLayerSettings extends IBaseGlLayerSettings {}
Expand All @@ -20,6 +20,10 @@ describe("BaseGlLayer", () => {
render(): this {
return this;
}

removeInstance(): this {
return this;
}
}
const defaultSettings: Partial<ITestLayerSettings> = {
longitudeKey: 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CanvasOverlay } from "./canvas-overlay";
import { CanvasOverlay } from "../canvas-overlay";
import {
Bounds,
LatLng,
Expand Down
2 changes: 1 addition & 1 deletion src/color.test.ts → src/tests/color.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fromHex, pallet, random } from "./color";
import { fromHex, pallet, random } from "../color";

describe("color", () => {
test("fromHex", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/errors.test.ts → src/tests/errors.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { notProperlyDefined } from "./errors";
import { notProperlyDefined } from "../errors";

describe("notProperlyDefined", () => {
it("uses properly defined message", () => {
Expand Down
12 changes: 6 additions & 6 deletions src/index.test.ts → src/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Glify } from "./index";
import { IPointsSettings, Points } from "./points";
import { ILinesSettings, Lines } from "./lines";
import { IShapesSettings, Shapes } from "./shapes";
import { Glify } from "../index";
import { IPointsSettings, Points } from "../points";
import { ILinesSettings, Lines } from "../lines";
import { IShapesSettings, Shapes } from "../shapes";
import { LatLng, LeafletMouseEvent, Map, Point } from "leaflet";
import { FeatureCollection, LineString, MultiPolygon } from "geojson";

jest.mock("./canvas-overlay");
jest.mock("../canvas-overlay");
type mouseEventFunction = (e: LeafletMouseEvent) => void;
jest.mock("./utils", () => {
jest.mock("../utils", () => {
return {
debounce: (fn: mouseEventFunction) => {
return (e: LeafletMouseEvent) => fn(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LineFeatureVertices } from "./line-feature-vertices";
import { LineFeatureVertices } from "../line-feature-vertices";
import { LatLng } from "leaflet";
import { IPixel } from "./pixel";
import { IPixel } from "../pixel";
import { Position } from "geojson";

describe("LineFeatureVertices", () => {
Expand Down
10 changes: 5 additions & 5 deletions src/lines.test.ts → src/tests/lines.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { LatLng, LatLngBounds, LeafletMouseEvent, Map, Point } from "leaflet";
import { Feature, FeatureCollection, LineString } from "geojson";
import { MapMatrix } from "./map-matrix";
import { ICanvasOverlayDrawEvent } from "./canvas-overlay";
import { ILinesSettings, Lines, WeightCallback } from "./lines";
import { MapMatrix } from "../map-matrix";
import { ICanvasOverlayDrawEvent } from "../canvas-overlay";
import { ILinesSettings, Lines, WeightCallback } from "../lines";

jest.mock("./canvas-overlay");
jest.mock("./utils", () => {
jest.mock("../canvas-overlay");
jest.mock("../utils", () => {
return {
inBounds: () => true,
latLngDistance: () => 2,
Expand Down
2 changes: 1 addition & 1 deletion src/map-matrix.test.ts → src/tests/map-matrix.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MapMatrix } from "./map-matrix";
import { MapMatrix } from "../map-matrix";

describe("MapMatrix", () => {
describe("constructor", () => {
Expand Down
6 changes: 3 additions & 3 deletions src/points.test.ts → src/tests/points.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { LatLng, LatLngBounds, Map, Point } from "leaflet";
import { FeatureCollection, Point as GeoPoint } from "geojson";
import { IPointVertex, IPointsSettings, Points } from "./points";
import { ICanvasOverlayDrawEvent } from "./canvas-overlay";
import { IPointVertex, IPointsSettings, Points } from "../points";
import { ICanvasOverlayDrawEvent } from "../canvas-overlay";

jest.mock("./canvas-overlay");
jest.mock("../canvas-overlay");

function getPoints(settings?: Partial<IPointsSettings>): Points {
const element = document.createElement("div");
Expand Down
6 changes: 3 additions & 3 deletions src/shapes.test.ts → src/tests/shapes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import geojsonFlatten from "geojson-flatten";
import PolygonLookup from "polygon-lookup";
import earcut from "earcut";

import { IShapesSettings, Shapes } from "./shapes";
import { notProperlyDefined } from "./errors";
import { IShapesSettings, Shapes } from "../shapes";
import { notProperlyDefined } from "../errors";

jest.mock("./canvas-overlay");
jest.mock("../canvas-overlay");
jest.mock("geojson-flatten", () => {
const realGeojsonFlatten = jest.requireActual<typeof geojsonFlatten>(
"geojson-flatten"
Expand Down
2 changes: 1 addition & 1 deletion src/utils.test.ts → src/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
latLngDistance,
pixelInCircle,
vectorDistance,
} from "./utils";
} from "../utils";

describe("utils", () => {
describe("latLonToPixel", () => {
Expand Down