add local storage/lru cache

This commit is contained in:
Michael Fatemi 2021-06-26 20:40:00 -04:00
parent 0758b36479
commit a6077d7750
3 changed files with 99 additions and 5 deletions

View File

@ -1,5 +1,4 @@
import { useState } from 'react';
import Availability, { AvailabilityKind } from './Availability';
import UIButton from './UIButton';
import UIPlacesAutocomplete from './UIPlacesAutocomplete';
import UISecondaryBox from './UISecondaryBox';
@ -108,16 +107,20 @@ export default function Event({
const [rideTherePickupPlaceID, setRideTherePickupPlaceID] = useState('');
const [rideBackDropoffPlaceID, setRideBackDropoffPlaceID] = useState('');
const [confirmed, setConfirmed] = useState(false);
const [availability, setAvailability] =
useState<AvailabilityKind>('not-interested');
const [interested, setInterested] = useState(false);
return (
<UISecondaryBox>
<UISecondaryHeader>{name}</UISecondaryHeader>
<GroupName name={group} />
<Details {...{ startTime, endTime, formattedAddress }} />
<Availability selected={availability} onSelected={setAvailability} />
{availability === 'interested' && (
<UIButton
onClick={() => setInterested((i) => !i)}
style={{ backgroundColor: '#e0e0e0' }}
>
{interested ? 'Interested' : 'Not interested'}
</UIButton>
{interested && (
<>
<div
style={{

View File

@ -0,0 +1,45 @@
const KEY = 'ws.addresslist';
interface Address {
placeId: string;
formattedAddress: string;
latitude: string;
longitude: string;
}
type AddressBook = Map<string, Address>;
function load(): AddressBook {
const string = localStorage.getItem(KEY);
if (string) {
const object = JSON.parse(string);
const map = new Map<string, Address>(Object.entries(object));
return map;
}
localStorage.setItem(KEY, '{}');
return new Map();
}
function save(map: AddressBook) {
const object = Object.fromEntries(map);
const string = JSON.stringify(object);
localStorage.setItem(KEY, string);
}
class LocalStorageManager {
private internalValue: AddressBook;
constructor(capacity = 5) {
this.internalValue = load();
}
addAddress(address: Address) {
this.internalValue.set(address.placeId, address);
save(this.internalValue);
}
getAddresses() {
return Object.values(this.internalValue);
}
}
const WheelshareLocalStorage = new LocalStorageManager();
export default WheelshareLocalStorage;

View File

@ -0,0 +1,46 @@
class LRUCache<K, V> {
private cap: number;
private map: Map<K, V>;
constructor(capacity: number) {
this.cap = capacity;
this.map = new Map();
}
set(key: K, value: V) {
this.map.delete(key);
this.map.set(key, value);
if (this.map.size > this.cap) {
const next = this.map.keys().next();
if (!next.done) this.map.delete(next.value);
}
}
get(key: K) {
var value = this.map.get(key);
if (value != null) {
this.map.delete(key);
this.map.set(key, value);
}
return value;
}
has(key: K) {
return this.map.has(key);
}
delete(key: K) {
this.map.delete(key);
}
size() {
return this.map.size;
}
capacity() {
return this.cap - this.map.size;
}
clear() {
this.map.clear();
}
}
export function create(capacity: number) {
return new LRUCache(capacity);
}