Skip to content

Commit

Permalink
Add real units
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Apr 24, 2024
1 parent 7a48f6b commit a79d608
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
60 changes: 54 additions & 6 deletions packages/web-api/src/geolocation.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -46,28 +55,67 @@ type CustomProvider = (params: GeolocationParams) => {
};

type Geolocation = {
$location: Store<number | null>;
$location: Store<{ latitude: number; longitude: number } | null>;
$latitude: Store<number | null>;
$longitude: Store<{ latitude: number; longitude: number } | null>;
$longitude: Store<number | null>;
request: EventCallable<void>;
watching: {
start: EventCallable<void>;
stop: EventCallable<void>;
$active: Store<boolean>;
};
reporting: {
failed: Event<unknown>;
failed: Event<CustomGeolocationError>;
};
};

const BrowserProvider = Symbol('BrowserProvider');

export function trackGeolocation(
params: GeolocationParams & {
providers?: Array<CustomProvider>;
providers?: Array<CustomProvider | globalThis.Geolocation>;
}
): 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<CustomGeolocationError>();

return {
$location: readonly($location),
$longitude,
$latitude,
request,
watching: {
start: startWatching,
stop: stopWatching,
$active: readonly($watchingActive),
},
reporting: {
failed: readonly(failed),
},
};
}

trackGeolocation.browserProvider = BrowserProvider;
12 changes: 11 additions & 1 deletion packages/web-api/src/shared.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {
Event,
type Event,
type Store,
type EventCallable,
type StoreWritable,
attach,
createEffect,
createEvent,
Expand Down Expand Up @@ -104,3 +107,10 @@ export function setupListener<T>(

return event;
}

export function readonly<T>(unit: StoreWritable<T>): Store<T>;
export function readonly<T>(unit: EventCallable<T>): Event<T>;

export function readonly(unit: any) {
return unit.map((v: any) => v);
}

0 comments on commit a79d608

Please sign in to comment.