Skip to content

Commit

Permalink
chore: update prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoishin committed Jan 1, 2024
1 parent 33a7261 commit 5e8766b
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 143 deletions.
8 changes: 4 additions & 4 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
coverage:
notify:
slack:
default:
url: secret:kFNdViKBbWbr8jBt/VQYnmKF+bwuJY+6MTjCLVYtGov7Dqrzxkzu3nzcwbk4JL3869xIh4O7oir1/P7su/t7UkB5K3yN7wMlSWsABSBZK5cSa0K5ZkvnNvszoGGw5CpUeE8IeFEr+c+QvijvSfv0DvY/ZwTJHOXMXXrjUf3GZj8=
notify:
slack:
default:
url: secret:kFNdViKBbWbr8jBt/VQYnmKF+bwuJY+6MTjCLVYtGov7Dqrzxkzu3nzcwbk4JL3869xIh4O7oir1/P7su/t7UkB5K3yN7wMlSWsABSBZK5cSa0K5ZkvnNvszoGGw5CpUeE8IeFEr+c+QvijvSfv0DvY/ZwTJHOXMXXrjUf3GZj8=
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = unset
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = true

[*.{md,yml,yaml}]
indent_style = space
indent_size = 2
indent_style = space

[*.md]
trim_trailing_whitespace = false
32 changes: 16 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
name: CI

on:
push:
branches:
- main
pull_request:
branches:
- main
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm ci
- run: npm run test
- run: npm run build
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "18"
- run: npm ci
- run: npm run test
- run: npm run build
58 changes: 29 additions & 29 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
on:
push:
branches:
- main
push:
branches:
- main

permissions:
contents: write
pull-requests: write
contents: write
pull-requests: write

name: release-please

jobs:
release-please:
runs-on: ubuntu-latest
outputs:
release_created: ${{ steps.release-please.outputs.release_created }}
release_name: ${{ steps.release-please.outputs.release_name }}
steps:
- uses: google-github-actions/release-please-action@v3
id: release-please
with:
release-type: node
package-name: '@nodecg/react-hooks'
release-please:
runs-on: ubuntu-latest
outputs:
release_created: ${{ steps.release-please.outputs.release_created }}
release_name: ${{ steps.release-please.outputs.release_name }}
steps:
- uses: google-github-actions/release-please-action@v3
id: release-please
with:
release-type: node
package-name: "@nodecg/react-hooks"

