From 479e4686a410ad9ebb02ed50a15509a9051ec073 Mon Sep 17 00:00:00 2001 From: Konstantinos K <47348484+KonstantinosKoulaxizis@users.noreply.github.com> Date: Fri, 27 Jan 2023 00:42:45 +0100 Subject: [PATCH] fix: refactor allSettled to remove dependencies (#746) Co-authored-by: Oscar Bazaldua <511911+oscb@users.noreply.github.com> --- .gitignore | 6 +- packages/core/package.json | 2 - packages/core/src/__tests__/util.test.ts | 28 +++++++- packages/core/src/analytics.ts | 6 +- packages/core/src/info.ts | 2 +- packages/core/src/util.ts | 33 ++++++++++ yarn.lock | 83 ------------------------ 7 files changed, 68 insertions(+), 92 deletions(-) diff --git a/.gitignore b/.gitignore index 3131421cd..387530281 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,9 @@ project.xcworkspace .gradle local.properties android.iml +.settings/ +bin/ + # Cocoapods # @@ -54,6 +57,7 @@ buck-out/ android/app/libs android/keystores/debug.keystore + # Expo .expo/* @@ -64,4 +68,4 @@ lib/ coverage/ # Typescript -tsconfig.tsbuildinfo \ No newline at end of file +tsconfig.tsbuildinfo diff --git a/packages/core/package.json b/packages/core/package.json index 9ba9185f2..7a303d413 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -50,7 +50,6 @@ "@segment/sovran-react-native": "^0.4.5", "deepmerge": "^4.2.2", "js-base64": "^3.7.2", - "promise.allsettled": "^1.0.5", "react-native-uuid": "^2.0.1" }, "peerDependencies": { @@ -69,7 +68,6 @@ "@semantic-release/release-notes-generator": "^10.0.3", "@types/jest": "^27.0.3", "@types/js-base64": "^3.3.1", - "@types/promise.allsettled": "^1.0.3", "@types/react": "18.0.15", "@types/react-native": "0.69.3", "@types/uuid": "^8.3.3", diff --git a/packages/core/src/__tests__/util.test.ts b/packages/core/src/__tests__/util.test.ts index 18b897b47..0a29e0041 100644 --- a/packages/core/src/__tests__/util.test.ts +++ b/packages/core/src/__tests__/util.test.ts @@ -1,4 +1,4 @@ -import { chunk } from '../util'; +import { chunk, allSettled } from '../util'; describe('#chunk', () => { it('handles empty array', () => { @@ -36,3 +36,29 @@ describe('#chunk', () => { ).toEqual([[about500bString, about500bString], [about500bString]]); }); }); + +describe('allSettled', () => { + it('handles all resolved and rejected promises properly', async () => { + const promises: (Promise | number)[] = []; + promises.push(Promise.resolve(1)); + promises.push(2); + promises.push(Promise.reject(3)); + + const results = await allSettled(promises); + + expect(results).toEqual([ + { + status: 'fulfilled', + value: 1, + }, + { + status: 'fulfilled', + value: 2, + }, + { + status: 'rejected', + reason: 3, + }, + ]); + }); +}); diff --git a/packages/core/src/analytics.ts b/packages/core/src/analytics.ts index e1a3ceca3..f2efdba16 100644 --- a/packages/core/src/analytics.ts +++ b/packages/core/src/analytics.ts @@ -1,7 +1,6 @@ //@ts-ignore import type { Rule } from '@segment/tsub/dist/store'; import deepmerge from 'deepmerge'; -import allSettled from 'promise.allsettled'; import { AppState, AppStateStatus } from 'react-native'; import { settingsCDN, workspaceDestinationFilterKey } from './constants'; import { getContext } from './context'; @@ -43,7 +42,7 @@ import { UserInfoState, UserTraits, } from './types'; -import { getPluginsWithFlush, getPluginsWithReset } from './util'; +import { allSettled, getPluginsWithFlush, getPluginsWithReset } from './util'; import { getUUID } from './uuid'; import type { FlushPolicy } from './flushPolicies'; import { @@ -464,8 +463,7 @@ export class SegmentClient { }); await allSettled(promises); - - return Promise.resolve(); + return; } async screen(name: string, options?: JsonMap) { diff --git a/packages/core/src/info.ts b/packages/core/src/info.ts index 15da561ab..4369c2272 100644 --- a/packages/core/src/info.ts +++ b/packages/core/src/info.ts @@ -1,4 +1,4 @@ export const libraryInfo = { name: '@segment/analytics-react-native', - version: '2.10.0', + version: '2.10.1', }; diff --git a/packages/core/src/util.ts b/packages/core/src/util.ts index 663ab3c51..5459b47d0 100644 --- a/packages/core/src/util.ts +++ b/packages/core/src/util.ts @@ -94,3 +94,36 @@ export const getPluginsWithReset = (timeline: Timeline) => { return eventPlugins; }; + +type PromiseResult = + | { + status: 'fulfilled'; + value: T; + } + | { + status: 'rejected'; + reason: unknown; + }; + +const settlePromise = async ( + promise: Promise | T +): Promise> => { + try { + const result = await promise; + return { + status: 'fulfilled', + value: result, + }; + } catch (error) { + return { + status: 'rejected', + reason: error, + }; + } +}; + +export const allSettled = async ( + promises: (Promise | T)[] +): Promise[]> => { + return Promise.all(promises.map(settlePromise)); +}; diff --git a/yarn.lock b/yarn.lock index 1d8b38b23..bb660cc8f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3634,11 +3634,6 @@ resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.6.3.tgz#68ada76827b0010d0db071f739314fa429943d0a" integrity sha512-ymZk3LEC/fsut+/Q5qejp6R9O1rMxz3XaRHDV6kX8MrGAhOSPqVARbDi+EZvInBpw+BnCX3TD240byVkOfQsHg== -"@types/promise.allsettled@^1.0.3": - version "1.0.3" - resolved "https://registry.yarnpkg.com/@types/promise.allsettled/-/promise.allsettled-1.0.3.tgz#6f3166618226a570b98c8250fc78687a912e56d5" - integrity sha512-b/IFHHTkYkTqu41IH9UtpICwqrpKj2oNlb4KHPzFQDMiz+h1BgAeATeO0/XTph4+UkH9W2U0E4B4j64KWOovag== - "@types/prop-types@*": version "15.7.5" resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.5.tgz#5f19d2b85a98e9558036f6a3cacc8819420f05cf" @@ -4087,17 +4082,6 @@ array.prototype.flatmap@^1.3.0: es-abstract "^1.19.2" es-shim-unscopables "^1.0.0" -array.prototype.map@^1.0.4: - version "1.0.4" - resolved "https://registry.yarnpkg.com/array.prototype.map/-/array.prototype.map-1.0.4.tgz#0d97b640cfdd036c1b41cfe706a5e699aa0711f2" - integrity sha512-Qds9QnX7A0qISY7JT5WuJO0NJPE9CMlC6JzHQfhpqAAQQzufVRoeH7EzUY5GcPTx72voG8LV/5eo+b8Qi8hmhA== - dependencies: - call-bind "^1.0.2" - define-properties "^1.1.3" - es-abstract "^1.19.0" - es-array-method-boxes-properly "^1.0.0" - is-string "^1.0.7" - arrify@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/arrify/-/arrify-1.0.1.tgz#898508da2226f380df904728456849c1501a4b0d" @@ -5641,25 +5625,6 @@ es-abstract@^1.19.0, es-abstract@^1.19.1, es-abstract@^1.19.2, es-abstract@^1.19 string.prototype.trimstart "^1.0.5" unbox-primitive "^1.0.2" -es-array-method-boxes-properly@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/es-array-method-boxes-properly/-/es-array-method-boxes-properly-1.0.0.tgz#873f3e84418de4ee19c5be752990b2e44718d09e" - integrity sha512-wd6JXUmyHmt8T5a2xreUwKcGPq6f1f+WwIJkijUqiGcJz1qqnZgP6XIK+QyIWU5lT7imeNxUll48bziG+TSYcA== - -es-get-iterator@^1.0.2: - version "1.1.2" - resolved "https://registry.yarnpkg.com/es-get-iterator/-/es-get-iterator-1.1.2.tgz#9234c54aba713486d7ebde0220864af5e2b283f7" - integrity sha512-+DTO8GYwbMCwbywjimwZMHp8AuYXOS2JZFWoi2AlPOS3ebnII9w/NLpNZtA7A0YLaVDw+O7KFCeoIV7OPvM7hQ== - dependencies: - call-bind "^1.0.2" - get-intrinsic "^1.1.0" - has-symbols "^1.0.1" - is-arguments "^1.1.0" - is-map "^2.0.2" - is-set "^2.0.2" - is-string "^1.0.5" - isarray "^2.0.5" - es-shim-unscopables@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/es-shim-unscopables/-/es-shim-unscopables-1.0.0.tgz#702e632193201e3edf8713635d083d378e510241" @@ -7185,14 +7150,6 @@ is-accessor-descriptor@^1.0.0: dependencies: kind-of "^6.0.0" -is-arguments@^1.1.0: - version "1.1.1" - resolved "https://registry.yarnpkg.com/is-arguments/-/is-arguments-1.1.1.tgz#15b3f88fda01f2a97fec84ca761a560f123efa9b" - integrity sha512-8Q7EARjzEnKpt/PCD7e1cgUS0a6X8u5tdSiMqXhojOdoV9TsMsiO+9VLC5vAmO8N7/GmXn7yjR8qnA6bVAEzfA== - dependencies: - call-bind "^1.0.2" - has-tostringtag "^1.0.0" - is-arrayish@^0.2.1: version "0.2.1" resolved "https://registry.yarnpkg.com/is-arrayish/-/is-arrayish-0.2.1.tgz#77c99840527aa8ecb1a8ba697b80645a7a926a9d" @@ -7373,11 +7330,6 @@ is-lambda@^1.0.1: resolved "https://registry.yarnpkg.com/is-lambda/-/is-lambda-1.0.1.tgz#3d9877899e6a53efc0160504cde15f82e6f061d5" integrity sha512-z7CMFGNrENq5iFB9Bqo64Xk6Y9sg+epq1myIcdHaGnbMTYOxvzsEtdYqQUylB7LxfkvgrrjP32T6Ywciio9UIQ== -is-map@^2.0.2: - version "2.0.2" - resolved "https://registry.yarnpkg.com/is-map/-/is-map-2.0.2.tgz#00922db8c9bf73e81b7a335827bc2a43f2b91127" - integrity sha512-cOZFQQozTha1f4MxLFzlgKYPTyj26picdZTx82hbc/Xf4K/tZOOXSCkMvU4pKioRXGDLJRn0GM7Upe7kR721yg== - is-negative-zero@^2.0.2: version "2.0.2" resolved "https://registry.yarnpkg.com/is-negative-zero/-/is-negative-zero-2.0.2.tgz#7bf6f03a28003b8b3965de3ac26f664d765f3150" @@ -7464,11 +7416,6 @@ is-relative@^1.0.0: dependencies: is-unc-path "^1.0.0" -is-set@^2.0.2: - version "2.0.2" - resolved "https://registry.yarnpkg.com/is-set/-/is-set-2.0.2.tgz#90755fa4c2562dc1c5d4024760d6119b94ca18ec" - integrity sha512-+2cnTEZeY5z/iXGbLhPrOAaK/Mau5k5eXq9j14CpRTftq0pAJu2MwVRSZhyZWBzx3o6X795Lz6Bpb6R0GKf37g== - is-shared-array-buffer@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/is-shared-array-buffer/-/is-shared-array-buffer-1.0.2.tgz#8f259c573b60b6a32d4058a1a07430c0a7344c79" @@ -7572,11 +7519,6 @@ isarray@1.0.0, isarray@~1.0.0: resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" integrity sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ== -isarray@^2.0.5: - version "2.0.5" - resolved "https://registry.yarnpkg.com/isarray/-/isarray-2.0.5.tgz#8af1e4c1221244cc62459faf38940d4e644a5723" - integrity sha512-xHjhDr3cNBK0BzdUJSPXZntQUx/mwMS5Rw4A7lPJ90XGAO6ISP/ePDNuo0vhqOZU+UD5JoodwCAAoZQd3FeAKw== - isexe@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10" @@ -7647,19 +7589,6 @@ istanbul-reports@^3.1.3: html-escaper "^2.0.0" istanbul-lib-report "^3.0.0" -iterate-iterator@^1.0.1: - version "1.0.2" - resolved "https://registry.yarnpkg.com/iterate-iterator/-/iterate-iterator-1.0.2.tgz#551b804c9eaa15b847ea6a7cdc2f5bf1ec150f91" - integrity sha512-t91HubM4ZDQ70M9wqp+pcNpu8OyJ9UAtXntT/Bcsvp5tZMnz9vRa+IunKXeI8AnfZMTv0jNuVEmGeLSMjVvfPw== - -iterate-value@^1.0.2: - version "1.0.2" - resolved "https://registry.yarnpkg.com/iterate-value/-/iterate-value-1.0.2.tgz#935115bd37d006a52046535ebc8d07e9c9337f57" - integrity sha512-A6fMAio4D2ot2r/TYzr4yUWrmwNdsN5xL7+HUiyACE4DXm+q8HtPcnFTp+NnW3k4N05tZ7FVYFFb2CR13NxyHQ== - dependencies: - es-get-iterator "^1.0.2" - iterate-iterator "^1.0.1" - java-properties@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/java-properties/-/java-properties-1.0.2.tgz#ccd1fa73907438a5b5c38982269d0e771fe78211" @@ -10431,18 +10360,6 @@ promise-retry@^2.0.1: err-code "^2.0.2" retry "^0.12.0" -promise.allsettled@^1.0.5: - version "1.0.5" - resolved "https://registry.yarnpkg.com/promise.allsettled/-/promise.allsettled-1.0.5.tgz#2443f3d4b2aa8dfa560f6ac2aa6c4ea999d75f53" - integrity sha512-tVDqeZPoBC0SlzJHzWGZ2NKAguVq2oiYj7gbggbiTvH2itHohijTp7njOUA0aQ/nl+0lr/r6egmhoYu63UZ/pQ== - dependencies: - array.prototype.map "^1.0.4" - call-bind "^1.0.2" - define-properties "^1.1.3" - es-abstract "^1.19.1" - get-intrinsic "^1.1.1" - iterate-value "^1.0.2" - promise@^8.0.3: version "8.1.0" resolved "https://registry.yarnpkg.com/promise/-/promise-8.1.0.tgz#697c25c3dfe7435dd79fcd58c38a135888eaf05e"