From 475e4c1d7a88961683024344b02077458a19479f Mon Sep 17 00:00:00 2001 From: Ivan Belikov Date: Fri, 8 Dec 2023 13:18:31 +0900 Subject: [PATCH] refactor: abstract to interface --- packages/core/package.json | 5 ++++- packages/core/src/handlers.ts | 4 ++-- packages/core/src/index.ts | 2 +- packages/core/src/router.ts | 17 ++++++++--------- packages/core/src/utils.ts | 1 - 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index 3cac91f..01d6b9b 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -13,7 +13,10 @@ "hooks", "url", "state", - "url-state" + "url-state", + "search", + "params", + "query" ], "description": "React hook for managing state in the URL", "dependencies": {}, diff --git a/packages/core/src/handlers.ts b/packages/core/src/handlers.ts index 5110e43..e9b7837 100644 --- a/packages/core/src/handlers.ts +++ b/packages/core/src/handlers.ts @@ -1,4 +1,4 @@ -import { useCallback, useMemo } from 'react'; +import { MutableRefObject, useCallback, useMemo } from 'react'; import { UrlStateRouter } from './router'; import { DefaultSchema, UrlState, UrlStateMethods } from './types'; import { serializeObjectToUrlParams } from './utils'; @@ -16,7 +16,7 @@ export function usePush(router: UrlStateRouter): Push { export function useHandlers( push: Push, - stateRef: React.MutableRefObject>, + stateRef: MutableRefObject>, ) { const setState = useCallback['setState']>( (state) => { diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 6dbf2b7..69331e5 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,6 +1,6 @@ import { useCallback, useEffect, useRef, useState } from 'react'; import { useHandlers, usePush } from './handlers'; -import { GenericRouter, getGenericRouter, UrlStateRouter } from './router'; +import { getGenericRouter, UrlStateRouter } from './router'; import { DefaultSchema, UrlState, diff --git a/packages/core/src/router.ts b/packages/core/src/router.ts index b38fc57..65704dc 100644 --- a/packages/core/src/router.ts +++ b/packages/core/src/router.ts @@ -1,10 +1,10 @@ export type Callback = (newSearchParams: string) => void; -export abstract class UrlStateRouter { - push(href: string): void {} +export interface UrlStateRouter { + push(href: string): void; - subscribe(fn: Callback): void {} - unsubscribe(fn: Callback): void {} + subscribe(fn: Callback): void; + unsubscribe(fn: Callback): void; } export type GenericRouterOptions = { @@ -14,20 +14,19 @@ export type GenericRouterOptions = { const subscribers = new Map(); let genericRouterCurrentStateString = ''; -export class GenericRouter extends UrlStateRouter { +export class GenericRouter implements UrlStateRouter { private interval: number = 0; constructor(private options: GenericRouterOptions) { - super(); this.options = { poolingIntervalMs: 100, ...options }; } - override push(href: string): void { + push(href: string): void { window.history.pushState({}, '', href); this.onSearchParamsChange(); } - override subscribe(fn: Callback): void { + subscribe(fn: Callback): void { subscribers.set(fn, fn); if (!this.interval) { @@ -35,7 +34,7 @@ export class GenericRouter extends UrlStateRouter { } } - override unsubscribe(fn: Callback): void { + unsubscribe(fn: Callback): void { subscribers.delete(fn); if (subscribers.size === 0) { diff --git a/packages/core/src/utils.ts b/packages/core/src/utils.ts index 2c08b5c..10865b9 100644 --- a/packages/core/src/utils.ts +++ b/packages/core/src/utils.ts @@ -1,5 +1,4 @@ import { useRef } from 'react'; -import { DefaultSchema } from './types'; function isNil(value: unknown): value is null | undefined { return value === null || value === undefined;