import MagicString from 'magic-string' ; import { IfScope } from './if-scope' ; import { TemplateScopeManager } from './template-scope' ; import { surroundWithIgnoreComments } from '../../utils/ignore' ; import { BaseNode } from '../../interfaces' ; import { extractConstTags } from './const-tag' ; /**
 * Transform {#await ...} into something JSX understands
 */ export function handleAwait ( htmlx , str , awaitBlock , ifScope , templateScopeManager ) { let ifCondition = ifScope . getFullCondition ( ) ; ifCondition = ifCondition ? surroundWithIgnoreComments ( `if(${ ifCondition }) {` ) : '' ; templateScopeManager . awaitEnter ( awaitBlock ) ; const constRedeclares = ifScope . getConstDeclaration ( ) ; str . overwrite ( awaitBlock . start , awaitBlock . expression . start , `{() => {${ constRedeclares }${ ifCondition }let _$$p = (` ) ; const awaitEndStart = htmlx . lastIndexOf ( '{' , awaitBlock . end - 1 ) ; str . overwrite ( awaitEndStart , awaitBlock . end , '</>})}}' + ( ifCondition ? '}' : '' ) ) ; } export function handleAwaitPending ( awaitBlock , htmlx , str , ifScope ) { if ( awaitBlock . pending . skip ) { return ; } const pendingStart = htmlx . indexOf ( '}' , awaitBlock . expression . end ) ; const pendingEnd = ! awaitBlock . then . skip ? awaitBlock . then . start : ! awaitBlock . catch . skip ? awaitBlock . catch . start : htmlx . lastIndexOf ( '{' , awaitBlock . end ) ; str . overwrite ( awaitBlock . expression . end , pendingStart + 1 , ');' ) ; str . appendRight ( pendingStart + 1 , ` ${ ifScope . addPossibleIfCondition ( ) }<>` ) ; str . appendLeft ( pendingEnd , '</>; ' ) ; if ( ! awaitBlock . then . skip ) { return ; } str . appendLeft ( pendingEnd , '__sveltets_1_awaitThen(_$$p, () => {<>' ) ; } export function handleAwaitThen ( awaitBlock , htmlx , str , ifScope ) { if ( awaitBlock . then . skip ) { return ; } let thenStart ; let thenEnd ; if ( ! awaitBlock . pending . skip ) { thenStart = awaitBlock . then . start ; if ( awaitBlock . value ) { thenEnd = htmlx . indexOf ( '}' , awaitBlock . value . end ) + 1 ; } else { thenEnd = htmlx . indexOf ( '}' , awaitBlock . then . start ) + 1 ; } } else { thenStart = htmlx . indexOf ( 'then' , awaitBlock . expression . end ) ; thenEnd = htmlx . lastIndexOf ( '}' , awaitBlock . then . start ) + 1 ; str . overwrite ( awaitBlock . expression . end , thenStart , '); ' ) ; } if ( awaitBlock . value ) { str . overwrite ( thenStart , awaitBlock . value . start , '__sveltets_1_awaitThen(_$$p, (' ) ; str . overwrite ( awaitBlock . value . end , thenEnd , ') => {' ) ; extractConstTags ( awaitBlock . then . children ) . forEach ( ( insertion ) => { insertion ( thenEnd , str ) ; } ) ; str . appendRight ( thenEnd , `${ ifScope . addPossibleIfCondition ( ) }<>` ) ; } else { const awaitThenFn = '__sveltets_1_awaitThen(_$$p, () => {' ; if ( thenStart === thenEnd ) { str . appendLeft ( thenStart , awaitThenFn ) ; } else { str . overwrite ( thenStart , thenEnd , awaitThenFn ) ; } extractConstTags ( awaitBlock . then . children ) . forEach ( ( insertion ) => { insertion ( thenEnd , str ) ; } ) ; str . appendRight ( thenEnd , `${ ifScope . addPossibleIfCondition ( ) }<>` ) ; } } export function handleAwaitCatch ( awaitBlock , htmlx , str , ifScope ) { if ( awaitBlock . catch . skip ) { return ; } if ( awaitBlock . pending . skip && awaitBlock . then . skip ) { if ( awaitBlock . error ) { const catchBegin = htmlx . indexOf ( '}' , awaitBlock . error . end ) + 1 ; str . overwrite ( awaitBlock . expression . end , awaitBlock . error . start , '); __sveltets_1_awaitThen(_$$p, () => {}, (' ) ; str . overwrite ( awaitBlock . error . end , catchBegin , ') => {' ) ; extractConstTags ( awaitBlock . catch . children ) . forEach ( ( insertion ) => { insertion ( catchBegin , str ) ; } ) ; str . appendRight ( catchBegin , '<>' ) ; } else { const catchBegin = htmlx . indexOf ( '}' , awaitBlock . expression . end ) + 1 ; str . overwrite ( awaitBlock . expression . end , catchBegin , '); __sveltets_1_awaitThen(_$$p, () => {}, () => {' ) ; extractConstTags ( awaitBlock . catch . children ) . forEach ( ( insertion ) => { insertion ( catchBegin , str ) ; } ) ; str . appendRight ( catchBegin , '<>' ) ; } } else { const catchStart = awaitBlock . catch . start ; const catchSymbolEnd = htmlx . indexOf ( ':catch' , catchStart ) + ':catch' . length ; const errorStart = awaitBlock . error ? awaitBlock . error . start : catchSymbolEnd ; const errorEnd = awaitBlock . error ? awaitBlock . error . end : errorStart ; const catchEnd = htmlx . indexOf ( '}' , errorEnd ) + 1 ; str . overwrite ( catchStart , errorStart , '</>}, (' ) ; str . overwrite ( errorEnd , catchEnd , ') => {' ) ; extractConstTags ( awaitBlock . catch . children ) . forEach ( ( insertion ) => { insertion ( catchEnd , str ) ; } ) ; str . appendRight ( catchEnd , `${ ifScope . addPossibleIfCondition ( ) }<>` ) ; } }