Skip to content

Commit a79d608

Browse files
committed
Add real units
1 parent 7a48f6b commit a79d608

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

packages/web-api/src/geolocation.ts

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import { Event, EventCallable, Store } from 'effector';
1+
import {
2+
type Event,
3+
type EventCallable,
4+
type Store,
5+
combine,
6+
createEvent,
7+
createStore,
8+
} from 'effector';
9+
10+
import { readonly } from './shared';
211

312
type GeolocationParams = {
413
maximumAge?: number;
@@ -46,28 +55,67 @@ type CustomProvider = (params: GeolocationParams) => {
4655
};
4756

4857
type Geolocation = {
49-
$location: Store<number | null>;
58+
$location: Store<{ latitude: number; longitude: number } | null>;
5059
$latitude: Store<number | null>;
51-
$longitude: Store<{ latitude: number; longitude: number } | null>;
60+
$longitude: Store<number | null>;
5261
request: EventCallable<void>;
5362
watching: {
5463
start: EventCallable<void>;
5564
stop: EventCallable<void>;
5665
$active: Store<boolean>;
5766
};
5867
reporting: {
59-
failed: Event<unknown>;
68+
failed: Event<CustomGeolocationError>;
6069
};
6170
};
6271

6372
const BrowserProvider = Symbol('BrowserProvider');
6473

6574
export function trackGeolocation(
6675
params: GeolocationParams & {
67-
providers?: Array<CustomProvider>;
76+
providers?: Array<CustomProvider | globalThis.Geolocation>;
6877
}
6978
): Geolocation {
70-
return {} as any;
79+
// In case of no providers, we will use the default one only
80+
const providres = params.providers ?? [BrowserProvider];
81+
82+
const $location = createStore<{
83+
latitude: number;
84+
longitude: number;
85+
} | null>(null);
86+
87+
const $longitude = combine(
88+
$location,
89+
(location) => location?.longitude ?? null
90+
);
91+
92+
const $latitude = combine(
93+
$location,
94+
(location) => location?.latitude ?? null
95+
);
96+
97+
const request = createEvent();
98+
99+
const startWatching = createEvent();
100+
const stopWatching = createEvent();
101+
const $watchingActive = createStore(false);
102+
103+
const failed = createEvent<CustomGeolocationError>();
104+
105+
return {
106+
$location: readonly($location),
107+
$longitude,
108+
$latitude,
109+
request,
110+
watching: {
111+
start: startWatching,
112+
stop: stopWatching,
113+
$active: readonly($watchingActive),
114+
},
115+
reporting: {
116+
failed: readonly(failed),
117+
},
118+
};
71119
}
72120

73121
trackGeolocation.browserProvider = BrowserProvider;

packages/web-api/src/shared.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import {
2-
Event,
2+
type Event,
3+
type Store,
4+
type EventCallable,
5+
type StoreWritable,
36
attach,
47
createEffect,
58
createEvent,
@@ -104,3 +107,10 @@ export function setupListener<T>(
104107

105108
return event;
106109
}
110+
111+
export function readonly<T>(unit: StoreWritable<T>): Store<T>;
112+
export function readonly<T>(unit: EventCallable<T>): Event<T>;
113+
114+
export function readonly(unit: any) {
115+
return unit.map((v: any) => v);
116+
}

0 commit comments

Comments
 (0)