index ↓

geo

client.geo

Position fixes from the connected phone. watch subscribes and getOnce returns a single fix. Declare the geo permission in the webapp manifest, or both fail.

requests

you ask, the daemon answers. await the tagged result and check .ok

watch(GeoWatch): Promise<TypedRequestResult<GeoWatchReply, GeoErrorReply>>

push form: onWatchReply (subscribe instead of awaiting)

const res = await client.geo.watch({ accuracy: 'coarse', minIntervalMs: 0 });
if (res.ok) {
  console.log(res.response.token);
} else {
  console.warn(res.kind, res.error);
}

getOnce(GeoGetOnce): Promise<TypedRequestResult<GeoGetOnceReply, GeoErrorReply>>

push form: onGetOnceReply (subscribe instead of awaiting)

const res = await client.geo.getOnce({ accuracy: 'coarse' });
if (res.ok) {
  console.log(res.response.position);
} else {
  console.warn(res.kind, res.error);
}

commands

fire-and-forget. the promise resolves once the daemon has taken the message

unwatch(GeoUnwatch): Promise<void>

await client.geo.unwatch({ token: '...' });

events

the daemon pushes these unprompted. subscribing returns an unsubscribe function

onPosition(handler: (Position) => void): () => void

const off = client.geo.onPosition((position) => {
  console.log(position.lat);
});
// call off() to unsubscribe

onErrorEvent(handler: (GeoErrorReply) => void): () => void

const off = client.geo.onErrorEvent((reply) => {
  console.log(reply.error);
});
// call off() to unsubscribe

types

shapes referenced above, as the sdk types them

type Position = {
  lat: number;
  lon: number;
  altM?: number;
  accuracyM: number; // Uncertainty radius in meters.
  speedMps?: number;
  headingDeg?: number;
  tsUnixS: number; // Fix time, not arrival time.
};
type GeoErrorReply = {
  error: GeoError;
};
type GeoWatch = {
  accuracy: GeoAccuracy;
  minIntervalMs: number;
};

Pass token to unwatch to stop the watch.

type GeoWatchReply = {
  token: string;
};
type GeoGetOnce = {
  accuracy: GeoAccuracy;
};
type GeoGetOnceReply = {
  position: Position;
};
type GeoUnwatch = {
  token: string;
};
type GeoError =
  | 'permissionDenied' // The phone denies location to the companion app.
  | 'notDeclared' // The active webapp must list `geo` in its manifest permissions.
  | 'unavailable' // The phone is connected but produced no fix.
  | 'unknownToken' // The token does not match an open subscription.;

coarse asks for a lower-power, less precise fix. Any open fine subscription raises it for all.

type GeoAccuracy = 'coarse' | 'fine';