Skip to content

Commit 479e468

Browse files
fix: refactor allSettled to remove dependencies (#746)
Co-authored-by: Oscar Bazaldua <[email protected]>
1 parent 2b0ebed commit 479e468

File tree

7 files changed

+68
-92
lines changed

7 files changed

+68
-92
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ project.xcworkspace
3535
.gradle
3636
local.properties
3737
android.iml
38+
.settings/
39+
bin/
40+
3841

3942
# Cocoapods
4043
#
@@ -54,6 +57,7 @@ buck-out/
5457
android/app/libs
5558
android/keystores/debug.keystore
5659

60+
5761
# Expo
5862
.expo/*
5963

@@ -64,4 +68,4 @@ lib/
6468
coverage/
6569

6670
# Typescript
67-
tsconfig.tsbuildinfo
71+
tsconfig.tsbuildinfo

packages/core/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
"@segment/sovran-react-native": "^0.4.5",
5151
"deepmerge": "^4.2.2",
5252
"js-base64": "^3.7.2",
53-
"promise.allsettled": "^1.0.5",
5453
"react-native-uuid": "^2.0.1"
5554
},
5655
"peerDependencies": {
@@ -69,7 +68,6 @@
6968
"@semantic-release/release-notes-generator": "^10.0.3",
7069
"@types/jest": "^27.0.3",
7170
"@types/js-base64": "^3.3.1",
72-
"@types/promise.allsettled": "^1.0.3",
7371
"@types/react": "18.0.15",
7472
"@types/react-native": "0.69.3",
7573
"@types/uuid": "^8.3.3",

packages/core/src/__tests__/util.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { chunk } from '../util';
1+
import { chunk, allSettled } from '../util';
22

33
describe('#chunk', () => {
44
it('handles empty array', () => {
@@ -36,3 +36,29 @@ describe('#chunk', () => {
3636
).toEqual([[about500bString, about500bString], [about500bString]]);
3737
});
3838
});
39+
40+
describe('allSettled', () => {
41+
it('handles all resolved and rejected promises properly', async () => {
42+
const promises: (Promise<number> | number)[] = [];
43+
promises.push(Promise.resolve(1));
44+
promises.push(2);
45+
promises.push(Promise.reject(3));
46+
47+
const results = await allSettled(promises);
48+
49+
expect(results).toEqual([
50+
{
51+
status: 'fulfilled',
52+
value: 1,
53+
},
54+
{
55+
status: 'fulfilled',
56+
value: 2,
57+
},
58+
{
59+
status: 'rejected',
60+
reason: 3,
61+
},
62+
]);
63+
});
64+
});

packages/core/src/analytics.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//@ts-ignore
22
import type { Rule } from '@segment/tsub/dist/store';
33
import deepmerge from 'deepmerge';
4-
import allSettled from 'promise.allsettled';
54
import { AppState, AppStateStatus } from 'react-native';
65
import { settingsCDN, workspaceDestinationFilterKey } from './constants';
76
import { getContext } from './context';
@@ -43,7 +42,7 @@ import {
4342
UserInfoState,
4443
UserTraits,
4544
} from './types';
46-
import { getPluginsWithFlush, getPluginsWithReset } from './util';
45+
import { allSettled, getPluginsWithFlush, getPluginsWithReset } from './util';
4746
import { getUUID } from './uuid';
4847
import type { FlushPolicy } from './flushPolicies';
4948
import {
@@ -464,8 +463,7 @@ export class SegmentClient {
464463
});
465464

466465
await allSettled(promises);
467-
468-
return Promise.resolve();
466+
return;
469467
}
470468

471469
async screen(name: string, options?: JsonMap) {

packages/core/src/info.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export const libraryInfo = {
22
name: '@segment/analytics-react-native',
3-
version: '2.10.0',
3+
version: '2.10.1',
44
};

packages/core/src/util.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,36 @@ export const getPluginsWithReset = (timeline: Timeline) => {
9494

9595
return eventPlugins;
9696
};
97+
98+
type PromiseResult<T> =
99+
| {
100+
status: 'fulfilled';
101+
value: T;
102+
}
103+
| {
104+
status: 'rejected';
105+
reason: unknown;
106+
};
107+
108+
const settlePromise = async <T>(
109+
promise: Promise<T> | T
110+
): Promise<PromiseResult<T>> => {
111+
try {
112+
const result = await promise;
113+
return {
114+
status: 'fulfilled',
115+
value: result,
116+
};
117+
} catch (error) {
118+
return {
119+
status: 'rejected',
120+
reason: error,
121+
};
122+
}
123+
};
124+
125+
export const allSettled = async <T>(
126+
promises: (Promise<T> | T)[]
127+
): Promise<PromiseResult<T>[]> => {
128+
return Promise.all(promises.map(settlePromise));
129+
};

yarn.lock

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3634,11 +3634,6 @@
36343634
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.6.3.tgz#68ada76827b0010d0db071f739314fa429943d0a"
36353635
integrity sha512-ymZk3LEC/fsut+/Q5qejp6R9O1rMxz3XaRHDV6kX8MrGAhOSPqVARbDi+EZvInBpw+BnCX3TD240byVkOfQsHg==
36363636

3637-
"@types/promise.allsettled@^1.0.3":
3638-
version "1.0.3"
3639-
resolved "https://registry.yarnpkg.com/@types/promise.allsettled/-/promise.allsettled-1.0.3.tgz#6f3166618226a570b98c8250fc78687a912e56d5"
3640-
integrity sha512-b/IFHHTkYkTqu41IH9UtpICwqrpKj2oNlb4KHPzFQDMiz+h1BgAeATeO0/XTph4+UkH9W2U0E4B4j64KWOovag==
3641-
36423637
"@types/prop-types@*":
36433638
version "15.7.5"
36443639
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:
40874082
es-abstract "^1.19.2"
40884083
es-shim-unscopables "^1.0.0"
40894084

4090-
array.prototype.map@^1.0.4:
4091-
version "1.0.4"
4092-
resolved "https://registry.yarnpkg.com/array.prototype.map/-/array.prototype.map-1.0.4.tgz#0d97b640cfdd036c1b41cfe706a5e699aa0711f2"
4093-
integrity sha512-Qds9QnX7A0qISY7JT5WuJO0NJPE9CMlC6JzHQfhpqAAQQzufVRoeH7EzUY5GcPTx72voG8LV/5eo+b8Qi8hmhA==
4094-
dependencies:
4095-
call-bind "^1.0.2"
4096-
define-properties "^1.1.3"
4097-
es-abstract "^1.19.0"
4098-
es-array-method-boxes-properly "^1.0.0"
4099-
is-string "^1.0.7"
4100-
41014085
arrify@^1.0.1:
41024086
version "1.0.1"
41034087
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
56415625
string.prototype.trimstart "^1.0.5"
56425626
unbox-primitive "^1.0.2"
56435627

5644-
es-array-method-boxes-properly@^1.0.0:
5645-
version "1.0.0"
5646-
resolved "https://registry.yarnpkg.com/es-array-method-boxes-properly/-/es-array-method-boxes-properly-1.0.0.tgz#873f3e84418de4ee19c5be752990b2e44718d09e"
5647-
integrity sha512-wd6JXUmyHmt8T5a2xreUwKcGPq6f1f+WwIJkijUqiGcJz1qqnZgP6XIK+QyIWU5lT7imeNxUll48bziG+TSYcA==
5648-
5649-
es-get-iterator@^1.0.2:
5650-
version "1.1.2"
5651-
resolved "https://registry.yarnpkg.com/es-get-iterator/-/es-get-iterator-1.1.2.tgz#9234c54aba713486d7ebde0220864af5e2b283f7"
5652-
integrity sha512-+DTO8GYwbMCwbywjimwZMHp8AuYXOS2JZFWoi2AlPOS3ebnII9w/NLpNZtA7A0YLaVDw+O7KFCeoIV7OPvM7hQ==
5653-
dependencies:
5654-
call-bind "^1.0.2"
5655-
get-intrinsic "^1.1.0"
5656-
has-symbols "^1.0.1"
5657-
is-arguments "^1.1.0"
5658-
is-map "^2.0.2"
5659-
is-set "^2.0.2"
5660-
is-string "^1.0.5"
5661-
isarray "^2.0.5"
5662-
56635628
es-shim-unscopables@^1.0.0:
56645629
version "1.0.0"
56655630
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:
71857150
dependencies:
71867151
kind-of "^6.0.0"
71877152

7188-
is-arguments@^1.1.0:
7189-
version "1.1.1"
7190-
resolved "https://registry.yarnpkg.com/is-arguments/-/is-arguments-1.1.1.tgz#15b3f88fda01f2a97fec84ca761a560f123efa9b"
7191-
integrity sha512-8Q7EARjzEnKpt/PCD7e1cgUS0a6X8u5tdSiMqXhojOdoV9TsMsiO+9VLC5vAmO8N7/GmXn7yjR8qnA6bVAEzfA==
7192-
dependencies:
7193-
call-bind "^1.0.2"
7194-
has-tostringtag "^1.0.0"
7195-
71967153
is-arrayish@^0.2.1:
71977154
version "0.2.1"
71987155
resolved "https://registry.yarnpkg.com/is-arrayish/-/is-arrayish-0.2.1.tgz#77c99840527aa8ecb1a8ba697b80645a7a926a9d"
@@ -7373,11 +7330,6 @@ is-lambda@^1.0.1:
73737330
resolved "https://registry.yarnpkg.com/is-lambda/-/is-lambda-1.0.1.tgz#3d9877899e6a53efc0160504cde15f82e6f061d5"
73747331
integrity sha512-z7CMFGNrENq5iFB9Bqo64Xk6Y9sg+epq1myIcdHaGnbMTYOxvzsEtdYqQUylB7LxfkvgrrjP32T6Ywciio9UIQ==
73757332

7376-
is-map@^2.0.2:
7377-
version "2.0.2"
7378-
resolved "https://registry.yarnpkg.com/is-map/-/is-map-2.0.2.tgz#00922db8c9bf73e81b7a335827bc2a43f2b91127"
7379-
integrity sha512-cOZFQQozTha1f4MxLFzlgKYPTyj26picdZTx82hbc/Xf4K/tZOOXSCkMvU4pKioRXGDLJRn0GM7Upe7kR721yg==
7380-
73817333
is-negative-zero@^2.0.2:
73827334
version "2.0.2"
73837335
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:
74647416
dependencies:
74657417
is-unc-path "^1.0.0"
74667418

7467-
is-set@^2.0.2:
7468-
version "2.0.2"
7469-
resolved "https://registry.yarnpkg.com/is-set/-/is-set-2.0.2.tgz#90755fa4c2562dc1c5d4024760d6119b94ca18ec"
7470-
integrity sha512-+2cnTEZeY5z/iXGbLhPrOAaK/Mau5k5eXq9j14CpRTftq0pAJu2MwVRSZhyZWBzx3o6X795Lz6Bpb6R0GKf37g==
7471-
74727419
is-shared-array-buffer@^1.0.2:
74737420
version "1.0.2"
74747421
resolved "https://registry.yarnpkg.com/is-shared-array-buffer/-/is-shared-array-buffer-1.0.2.tgz#8f259c573b60b6a32d4058a1a07430c0a7344c79"
@@ -7572,11 +7519,6 @@ [email protected], isarray@~1.0.0:
75727519
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
75737520
integrity sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==
75747521

7575-
isarray@^2.0.5:
7576-
version "2.0.5"
7577-
resolved "https://registry.yarnpkg.com/isarray/-/isarray-2.0.5.tgz#8af1e4c1221244cc62459faf38940d4e644a5723"
7578-
integrity sha512-xHjhDr3cNBK0BzdUJSPXZntQUx/mwMS5Rw4A7lPJ90XGAO6ISP/ePDNuo0vhqOZU+UD5JoodwCAAoZQd3FeAKw==
7579-
75807522
isexe@^2.0.0:
75817523
version "2.0.0"
75827524
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
@@ -7647,19 +7589,6 @@ istanbul-reports@^3.1.3:
76477589
html-escaper "^2.0.0"
76487590
istanbul-lib-report "^3.0.0"
76497591

7650-
iterate-iterator@^1.0.1:
7651-
version "1.0.2"
7652-
resolved "https://registry.yarnpkg.com/iterate-iterator/-/iterate-iterator-1.0.2.tgz#551b804c9eaa15b847ea6a7cdc2f5bf1ec150f91"
7653-
integrity sha512-t91HubM4ZDQ70M9wqp+pcNpu8OyJ9UAtXntT/Bcsvp5tZMnz9vRa+IunKXeI8AnfZMTv0jNuVEmGeLSMjVvfPw==
7654-
7655-
iterate-value@^1.0.2:
7656-
version "1.0.2"
7657-
resolved "https://registry.yarnpkg.com/iterate-value/-/iterate-value-1.0.2.tgz#935115bd37d006a52046535ebc8d07e9c9337f57"
7658-
integrity sha512-A6fMAio4D2ot2r/TYzr4yUWrmwNdsN5xL7+HUiyACE4DXm+q8HtPcnFTp+NnW3k4N05tZ7FVYFFb2CR13NxyHQ==
7659-
dependencies:
7660-
es-get-iterator "^1.0.2"
7661-
iterate-iterator "^1.0.1"
7662-
76637592
java-properties@^1.0.0:
76647593
version "1.0.2"
76657594
resolved "https://registry.yarnpkg.com/java-properties/-/java-properties-1.0.2.tgz#ccd1fa73907438a5b5c38982269d0e771fe78211"
@@ -10431,18 +10360,6 @@ promise-retry@^2.0.1:
1043110360
err-code "^2.0.2"
1043210361
retry "^0.12.0"
1043310362

10434-
promise.allsettled@^1.0.5:
10435-
version "1.0.5"
10436-
resolved "https://registry.yarnpkg.com/promise.allsettled/-/promise.allsettled-1.0.5.tgz#2443f3d4b2aa8dfa560f6ac2aa6c4ea999d75f53"
10437-
integrity sha512-tVDqeZPoBC0SlzJHzWGZ2NKAguVq2oiYj7gbggbiTvH2itHohijTp7njOUA0aQ/nl+0lr/r6egmhoYu63UZ/pQ==
10438-
dependencies:
10439-
array.prototype.map "^1.0.4"
10440-
call-bind "^1.0.2"
10441-
define-properties "^1.1.3"
10442-
es-abstract "^1.19.1"
10443-
get-intrinsic "^1.1.1"
10444-
iterate-value "^1.0.2"
10445-
1044610363
promise@^8.0.3:
1044710364
version "8.1.0"
1044810365
resolved "https://registry.yarnpkg.com/promise/-/promise-8.1.0.tgz#697c25c3dfe7435dd79fcd58c38a135888eaf05e"

0 commit comments

Comments
 (0)