@ailuracode/alpine-geo 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -33,6 +33,7 @@ function clearPosition(store) {
|
|
|
33
33
|
function geoPlugin(Alpine) {
|
|
34
34
|
const supported = typeof navigator.geolocation?.getCurrentPosition === "function";
|
|
35
35
|
let watchId = null;
|
|
36
|
+
let requestGeneration = 0;
|
|
36
37
|
const geoStore = {
|
|
37
38
|
latitude: null,
|
|
38
39
|
longitude: null,
|
|
@@ -70,14 +71,23 @@ function geoPlugin(Alpine) {
|
|
|
70
71
|
this.loading = true;
|
|
71
72
|
this.error = null;
|
|
72
73
|
this.errorCode = null;
|
|
74
|
+
const generation = ++requestGeneration;
|
|
73
75
|
return new Promise((resolve) => {
|
|
74
76
|
navigator.geolocation.getCurrentPosition(
|
|
75
77
|
(position) => {
|
|
78
|
+
if (generation !== requestGeneration) {
|
|
79
|
+
resolve(false);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
76
82
|
applyPosition(this, position);
|
|
77
83
|
this.loading = false;
|
|
78
84
|
resolve(true);
|
|
79
85
|
},
|
|
80
86
|
(error) => {
|
|
87
|
+
if (generation !== requestGeneration) {
|
|
88
|
+
resolve(false);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
81
91
|
clearCoords(this);
|
|
82
92
|
applyError(this, error);
|
|
83
93
|
this.loading = false;
|
|
@@ -98,6 +108,7 @@ function geoPlugin(Alpine) {
|
|
|
98
108
|
applyPosition(this, position);
|
|
99
109
|
},
|
|
100
110
|
(error) => {
|
|
111
|
+
clearCoords(this);
|
|
101
112
|
applyError(this, error);
|
|
102
113
|
},
|
|
103
114
|
options
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type AlpineType from \"alpinejs\";\n\nexport interface GeoPositionOptions {\n enableHighAccuracy?: boolean;\n timeout?: number;\n maximumAge?: number;\n}\n\nexport interface GeoStore {\n latitude: number | null;\n longitude: number | null;\n accuracy: number | null;\n altitude: number | null;\n altitudeAccuracy: number | null;\n heading: number | null;\n speed: number | null;\n timestamp: number | null;\n error: string | null;\n errorCode: number | null;\n loading: boolean;\n watching: boolean;\n request(options?: GeoPositionOptions): Promise<boolean>;\n watch(options?: GeoPositionOptions): boolean;\n unwatch(): boolean;\n reset(): boolean;\n readonly hasPosition: boolean;\n readonly isSupported: boolean;\n readonly isWatching: boolean;\n readonly isLoading: boolean;\n readonly hasError: boolean;\n}\n\nfunction applyPosition(store: GeoStore, position: GeolocationPosition): void {\n store.latitude = position.coords.latitude;\n store.longitude = position.coords.longitude;\n store.accuracy = position.coords.accuracy;\n store.altitude = position.coords.altitude;\n store.altitudeAccuracy = position.coords.altitudeAccuracy;\n store.heading = position.coords.heading;\n store.speed = position.coords.speed;\n store.timestamp = position.timestamp;\n store.error = null;\n store.errorCode = null;\n}\n\nfunction applyError(store: GeoStore, error: GeolocationPositionError): void {\n store.error = error.message;\n store.errorCode = error.code;\n}\n\nfunction clearCoords(store: GeoStore): void {\n store.latitude = null;\n store.longitude = null;\n store.accuracy = null;\n store.altitude = null;\n store.altitudeAccuracy = null;\n store.heading = null;\n store.speed = null;\n store.timestamp = null;\n}\n\nfunction clearPosition(store: GeoStore): void {\n clearCoords(store);\n store.error = null;\n store.errorCode = null;\n}\n\n/** Alpine.js geolocation plugin. Registers `$store.geo`. */\nexport default function geoPlugin(Alpine: AlpineType.Alpine): void {\n const supported = typeof navigator.geolocation?.getCurrentPosition === \"function\";\n let watchId: number | null = null;\n\n const geoStore: GeoStore = {\n latitude: null,\n longitude: null,\n accuracy: null,\n altitude: null,\n altitudeAccuracy: null,\n heading: null,\n speed: null,\n timestamp: null,\n error: null,\n errorCode: null,\n loading: false,\n watching: false,\n\n get hasPosition() {\n return this.latitude !== null && this.longitude !== null;\n },\n\n get isSupported() {\n return supported;\n },\n\n get isWatching() {\n return this.watching;\n },\n\n get isLoading() {\n return this.loading;\n },\n\n get hasError() {\n return this.error !== null;\n },\n\n request(options?: GeoPositionOptions) {\n if (!supported) {\n this.error = \"Geolocation is not supported\";\n this.errorCode = null;\n return Promise.resolve(false);\n }\n\n this.loading = true;\n this.error = null;\n this.errorCode = null;\n\n return new Promise((resolve) => {\n navigator.geolocation.getCurrentPosition(\n (position) => {\n applyPosition(this, position);\n this.loading = false;\n resolve(true);\n },\n (error) => {\n clearCoords(this);\n applyError(this, error);\n this.loading = false;\n resolve(false);\n },\n options\n );\n });\n },\n\n watch(options?: GeoPositionOptions) {\n if (!supported || this.watching) {\n return false;\n }\n\n this.error = null;\n this.errorCode = null;\n\n watchId = navigator.geolocation.watchPosition(\n (position) => {\n applyPosition(this, position);\n },\n (error) => {\n applyError(this, error);\n },\n options\n );\n\n this.watching = true;\n return true;\n },\n\n unwatch() {\n if (watchId === null) {\n return false;\n }\n\n navigator.geolocation.clearWatch(watchId);\n watchId = null;\n this.watching = false;\n return true;\n },\n\n reset() {\n clearPosition(this);\n return true;\n },\n };\n\n Alpine.store(\"geo\", geoStore);\n}\n\ndeclare global {\n namespace Alpine {\n interface Stores {\n geo: GeoStore;\n }\n }\n}\n"],"mappings":";AAgCA,SAAS,cAAc,OAAiB,UAAqC;AAC3E,QAAM,WAAW,SAAS,OAAO;AACjC,QAAM,YAAY,SAAS,OAAO;AAClC,QAAM,WAAW,SAAS,OAAO;AACjC,QAAM,WAAW,SAAS,OAAO;AACjC,QAAM,mBAAmB,SAAS,OAAO;AACzC,QAAM,UAAU,SAAS,OAAO;AAChC,QAAM,QAAQ,SAAS,OAAO;AAC9B,QAAM,YAAY,SAAS;AAC3B,QAAM,QAAQ;AACd,QAAM,YAAY;AACpB;AAEA,SAAS,WAAW,OAAiB,OAAuC;AAC1E,QAAM,QAAQ,MAAM;AACpB,QAAM,YAAY,MAAM;AAC1B;AAEA,SAAS,YAAY,OAAuB;AAC1C,QAAM,WAAW;AACjB,QAAM,YAAY;AAClB,QAAM,WAAW;AACjB,QAAM,WAAW;AACjB,QAAM,mBAAmB;AACzB,QAAM,UAAU;AAChB,QAAM,QAAQ;AACd,QAAM,YAAY;AACpB;AAEA,SAAS,cAAc,OAAuB;AAC5C,cAAY,KAAK;AACjB,QAAM,QAAQ;AACd,QAAM,YAAY;AACpB;AAGe,SAAR,UAA2B,QAAiC;AACjE,QAAM,YAAY,OAAO,UAAU,aAAa,uBAAuB;AACvE,MAAI,UAAyB;
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type AlpineType from \"alpinejs\";\n\nexport interface GeoPositionOptions {\n enableHighAccuracy?: boolean;\n timeout?: number;\n maximumAge?: number;\n}\n\nexport interface GeoStore {\n latitude: number | null;\n longitude: number | null;\n accuracy: number | null;\n altitude: number | null;\n altitudeAccuracy: number | null;\n heading: number | null;\n speed: number | null;\n timestamp: number | null;\n error: string | null;\n errorCode: number | null;\n loading: boolean;\n watching: boolean;\n request(options?: GeoPositionOptions): Promise<boolean>;\n watch(options?: GeoPositionOptions): boolean;\n unwatch(): boolean;\n reset(): boolean;\n readonly hasPosition: boolean;\n readonly isSupported: boolean;\n readonly isWatching: boolean;\n readonly isLoading: boolean;\n readonly hasError: boolean;\n}\n\nfunction applyPosition(store: GeoStore, position: GeolocationPosition): void {\n store.latitude = position.coords.latitude;\n store.longitude = position.coords.longitude;\n store.accuracy = position.coords.accuracy;\n store.altitude = position.coords.altitude;\n store.altitudeAccuracy = position.coords.altitudeAccuracy;\n store.heading = position.coords.heading;\n store.speed = position.coords.speed;\n store.timestamp = position.timestamp;\n store.error = null;\n store.errorCode = null;\n}\n\nfunction applyError(store: GeoStore, error: GeolocationPositionError): void {\n store.error = error.message;\n store.errorCode = error.code;\n}\n\nfunction clearCoords(store: GeoStore): void {\n store.latitude = null;\n store.longitude = null;\n store.accuracy = null;\n store.altitude = null;\n store.altitudeAccuracy = null;\n store.heading = null;\n store.speed = null;\n store.timestamp = null;\n}\n\nfunction clearPosition(store: GeoStore): void {\n clearCoords(store);\n store.error = null;\n store.errorCode = null;\n}\n\n/** Alpine.js geolocation plugin. Registers `$store.geo`. */\nexport default function geoPlugin(Alpine: AlpineType.Alpine): void {\n const supported = typeof navigator.geolocation?.getCurrentPosition === \"function\";\n let watchId: number | null = null;\n let requestGeneration = 0;\n\n const geoStore: GeoStore = {\n latitude: null,\n longitude: null,\n accuracy: null,\n altitude: null,\n altitudeAccuracy: null,\n heading: null,\n speed: null,\n timestamp: null,\n error: null,\n errorCode: null,\n loading: false,\n watching: false,\n\n get hasPosition() {\n return this.latitude !== null && this.longitude !== null;\n },\n\n get isSupported() {\n return supported;\n },\n\n get isWatching() {\n return this.watching;\n },\n\n get isLoading() {\n return this.loading;\n },\n\n get hasError() {\n return this.error !== null;\n },\n\n request(options?: GeoPositionOptions) {\n if (!supported) {\n this.error = \"Geolocation is not supported\";\n this.errorCode = null;\n return Promise.resolve(false);\n }\n\n this.loading = true;\n this.error = null;\n this.errorCode = null;\n\n const generation = ++requestGeneration;\n\n return new Promise((resolve) => {\n navigator.geolocation.getCurrentPosition(\n (position) => {\n if (generation !== requestGeneration) {\n resolve(false);\n return;\n }\n\n applyPosition(this, position);\n this.loading = false;\n resolve(true);\n },\n (error) => {\n if (generation !== requestGeneration) {\n resolve(false);\n return;\n }\n\n clearCoords(this);\n applyError(this, error);\n this.loading = false;\n resolve(false);\n },\n options\n );\n });\n },\n\n watch(options?: GeoPositionOptions) {\n if (!supported || this.watching) {\n return false;\n }\n\n this.error = null;\n this.errorCode = null;\n\n watchId = navigator.geolocation.watchPosition(\n (position) => {\n applyPosition(this, position);\n },\n (error) => {\n clearCoords(this);\n applyError(this, error);\n },\n options\n );\n\n this.watching = true;\n return true;\n },\n\n unwatch() {\n if (watchId === null) {\n return false;\n }\n\n navigator.geolocation.clearWatch(watchId);\n watchId = null;\n this.watching = false;\n return true;\n },\n\n reset() {\n clearPosition(this);\n return true;\n },\n };\n\n Alpine.store(\"geo\", geoStore);\n}\n\ndeclare global {\n namespace Alpine {\n interface Stores {\n geo: GeoStore;\n }\n }\n}\n"],"mappings":";AAgCA,SAAS,cAAc,OAAiB,UAAqC;AAC3E,QAAM,WAAW,SAAS,OAAO;AACjC,QAAM,YAAY,SAAS,OAAO;AAClC,QAAM,WAAW,SAAS,OAAO;AACjC,QAAM,WAAW,SAAS,OAAO;AACjC,QAAM,mBAAmB,SAAS,OAAO;AACzC,QAAM,UAAU,SAAS,OAAO;AAChC,QAAM,QAAQ,SAAS,OAAO;AAC9B,QAAM,YAAY,SAAS;AAC3B,QAAM,QAAQ;AACd,QAAM,YAAY;AACpB;AAEA,SAAS,WAAW,OAAiB,OAAuC;AAC1E,QAAM,QAAQ,MAAM;AACpB,QAAM,YAAY,MAAM;AAC1B;AAEA,SAAS,YAAY,OAAuB;AAC1C,QAAM,WAAW;AACjB,QAAM,YAAY;AAClB,QAAM,WAAW;AACjB,QAAM,WAAW;AACjB,QAAM,mBAAmB;AACzB,QAAM,UAAU;AAChB,QAAM,QAAQ;AACd,QAAM,YAAY;AACpB;AAEA,SAAS,cAAc,OAAuB;AAC5C,cAAY,KAAK;AACjB,QAAM,QAAQ;AACd,QAAM,YAAY;AACpB;AAGe,SAAR,UAA2B,QAAiC;AACjE,QAAM,YAAY,OAAO,UAAU,aAAa,uBAAuB;AACvE,MAAI,UAAyB;AAC7B,MAAI,oBAAoB;AAExB,QAAM,WAAqB;AAAA,IACzB,UAAU;AAAA,IACV,WAAW;AAAA,IACX,UAAU;AAAA,IACV,UAAU;AAAA,IACV,kBAAkB;AAAA,IAClB,SAAS;AAAA,IACT,OAAO;AAAA,IACP,WAAW;AAAA,IACX,OAAO;AAAA,IACP,WAAW;AAAA,IACX,SAAS;AAAA,IACT,UAAU;AAAA,IAEV,IAAI,cAAc;AAChB,aAAO,KAAK,aAAa,QAAQ,KAAK,cAAc;AAAA,IACtD;AAAA,IAE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AACR,UAAI,YAAY,MAAM;AACpB,eAAO;AAAA,MACT;AAEA,gBAAU,YAAY,WAAW,OAAO;AACxC,gBAAU;AACV,WAAK,WAAW;AAChB,aAAO;AAAA,IACT;AAAA,IAEA,QAAQ;AACN,oBAAc,IAAI;AAClB,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO,MAAM,OAAO,QAAQ;AAC9B;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ailuracode/alpine-geo",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Alpine.js geolocation store",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -50,6 +50,6 @@
|
|
|
50
50
|
],
|
|
51
51
|
"scripts": {
|
|
52
52
|
"build": "tsup src/index.ts --format esm --dts --clean --sourcemap --out-dir dist && cp src/global.d.ts dist/global.d.ts",
|
|
53
|
-
"test": "vitest run --config ../../vitest.config.
|
|
53
|
+
"test": "vitest run --config ../../vitest.config.ts test"
|
|
54
54
|
}
|
|
55
55
|
}
|