Skip to content

Commit 475e4c1

Browse files
committed
refactor: abstract to interface
1 parent e685d4b commit 475e4c1

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

packages/core/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
"hooks",
1414
"url",
1515
"state",
16-
"url-state"
16+
"url-state",
17+
"search",
18+
"params",
19+
"query"
1720
],
1821
"description": "React hook for managing state in the URL",
1922
"dependencies": {},

packages/core/src/handlers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useMemo } from 'react';
1+
import { MutableRefObject, useCallback, useMemo } from 'react';
22
import { UrlStateRouter } from './router';
33
import { DefaultSchema, UrlState, UrlStateMethods } from './types';
44
import { serializeObjectToUrlParams } from './utils';
@@ -16,7 +16,7 @@ export function usePush(router: UrlStateRouter): Push {
1616

1717
export function useHandlers<T extends DefaultSchema>(
1818
push: Push,
19-
stateRef: React.MutableRefObject<UrlState<T>>,
19+
stateRef: MutableRefObject<UrlState<T>>,
2020
) {
2121
const setState = useCallback<UrlStateMethods<T>['setState']>(
2222
(state) => {

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useCallback, useEffect, useRef, useState } from 'react';
22
import { useHandlers, usePush } from './handlers';
3-
import { GenericRouter, getGenericRouter, UrlStateRouter } from './router';
3+
import { getGenericRouter, UrlStateRouter } from './router';
44
import {
55
DefaultSchema,
66
UrlState,

packages/core/src/router.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
export type Callback = (newSearchParams: string) => void;
22

3-
export abstract class UrlStateRouter {
4-
push(href: string): void {}
3+
export interface UrlStateRouter {
4+
push(href: string): void;
55

6-
subscribe(fn: Callback): void {}
7-
unsubscribe(fn: Callback): void {}
6+
subscribe(fn: Callback): void;
7+
unsubscribe(fn: Callback): void;
88
}
99

1010
export type GenericRouterOptions = {
@@ -14,28 +14,27 @@ export type GenericRouterOptions = {
1414
const subscribers = new Map<Callback, Callback>();
1515

1616
let genericRouterCurrentStateString = '';
17-
export class GenericRouter extends UrlStateRouter {
17+
export class GenericRouter implements UrlStateRouter {
1818
private interval: number = 0;
1919

2020
constructor(private options: GenericRouterOptions) {
21-
super();
2221
this.options = { poolingIntervalMs: 100, ...options };
2322
}
2423

25-
override push(href: string): void {
24+
push(href: string): void {
2625
window.history.pushState({}, '', href);
2726
this.onSearchParamsChange();
2827
}
2928

30-
override subscribe(fn: Callback): void {
29+
subscribe(fn: Callback): void {
3130
subscribers.set(fn, fn);
3231

3332
if (!this.interval) {
3433
this.startPolling();
3534
}
3635
}
3736

38-
override unsubscribe(fn: Callback): void {
37+
unsubscribe(fn: Callback): void {
3938
subscribers.delete(fn);
4039

4140
if (subscribers.size === 0) {

packages/core/src/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { useRef } from 'react';
2-
import { DefaultSchema } from './types';
32

43
function isNil(value: unknown): value is null | undefined {
54
return value === null || value === undefined;

0 commit comments

Comments
 (0)