@@ -5,6 +5,10 @@ import {
5
5
combine ,
6
6
createEvent ,
7
7
createStore ,
8
+ createEffect ,
9
+ sample ,
10
+ restore ,
11
+ attach ,
8
12
} from 'effector' ;
9
13
10
14
import { readonly } from './shared' ;
@@ -65,7 +69,7 @@ type Geolocation = {
65
69
$active : Store < boolean > ;
66
70
} ;
67
71
reporting : {
68
- failed : Event < CustomGeolocationError > ;
72
+ failed : Event < CustomGeolocationError | globalThis . GeolocationPositionError > ;
69
73
} ;
70
74
} ;
71
75
@@ -79,6 +83,8 @@ export function trackGeolocation(
79
83
// In case of no providers, we will use the default one only
80
84
const providres = params . providers ?? [ BrowserProvider ] ;
81
85
86
+ // -- units
87
+
82
88
const $location = createStore < {
83
89
latitude : number ;
84
90
longitude : number ;
@@ -100,7 +106,65 @@ export function trackGeolocation(
100
106
const stopWatching = createEvent ( ) ;
101
107
const $watchingActive = createStore ( false ) ;
102
108
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
104
168
105
169
return {
106
170
$location : readonly ( $location ) ,
0 commit comments