Skip to content

Commit 354d975

Browse files
committed
Use defer() instead of of(null) for side effects on subscribe
1 parent 076d437 commit 354d975

1 file changed

Lines changed: 112 additions & 121 deletions

File tree

src/app/services/raiden.service.ts

Lines changed: 112 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
Subject,
1616
throwError,
1717
forkJoin,
18+
defer,
1819
} from 'rxjs';
1920
import { fromPromise } from 'rxjs/internal-compatibility';
2021
import {
@@ -294,47 +295,45 @@ export class RaidenService {
294295
const partnerLabel = this.getContactLabel(partnerAddress);
295296
let notificationIdentifier: number;
296297

297-
return of(null).pipe(
298-
tap(() => {
299-
const formattedBalance = amountToDecimal(
300-
balance,
301-
token.decimals
302-
).toFixed();
303-
const message: UiMessage = {
304-
title: 'Opening channel',
305-
description: `with ${partnerLabel} ${partnerAddress} and ${formattedBalance} ${token.symbol} deposit`,
306-
icon: 'channel',
307-
identiconAddress: partnerAddress,
308-
userToken: token,
309-
};
310-
notificationIdentifier = this.notificationService.addPendingAction(
311-
message
312-
);
298+
return defer(() => {
299+
const formattedBalance = amountToDecimal(
300+
balance,
301+
token.decimals
302+
).toFixed();
303+
const message: UiMessage = {
304+
title: 'Opening channel',
305+
description: `with ${partnerLabel} ${partnerAddress} and ${formattedBalance} ${token.symbol} deposit`,
306+
icon: 'channel',
307+
identiconAddress: partnerAddress,
308+
userToken: token,
309+
};
310+
notificationIdentifier = this.notificationService.addPendingAction(
311+
message
312+
);
313313

314-
if (!this.pendingChannels[tokenAddress]) {
315-
this.pendingChannels[tokenAddress] = {};
316-
}
317-
this.pendingChannels[tokenAddress][partnerAddress] = {
318-
channel_identifier: new BigNumber(0),
319-
state: 'waiting_for_open',
320-
total_deposit: new BigNumber(0),
321-
total_withdraw: new BigNumber(0),
322-
balance: new BigNumber(0),
323-
reveal_timeout: 0,
324-
settle_timeout: settleTimeout,
325-
token_address: tokenAddress,
326-
partner_address: partnerAddress,
327-
depositPending: true,
328-
userToken: token,
329-
};
330-
this.pendingChannelsSubject.next(this.pendingChannels);
331-
}),
332-
switchMap(() =>
333-
this.http.put<Channel>(
334-
`${this.raidenConfig.api}/channels`,
335-
body
336-
)
337-
),
314+
if (!this.pendingChannels[tokenAddress]) {
315+
this.pendingChannels[tokenAddress] = {};
316+
}
317+
this.pendingChannels[tokenAddress][partnerAddress] = {
318+
channel_identifier: new BigNumber(0),
319+
state: 'waiting_for_open',
320+
total_deposit: new BigNumber(0),
321+
total_withdraw: new BigNumber(0),
322+
balance: new BigNumber(0),
323+
reveal_timeout: 0,
324+
settle_timeout: settleTimeout,
325+
token_address: tokenAddress,
326+
partner_address: partnerAddress,
327+
depositPending: true,
328+
userToken: token,
329+
};
330+
this.pendingChannelsSubject.next(this.pendingChannels);
331+
332+
return this.http.put<Channel>(
333+
`${this.raidenConfig.api}/channels`,
334+
body
335+
);
336+
}).pipe(
338337
map((channel: Channel) => {
339338
channel.settle_timeout = (<BigNumber>(
340339
(<unknown>channel.settle_timeout)
@@ -605,23 +604,21 @@ export class RaidenService {
605604
public registerToken(tokenAddress: string): Observable<void> {
606605
let notificationIdentifier: number;
607606

608-
return of(null).pipe(
609-
tap(() => {
610-
const message: UiMessage = {
611-
title: 'Registering token',
612-
description: tokenAddress,
613-
icon: 'add',
614-
};
615-
notificationIdentifier = this.notificationService.addPendingAction(
616-
message
617-
);
618-
}),
619-
switchMap(() =>
620-
this.http.put(
621-
`${this.raidenConfig.api}/tokens/${tokenAddress}`,
622-
{}
623-
)
624-
),
607+
return defer(() => {
608+
const message: UiMessage = {
609+
title: 'Registering token',
610+
description: tokenAddress,
611+
icon: 'add',
612+
};
613+
notificationIdentifier = this.notificationService.addPendingAction(
614+
message
615+
);
616+
617+
return this.http.put(
618+
`${this.raidenConfig.api}/tokens/${tokenAddress}`,
619+
{}
620+
);
621+
}).pipe(
625622
mapTo(null),
626623
tap(() => {
627624
const message: UiMessage = {
@@ -654,35 +651,33 @@ export class RaidenService {
654651
let notificationIdentifier: number;
655652
let errorCount = 0;
656653

657-
return of(null).pipe(
658-
tap(() => {
659-
this.quickConnectPending[token.address] = true;
660-
const message: UiMessage = {
661-
title: 'Quick Connect',
662-
description: `${connectionChoices.length} channels on ${token.symbol}`,
663-
icon: 'thunderbolt',
664-
userToken: token,
665-
};
666-
notificationIdentifier = this.notificationService.addPendingAction(
667-
message
668-
);
669-
}),
670-
switchMap(() => {
671-
const openChannelObservables = connectionChoices.map((choice) =>
672-
this.openChannel(
673-
token.address,
674-
choice.partnerAddress,
675-
this.raidenConfig.config.settle_timeout,
676-
choice.deposit
677-
).pipe(
678-
catchError(() => {
679-
errorCount++;
680-
return of(null);
681-
})
682-
)
683-
);
684-
return forkJoin(openChannelObservables);
685-
}),
654+
return defer(() => {
655+
this.quickConnectPending[token.address] = true;
656+
const message: UiMessage = {
657+
title: 'Quick Connect',
658+
description: `${connectionChoices.length} channels on ${token.symbol}`,
659+
icon: 'thunderbolt',
660+
userToken: token,
661+
};
662+
notificationIdentifier = this.notificationService.addPendingAction(
663+
message
664+
);
665+
666+
const openChannelObservables = connectionChoices.map((choice) =>
667+
this.openChannel(
668+
token.address,
669+
choice.partnerAddress,
670+
this.raidenConfig.config.settle_timeout,
671+
choice.deposit
672+
).pipe(
673+
catchError(() => {
674+
errorCount++;
675+
return of(null);
676+
})
677+
)
678+
);
679+
return forkJoin(openChannelObservables);
680+
}).pipe(
686681
switchMap(() =>
687682
errorCount === connectionChoices.length
688683
? throwError('All channel creations failed')
@@ -720,23 +715,21 @@ export class RaidenService {
720715
public leaveTokenNetwork(userToken: UserToken): Observable<void> {
721716
let notificationIdentifier: number;
722717

723-
return of(null).pipe(
724-
tap(() => {
725-
const message: UiMessage = {
726-
title: 'Leaving token network',
727-
description: `${userToken.symbol}`,
728-
icon: 'close',
729-
userToken: userToken,
730-
};
731-
notificationIdentifier = this.notificationService.addPendingAction(
732-
message
733-
);
734-
}),
735-
switchMap(() =>
736-
this.http.delete(
737-
`${this.raidenConfig.api}/connections/${userToken.address}`
738-
)
739-
),
718+
return defer(() => {
719+
const message: UiMessage = {
720+
title: 'Leaving token network',
721+
description: `${userToken.symbol}`,
722+
icon: 'close',
723+
userToken: userToken,
724+
};
725+
notificationIdentifier = this.notificationService.addPendingAction(
726+
message
727+
);
728+
729+
return this.http.delete(
730+
`${this.raidenConfig.api}/connections/${userToken.address}`
731+
);
732+
}).pipe(
740733
mapTo(null),
741734
tap(() => {
742735
const message: UiMessage = {
@@ -800,24 +793,22 @@ export class RaidenService {
800793
let notificationIdentifier: number;
801794
const formattedAmount = amountToDecimal(amount, token.decimals);
802795

803-
return of(null).pipe(
804-
tap(() => {
805-
const message: UiMessage = {
806-
title: 'Minting',
807-
description: `${formattedAmount} ${token.symbol} on-chain`,
808-
icon: 'token',
809-
userToken: token,
810-
};
811-
notificationIdentifier = this.notificationService.addPendingAction(
812-
message
813-
);
814-
}),
815-
switchMap(() =>
816-
this.http.post(
817-
`${this.raidenConfig.api}/_testing/tokens/${token.address}/mint`,
818-
{ to: targetAddress, value: amount }
819-
)
820-
),
796+
return defer(() => {
797+
const message: UiMessage = {
798+
title: 'Minting',
799+
description: `${formattedAmount} ${token.symbol} on-chain`,
800+
icon: 'token',
801+
userToken: token,
802+
};
803+
notificationIdentifier = this.notificationService.addPendingAction(
804+
message
805+
);
806+
807+
return this.http.post(
808+
`${this.raidenConfig.api}/_testing/tokens/${token.address}/mint`,
809+
{ to: targetAddress, value: amount }
810+
);
811+
}).pipe(
821812
mapTo(null),
822813
tap(() => {
823814
const message: UiMessage = {

0 commit comments

Comments
 (0)