import { AddInitializedProp , PickObservableValues , WebpackJsonpArrayItem , WebpackJsonpPropAwareWindow } from "./model" ; import { Logger } from "src/shared/model/common" ; import { assertTypeOf } from "src/shared/util" ; export const buildFullyInitializedResolver = < T extends Record < string , { initialized ? : boolean ; } > > ( result , resolve , logger ) : ( ) => void => { return ( ) => { const unInitializedEntries = Object . entries ( result ) . filter ( ( [ , value ] ) => ! value . initialized ) ; if ( unInitializedEntries . length ) { const uninitializedKeys = unInitializedEntries . map ( ( [ key ] ) => key ) ; logger . verbose ( `uninitialized keys (${ uninitializedKeys . length }):` , JSON . stringify ( uninitializedKeys ) ) ; return ; } logger . info ( nameof ( buildFullyInitializedResolver ) ) ; resolve ( result ) ; } ; } ; export const markInternalsRecordAsInitialized = < T > ( result , resultKey , resolveIfAllInitialized , logger ) : void => { if ( result [ resultKey ] . initialized ) { return ; } result [ resultKey ] . initialized = true ; logger . verbose ( nameof ( markInternalsRecordAsInitialized ) , `internals record initialized: ${ JSON . stringify ( { resultKey : resultKey } ) }` ) ; resolveIfAllInitialized ( ) ; } ; export const plainChunkItemHandler = < T , RI extends { value : Record < string , unknown > ; } > ( { resultKey , resultItem , markInternalsRecordAsInitialized , webpack_exports , logger , } ) : void => { for ( const [ key , valueStub ] of Object . entries ( resultItem . value ) ) { if ( ! ( key in webpack_exports ) ) { throw new Error ( `Failed to locate expected "${ key }" in the webpack exports object` ) ; } assertTypeOf ( { value : webpack_exports [ key ] , expectedType : typeof valueStub } , "Failed to locate expected value type" ) ; } ( resultItem as Mutable < typeof resultItem > ) . value = webpack_exports as any ; markInternalsRecordAsInitialized ( ) ; { const typeOf = typeof resultItem . value ; const details = { resultKey , typeOf , ... ( typeOf === "object" && { ownPropertyNames : Object . getOwnPropertyNames ( resultItem . value ) } ) , } ; logger . verbose ( `initialized: ${ JSON . stringify ( details ) }` ) ; } } ; export const overridePushMethodGlobally = < T > ( { resultKeys , preChunkItemOverridingHook , chunkItemHook , logger , } ) : void => { const resolveOverriddenPush = ( originalPush ) : typeof originalPush => { const result = ( firstArg , ... restArgs ) => { const [ , chunkItemsRecord ] = firstArg ; for ( const resultKey of resultKeys ) { const [ chunkItemKey , chunkItemValue ] = Object . entries ( chunkItemsRecord ) . find ( ( [ key ] ) => key === resultKey ) ?? [ null , null ] as const ; if ( ! chunkItemKey || ! chunkItemValue ) { continue ; } if ( preChunkItemOverridingHook ) { preChunkItemOverridingHook ( { resultKey } ) ; } chunkItemsRecord [ chunkItemKey ] = function ( ... args ) { const chunkItemValueCallResult = chunkItemValue . apply ( this , args ) ; const [ , webpack_exports , webpack_require ] = args ; chunkItemHook ( { resultKey , webpack_exports , webpack_require } ) ; return chunkItemValueCallResult ; } ; } return originalPush ( firstArg , ... restArgs ) ; } ; return result ; } ; ( window as WebpackJsonpPropAwareWindow ) . webpackJsonp = new Proxy ( ( window as WebpackJsonpPropAwareWindow ) . webpackJsonp ??= [ ] , { set ( webpackJsonp , prop , value ) { if ( prop !== "push" ) { Object . defineProperty ( webpackJsonp , prop , { value } ) ; return true ; } logger . verbose ( `override "webpackJsonp.push"` ) ; webpackJsonp . push = resolveOverriddenPush ( value as typeof webpackJsonp . push ) ; return true ; } , } ) ; } ; export const handleObservableValue = < R , T extends AddInitializedProp < PickObservableValues < R > > , K extends Extract < keyof T , string > > ( result , { resultKey , webpack_exports , itemKey = "default" , itemName , itemCallResultHandler , itemCallResultTypeValidation , resolveIfFullyInitialized , } , logger ) : void => { logger . verbose ( nameof ( handleObservableValue ) ) ; const markAsInitialized = ( ) : void => { markInternalsRecordAsInitialized ( result , resultKey , resolveIfFullyInitialized , logger ) ; } ; type ReactForwardRef = { $$typeof : string ; render : T [ K ] [ "_valueShape" ] ; } ; const resolvedExportsItem = ( ( ) => { const rawItem = webpack_exports [ itemKey ] as ReactForwardRef [ "render" ] | ReactForwardRef ; if ( typeof rawItem !== "object" ) { return { item : rawItem } ; } if ( String ( rawItem . $$typeof ) === "Symbol(react.forward_ref)" && typeof rawItem . render === "function" ) { return { item : rawItem . render , forwardRef : rawItem } ; } throw new Error ( `Unexpected exported item type (${ JSON . stringify ( { itemKey , itemName } ) })` ) ; } ) ( ) ; const { item } = resolvedExportsItem ; assertTypeOf ( { value : item , expectedType : "function" } , `Invalid exported item type (${ JSON . stringify ( { itemKey , itemName } ) })` ) ; { const { name : actualName } = item ; const expectedName = itemName ; if ( actualName !== expectedName ) { throw new Error ( `Invalid exported item name: ${ JSON . stringify ( { resultKey , actualName , expectedName } ) }` ) ; } } const exportsItemOverridden = function ( ... args ) { const itemCallResult = item ( ... args ) ; if ( itemCallResultTypeValidation ) { assertTypeOf ( { value : itemCallResult , expectedType : itemCallResultTypeValidation } , "Unexpected exported item call result type" ) ; } const itemCallResultCustom = itemCallResultHandler && itemCallResultHandler ( itemCallResult as ReturnType < typeof item > , ( notification ) => { result [ resultKey ] . value$ . next ( notification as any ) ; } , markAsInitialized ) ; return itemCallResultCustom ?? itemCallResult ; } ; if ( resolvedExportsItem . forwardRef ) { resolvedExportsItem . forwardRef . render = exportsItemOverridden ; webpack_exports [ itemKey ] = resolvedExportsItem . forwardRef ; return ; } webpack_exports [ itemKey ] = exportsItemOverridden ; } ;