@@ -12,7 +12,7 @@ import {
1212import type { HeliaLibp2p } from "helia" ;
1313import type { Libp2p } from "libp2p" ;
1414import type { ServiceMap } from "@libp2p/interface" ;
15- import itAll from ' it-all'
15+ import itAll from " it-all" ;
1616import { getScalePosition } from "./utils.js" ;
1717
1818export type OrderedKeyValueDatabaseType = Awaited <
@@ -92,7 +92,6 @@ export const OrderedKeyValueApi = ({
9292} : {
9393 database : InternalDatabase ;
9494} ) => {
95-
9695 const put = async (
9796 key : string ,
9897 value : DagCborEncodable ,
@@ -102,26 +101,24 @@ export const OrderedKeyValueApi = ({
102101 const entries = await itAll ( iterator ( ) ) ;
103102 const entryValue : { value : DagCborEncodable ; position : number } = {
104103 value,
105- position : await getScalePosition ( { entries, key, position} ) ,
104+ position : await getScalePosition ( { entries, key, position } ) ,
106105 } ;
107106 return database . addOperation ( { op : "PUT" , key, value : entryValue } ) ;
108107 } ;
109108
110109 const move = async ( key : string , position : number ) : Promise < void > => {
111110 // Somewhat inefficient, I suppose, but we need to know which entries are already present.
112111 const entries = await itAll ( iterator ( ) ) ;
113- position = await getScalePosition ( { entries, key, position} ) ;
114-
112+ position = await getScalePosition ( { entries, key, position } ) ;
113+
115114 await database . addOperation ( { op : "MOVE" , key, value : position } ) ;
116115 } ;
117116
118117 const del = async ( key : string ) : Promise < string > => {
119118 return database . addOperation ( { op : "DEL" , key, value : null } ) ;
120119 } ;
121120
122- const get = async (
123- key : string ,
124- ) : Promise < DagCborEncodable | undefined > => {
121+ const get = async ( key : string ) : Promise < DagCborEncodable | undefined > => {
125122 for await ( const entry of database . log . traverse ( ) ) {
126123 const { op, key : k , value } = entry . payload ;
127124 if ( op === "PUT" && k === key ) {
@@ -146,8 +143,8 @@ export const OrderedKeyValueApi = ({
146143 unknown
147144 > {
148145 let count = 0 ;
149- const keys : { [ key : string ] : true } = { } ;
150- const positions : { [ key : string ] : number } = { } ;
146+ const keys : { [ key : string ] : true } = { } ;
147+ const positions : { [ key : string ] : number } = { } ;
151148
152149 for await ( const entry of database . log . traverse ( ) ) {
153150 const { op, key, value } = entry . payload ;
@@ -157,17 +154,16 @@ export const OrderedKeyValueApi = ({
157154 if ( op === "PUT" ) {
158155 const hash = entry . hash ;
159156 const putValue = value as { value : unknown ; position ?: number } ;
160-
157+
161158 keys [ key ] = true ;
162159 count ++ ;
163-
160+
164161 yield {
165162 key,
166163 value : putValue . value ,
167164 position : positions [ key ] ?? putValue . position ?? - 1 ,
168165 hash,
169- }
170-
166+ } ;
171167 } else if ( op === "MOVE" ) {
172168 if ( positions [ key ] !== undefined || keys [ key ] ) continue ;
173169 positions [ key ] = value as number ;
@@ -178,7 +174,6 @@ export const OrderedKeyValueApi = ({
178174 break ;
179175 }
180176 }
181-
182177 } ;
183178
184179 const all = async ( ) => {
@@ -191,11 +186,13 @@ export const OrderedKeyValueApi = ({
191186 for await ( const entry of iterator ( ) ) {
192187 entries . push ( entry ) ;
193188 }
194- return entries . sort ( ( a , b ) => a . position - b . position ) . map ( e => ( {
195- key : e . key ,
196- value : e . value ,
197- hash : e . hash ,
198- } ) ) ;
189+ return entries
190+ . sort ( ( a , b ) => a . position - b . position )
191+ . map ( ( e ) => ( {
192+ key : e . key ,
193+ value : e . value ,
194+ hash : e . hash ,
195+ } ) ) ;
199196 } ;
200197
201198 return {
0 commit comments