Added subscriptions in redemption resolver to keep up with changes. Fixed double data entries in some cases (due to resolver changes).

This commit is contained in:
Tom 2025-01-14 01:02:35 +00:00
parent 04a50f6db0
commit c5cdf84f3b
2 changed files with 54 additions and 47 deletions

View File

@ -36,7 +36,7 @@ import { HermesClientService } from '../../hermes-client.service';
templateUrl: './redemption-list.component.html', templateUrl: './redemption-list.component.html',
styleUrl: './redemption-list.component.scss' styleUrl: './redemption-list.component.scss'
}) })
export class RedemptionListComponent implements OnInit, OnDestroy { export class RedemptionListComponent implements OnDestroy {
private readonly client = inject(HermesClientService); private readonly client = inject(HermesClientService);
private readonly redemptionService = inject(RedemptionService); private readonly redemptionService = inject(RedemptionService);
private readonly route = inject(ActivatedRoute); private readonly route = inject(ActivatedRoute);
@ -58,56 +58,52 @@ export class RedemptionListComponent implements OnInit, OnDestroy {
this._twitchRedemptionsDict = Object.assign({}, ...r['twitchRedemptions'].map((t: TwitchRedemption) => ({ [t.id]: t.title }))); this._twitchRedemptionsDict = Object.assign({}, ...r['twitchRedemptions'].map((t: TwitchRedemption) => ({ [t.id]: t.title })));
this._actions = r['redeemableActions']; this._actions = r['redeemableActions'];
let redemptions = r['redemptions']; let redemptions = [...r['redemptions']];
redemptions.sort((a: Redemption, b: Redemption) => this.compare(a, b)); redemptions.sort((a: Redemption, b: Redemption) => this.compare(a, b));
this._redemptions = redemptions; this._redemptions = redemptions;
let subscription = this.redemptionService.create$?.subscribe(d => {
if (d.error || !d.data || d.request.nounce != null && d.request.nounce.startsWith(this.client.session_id)) {
return;
}
let index = -1;
for (let i = 0; i < this._redemptions.length; i++) {
const comp = this.compare(d.data, this._redemptions[i]);
if (comp < 0) {
index = i;
break;
}
}
this._redemptions.splice(index >= 0 ? index : this._redemptions.length, 0, d.data);
});
if (subscription)
this._subscriptions.push(subscription);
subscription = this.redemptionService.update$?.subscribe(d => {
if (d.error || !d.data || d.request.nounce != null && d.request.nounce.startsWith(this.client.session_id))
return;
const redemption = this._redemptions.find(r => r.id = d.data.id);
if (redemption) {
redemption.action_name = d.data.action_name;
redemption.redemption_id = d.data.redemption_id;
redemption.order = d.data.order;
redemption.state = d.data.state;
}
});
if (subscription)
this._subscriptions.push(subscription);
subscription = this.redemptionService.delete$?.subscribe(d => {
if (d.error || d.request.nounce != null && d.request.nounce.startsWith(this.client.session_id))
return;
this._redemptions = this._redemptions.filter(r => r.id != d.request.data.id);
});
if (subscription)
this._subscriptions.push(subscription);
}); });
}
ngOnInit(): void { let subscription = this.redemptionService.create$?.subscribe(d => {
if (d.error || !d.data || d.request.nounce != null && d.request.nounce.startsWith(this.client.session_id)) {
return;
}
let index = -1;
for (let i = 0; i < this._redemptions.length; i++) {
const comp = this.compare(d.data, this._redemptions[i]);
if (comp < 0) {
index = i;
break;
}
}
this._redemptions.splice(index >= 0 ? index : this._redemptions.length, 0, d.data);
});
if (subscription)
this._subscriptions.push(subscription);
subscription = this.redemptionService.update$?.subscribe(d => {
if (d.error || !d.data || d.request.nounce != null && d.request.nounce.startsWith(this.client.session_id))
return;
const redemption = this._redemptions.find(r => r.id = d.data.id);
if (redemption) {
redemption.action_name = d.data.action_name;
redemption.redemption_id = d.data.redemption_id;
redemption.order = d.data.order;
redemption.state = d.data.state;
}
});
if (subscription)
this._subscriptions.push(subscription);
subscription = this.redemptionService.delete$?.subscribe(d => {
if (d.error || d.request.nounce != null && d.request.nounce.startsWith(this.client.session_id))
return;
this._redemptions = this._redemptions.filter(r => r.id != d.request.data.id);
});
if (subscription)
this._subscriptions.push(subscription);
} }
ngOnDestroy(): void { ngOnDestroy(): void {

View File

@ -10,7 +10,6 @@ export class RedemptionService {
private client = inject(HermesClientService); private client = inject(HermesClientService);
private data: Redemption[] = [] private data: Redemption[] = []
create$: Observable<any> | undefined; create$: Observable<any> | undefined;
fetch$: Observable<any> | undefined;
update$: Observable<any> | undefined; update$: Observable<any> | undefined;
delete$: Observable<any> | undefined; delete$: Observable<any> | undefined;
loaded = false; loaded = false;
@ -19,6 +18,18 @@ export class RedemptionService {
this.create$ = this.client.filterByRequestType('create_redemption'); this.create$ = this.client.filterByRequestType('create_redemption');
this.update$ = this.client.filterByRequestType('update_redemption'); this.update$ = this.client.filterByRequestType('update_redemption');
this.delete$ = this.client.filterByRequestType('delete_redemption'); this.delete$ = this.client.filterByRequestType('delete_redemption');
this.create$?.subscribe(d => this.data.push(d.data));
this.update$?.subscribe(d => {
const redemption = this.data.find(r => r.id == d.data.id);
if (redemption) {
redemption.action_name = d.data.action_name;
redemption.redemption_id = d.data.redemption_id;
redemption.order = d.data.order;
redemption.state = d.data.state;
}
});
this.delete$?.subscribe(d => this.data = this.data.filter(r => r.id != d.request.data.id));
} }