11import { fromBase64 , toBase64 } from '@cosmjs/encoding' ;
22import { compare } from '../helpers/byte-array' ;
3+ import Immutable from 'immutable' ;
34import { MAX_LENGTH_DB_KEY } from '../instance' ;
45
56export interface IStorage {
@@ -40,11 +41,11 @@ export interface IIterStorage {
4041
4142export class BasicKVStorage implements IStorage {
4243 // TODO: Add binary uint / typed Addr maps for cw-storage-plus compatibility
43- constructor ( public dict : { [ key : string ] : string | undefined } = { } ) { }
44+ constructor ( public dict : Immutable . Map < string , string > = Immutable . Map ( ) ) { }
4445
4546 get ( key : Uint8Array ) : Uint8Array | null {
4647 const keyStr = toBase64 ( key ) ;
47- const value = this . dict [ keyStr ] ;
48+ const value = this . dict . get ( keyStr ) ;
4849 if ( value === undefined ) {
4950 return null ;
5051 }
@@ -54,7 +55,7 @@ export class BasicKVStorage implements IStorage {
5455
5556 set ( key : Uint8Array , value : Uint8Array ) : void {
5657 const keyStr = toBase64 ( key ) ;
57- this . dict [ keyStr ] = toBase64 ( value ) ;
58+ this . dict = this . dict . set ( keyStr , toBase64 ( value ) ) ;
5859 }
5960
6061 remove ( key : Uint8Array ) : void {
@@ -63,7 +64,7 @@ export class BasicKVStorage implements IStorage {
6364 `Key length ${ key . length } exceeds maximum length ${ MAX_LENGTH_DB_KEY } .`
6465 ) ;
6566 }
66- this . dict [ toBase64 ( key ) ] = undefined ;
67+ this . dict = this . dict . remove ( toBase64 ( key ) ) ;
6768 }
6869}
6970
@@ -119,11 +120,11 @@ export class BasicKVIterStorage extends BasicKVStorage implements IIterStorage {
119120 }
120121
121122 let data : Record [ ] = [ ] ;
122- for ( const key of Object . keys ( this . dict ) ) {
123+ for ( const key of this . dict . keys ( ) ) {
123124 if ( start . length && compare ( start , fromBase64 ( key ) ) === 1 ) continue ;
124125 if ( end . length && compare ( fromBase64 ( key ) , end ) > - 1 ) break ;
125126
126- data . push ( { key : fromBase64 ( key ) , value : fromBase64 ( this . dict [ key ] ! ) } ) ;
127+ data . push ( { key : fromBase64 ( key ) , value : this . get ( fromBase64 ( key ) ) ! } ) ;
127128 }
128129
129130 if ( order === Order . Descending ) {
0 commit comments