|
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'; |
2 | 11 |
|
3 | 12 | type GeolocationParams = {
|
4 | 13 | maximumAge?: number;
|
@@ -46,28 +55,67 @@ type CustomProvider = (params: GeolocationParams) => {
|
46 | 55 | };
|
47 | 56 |
|
48 | 57 | type Geolocation = {
|
49 |
| - $location: Store<number | null>; |
| 58 | + $location: Store<{ latitude: number; longitude: number } | null>; |
50 | 59 | $latitude: Store<number | null>;
|
51 |
| - $longitude: Store<{ latitude: number; longitude: number } | null>; |
| 60 | + $longitude: Store<number | null>; |
52 | 61 | request: EventCallable<void>;
|
53 | 62 | watching: {
|
54 | 63 | start: EventCallable<void>;
|
55 | 64 | stop: EventCallable<void>;
|
56 | 65 | $active: Store<boolean>;
|
57 | 66 | };
|
58 | 67 | reporting: {
|
59 |
| - failed: Event<unknown>; |
| 68 | + failed: Event<CustomGeolocationError>; |
60 | 69 | };
|
61 | 70 | };
|
62 | 71 |
|
63 | 72 | const BrowserProvider = Symbol('BrowserProvider');
|
64 | 73 |
|
65 | 74 | export function trackGeolocation(
|
66 | 75 | params: GeolocationParams & {
|
67 |
| - providers?: Array<CustomProvider>; |
| 76 | + providers?: Array<CustomProvider | globalThis.Geolocation>; |
68 | 77 | }
|
69 | 78 | ): 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 | + }; |
71 | 119 | }
|
72 | 120 |
|
73 | 121 | trackGeolocation.browserProvider = BrowserProvider;
|
0 commit comments