(
growthBookClient: GrowthBook,
result: Awaited<ReturnType<GrowthBook['init']>>,
selectedClientKey: string,
)
| 635 | }) |
| 636 | |
| 637 | const finalizeInit = async ( |
| 638 | growthBookClient: GrowthBook, |
| 639 | result: Awaited<ReturnType<GrowthBook['init']>>, |
| 640 | selectedClientKey: string, |
| 641 | ): Promise<void> => { |
| 642 | // Guard: if this client was replaced by a newer one, skip processing |
| 643 | if (client !== growthBookClient) { |
| 644 | if (isInternalBuild()) { |
| 645 | logForDebugging( |
| 646 | 'GrowthBook: Skipping init callback for replaced client', |
| 647 | ) |
| 648 | } |
| 649 | return |
| 650 | } |
| 651 | |
| 652 | if (isInternalBuild()) { |
| 653 | logForDebugging( |
| 654 | `GrowthBook initialized successfully, source: ${result.source}, success: ${result.success}, clientKey: ${selectedClientKey}`, |
| 655 | ) |
| 656 | if (!result.success) { |
| 657 | logForDebugging( |
| 658 | `GrowthBook init failure detail: ${getGrowthBookInitErrorMessage(result.error)}`, |
| 659 | ) |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | if ( |
| 664 | isInternalBuild() && |
| 665 | !result.success && |
| 666 | selectedClientKey !== EXTERNAL_GROWTHBOOK_CLIENT_KEY && |
| 667 | isInvalidGrowthBookClientKey(result.error) |
| 668 | ) { |
| 669 | logForDebugging( |
| 670 | `GrowthBook: Client key ${selectedClientKey} rejected by project registry, retrying with external project key`, |
| 671 | ) |
| 672 | growthBookClient.destroy() |
| 673 | const fallbackClient = createClient(EXTERNAL_GROWTHBOOK_CLIENT_KEY) |
| 674 | client = fallbackClient |
| 675 | const fallbackResult = await fallbackClient.init({ timeout: 5000 }) |
| 676 | await finalizeInit( |
| 677 | fallbackClient, |
| 678 | fallbackResult, |
| 679 | EXTERNAL_GROWTHBOOK_CLIENT_KEY, |
| 680 | ) |
| 681 | return |
| 682 | } |
| 683 | |
| 684 | const hadFeatures = await processGrowthBookPayload(growthBookClient) |
| 685 | // Re-check: processGrowthBookPayload yields at `await setPayload`. |
| 686 | // Microtask-only today (no encryption, no sticky-bucket service), but |
| 687 | // the guard at the top of this callback runs before that await; |
| 688 | // this runs after. |
| 689 | if (client !== growthBookClient) return |
| 690 | |
| 691 | if (hadFeatures) { |
| 692 | for (const feature of pendingExposures) { |
| 693 | logExposureForFeature(feature) |
| 694 | } |
no test coverage detected