Skip to content

Commit 02b964b

Browse files
committed
Add basic relations
1 parent a79d608 commit 02b964b

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

packages/web-api/src/geolocation.ts

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import {
55
combine,
66
createEvent,
77
createStore,
8+
createEffect,
9+
sample,
10+
restore,
11+
attach,
812
} from 'effector';
913

1014
import { readonly } from './shared';
@@ -65,7 +69,7 @@ type Geolocation = {
6569
$active: Store<boolean>;
6670
};
6771
reporting: {
68-
failed: Event<CustomGeolocationError>;
72+
failed: Event<CustomGeolocationError | globalThis.GeolocationPositionError>;
6973
};
7074
};
7175

@@ -79,6 +83,8 @@ export function trackGeolocation(
7983
// In case of no providers, we will use the default one only
8084
const providres = params.providers ?? [BrowserProvider];
8185

86+
// -- units
87+
8288
const $location = createStore<{
8389
latitude: number;
8490
longitude: number;
@@ -100,7 +106,65 @@ export function trackGeolocation(
100106
const stopWatching = createEvent();
101107
const $watchingActive = createStore(false);
102108

103-
const failed = createEvent<CustomGeolocationError>();
109+
const failed = createEvent<
110+
CustomGeolocationError | globalThis.GeolocationPositionError
111+
>();
112+
113+
// -- shared logic
114+
115+
const newPosition = createEvent<
116+
CustomGeolocationPosition | globalThis.GeolocationPosition
117+
>();
118+
119+
sample({
120+
clock: newPosition,
121+
fn: (r) => ({ latitude: r.coords.latitude, longitude: r.coords.longitude }),
122+
target: $location,
123+
});
124+
125+
// -- get current position
126+
127+
const getCurrentPositionFx = createEffect<
128+
void,
129+
CustomGeolocationPosition | globalThis.GeolocationPosition,
130+
CustomGeolocationError | globalThis.GeolocationPositionError
131+
>(() => {
132+
// TODO: real code
133+
throw { code: 'POSITION_UNAVAILABLE', message: 'Not implemented' };
134+
});
135+
136+
sample({ clock: request, target: getCurrentPositionFx });
137+
sample({
138+
clock: getCurrentPositionFx.doneData,
139+
target: newPosition,
140+
});
141+
sample({ clock: getCurrentPositionFx.failData, target: failed });
142+
143+
// -- watch position
144+
145+
const saveUnsubscribe = createEvent<Unsubscribe>();
146+
const $unsubscribe = restore(saveUnsubscribe, null);
147+
148+
const watchPositionFx = createEffect(() => {
149+
// TODO: real code
150+
newPosition({} as any);
151+
failed({} as any);
152+
saveUnsubscribe(() => null);
153+
});
154+
155+
const unwatchPositionFx = attach({
156+
source: $unsubscribe,
157+
effect(unwatch) {
158+
unwatch?.();
159+
},
160+
});
161+
162+
sample({ clock: startWatching, target: watchPositionFx });
163+
sample({ clock: stopWatching, target: unwatchPositionFx });
164+
165+
$watchingActive.on(startWatching, () => true).on(stopWatching, () => false);
166+
167+
// -- public API
104168

105169
return {
106170
$location: readonly($location),

0 commit comments

Comments
 (0)