From 4d745adad257c42c811ed6ace293ca5156767b6b Mon Sep 17 00:00:00 2001 From: Marta Carlos Date: Mon, 6 Jan 2025 10:42:03 +0000 Subject: [PATCH] fix: typos and remove unnecessary callback https://outsystemsrd.atlassian.net/browse/RMET-3900 --- packages/cordova-plugin/dist/plugin.cjs | 4 +- packages/cordova-plugin/dist/plugin.js | 4 +- packages/cordova-plugin/dist/plugin.mjs | 4 +- packages/cordova-plugin/src/web.ts | 8 +-- packages/outsystems-wrapper/package.json | 2 +- packages/outsystems-wrapper/src/index.ts | 62 +++++++++++------------- 6 files changed, 40 insertions(+), 44 deletions(-) diff --git a/packages/cordova-plugin/dist/plugin.cjs b/packages/cordova-plugin/dist/plugin.cjs index 869d0a2..6f848a0 100644 --- a/packages/cordova-plugin/dist/plugin.cjs +++ b/packages/cordova-plugin/dist/plugin.cjs @@ -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 }; @@ -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, diff --git a/packages/cordova-plugin/dist/plugin.js b/packages/cordova-plugin/dist/plugin.js index d3e8ca1..ed01006 100644 --- a/packages/cordova-plugin/dist/plugin.js +++ b/packages/cordova-plugin/dist/plugin.js @@ -91,7 +91,7 @@ accuracy: position.accuracy, heading: position.heading, speed: position.speed, - altitudeAccuracy: position.accuracy + altitudeAccuracy: position.altitudeAccuracy }, timestamp: position.timestamp }; @@ -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, diff --git a/packages/cordova-plugin/dist/plugin.mjs b/packages/cordova-plugin/dist/plugin.mjs index 8c3b503..e1bca81 100644 --- a/packages/cordova-plugin/dist/plugin.mjs +++ b/packages/cordova-plugin/dist/plugin.mjs @@ -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 }; @@ -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, diff --git a/packages/cordova-plugin/src/web.ts b/packages/cordova-plugin/src/web.ts index e4314cf..5a22da8 100644 --- a/packages/cordova-plugin/src/web.ts +++ b/packages/cordova-plugin/src/web.ts @@ -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, } @@ -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, } @@ -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 = { diff --git a/packages/outsystems-wrapper/package.json b/packages/outsystems-wrapper/package.json index 0a6c432..051e7a3 100644 --- a/packages/outsystems-wrapper/package.json +++ b/packages/outsystems-wrapper/package.json @@ -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" diff --git a/packages/outsystems-wrapper/src/index.ts b/packages/outsystems-wrapper/src/index.ts index 350f876..b13b6a8 100644 --- a/packages/outsystems-wrapper/src/index.ts +++ b/packages/outsystems-wrapper/src/index.ts @@ -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 | undefined; } = {} + #timers: { [key: string]: ReturnType | 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 | undefined; + let id = uuidv4() + let timeoutID: ReturnType | 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)) { @@ -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 } @@ -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 | undefined; + let watchId = uuidv4() + let timeoutID: ReturnType | 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)) { @@ -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 @@ -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) } @@ -141,24 +145,16 @@ class OSGeolocation { */ #createTimeout(onError: (error: PluginError) => void, timeout: number | undefined, isWatch: boolean, id: string): ReturnType { - 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 } /** @@ -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(); \ No newline at end of file +export const OSGeolocationInstance = new OSGeolocation() \ No newline at end of file