Skip to content

Commit

Permalink
Implement delegated Geolocation
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Sep 6, 2023
1 parent 9ccb366 commit d0b4f79
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/delegated-geolocation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { StdGeolocation } from "./types/std.js";

let canConstruct = false;

export function createDelegatedGeolocation(_: {
delegates: StdGeolocation[];
}): {
geolocation: StdGeolocation;
selectDelegate: SelectDelegate;
} {
canConstruct = true;

return {
geolocation: new Geolocation(),

selectDelegate() {},
};
}

export type SelectDelegate = (delegate: StdGeolocation) => void;

export class Geolocation {
/**
* @deprecated Use the `createDelegatedPermissions()` function instead.
*/
constructor() {
if (!canConstruct) throw new TypeError("Illegal constructor");
canConstruct = false;
}

getCurrentPosition(): void {
throw new Error("Not implemented");
}

watchPosition(): number {
throw new Error("Not implemented");
}

clearWatch(): void {
throw new Error("Not implemented");
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { createDelegatedGeolocation } from "./delegated-geolocation.js";
export type { SelectDelegate } from "./delegated-geolocation.js";
export {
GeolocationCoordinates,
createCoordinates,
Expand Down
84 changes: 84 additions & 0 deletions test/jest/delegated.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import {
Permissions,
createPermissionStore,
createPermissions,
} from "fake-permissions";
import { GEOLOCATION } from "fake-permissions/constants/permission-name";
import { PROMPT } from "fake-permissions/constants/permission-state";
import {
MutableLocationServices,
SelectDelegate,
User,
createDelegatedGeolocation,
createGeolocation,
createLocationServices,
createUser,
} from "../../src/index.js";
import { StdGeolocation } from "../../src/types/std.js";

describe("Delegated geolocation", () => {
let locationServicesA: MutableLocationServices;
let locationServicesB: MutableLocationServices;
let permissionsA: Permissions<typeof GEOLOCATION>;
let permissionsB: Permissions<typeof GEOLOCATION>;
let userA: User<typeof GEOLOCATION>;
let userB: User<typeof GEOLOCATION>;
let delegateA: StdGeolocation;
let delegateB: StdGeolocation;
let geolocation: StdGeolocation;
let selectDelegate: SelectDelegate;

Check failure on line 29 in test/jest/delegated.spec.ts

View workflow job for this annotation

GitHub Actions / CI

'selectDelegate' is assigned a value but never used

beforeEach(() => {
locationServicesA = createLocationServices();
locationServicesB = createLocationServices();

const permissionStoreA = createPermissionStore({
initialStates: new Map([[{ name: GEOLOCATION }, PROMPT]]),
});
const permissionStoreB = createPermissionStore({
initialStates: new Map([[{ name: GEOLOCATION }, PROMPT]]),
});

permissionsA = createPermissions({ permissionStore: permissionStoreA });
permissionsB = createPermissions({ permissionStore: permissionStoreB });

userA = createUser({
locationServices: locationServicesA,
permissionStore: permissionStoreA,
});
userB = createUser({
locationServices: locationServicesB,
permissionStore: permissionStoreB,
});

delegateA = createGeolocation({
async requestPermission(descriptor) {
return userA.requestPermission(descriptor);
},

locationServices: locationServicesA,
permissions: permissionsA,
});
delegateB = createGeolocation({
async requestPermission(descriptor) {
return userB.requestPermission(descriptor);
},

locationServices: locationServicesB,
permissions: permissionsB,
});

({ geolocation, selectDelegate } = createDelegatedGeolocation({
delegates: [delegateA, delegateB],
}));
});

it("cannot be instantiated directly", async () => {
const instantiateGeolocation = () => {
new (geolocation.constructor as new (p: object) => unknown)({});
};

expect(instantiateGeolocation).toThrow(TypeError);
expect(instantiateGeolocation).toThrow("Illegal constructor");
});
});

0 comments on commit d0b4f79

Please sign in to comment.