Files
discord-decomp/modules/premium/promotions/PromotionsManager.tsx
T
2026-11-01 12:12:55 +02:00

84 lines
3.7 KiB
TypeScript

// Metro module 16302 (f16303).
import _asyncToGenerator from "../../../_bundle/_asyncToGenerator";
import LocaleStore from "../../user_settings/LocaleStore";
import UserStore from "../../../stores/UserStore";
import SubscriptionStore from "../../../stores/billing/SubscriptionStore";
import PromotionsStore from "./PromotionsStore";
import { PremiumTypes } from "../PremiumConstants";
import { EntitlementTypes } from "../../../Constants";
import AutomaticLifecycleManager from "../../../lib/AutomaticLifecycleManager";
import { maybeFetchActivePromotions } from "./PromotionsActionCreators";
import PremiumUtils from "../../../utils/PremiumUtils";
import { maybeFetchActiveBogoPromotion } from "../PremiumBogoPromotionUtilsCommon";
class PromotionsManager extends AutomaticLifecycleManager {
actions = {
POST_CONNECTION_OPEN: this.onPostConnectionOpen.bind(this),
EXPERIMENTS_FETCH_SUCCESS: this.onPostConnectionOpen.bind(this),
IAP_PURCHASE_PRODUCT_SUCCESS: this.onMobilePurchaseSuccess.bind(this),
GPLAY_PURCHASE_VERIFIED: this.onMobilePurchaseSuccess.bind(this),
BILLING_USER_OFFER_ACKNOWLEDGED_SUCCESS: this.onOfferUpdated.bind(this),
BILLING_USER_TRIAL_OFFER_ACKNOWLEDGED_SUCCESS: this.onOfferUpdated.bind(this),
BILLING_REFERRAL_RESOLVE_SUCCESS: this.onOfferUpdated.bind(this),
VIRTUAL_CURRENCY_REDEEM_SUCCESS: this.onVCRedeemed.bind(this)
};
stores = new Map().set(LocaleStore, this.onLocaleChanged.bind(this)).set(SubscriptionStore, this.onSubscriptionStateChanged.bind(this)).set(PromotionsStore, this.onPromotionsFetchSettled.bind(this));
lastSubscriptionStateSignature = null;
hasPendingSubscriptionRefetch = false;
maybeFetchBogoPromotion = _asyncToGenerator(function*() {
L11: {
if (PremiumUtils.isPremiumExactly(UserStore.getCurrentUser(), PremiumTypes.TIER_2)) {
break L11;
}
yield maybeFetchActiveBogoPromotion();
return void 0;
}
});
_terminate() {
this.hasPendingSubscriptionRefetch = false;
this.lastSubscriptionStateSignature = null;
}
onLocaleChanged() {
if (PromotionsStore.lastFetchedActivePromotions != null && PromotionsStore.lastFetchedActivePromotionsLocale !== LocaleStore.locale) {
maybeFetchActivePromotions(false);
}
}
async onPostConnectionOpen() {
const _this = this;
maybeFetchActivePromotions();
await _this.maybeFetchBogoPromotion();
}
onSubscriptionStateChanged() {
const subscriptions = SubscriptionStore.getSubscriptions(false);
const lastSubscriptionStateSignature = subscriptions == null ? "" : Object.values(subscriptions).map((item) => `${item.id}:${item.type}:${item.status}:${item.items.map((item) => item.planId).sort().join("|")}`).sort().join(",");
if (lastSubscriptionStateSignature !== this.lastSubscriptionStateSignature) {
this.lastSubscriptionStateSignature = lastSubscriptionStateSignature;
if (PromotionsStore.lastFetchedActivePromotions != null) {
if (PromotionsStore.isFetchingActivePromotions) {
this.hasPendingSubscriptionRefetch = true;
} else {
maybeFetchActivePromotions(false);
}
}
}
}
onPromotionsFetchSettled() {
if (this.hasPendingSubscriptionRefetch && !PromotionsStore.isFetchingActivePromotions) {
this.hasPendingSubscriptionRefetch = false;
maybeFetchActivePromotions(false);
}
}
onMobilePurchaseSuccess() {
maybeFetchActivePromotions(false);
}
onOfferUpdated() {
maybeFetchActivePromotions(false);
}
onVCRedeemed(a0) {
if (a0.entitlements.some((entitlement) => entitlement.type === EntitlementTypes.FRACTIONAL_REDEMPTION)) {
maybeFetchActivePromotions(false);
}
}
}
const promotionsManager = new PromotionsManager();
export default promotionsManager;