From a79d608cefc98c6e5e637fef26f698016fd8d317 Mon Sep 17 00:00:00 2001 From: Igor Kamyshev Date: Wed, 24 Apr 2024 13:40:57 +0700 Subject: [PATCH] Add real units --- packages/web-api/src/geolocation.ts | 60 ++++++++++++++++++++++++++--- packages/web-api/src/shared.ts | 12 +++++- 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/packages/web-api/src/geolocation.ts b/packages/web-api/src/geolocation.ts index f8520140..c7160999 100644 --- a/packages/web-api/src/geolocation.ts +++ b/packages/web-api/src/geolocation.ts @@ -1,4 +1,13 @@ -import { Event, EventCallable, Store } from 'effector'; +import { + type Event, + type EventCallable, + type Store, + combine, + createEvent, + createStore, +} from 'effector'; + +import { readonly } from './shared'; type GeolocationParams = { maximumAge?: number; @@ -46,9 +55,9 @@ type CustomProvider = (params: GeolocationParams) => { }; type Geolocation = { - $location: Store; + $location: Store<{ latitude: number; longitude: number } | null>; $latitude: Store; - $longitude: Store<{ latitude: number; longitude: number } | null>; + $longitude: Store; request: EventCallable; watching: { start: EventCallable; @@ -56,7 +65,7 @@ type Geolocation = { $active: Store; }; reporting: { - failed: Event; + failed: Event; }; }; @@ -64,10 +73,49 @@ const BrowserProvider = Symbol('BrowserProvider'); export function trackGeolocation( params: GeolocationParams & { - providers?: Array; + providers?: Array; } ): Geolocation { - return {} as any; + // In case of no providers, we will use the default one only + const providres = params.providers ?? [BrowserProvider]; + + const $location = createStore<{ + latitude: number; + longitude: number; + } | null>(null); + + const $longitude = combine( + $location, + (location) => location?.longitude ?? null + ); + + const $latitude = combine( + $location, + (location) => location?.latitude ?? null + ); + + const request = createEvent(); + + const startWatching = createEvent(); + const stopWatching = createEvent(); + const $watchingActive = createStore(false); + + const failed = createEvent(); + + return { + $location: readonly($location), + $longitude, + $latitude, + request, + watching: { + start: startWatching, + stop: stopWatching, + $active: readonly($watchingActive), + }, + reporting: { + failed: readonly(failed), + }, + }; } trackGeolocation.browserProvider = BrowserProvider; diff --git a/packages/web-api/src/shared.ts b/packages/web-api/src/shared.ts index e9eca7fa..f49213ca 100644 --- a/packages/web-api/src/shared.ts +++ b/packages/web-api/src/shared.ts @@ -1,5 +1,8 @@ import { - Event, + type Event, + type Store, + type EventCallable, + type StoreWritable, attach, createEffect, createEvent, @@ -104,3 +107,10 @@ export function setupListener( return event; } + +export function readonly(unit: StoreWritable): Store; +export function readonly(unit: EventCallable): Event; + +export function readonly(unit: any) { + return unit.map((v: any) => v); +}