Skip to content

Commit

Permalink
Use shallow reactivity where possible to reduce memory overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Aug 30, 2023
1 parent 11fa92f commit dda3cfc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/stores/cluster.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { defineStore, storeToRefs } from 'pinia'
import type { AnyProps } from 'supercluster'
import Supercluster from 'supercluster'
import { ref } from 'vue'
import { shallowRef } from 'vue'
import { useFilters } from './filters'
import type { BoundingBox, Cluster, Location, MemoizedCluster, Point } from '@/types'

export const useCluster = defineStore('cluster', () => {
const { selectedCategories, selectedCurrencies } = storeToRefs(useFilters())

const clusters = ref<Cluster[]>([])
const clusters = shallowRef<Cluster[]>([])

// All items that are not clustered
const singles = ref<Location[]>([])
const singles = shallowRef<Location[]>([])

const BASE_RADIUS = 140 // This is the max cluster radius at zoom level 0
const DECAY_FACTOR = 1.05 // You can adjust this to change how fast the radius decreases
Expand All @@ -26,9 +26,9 @@ export const useCluster = defineStore('cluster', () => {
- Before re-clustering, we check for existing data matching the current zoom, bounding box, and filters.
- If a match is found, we reuse stored clusters; otherwise, new clusters are computed and stored.
*/
const memoizedCluster = ref<Map<number, MemoizedCluster[]>>(new Map())
const memoizedCluster = shallowRef<Map<number, MemoizedCluster[]>>(new Map())

const clusterAlgorithm = ref<Supercluster>()
const clusterAlgorithm = shallowRef<Supercluster>()

function cluster(locations: Location[], { neLat, neLng, swLat, swLng }: BoundingBox, zoom: number) {
const existingData = memoizedCluster.value.get(zoom)
Expand Down
10 changes: 5 additions & 5 deletions src/stores/locations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineStore, storeToRefs } from 'pinia'
import { computed, reactive, ref, watch } from 'vue'
import { computed, ref, shallowReactive, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useRouteQuery } from '@vueuse/router'
import { useMap } from './map'
Expand All @@ -19,8 +19,8 @@ export const useLocations = defineStore('locations', () => {
- Before fetching, we check if the current bounding box is within a larger fetched bounding box.
- If so, the fetch is skipped; otherwise, a new fetch occurs and `memoizedLocations` is updated.
*/
const memoizedLocations = ref<BoundingBox[]>([])
const locationsMap = reactive(new Map<string, Location>())
const memoizedLocations: BoundingBox[] = []
const locationsMap = shallowReactive(new Map<string, Location>())
const locations = computed(() => {
if (!currentBoundingBox.value)
return []
Expand All @@ -36,7 +36,7 @@ export const useLocations = defineStore('locations', () => {
currentBoundingBox.value = boundingBox

// Check if the current bounding box is within an already fetched bounding box
for (const { neLat, neLng, swLat, swLng } of memoizedLocations.value) {
for (const { neLat, neLng, swLat, swLng } of memoizedLocations) {
if (boundingBox.neLat <= neLat && boundingBox.neLng <= neLng && boundingBox.swLat >= swLat && boundingBox.swLng >= swLng)
return
}
Expand All @@ -45,7 +45,7 @@ export const useLocations = defineStore('locations', () => {
newLocations.forEach(newLocation => locationsMap.set(newLocation.uuid, newLocation))

// Update memoizedLocations
memoizedLocations.value.push(boundingBox)
memoizedLocations.push(boundingBox)
loaded.value = true
}

Expand Down
4 changes: 2 additions & 2 deletions src/stores/map.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computed, ref } from 'vue'
import { computed, shallowRef } from 'vue'
import { defineStore, storeToRefs } from 'pinia'
import type { GoogleMap } from 'vue3-google-map'
import { useDebounceFn } from '@vueuse/core'
Expand All @@ -8,7 +8,7 @@ import { useCluster } from './cluster'
import type { EstimatedMapPosition, MapPosition, Point } from '@/types'

export const useMap = defineStore('map', () => {
const mapInstance = ref<typeof GoogleMap>()
const mapInstance = shallowRef<typeof GoogleMap>()
const map = computed(() => mapInstance.value?.map as google.maps.Map)
const center = () => map.value?.getCenter()?.toJSON() as Point
const zoom = () => map.value?.getZoom() as number
Expand Down

0 comments on commit dda3cfc

Please sign in to comment.