diff --git a/.babelrc b/.babelrc index a892b02..14d0d1f 100644 --- a/.babelrc +++ b/.babelrc @@ -8,7 +8,6 @@ ], "@babel/react" ], - "plugins": ["@babel/transform-runtime"], "env": { "production-esm": { "presets": [ @@ -20,8 +19,7 @@ } ], "@babel/react" - ], - "plugins": [["@babel/transform-runtime", { "useESModules": true }]] + ] } } } diff --git a/package.json b/package.json index 108af0b..cd48f7b 100644 --- a/package.json +++ b/package.json @@ -28,14 +28,12 @@ }, "license": "MIT", "dependencies": { - "@babel/runtime": "^7.0.0", "make-cancellable-promise": "^1.0.0", "prop-types": "^15.6.0" }, "devDependencies": { "@babel/cli": "^7.15.0", "@babel/core": "^7.15.0", - "@babel/plugin-transform-runtime": "^7.15.0", "@babel/preset-env": "^7.15.0", "@babel/preset-react": "^7.14.0", "@testing-library/dom": "^8.11.0", diff --git a/src/index.jsx b/src/index.jsx index cf07d2b..2dab747 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -37,26 +37,41 @@ const AsyncButton = React.forwardRef( ); const onClickInternal = useCallback( - async (event) => { + (event) => { clearTimeout(timeout.current); + const onSuccess = () => { + setButtonState(STATES.SUCCESS); + }; + + const onError = () => { + setButtonState(STATES.ERROR); + }; + + const finallyCallback = () => { + timeout.current = setTimeout(() => { + setButtonState(STATES.INIT); + }, resetTimeout); + }; + try { const result = onClick(event); setButtonState(STATES.PENDING); if (result instanceof Promise) { cancellablePromise.current = makeCancellable(result); - await cancellablePromise.current.promise; + cancellablePromise.current.promise + .then(onSuccess) + .catch(onError) + .finally(finallyCallback); + } else { + onSuccess(); + finallyCallback(); } - - setButtonState(STATES.SUCCESS); } catch (error) { - setButtonState(STATES.ERROR); + onError(); + finallyCallback(); } - - timeout.current = setTimeout(() => { - setButtonState(STATES.INIT); - }, resetTimeout); }, [onClick, resetTimeout], ); diff --git a/test/yarn.lock b/test/yarn.lock index 47c2636..7c85e98 100644 --- a/test/yarn.lock +++ b/test/yarn.lock @@ -1278,7 +1278,7 @@ __metadata: languageName: node linkType: hard -"@babel/runtime@npm:^7.0.0, @babel/runtime@npm:^7.8.4": +"@babel/runtime@npm:^7.8.4": version: 7.19.4 resolution: "@babel/runtime@npm:7.19.4" dependencies: @@ -1953,7 +1953,6 @@ __metadata: version: 0.0.0-use.local resolution: "@wojtekmaj/react-async-button@portal:../::locator=react-async-button-test-page%40workspace%3A." dependencies: - "@babel/runtime": ^7.0.0 make-cancellable-promise: ^1.0.0 prop-types: ^15.6.0 peerDependencies: diff --git a/yarn.lock b/yarn.lock index 4e2193a..beeeb56 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1150,22 +1150,6 @@ __metadata: languageName: node linkType: hard -"@babel/plugin-transform-runtime@npm:^7.15.0": - version: 7.19.6 - resolution: "@babel/plugin-transform-runtime@npm:7.19.6" - dependencies: - "@babel/helper-module-imports": ^7.18.6 - "@babel/helper-plugin-utils": ^7.19.0 - babel-plugin-polyfill-corejs2: ^0.3.3 - babel-plugin-polyfill-corejs3: ^0.6.0 - babel-plugin-polyfill-regenerator: ^0.4.1 - semver: ^6.3.0 - peerDependencies: - "@babel/core": ^7.0.0-0 - checksum: ef93efbcbb00dcf4da6dcc55bda698a2a57fca3fb05a6a13e932ecfdb7c1c5d2f0b5b245c1c4faca0318853937caba0d82442f58b7653249f64275d08052fbd8 - languageName: node - linkType: hard - "@babel/plugin-transform-shorthand-properties@npm:^7.18.6": version: 7.18.6 resolution: "@babel/plugin-transform-shorthand-properties@npm:7.18.6" @@ -1371,7 +1355,7 @@ __metadata: languageName: node linkType: hard -"@babel/runtime@npm:^7.0.0, @babel/runtime@npm:^7.10.2, @babel/runtime@npm:^7.12.5, @babel/runtime@npm:^7.18.9, @babel/runtime@npm:^7.8.4, @babel/runtime@npm:^7.9.2": +"@babel/runtime@npm:^7.10.2, @babel/runtime@npm:^7.12.5, @babel/runtime@npm:^7.18.9, @babel/runtime@npm:^7.8.4, @babel/runtime@npm:^7.9.2": version: 7.19.4 resolution: "@babel/runtime@npm:7.19.4" dependencies: @@ -2130,10 +2114,8 @@ __metadata: dependencies: "@babel/cli": ^7.15.0 "@babel/core": ^7.15.0 - "@babel/plugin-transform-runtime": ^7.15.0 "@babel/preset-env": ^7.15.0 "@babel/preset-react": ^7.14.0 - "@babel/runtime": ^7.0.0 "@testing-library/dom": ^8.11.0 "@testing-library/jest-dom": ^5.15.0 "@testing-library/react": ^13.4.0