Skip to content

Commit

Permalink
fix recursive requests
Browse files Browse the repository at this point in the history
  • Loading branch information
assoft committed Feb 26, 2023
1 parent 42ecfbe commit 8b685db
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 163 deletions.
23 changes: 6 additions & 17 deletions app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@
<Body class="dark font-sans relative">
<div style="padding: 4px; height: calc(100vh - 12px);" data-tauri-drag-region class="select-none">
<div class="pb-16">
<ListAfad v-if="source === 'afad'" />
<ListKandilli v-else />
<NuxtPage />
</div>

<div class="fixed bottom-0 inset-x-0 select-none">
<div class="flex items-center justify-between bg-dark-3 p-2 dark:text-white select-none">
<div class="flex items-center justify-center gap-4">
<div class="inline-flex items-center gap-1">
<input class="w-6 h-6 text-red" type="radio" id="afad" name="source" v-model="source" value="afad"
checked>
<label class="text-sm" for="afad">Afad</label>
</div>
<div class="inline-flex items-center gap-1">
<input class="w-6 h-6 text-amber" type="radio" id="kandilli" name="source" v-model="source" value="kandilli">
<label class="text-sm" for="kandilli">Kandilli</label>
</div>

<div class="flex items-center justify-center gap-2">
<NuxtLink class="decoration-none text-white px-1.5 py-1 rounded" exact-active-class="bg-white/20" to="/">Afad</NuxtLink>
<NuxtLink class="decoration-none text-white px-1.5 py-1 rounded" exact-active-class="bg-white/20" to="/kandilli">Kandilli</NuxtLink>
</div>

<div class="inline-flex items-center gap-1">
Expand Down Expand Up @@ -51,7 +44,7 @@
<script setup lang="ts">
import { invoke } from '@tauri-apps/api';
const source = useState<string>('activeSource', () => 'afad');
// const source = useState<string>('activeSource', () => 'afad');
const refreshFrequency = useState<number>('refreshFrequency', () => 60);
const notifyQuakeSize = useState<number>('notifyQuakeSize', () => 3);
const frequencies = ref<number[]>([10, 30, 60, 120, 300, 600])
Expand All @@ -60,10 +53,6 @@ const frequencies = ref<number[]>([10, 30, 60, 120, 300, 600])
const setRefreshFrequency = (event: Event) => refreshFrequency.value = event.target.value
// @ts-ignore
const setNotifyQuakeSize = (event: Event) => notifyQuakeSize.value = event.target.value
const disableScroll = (event: any) => {
// console.log({ event })
}
</script>

<style>
Expand Down
68 changes: 0 additions & 68 deletions components/ListAfad.vue

This file was deleted.

77 changes: 0 additions & 77 deletions components/ListKandilli.vue

This file was deleted.

2 changes: 1 addition & 1 deletion nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
ssr: false,
modules: ['@unocss/nuxt'],
modules: ['@unocss/nuxt', '@vueuse/nuxt'],
})
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
},
"dependencies": {
"@tauri-apps/api": "^1.2.0",
"@vueuse/core": "^9.13.0",
"@vueuse/nuxt": "^9.13.0",
"luxon": "^3.2.1"
},
"pnpm": {
Expand Down
62 changes: 62 additions & 0 deletions pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<template>
<div v-if="quakes?.length > 0" class="grid">
<QuakeItem :has-time-zone="true" v-for="quake in sortedQuakes" :key="quake.id" :quake="quake" />
<Loading v-if="loading" />
</div>
</template>

<script setup lang="ts">
import { invoke } from "@tauri-apps/api"
import type { IQuake } from "@/types"
const router = useRouter();
const quakes = ref<IQuake[]>([])
const loading = ref<boolean>(false);
const refreshFrequency = useState<number>('refreshFrequency')
const notifyQuakeSize = useState<number>('notifyQuakeSize')
// @ts-ignore
const sortedQuakes = computed(() => quakes.value.sort((a: IQuake, b: IQuake) => a.eventDate < b.eventDate))
const fetchLatestQuakesFromAfad = async () => {
if (router.currentRoute.value.name === 'index') {
loading.value = true;
const result = await invoke<string>("get_last_five_events")
const parsed: IQuake[] = JSON.parse(result) as IQuake[]
if (quakes.value.length >= 100) {
quakes.value = [];
}
if (quakes.value.length > 0) {
parsed.map(async (quake) => {
const filtered = quakes.value.find(x => x.id === quake.id)
if (!filtered) {
if (quake.magnitude >= notifyQuakeSize.value) {
await pushMessage({ title: `Afad Deprem Merkezi`, body: `${quake.location}: ${quake.magnitude} [${quake.depth}km]` });
}
quakes.value.push(quake)
}
})
} else {
quakes.value = parsed;
}
setTimeout(() => loading.value = false, 1800)
}
}
const { resume, pause } = useIntervalFn(() => {
fetchLatestQuakesFromAfad()
}, () => refreshFrequency.value * 1000)
onMounted(async () => {
quakes.value = await fecthLatestFiftyEvents();
resume();
})
watchEffect(() => resume())
onUnmounted(() => pause())
</script>
72 changes: 72 additions & 0 deletions pages/kandilli.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<template>
<div class="grid">
<template v-if="quakes?.length > 0">
<QuakeItem :has-time-zone="false" v-for="quake in sortedQuakes" :key="quake.id" :quake="quake" />
</template>
<Loading v-if="loading" />
</div>
</template>

<script setup lang="ts">
import { invoke } from "@tauri-apps/api"
import { useIntervalFn } from '@vueuse/core'
import type { PIQuake } from "@/types"
const quakes = ref<PIQuake[]>([])
const loading = ref<boolean>(false);
const refreshFrequency = useState<number>('refreshFrequency')
const notifyQuakeSize = useState<number>('notifyQuakeSize')
const router = useRouter();
// @ts-ignore
const sortedQuakes = computed(() => quakes.value.sort((a: PIQuake, b: PIQuake) => a.eventDate < b.eventDate))
const fetchLatestQuakesFromKandilli = async () => {
if (router.currentRoute.value.name === 'kandilli') {
loading.value = true;
const result = await invoke<string>("fetch_quakes")
const parsed: PIQuake[] = JSON.parse(result) as PIQuake[]
const mapped: PIQuake[] = parsed.map((quake: PIQuake) => {
return {
id: quake.id,
eventDate: quake.eventDate,
magnitude: parseFloat(String(quake.magnitude)),
location: quake.location,
depth: parseInt(String(quake.depth))
}
})
if (quakes.value.length > 0) {
mapped.map(async (quake) => {
const filtered = quakes.value.find((x: PIQuake) => x.id === quake.id)
if (!filtered) {
if (quake.magnitude >= notifyQuakeSize.value) {
await pushMessage({ title: `Kandilli Rasathanesi`, body: `${quake.location}: ${quake.magnitude} [${quake.depth}km]` });
}
quakes.value.push(quake)
}
})
} else {
quakes.value = mapped;
}
setTimeout(() => loading.value = false, 1700)
}
}
const { resume, pause } = useIntervalFn(() => {
fetchLatestQuakesFromKandilli()
}, () => refreshFrequency.value * 1000)
onMounted(() => {
fetchLatestQuakesFromKandilli()
resume()
})
onUnmounted(() => pause())
</script>
Loading

0 comments on commit 8b685db

Please sign in to comment.