Skip to content

Commit

Permalink
refactor: abstract to interface
Browse files Browse the repository at this point in the history
  • Loading branch information
wanjas committed Dec 8, 2023
1 parent e685d4b commit 475e4c1
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
5 changes: 4 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
"hooks",
"url",
"state",
"url-state"
"url-state",
"search",
"params",
"query"
],
"description": "React hook for managing state in the URL",
"dependencies": {},
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/handlers.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -16,7 +16,7 @@ export function usePush(router: UrlStateRouter): Push {

export function useHandlers<T extends DefaultSchema>(
push: Push,
stateRef: React.MutableRefObject<UrlState<T>>,
stateRef: MutableRefObject<UrlState<T>>,
) {
const setState = useCallback<UrlStateMethods<T>['setState']>(
(state) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
17 changes: 8 additions & 9 deletions packages/core/src/router.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -14,28 +14,27 @@ export type GenericRouterOptions = {
const subscribers = new Map<Callback, Callback>();

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) {
this.startPolling();
}
}

override unsubscribe(fn: Callback): void {
unsubscribe(fn: Callback): void {
subscribers.delete(fn);

if (subscribers.size === 0) {
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 475e4c1

Please sign in to comment.