import * as Storage from './storage' ; export function compare ( current , last ) { if ( current . major !== last . major ) return current . major < last . major ? MP . VersionChangeType . Rollback : MP . VersionChangeType . Upgrade ; if ( current . minor !== last . minor ) return current . minor < last . minor ? MP . VersionChangeType . Rollback : MP . VersionChangeType . Upgrade ; if ( current . patch !== last . patch ) return current . patch < last . patch ? MP . VersionChangeType . Rollback : MP . VersionChangeType . Upgrade ; return MP . VersionChangeType . NotChange ; } export function parse ( versionStr ) { const matchRes = versionStr . trim ( ) . match ( /^[^\d]*(\d*)(?:\.(\d*))?(?:\.(\d*))?$/ ) ; const version = matchRes ?. slice ( 1 , 4 ) . map ( mark => ( mark ? parseInt ( mark ) : 0 ) ) ; return version && { major : version [ 0 ] , minor : version [ 1 ] , patch : version [ 2 ] } ; } export function stringify ( version , prefix = '' ) { return `${ prefix }${ version . major }.${ version . minor }.${ version . patch }` ; } export const OnMPUpgradeCallbackQueue = new Map < MP . OnVersionChangeCallback , number > ( ) ; export const OnMPRollbackCallbackQueue = new Map < MP . OnVersionChangeCallback , number > ( ) ; export function onMPRollback ( callback , weight = 1 ) { if ( weight < 0 ) console . warn ( 'the weight of `OnMPVersionChangeCallback` SHOULD NOT less than 0' ) ; OnMPRollbackCallbackQueue . set ( callback , weight ) ; } export function onMPUpgrade ( callback , weight = 1 ) { if ( weight < 0 ) console . warn ( 'the weight of `OnMPVersionChangeCallback` SHOULD NOT less than 0' ) ; OnMPUpgradeCallbackQueue . set ( callback , weight ) ; } export function onMPVersionChange ( callback , weight = 1 ) { if ( weight < 0 ) console . warn ( 'the weight of `OnMPVersionChangeCallback` SHOULD NOT less than 0' ) ; OnMPUpgradeCallbackQueue . set ( callback , weight ) ; OnMPRollbackCallbackQueue . set ( callback , weight ) ; } export function applyMPVersion ( current , meta ) { return new Promise < any > ( ( success , fail ) => wx . getStorage ( { key : Storage . Key . Version , success , fail } ) ) . then ( ( r ) => [ compare ( current , r . data ) , r . data ] as const ) . catch ( ( ) => [ MP . VersionChangeType . Upgrade , parse ( '0' ) ! ] as const ) . then ( ( [ changeType , last ] ) => changeType === MP . VersionChangeType . Upgrade ? applyMPUpgrade ( current , last , meta ) : changeType === MP . VersionChangeType . Rollback ? applyMPRollback ( current , last , meta ) : void 0 ) ; } async function applyMPRollback ( current , last , meta ) { const tasks = [ ... OnMPRollbackCallbackQueue . entries ( ) ] . sort ( ( left , right ) => right [ 1 ] - left [ 1 ] ) . map ( ( [ callback , _weight ] ) => callback ( current , last , meta ) ) ; await Promise . all ( tasks ) ; } async function applyMPUpgrade ( current , last , meta ) { const tasks = [ ... OnMPUpgradeCallbackQueue . entries ( ) ] . sort ( ( left , right ) => right [ 1 ] - left [ 1 ] ) . map ( ( [ callback , _weight ] ) => callback ( current , last , meta ) ) ; await Promise . all ( tasks ) ; }