import type { State , StateVersion } from "./latest" ; import { migrate as migrate0to1 } from "./1" ; import { migrate as migrate1to2 } from "./2" ; import { migrate as migrate2to3 } from "./3" ; import { migrate as migrate3to4 } from "./4" ; import { migrate as migrate4to5 } from "./5" ; import { migrate as migrate5to6 } from "./6" ; import { migrate as migrate6to7 } from "./7" ; const LATEST_STATE_VERSION = 7 ; const MIGRATIONS = [ migrate0to1 , migrate1to2 , migrate2to3 , migrate3to4 , migrate4to5 , migrate5to6 , migrate6to7 , ] ; interface AnyStateVersion { stateVersion : number ; } function isVersioned ( state ) { return state && ( state as AnyStateVersion ) . stateVersion != null ; } function getStartingVersion ( state ) { if ( state == null ) { return 0 ; } else if ( isVersioned ( state ) ) { return state . stateVersion ; } else if ( state . tasks != null ) { return 1 ; } else { return 0 ; } } export function migrateState ( state ) { let version = getStartingVersion ( state ) ; if ( version > LATEST_STATE_VERSION ) { version = 0 ; } MIGRATIONS . slice ( version ) . forEach ( ( migration ) => { state = migration ( state ) ; } ) ; return state ; }