@tokamakdev/plugin-location 0.1.0-beta.107 → 0.1.0-beta.114

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/README.md CHANGED
@@ -8,11 +8,20 @@ and continuous tracking, all of which reach the app's Worker off screen.
8
8
  npm install @tokamakdev/plugin-location
9
9
  ```
10
10
 
11
+ Pages import the generated `/client` SDK. In `src/tokamak.ts`, import the
12
+ package root and call its factory with the handler's `ctx`. The main worker
13
+ uses its generated `env.TOKAMAK_LOCATION` service binding when present.
14
+
11
15
  ```ts
12
- import { location } from "@tokamakdev/plugin-location";
16
+ import location from "@tokamakdev/plugin-location/client";
17
+ import { decodeValues } from "@tokamakdev/plugin/streams";
18
+ import type { Position } from "@tokamakdev/plugin-location";
13
19
 
14
20
  const position = await location.getCurrentPosition();
15
- const stop = location.watchPosition((next) => console.log(next.coords), console.error);
21
+ for await (const next of decodeValues<Position>(location.watchPosition())) {
22
+ console.log(next.coords);
23
+ break; // Cancels the watch.
24
+ }
16
25
 
17
26
  await location.requestPermission({ background: true });
18
27
  await location.addRegion({ id: "home", latitude: 51.5, longitude: -0.12, radius: 150 });
@@ -24,8 +33,8 @@ import { location } from "@tokamakdev/plugin-location";
24
33
  import { onLocationUpdate, onRegionTransition } from "@tokamakdev/plugin-location/events";
25
34
  import { onStart } from "@tokamakdev/tok/events";
26
35
 
27
- onStart(async () => {
28
- await location.setup({ significantChanges: true, visits: true });
36
+ onStart(async (_event, _env, ctx) => {
37
+ await location(ctx).setup({ significantChanges: true, visits: true });
29
38
  });
30
39
  onLocationUpdate(async ({ positions }) => {
31
40
  await fetch("https://example.com/whereabouts", { method: "POST", body: JSON.stringify(positions) });
@@ -41,8 +50,8 @@ milliseconds, 0 by default, or takes a new one, and rejects with
41
50
  `TimeoutError` when none arrives within `timeout` milliseconds, unlimited by
42
51
  default. The timeout starts once the user allows location.
43
52
 
44
- `watchPosition(next, error)` calls `next` with each new position until the
45
- function it returns is called.
53
+ `watchPosition()` returns a byte stream of JSON positions. `decodeValues<Position>`
54
+ decodes them. Cancelling the stream ends the native watch; errors terminate it.
46
55
 
47
56
  Both ask for in-use permission when the user has not answered. Off screen
48
57
  without background permission, the platforms stop locating the app, so
@@ -135,16 +144,17 @@ device is still.
135
144
 
136
145
  The plugin's `onLocationUpdate`, `onRegionTransition`, `onVisit` and
137
146
  `onLocationError` methods receive the same events while the listening page or
138
- Worker runs, and return the function that removes the listener.
147
+ Tokamak worker runs. They return byte streams consumed with `decodeValues<T>`;
148
+ cancelling a stream removes its native subscription.
139
149
 
140
150
  ## Long-term awareness
141
151
 
142
152
  `setup(config)` keeps the app aware of where the device is over days, with
143
153
  little power and no indicator. Call it from `onStart` in `src/tokamak.ts`, so
144
154
  it applies at every launch; the plugin also keeps it and applies it again
145
- itself. The configuration replaces the previous one, and an invalid one
146
- rejects with `TypeError` and changes nothing. Apple platforms reject with
147
- `NotSupportedError` where the device cannot monitor significant changes.
155
+ itself. The configuration replaces the previous one; a field it leaves out is
156
+ off. Apple platforms reject with `NotSupportedError` where the device cannot
157
+ monitor significant changes.
148
158
 
149
159
  - `significantChanges` delivers moves of 500 metres or more, at most every 5
150
160
  minutes, to `onLocationUpdate`, located from cellular and Wi-Fi rather than
@@ -233,8 +243,15 @@ and background permission off screen.
233
243
 
234
244
  The web uses `navigator.geolocation`. `permission()` reports `inUse` from the
235
245
  Permissions API, and the other additions reject with `NotSupportedError`,
236
- while the listener methods receive nothing. Windows uses WebView2's location
237
- for `getCurrentPosition` and `watchPosition`.
246
+ including the event-stream methods. Native support is provided on Apple and Android.
247
+
248
+ ## Platform bindings
249
+
250
+ `@tokamakdev/plugin-location/apple` is Core Location, and
251
+ `@tokamakdev/plugin-location/android` is `android.location` with the
252
+ `Context`, `CancellationSignal`, `SystemClock`, `Executor` and `Consumer`
253
+ types it uses, for Worker code that needs what this API does not offer.
254
+ `getCurrentPosition` and `watchPosition` are built on them.
238
255
 
239
256
  ## Errors
240
257
 
@@ -242,4 +259,4 @@ Rejections and `onLocationError` events are `DOMException` names:
242
259
  `NotAllowedError` (no permission, or none for the background),
243
260
  `NotReadableError` (location unavailable, or services off), `NeedsUIError` (a
244
261
  prompt or a start off screen), `NotSupportedError`, `QuotaExceededError`,
245
- `TimeoutError` and `TypeError`.
262
+ `TimeoutError`, and `TypeError` for an argument the native code cannot read.