publish:
needs: release-please
runs-on: ubuntu-latest
if: ${{ needs.release-please.outputs.release_created }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: lts
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
publish:
needs: release-please
runs-on: ubuntu-latest
if: ${{ needs.release-please.outputs.release_created }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: lts
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
74 changes: 37 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ The React Hooks are a new way of **sharing code** between components, introduced

**_Please read the documentation of React Hooks thoroughly before using them._**

- [Video introduction](https://youtu.be/dpw9EHDh2bM)
- [The Motivation](https://reactjs.org/docs/hooks-intro.html#motivation)
- [Rules](https://reactjs.org/docs/hooks-rules.html)
- [Hooks API reference](https://reactjs.org/docs/hooks-reference.html)
- [FAQ](https://reactjs.org/docs/hooks-faq.html)
- [Video introduction](https://youtu.be/dpw9EHDh2bM)
- [The Motivation](https://reactjs.org/docs/hooks-intro.html#motivation)
- [Rules](https://reactjs.org/docs/hooks-rules.html)
- [Hooks API reference](https://reactjs.org/docs/hooks-reference.html)
- [FAQ](https://reactjs.org/docs/hooks-faq.html)

It also helps to learn the background mechanism of React Hooks.
[React hooks: not magic, just arrays](https://medium.com/@ryardley/react-hooks-not-magic-just-arrays-cd4f1857236e)
Expand All @@ -39,53 +39,53 @@ Use [eslint-plugin-react-hooks](https://www.npmjs.com/package/eslint-plugin-reac

### `useReplicant`

- Subscribes to specified replicant and returns the value as state.
- Allows you to use replicant values in function component.
- Subscribes to specified replicant and returns the value as state.
- Allows you to use replicant values in function component.

```tsx
import {useReplicant} from '@nodecg/react-hooks';
import { useReplicant } from "@nodecg/react-hooks";

// This component will re-render when the `counter replicant value changes
export function RunnerName() {
const [count, setCount] = useReplicant('counter');
return (
<div>
<div>{count}</div>
<button onClick={() => setCount(count + 1)} />
</div>
);
const [count, setCount] = useReplicant("counter");
return (
<div>
<div>{count}</div>
<button onClick={() => setCount(count + 1)} />
</div>
);
}
```

### `useListenFor`

- Subscribes messages with `listenFor`, and unlistens on unmount.
- Combining with other hooks enables powerful stateful features with function component
- Subscribes messages with `listenFor`, and unlistens on unmount.
- Combining with other hooks enables powerful stateful features with function component

```tsx
import {useListenFor} from '@nodecg/react-hooks';
import { useListenFor } from "@nodecg/react-hooks";

// Shows modal for 1 second when NodeCG receives 'errorHappened' message from the server
export function AlertOnMessage() {
const [showAlert, setShowAlert] = useState(false);
useListenFor('errorHappened', () => {
setShowAlert(true);
});
useEffect(() => {
if (!showAlert) {
return;
}
// Disappear alert 1 second after
const timer = setTimeout(() => {
setShowAlert(false);
}, 1000);
// Make sure to return cleanup function
return () => {
clearTimeout(timer);
};
}, [showAlert]);

return <Modal show={showAlert} />;
const [showAlert, setShowAlert] = useState(false);
useListenFor("errorHappened", () => {
setShowAlert(true);
});
useEffect(() => {
if (!showAlert) {
return;
}
// Disappear alert 1 second after
const timer = setTimeout(() => {
setShowAlert(false);
}, 1000);
// Make sure to return cleanup function
return () => {
clearTimeout(timer);
};
}, [showAlert]);

return <Modal show={showAlert} />;
}
```

Expand Down
10 changes: 5 additions & 5 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type {JestConfigWithTsJest} from 'ts-jest';
import type { JestConfigWithTsJest } from "ts-jest";

export default {
transform: {
'^.+\\.tsx?$': [
'ts-jest',
"^.+\\.tsx?$": [
"ts-jest",
{
tsconfig: './tests/tsconfig.json',
tsconfig: "./tests/tsconfig.json",
},
],
},
collectCoverage: true,
collectCoverageFrom: ['src/**/*.{ts,tsx}'],
collectCoverageFrom: ["src/**/*.{ts,tsx}"],
} satisfies JestConfigWithTsJest;
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@
"esm"
],
"scripts": {
"_prettier": "prettier \"**/*.{js,ts,tsx,md,yml,yaml,json}\"",
"build": "del-cli cjs esm && tsc -b src/tsconfig.json src/tsconfig.cjs.json",
"fmt": "npm run _prettier -- --write",
"fmt": "prettier . --write",
"prepublishOnly": "npm run build",
"test": "run-s test:*",
"test:fmt": "npm run _prettier -- --check",
"test:fmt": "prettier . --check",
"test:jest": "jest",
"test:lint": "eslint \"**/*.{ts,tsx}\""
},
Expand All @@ -75,7 +74,6 @@
"git add"
]
},
"prettier": "@hoishin/prettierrc",
"dependencies": {
"@nodecg/types": "^2.1.8",
"klona": "^2.0.6",
Expand All @@ -100,7 +98,7 @@
"jest-environment-jsdom": "^29.5.0",
"lint-staged": "10.5.1",
"npm-run-all": "4.1.5",
"prettier": "^2.8.8",
"prettier": "^3.1.1",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"sort-package-json": "1.46.1",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './use-replicant';
export * from './use-listen-for';
export * from "./use-replicant";
export * from "./use-listen-for";
4 changes: 2 additions & 2 deletions src/use-listen-for.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useEffect} from 'react';
import { useEffect } from "react";

export interface UseListenForOptions {
bundle?: string;
Expand All @@ -7,7 +7,7 @@ export interface UseListenForOptions {
export const useListenFor = <T>(
messageName: string,
handler: (message: T) => void,
{bundle}: UseListenForOptions = {},
{ bundle }: UseListenForOptions = {}
): void => {
useEffect(() => {
if (bundle) {
Expand Down
18 changes: 9 additions & 9 deletions src/use-replicant.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {useEffect, useMemo, useState} from 'react';
import {klona as clone} from 'klona/json';
import { 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};
type Json = JsonValue | JsonValue[] | { [key: string]: Json };

export type UseReplicantOptions<T> = {
defaultValue?: T;
Expand All @@ -21,16 +21,16 @@ export type UseReplicantOptions<T> = {
*/
export const useReplicant = <T extends Json>(
replicantName: string,
{bundle, defaultValue, persistent}: UseReplicantOptions<T> = {},
{ bundle, defaultValue, persistent }: UseReplicantOptions<T> = {}
) => {
const replicant = useMemo(() => {
if (typeof bundle === 'string') {
if (typeof bundle === "string") {
return nodecg.Replicant<T>(replicantName, bundle, {
defaultValue,
persistent,
});
}
return nodecg.Replicant<T>(replicantName, {defaultValue, persistent});
return nodecg.Replicant<T>(replicantName, { defaultValue, persistent });
}, [bundle, defaultValue, persistent, replicantName]);

const [value, setValue] = useState(replicant.value);
Expand All @@ -44,16 +44,16 @@ export const useReplicant = <T extends Json>(
return clone(newValue);
});
};
replicant.on('change', changeHandler);
replicant.on("change", changeHandler);
return () => {
replicant.removeListener('change', changeHandler);
replicant.removeListener("change", changeHandler);
};
}, [replicant]);

return [
value,
(newValue: T | ((oldValue?: T) => void)) => {
if (typeof newValue === 'function') {
if (typeof newValue === "function") {
newValue(replicant.value);
} else {
replicant.value = newValue;
Expand Down
Loading

0 comments on commit 5e8766b

Please sign in to comment.