improved types

main
Inga 🏳‍🌈 1 year ago
parent 35cf2f4fba
commit 50d84576fc
  1. 5
      src/clients/geocoding/osm.ts
  2. 6
      src/clients/geocoding/types.ts
  3. 6
      src/storage/cache.ts
  4. 12
      src/storage/inMemoryDB.ts
  5. 4
      src/utils/throttle.ts

@ -3,8 +3,9 @@ import NodeGeocoder from 'node-geocoder';
import fetch from 'node-fetch'; import fetch from 'node-fetch';
import PQueue from 'p-queue'; import PQueue from 'p-queue';
import { mean } from '../../utils/math'; import { mean } from '../../utils/math';
import type { Geocoder } from './types';
export const createOsmClient = () => { export const createOsmClient = (): Geocoder => {
const geocoder = NodeGeocoder({ const geocoder = NodeGeocoder({
provider: 'openstreetmap', provider: 'openstreetmap',
fetch: (url, options) => { fetch: (url, options) => {
@ -28,7 +29,7 @@ export const createOsmClient = () => {
}); });
return { return {
geocode: async (query: string) => { geocode: async (query) => {
const result = await queue.add(() => geocoder.geocode(query)); const result = await queue.add(() => geocoder.geocode(query));
if (!result.length) { if (!result.length) {

@ -0,0 +1,6 @@
export type Geocoder = {
geocode(query: string): Promise<{
longitude: number;
latitude: number;
}>;
};

@ -1,6 +1,6 @@
import { throttle } from '../utils/throttle'; import { throttle } from '../utils/throttle';
import { StringifiableValue, createSerializer } from '../utils/serializer'; import { type StringifiableValue, createSerializer } from '../utils/serializer';
import { ClearableKeyValueStorage } from './types'; import type { ClearableKeyValueStorage } from './types';
type DataProvider<TKey, TValue> = (key: TKey) => Promise<TValue>; type DataProvider<TKey, TValue> = (key: TKey) => Promise<TValue>;
@ -20,7 +20,7 @@ export const createCachedDataProvider = <
cacheStorage: ClearableKeyValueStorage<string, CacheEntry<TValue>>; cacheStorage: ClearableKeyValueStorage<string, CacheEntry<TValue>>;
getNewValue: DataProvider<TKey, TValue>; getNewValue: DataProvider<TKey, TValue>;
ttlMs: number; ttlMs: number;
}) => { }): DataProvider<TKey, TValue> => {
const keySerializer = createSerializer<TKey>(); const keySerializer = createSerializer<TKey>();
const unsafeGet = async (key: TKey) => { const unsafeGet = async (key: TKey) => {
const cacheEntry = await cacheStorage.get(keySerializer.stringify(key)); const cacheEntry = await cacheStorage.get(keySerializer.stringify(key));

@ -1,10 +1,10 @@
import { sleep } from '../utils/eventLoop'; import { sleep } from '../utils/eventLoop';
import { StringifiableValue, createSerializer } from '../utils/serializer'; import { type StringifiableValue, createSerializer } from '../utils/serializer';
import { ClearableKeyValueStorage } from './types'; import type { ClearableKeyValueStorage } from './types';
export const createKeyValueStorage = <TValue extends StringifiableValue>( export const createKeyValueStorage = <TValue extends StringifiableValue>(
latencyMs: number, latencyMs: number,
) => { ): ClearableKeyValueStorage<string, TValue> => {
const withSimulatedLatency = async <TResult>( const withSimulatedLatency = async <TResult>(
f: () => Promise<TResult>, f: () => Promise<TResult>,
): Promise<TResult> => { ): Promise<TResult> => {
@ -17,7 +17,7 @@ export const createKeyValueStorage = <TValue extends StringifiableValue>(
const serializer = createSerializer<TValue>(); const serializer = createSerializer<TValue>();
const storage = new Map<string, string>(); const storage = new Map<string, string>();
return { return {
get: (key: string) => get: (key) =>
withSimulatedLatency(() => withSimulatedLatency(() =>
Promise.resolve( Promise.resolve(
storage.has(key) storage.has(key)
@ -32,7 +32,7 @@ export const createKeyValueStorage = <TValue extends StringifiableValue>(
}, },
), ),
), ),
set: (key: string, value: TValue) => set: (key, value) =>
withSimulatedLatency(() => withSimulatedLatency(() =>
Promise.resolve( Promise.resolve(
void storage.set(key, serializer.stringify(value)), void storage.set(key, serializer.stringify(value)),
@ -41,5 +41,5 @@ export const createKeyValueStorage = <TValue extends StringifiableValue>(
clear: () => clear: () =>
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
withSimulatedLatency(() => Promise.resolve(storage.clear())), withSimulatedLatency(() => Promise.resolve(storage.clear())),
} as ClearableKeyValueStorage<string, TValue>; };
}; };

@ -23,10 +23,10 @@ import { StringifiableValue, createSerializer } from './serializer';
*/ */
export const throttle = <TArgs extends StringifiableValue[], TResult>( export const throttle = <TArgs extends StringifiableValue[], TResult>(
f: (...args: TArgs) => Promise<TResult>, f: (...args: TArgs) => Promise<TResult>,
) => { ): ((...args: TArgs) => Promise<TResult>) => {
const argsSerializer = createSerializer<TArgs>(); const argsSerializer = createSerializer<TArgs>();
const promises = new Map<string, Promise<TResult>>(); const promises = new Map<string, Promise<TResult>>();
return (...args: TArgs) => { return (...args) => {
const promiseKey = argsSerializer.stringify(args); const promiseKey = argsSerializer.stringify(args);
if (!promises.has(promiseKey)) { if (!promises.has(promiseKey)) {
promises.set( promises.set(

Loading…
Cancel
Save