// import { createMachine, assign, sendParent } from 'xstate'; // import type { NavigatorModel, NavigatorContext, NavigatorEvent, NavigatorState } from '../models'; // const cleanContext = { // location: [], // lastLocated: null, // allowed: false, // watch: false // }; // const geolocate = ( context, event ) => new Promise( async (resolve, reject) => { // navigator.geolocation.getCurrentPosition( // (pos: any) => { // const location = [ pos.coords.longitude, pos.coords.latitude ]; // const lastLocated = new Date(); // resolve({ ...context, location, lastLocated }); // }, // (error: any) => { // reject(error); // }, // {enableHighAccuracy: true } // ); // }); // export const navigatorMachine = ( field: NavigatorModel ) => createMachine( // { // id: 'navigator', // initial: 'idle', // context: { ...cleanContext }, // states: { // idle: { // on: { // LOCATE: { // target: 'locating', // } // } // }, // locating: { // on: { // LOCATED: 'success', // ERROR: 'failed' // }, // invoke: { // id: 'navigator-locating', // src: 'geolocate', // onDone: { // target: 'success', // actions: assign((context, event) => { // return { ...context, ...event.data }; // }) // }, // onError: 'failed', // }, // }, // disallowed: { // on: { // RESET: { // target: 'idle', // actions: [ 'resetContext' ] // } // } // }, // success: { // entry: sendParent( ( context, event ) => { // return { type: 'CHILD_TO_PARENT', value: { center: context.location } }; // }), // on: { // RESET: { // target: 'idle', // } // }, // }, // failed: {}, // }, // }, // { // actions: { // resetContext: ( context, event ) => ({ ...cleanContext }) // }, // services: { // geolocate // } // });