Skip to content

Commit

Permalink
fix(types): allow using interface for replicant type
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoishin committed Mar 22, 2024
1 parent 8235937 commit 367c457
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
25 changes: 18 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
"dependencies": {
"@nodecg/types": "^2.1.8",
"klona": "^2.0.6",
"tslib": "^2.0.0"
"tslib": "^2.0.0",
"type-fest": "^4.13.1"
},
"devDependencies": {
"@hoishin/prettierrc": "2.1.1",
Expand Down
20 changes: 10 additions & 10 deletions src/use-replicant.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { useEffect, useMemo, useState } from "react";
import { useCallback, useEffect, useMemo, useState } from "react";
import { klona as clone } from "klona/json";

type JsonValue = boolean | number | string | null;

type Json = JsonValue | JsonValue[] | { [key: string]: Json } | Json[];
import { Jsonify } from "type-fest";

export type UseReplicantOptions<T> = {
defaultValue?: T;
Expand All @@ -19,7 +16,7 @@ export type UseReplicantOptions<T> = {
* @param initialValue Initial value to pass to `useState` function
* @param options Options object. Currently supports the optional `namespace` option
*/
export const useReplicant = <T extends Json>(
export const useReplicant = <V, T = Jsonify<V>>(
replicantName: string,
{ bundle, defaultValue, persistent }: UseReplicantOptions<T> = {},
) => {
Expand Down Expand Up @@ -50,14 +47,17 @@ export const useReplicant = <T extends Json>(
};
}, [replicant]);

return [
value,
const updateValue = useCallback(
(newValue: T | ((oldValue?: T) => void)) => {
if (typeof newValue === "function") {
newValue(replicant.value);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
(newValue as any)(replicant.value);

Check warning on line 54 in src/use-replicant.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
} else {
replicant.value = newValue;
}
},
] as const;
[replicant],
);

return [value, updateValue] as const;
};
29 changes: 28 additions & 1 deletion tests/use-replicant.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { EventEmitter } from "events";

import React from "react";
import React, { useEffect } from "react";
import { render, act, fireEvent } from "@testing-library/react";
import type { RenderResult } from "@testing-library/react";

Expand Down Expand Up @@ -78,6 +78,33 @@ type RunnerNameReplicant = {
};
};

interface DummyTypeInterface {
runner: {
name: string;
};
}

interface DummyTypeInterface2 {
foo: Date;
bar: () => number;
}

export const DummyComponent = () => {
const [_, setDummy] = useReplicant<DummyTypeInterface>("foo");

Check warning on line 93 in tests/use-replicant.spec.tsx

View workflow job for this annotation

GitHub Actions / build

'_' is assigned a value but never used
useEffect(() => {
setDummy({ runner: { name: "bar" } });
setDummy((oldValue) => {
if (oldValue) {
oldValue.runner.name += "name";
}
});
}, [setDummy]);

useReplicant<DummyTypeInterface2>("bar");

return null;
};

const RunnerName: React.FC<RunnerNameProps> = (props) => {
const { prefix } = props;
const repName = `${prefix ?? "default"}:currentRun`;
Expand Down

0 comments on commit 367c457

Please sign in to comment.