Skip to content

fix: refactor allSettled to remove dependencies #746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ project.xcworkspace
.gradle
local.properties
android.iml
.settings/
bin/


# Cocoapods
#
Expand All @@ -54,6 +57,7 @@ buck-out/
android/app/libs
android/keystores/debug.keystore


# Expo
.expo/*

Expand All @@ -64,4 +68,4 @@ lib/
coverage/

# Typescript
tsconfig.tsbuildinfo
tsconfig.tsbuildinfo
2 changes: 0 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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",
Expand Down
28 changes: 27 additions & 1 deletion packages/core/src/__tests__/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { chunk } from '../util';
import { chunk, allSettled } from '../util';

describe('#chunk', () => {
it('handles empty array', () => {
Expand Down Expand Up @@ -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> | 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,
},
]);
});
});
6 changes: 2 additions & 4 deletions packages/core/src/analytics.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -464,8 +463,7 @@ export class SegmentClient {
});

await allSettled(promises);

return Promise.resolve();
return;
}

async screen(name: string, options?: JsonMap) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/info.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const libraryInfo = {
name: '@segment/analytics-react-native',
version: '2.10.0',
version: '2.10.1',
};
33 changes: 33 additions & 0 deletions packages/core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,36 @@ export const getPluginsWithReset = (timeline: Timeline) => {

return eventPlugins;
};

type PromiseResult<T> =
Copy link

@silesky silesky Jan 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an excellent typescript definition that seems accurate to the spec.

| {
status: 'fulfilled';
value: T;
}
| {
status: 'rejected';
reason: unknown;
};

const settlePromise = async <T>(
promise: Promise<T> | T
): Promise<PromiseResult<T>> => {
try {
const result = await promise;
return {
status: 'fulfilled',
value: result,
};
} catch (error) {
return {
status: 'rejected',
reason: error,
};
}
};

export const allSettled = async <T>(
promises: (Promise<T> | T)[]
): Promise<PromiseResult<T>[]> => {
return Promise.all(promises.map(settlePromise));
};
83 changes: 0 additions & 83 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -7572,11 +7519,6 @@ [email protected], 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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down