1
- import { firstElement } from "@tabcat/ith-element" ;
1
+ import { firstElement , lastElement } from "@tabcat/ith-element" ;
2
2
import { union } from "@tabcat/ordered-sets/union" ;
3
3
import { pairwiseTraversal } from "@tabcat/ordered-sets/util" ;
4
4
import { Blockstore } from "interface-blockstore" ;
@@ -74,21 +74,33 @@ const handleUpdate = (
74
74
}
75
75
} ;
76
76
77
- async function takeOne < T > ( it : AwaitIterable < T > ) : Promise < T | void > {
78
- for await ( const v of it ) return v ;
77
+ async function takeOneUpdate (
78
+ updates : AwaitIterable < Update | Update [ ] > ,
79
+ ) : Promise < Update | void > {
80
+ for await ( const u of updates ) return Array . isArray ( u ) ? u [ 0 ] : u ;
79
81
}
80
82
81
83
async function populateUpdts (
82
- updates : AwaitIterable < Update > ,
84
+ updates : AwaitIterable < Update | Update [ ] > ,
83
85
updts : Update [ ] ,
84
86
updatee : Bucket ,
85
87
isHead : boolean ,
86
88
) : Promise < void > {
87
- for await ( const update of updates ) {
88
- updts . push ( update ) ;
89
- const boundary = updatee . getBoundary ( ) ;
89
+ for await ( let _updates of updates ) {
90
+ if ( ! Array . isArray ( _updates ) ) {
91
+ _updates = [ _updates ] ;
92
+ }
93
+
94
+ for ( const u of _updates ) {
95
+ updts . push ( u ) ;
96
+ }
90
97
91
- if ( boundary != null && ! isHead && compareTuples ( update , boundary ) >= 0 ) {
98
+ const boundary = updatee . getBoundary ( ) ;
99
+ if (
100
+ boundary != null &&
101
+ ! isHead &&
102
+ compareTuples ( lastElement ( updts ) , boundary ) >= 0
103
+ ) {
92
104
break ;
93
105
}
94
106
}
@@ -106,13 +118,14 @@ async function populateUpdts(
106
118
export async function * mutate (
107
119
blockstore : Blockstore ,
108
120
tree : ProllyTree ,
109
- updates : AwaitIterable < Update > ,
121
+ updates : AwaitIterable < Update | Update [ ] > ,
110
122
) : AsyncGenerator < ProllyTreeDiff > {
111
123
// whole function should be rewritten around updates async iterator, too complicated right now
112
124
if ( Array . isArray ( updates ) ) {
113
125
updates = updates [ Symbol . iterator ] ( ) ;
114
126
}
115
- const firstUpdate = await takeOne ( updates ) ;
127
+
128
+ const firstUpdate = await takeOneUpdate ( updates ) ;
116
129
117
130
if ( firstUpdate == null ) {
118
131
return tree ;
@@ -270,7 +283,7 @@ export async function* mutate(
270
283
break ;
271
284
}
272
285
273
- let nextUpdt = updts [ 0 ] ?? ( await takeOne ( updates ) ) ;
286
+ let nextUpdt = updts [ 0 ] ?? ( await takeOneUpdate ( updates ) ) ;
274
287
let nextLevel = level ;
275
288
276
289
if ( nextUpdt == null ) {
0 commit comments