improved types

main
Inga 🏳‍🌈 7 months 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 PQueue from 'p-queue';
import { mean } from '../../utils/math';
import type { Geocoder } from './types';
export const createOsmClient = () => {
export const createOsmClient = (): Geocoder => {
const geocoder = NodeGeocoder({
provider: 'openstreetmap',
fetch: (url, options) => {
@ -28,7 +29,7 @@ export const createOsmClient = () => {
});
return {
geocode: async (query: string) => {
geocode: async (query) => {
const result = await queue.add(() => geocoder.geocode(query));
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 { StringifiableValue, createSerializer } from '../utils/serializer';
import { ClearableKeyValueStorage } from './types';
import { type StringifiableValue, createSerializer } from '../utils/serializer';
import type { ClearableKeyValueStorage } from './types';
type DataProvider<TKey, TValue> = (key: TKey) => Promise<TValue>;
@ -20,7 +20,7 @@ export const createCachedDataProvider = <
cacheStorage: ClearableKeyValueStorage<string, CacheEntry<TValue>>;
getNewValue: DataProvider<TKey, TValue>;
ttlMs: number;
}) => {
}): DataProvider<TKey, TValue> => {
const keySerializer = createSerializer<TKey>();
const unsafeGet = async (key: TKey) => {
const cacheEntry = await cacheStorage.get(keySerializer.stringify(key));

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

Loading…
Cancel
Save