Skip to content

Commit

Permalink
Fix race compilation error when using TypeScript 5.0 (#8)
Browse files Browse the repository at this point in the history
* Change race signature to work with TS 5.0

* package.json tweaks

* Fix pre-commit not being executable

* Update documentation

* README updates

* Upgrade Yarn

* Upgrade all devDependencies

* Run prettier

* Update copyright date

* Changelog

* Changelog and bump version

* fix workflow
  • Loading branch information
srmagura authored Apr 14, 2023
1 parent 8f49c33 commit 7f91680
Show file tree
Hide file tree
Showing 34 changed files with 5,947 additions and 5,075 deletions.
4 changes: 3 additions & 1 deletion .config/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const path = require('path');

module.exports = {
timers: 'fake',
fakeTimers: {
enableGlobally: true,
},
resetMocks: true,

rootDir: path.resolve(__dirname, '..'),
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ jobs:
- name: Test
run: yarn test
- name: Lint
run: yarn run lint-all
run: yarn run lint
Empty file modified .husky/pre-commit
100644 → 100755
Empty file.
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist/
docs/
docs/
.yarn/
18 changes: 9 additions & 9 deletions .swcrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "es2021"
"jsc": {
"parser": {
"syntax": "typescript"
},
"module": {
"type": "commonjs"
},
"sourceMaps": true
"target": "es2021"
},
"module": {
"type": "commonjs"
},
"sourceMaps": true
}
823 changes: 0 additions & 823 deletions .yarn/releases/yarn-3.3.1.cjs

This file was deleted.

873 changes: 873 additions & 0 deletions .yarn/releases/yarn-3.5.0.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-3.3.1.cjs
yarnPath: .yarn/releases/yarn-3.5.0.cjs
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.2.0

### Chores

- Update the signature of `CancellablePromise.race` to match that of `Promise.race` in the latest version of TypeScript. This should not be a breaking change for the vast majority of users. (#8)

## 1.1.2

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2021 Sam Magura
Copyright 2023 Sam Magura

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,9 @@ Reference](https://srmagura.github.io/real-cancellable-promise/modules.html) for

# Examples

## React: Prevent setState after unmount
## React: Cancel the API call when the component unmounts

React will give you a warning if you attempt to update a component's state after
it has unmounted. This will happen if your component makes an API call but gets
unmounted before the API call completes.

You can fix this by canceling the API call in the cleanup function of an effect.
If your React component makes an API call, you probably don't care about the result of that API call after the component has unmounted. You can cancel the API in the cleanup function of an effect like this:

```tsx
function listBlogPosts(): CancellablePromise<Post[]> {
Expand Down Expand Up @@ -212,12 +208,14 @@ export function Blog() {
}
```

Before React 18, this was necessary to prevent the infamous "setState after unmount" warning. This warning was removed from React in React 18 because setting state after the component unmounts is usually not indicative of a real problem.

[CodeSandbox: prevent setState after
unmount](https://codesandbox.io/s/real-cancellable-promise-prevent-setstate-after-unmount-2zqb0?file=/src/App.tsx)

## React: Cancel the in-progress API call when query parameters change

Sometimes API calls have parameters, like a search string entered by the user.
Sometimes API calls have parameters, like a search string entered by the user. If the query parameters change, you should cancel any in-progress API calls.

```tsx
function searchUsers(searchTerm: string): CancellablePromise<User[]> {
Expand Down
34 changes: 17 additions & 17 deletions docs/assets/highlight.css
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
:root {
--light-hl-0: #0000FF;
--dark-hl-0: #569CD6;
--light-hl-1: #000000;
--dark-hl-1: #D4D4D4;
--light-hl-2: #795E26;
--dark-hl-2: #DCDCAA;
--light-hl-0: #000000;
--dark-hl-0: #D4D4D4;
--light-hl-1: #A31515;
--dark-hl-1: #CE9178;
--light-hl-2: #AF00DB;
--dark-hl-2: #C586C0;
--light-hl-3: #001080;
--dark-hl-3: #9CDCFE;
--light-hl-4: #267F99;
--dark-hl-4: #4EC9B0;
--light-hl-5: #AF00DB;
--dark-hl-5: #C586C0;
--light-hl-6: #0070C1;
--dark-hl-6: #4FC1FF;
--light-hl-4: #0000FF;
--dark-hl-4: #569CD6;
--light-hl-5: #0070C1;
--dark-hl-5: #4FC1FF;
--light-hl-6: #795E26;
--dark-hl-6: #DCDCAA;
--light-hl-7: #008000;
--dark-hl-7: #6A9955;
--light-hl-8: #A31515;
--dark-hl-8: #CE9178;
--light-hl-8: #267F99;
--dark-hl-8: #4EC9B0;
--light-hl-9: #000000FF;
--dark-hl-9: #D4D4D4;
--light-hl-10: #800000;
--dark-hl-10: #808080;
--light-hl-11: #800000;
--dark-hl-11: #569CD6;
--light-hl-12: #FF0000;
--light-hl-12: #E50000;
--dark-hl-12: #9CDCFE;
--light-hl-13: #098658;
--dark-hl-13: #B5CEA8;
Expand Down Expand Up @@ -67,7 +67,7 @@
--code-background: var(--dark-code-background);
} }

body.light {
:root[data-theme='light'] {
--hl-0: var(--light-hl-0);
--hl-1: var(--light-hl-1);
--hl-2: var(--light-hl-2);
Expand All @@ -85,7 +85,7 @@ body.light {
--code-background: var(--light-code-background);
}

body.dark {
:root[data-theme='dark'] {
--hl-0: var(--dark-hl-0);
--hl-1: var(--dark-hl-1);
--hl-2: var(--dark-hl-2);
Expand Down
Loading

0 comments on commit 7f91680

Please sign in to comment.