import { TopologyData } from '@topology/core' ; export function createCacheTable ( ) { const request = indexedDB . open ( 'topology-caches' ) ; request . onupgradeneeded = ( e ) => { const db = e . target . result as IDBDatabase ; if ( db . objectStoreNames . contains ( 'caches' ) ) { db . deleteObjectStore ( 'caches' ) ; } db . createObjectStore ( 'caches' , { keyPath : 'dbIndex' , } ) ; } ; request . onsuccess = ( event ) => { const db = request . result ; if ( ! db . objectStoreNames . contains ( 'caches' ) ) { return ; } const get = db . transaction ( [ 'caches' ] , 'readwrite' ) . objectStore ( 'caches' ) ; get . clear ( ) ; } ; request . onerror = ( e ) => { console . warn ( '数据库打开失败' + e ) ; } ; } export function spliceCache ( index ) { const request = indexedDB . open ( 'topology-caches' ) ; request . onsuccess = ( event ) => { const db = request . result ; if ( ! db . objectStoreNames . contains ( 'caches' ) ) { return ; } const get = db . transaction ( [ 'caches' ] , 'readwrite' ) . objectStore ( 'caches' ) ; get . delete ( IDBKeyRange . lowerBound ( index ) ) ; } ; request . onupgradeneeded = ( e ) => { const db = e . target . result as IDBDatabase ; if ( db . objectStoreNames . contains ( 'caches' ) ) { db . deleteObjectStore ( 'caches' ) ; } db . createObjectStore ( 'caches' , { keyPath : 'dbIndex' , } ) ; } ; } export function pushCache ( data , index , length ) { const request = indexedDB . open ( 'topology-caches' ) ; request . onsuccess = ( event ) => { const db = request . result ; if ( ! db . objectStoreNames . contains ( 'caches' ) ) { return ; } const push = db . transaction ( [ 'caches' ] , 'readwrite' ) . objectStore ( 'caches' ) ; data . dbIndex = index ; push . add ( data ) ; const result = push . count ( ) ; result . onsuccess = ( ) => { if ( result . result > length ) { push . delete ( index - length ) ; } } ; } ; request . onupgradeneeded = ( e ) => { const db = e . target . result as IDBDatabase ; if ( db . objectStoreNames . contains ( 'caches' ) ) { db . deleteObjectStore ( 'caches' ) ; } db . createObjectStore ( 'caches' , { keyPath : 'dbIndex' , } ) ; } ; } export function getCache ( index ) { return new Promise ( ( resolve , reject ) => { const request = indexedDB . open ( 'topology-caches' ) ; request . onsuccess = ( event ) => { const db = request . result ; if ( ! db . objectStoreNames . contains ( 'caches' ) ) { resolve ( null ) ; return ; } const objectStore = db . transaction ( [ 'caches' ] ) . objectStore ( 'caches' ) ; const get = objectStore . get ( index ) ; get . onsuccess = ( ) => { resolve ( get . result ) ; } ; } ; request . onupgradeneeded = ( e ) => { const db = e . target . result as IDBDatabase ; if ( db . objectStoreNames . contains ( 'caches' ) ) { db . deleteObjectStore ( 'caches' ) ; } db . createObjectStore ( 'caches' , { keyPath : 'dbIndex' , } ) ; } ; } ) ; }