Skip to content

Commit

Permalink
fix: typos and remove unnecessary callback
Browse files Browse the repository at this point in the history
  • Loading branch information
OS-martacarlos committed Jan 6, 2025
1 parent 741d72a commit 4d745ad
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 44 deletions.
4 changes: 2 additions & 2 deletions packages/cordova-plugin/dist/plugin.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function watchPosition(options, success, error) {
accuracy: position.accuracy,
heading: position.heading,
speed: position.speed,
altitudeAccuracy: position.accuracy
altitudeAccuracy: position.altitudeAccuracy
},
timestamp: position.timestamp
};
Expand All @@ -99,7 +99,7 @@ function watchPosition(options, success, error) {
}
function clearWatch(options, success, error) {
options = { ...ClearWatchOptionsDefault, ...options };
exec(success, error, "OSGeolocation", "clearWatch", [options.id]);
exec(success, error, "OSGeolocation", "clearWatch", [options]);
}
module.exports = {
getCurrentPosition,
Expand Down
4 changes: 2 additions & 2 deletions packages/cordova-plugin/dist/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
accuracy: position.accuracy,
heading: position.heading,
speed: position.speed,
altitudeAccuracy: position.accuracy
altitudeAccuracy: position.altitudeAccuracy
},
timestamp: position.timestamp
};
Expand All @@ -101,7 +101,7 @@
}
function clearWatch(options, success, error) {
options = { ...ClearWatchOptionsDefault, ...options };
exec(success, error, "OSGeolocation", "clearWatch", [options.id]);
exec(success, error, "OSGeolocation", "clearWatch", [options]);
}
module.exports = {
getCurrentPosition,
Expand Down
4 changes: 2 additions & 2 deletions packages/cordova-plugin/dist/plugin.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function watchPosition(options, success, error) {
accuracy: position.accuracy,
heading: position.heading,
speed: position.speed,
altitudeAccuracy: position.accuracy
altitudeAccuracy: position.altitudeAccuracy
},
timestamp: position.timestamp
};
Expand All @@ -98,7 +98,7 @@ function watchPosition(options, success, error) {
}
function clearWatch(options, success, error) {
options = { ...ClearWatchOptionsDefault, ...options };
exec(success, error, "OSGeolocation", "clearWatch", [options.id]);
exec(success, error, "OSGeolocation", "clearWatch", [options]);
}
module.exports = {
getCurrentPosition,
Expand Down
8 changes: 4 additions & 4 deletions packages/cordova-plugin/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function getCurrentPosition(options: CurrentPositionOptions, success: (output: P
accuracy: position.accuracy,
heading: position.heading,
speed: position.speed,
altitudeAccuracy: position.accuracy
altitudeAccuracy: position.altitudeAccuracy
},
timestamp: position.timestamp,
}
Expand All @@ -37,7 +37,7 @@ function watchPosition(options: WatchPositionOptions, success: (output: Position
accuracy: position.accuracy,
heading: position.heading,
speed: position.speed,
altitudeAccuracy: position.accuracy
altitudeAccuracy: position.altitudeAccuracy
},
timestamp: position.timestamp,
}
Expand All @@ -46,9 +46,9 @@ function watchPosition(options: WatchPositionOptions, success: (output: Position
exec(convertOnSuccess, error, 'OSGeolocation', 'watchPosition', [options]);
}

function clearWatch(options: ClearWatchOptions, success: (output: string) => void, error: (error: PluginError) => void): void {
function clearWatch(options: ClearWatchOptions, success: () => void, error: (error: PluginError) => void): void {
options = { ...ClearWatchOptionsDefault, ...options };
exec(success, error, "OSGeolocation", "clearWatch", [options.id]);
exec(success, error, "OSGeolocation", "clearWatch", [options]);
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion packages/outsystems-wrapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"rimraf": "^3.0.2",
"typescript": "~5.4.5",
"vite": "^5.2.11",
"vite-plugin-dts": "^3.9.1"
"vite-plugin-dts": "^4.4.0"
},
"dependencies": {
"uuid": "^11.0.3"
Expand Down
62 changes: 29 additions & 33 deletions packages/outsystems-wrapper/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { ClearWatchOptions, OSGLOCPosition, PluginError, Position, CurrentPositionOptions, WatchPositionOptions } from "../../cordova-plugin/src/definitions";
import { v4 as uuidv4 } from 'uuid';
import { ClearWatchOptions, OSGLOCPosition, PluginError, Position, CurrentPositionOptions, WatchPositionOptions } from "../../cordova-plugin/src/definitions"
import { v4 as uuidv4 } from 'uuid'

class OSGeolocation {
#lastPosition: Position | null = null
#timers: { [key: string]: ReturnType<typeof setTimeout> | undefined; } = {}
#timers: { [key: string]: ReturnType<typeof setTimeout> | undefined } = {}

getCurrentPosition(success: (position: Position) => void, error: (err: PluginError | GeolocationPositionError) => void, options: CurrentPositionOptions): void {
// @ts-ignore
if (typeof (CapacitorUtils) === 'undefined') {
// if we're not in synapse land, we call the good old bridge or web api
// (it's the same clobber)
navigator.geolocation.getCurrentPosition(success, error, options);
navigator.geolocation.getCurrentPosition(success, error, options)
return
}

let id = uuidv4();
let timeoutID: ReturnType<typeof setTimeout> | undefined;
let id = uuidv4()
let timeoutID: ReturnType<typeof setTimeout> | undefined
const successCallback = (position: Position | OSGLOCPosition) => {
if (typeof (this.#timers[id]) == 'undefined') {
// Timeout already happened, or native fired error callback for
// this geo request.
// Don't continue with success callback.
return;
return
}

if (this.#isLegacyPosition(position)) {
Expand All @@ -43,20 +43,20 @@ class OSGeolocation {
// Check our cached position, if its timestamp difference with current time is less than the maximumAge, then just
// fire the success callback with the cached position.
if (this.#lastPosition && options.maximumAge && (((new Date()).getTime() - this.#lastPosition.timestamp) <= options.maximumAge)) {
success(this.#lastPosition);
success(this.#lastPosition)
// If the cached position check failed and the timeout was set to 0, error out with a TIMEOUT error object.
} else if (options.timeout === 0) {
error({
code: 'OS-GLOC-0002',
message: "timeout value in CurrentPositionOptions set to 0 and no cached Position object available, or cached Position object's age exceeds provided CurrentPositionOptions' maximumAge parameter."
});
})
// Otherwise we have to call into native to retrieve a position.
} else {
if (options.timeout !== Infinity) {
// If the timeout value was not set to Infinity (default), then
// set up a timeout function that will fire the error callback
// if no successful position was retrieved before timeout expired.
timeoutID = this.#createTimeout(errorCallback, options.timeout, false, id);
timeoutID = this.#createTimeout(errorCallback, options.timeout, false, id)
this.#timers[id] = timeoutID
}

Expand All @@ -70,17 +70,17 @@ class OSGeolocation {
if (typeof (CapacitorUtils) === 'undefined') {
// if we're not in synapse land, we call the good old bridge or web api
// (it's the same clobber)
return navigator.geolocation.watchPosition(success, error, options);
return navigator.geolocation.watchPosition(success, error, options)
}

let watchId = uuidv4();
let timeoutID: ReturnType<typeof setTimeout> | undefined;
let watchId = uuidv4()
let timeoutID: ReturnType<typeof setTimeout> | undefined
const successCallback = (res: Position | OSGLOCPosition) => {
if (typeof (this.#timers[watchId]) == 'undefined') {
// Timeout already happened, or native fired error callback for
// this geo request.
// Don't continue with success callback.
return;
return
}

if (this.#isLegacyPosition(res)) {
Expand All @@ -102,7 +102,7 @@ class OSGeolocation {
// If the timeout value was not set to Infinity (default), then
// set up a timeout function that will fire the error callback
// if no successful position was retrieved before timeout expired.
timeoutID = this.#createTimeout(errorCallback, options.timeout, true, watchId);
timeoutID = this.#createTimeout(errorCallback, options.timeout, true, watchId)
this.#timers[watchId] = timeoutID
}
options.id = watchId
Expand All @@ -115,17 +115,21 @@ class OSGeolocation {
/**
* Clears the specified heading watch.
*/
clearWatch(success: (output: string) => void, error: (error: PluginError | GeolocationPositionError) => void, options: ClearWatchOptions): void {
clearWatch(options: ClearWatchOptions, success?: () => void, error?: (error: PluginError | GeolocationPositionError) => void): void {
// @ts-ignore
if (typeof (CapacitorUtils) === 'undefined') {
// if we're not in synapse land, we call the good old bridge or web api
// (it's the same clobber)
// @ts-ignore
navigator.geolocation.clearWatch(options.id);
navigator.geolocation.clearWatch(options.id)
return
}

clearTimeout(this.#timers[options.id]);
delete this.#timers[options.id];
if (!success) success = () => { }
if (!error) error = () => { }

clearTimeout(this.#timers[options.id])
delete this.#timers[options.id]
// @ts-ignore
CapacitorUtils.Synapse.OSGeolocation.clearWatch(options, success, error)
}
Expand All @@ -141,24 +145,16 @@ class OSGeolocation {
*/
#createTimeout(onError: (error: PluginError) => void, timeout: number | undefined, isWatch: boolean, id: string): ReturnType<typeof setTimeout> {

function onClearWatchSuccess(output: string) {
// do nothing
}

function onClearWatchError(error: PluginError | GeolocationPositionError) {
// do nothing
}

let t = setTimeout(() => {
if (isWatch === true) {
this.clearWatch(onClearWatchSuccess, onClearWatchError, { id });
this.clearWatch({ id })
}
onError({
code: 'OS-GLOC-0001',
message: 'Position retrieval timed out.'
});
}, timeout);
return t;
})
}, timeout)
return t
}

/**
Expand Down Expand Up @@ -188,8 +184,8 @@ class OSGeolocation {
* @returns true if the object contains the `velocity` property
*/
#isLegacyPosition(position: Position | OSGLOCPosition): position is OSGLOCPosition {
return (position as OSGLOCPosition).velocity !== undefined;
return (position as OSGLOCPosition).velocity !== undefined
}
}

export const OSGeolocationInstance = new OSGeolocation();
export const OSGeolocationInstance = new OSGeolocation()

0 comments on commit 4d745ad

Please sign in to comment.