@@ -12,7 +12,7 @@ export type Keep<Shard> =
12
12
| [ 'none' ]
13
13
| [ 'count' , number ]
14
14
| [ 'first' , ShardSeekFn < Shard > ]
15
- | [ 'since' , ( now : number ) => number ]
15
+ | [ 'since' , number ]
16
16
| [ 'min' , Keep < Shard > [ ] ]
17
17
| [ 'max' , Keep < Shard > [ ] ] ;
18
18
@@ -58,6 +58,8 @@ export default class Crystalizer<
58
58
[ count : number ] : { crystal : Crystal ; shards : Shard [ ] } ;
59
59
} = { } ;
60
60
61
+ private sorts : SingleSort < Shard > [ ] ;
62
+
61
63
constructor ( _opts : UserOpts < Crystal , Shard > | Opts < Crystal , Shard > ) {
62
64
let opts : Opts < Crystal , Shard > = {
63
65
keep : [ 'all' ] ,
@@ -73,6 +75,20 @@ export default class Crystalizer<
73
75
crystal : deepCopy ( opts . initial ) ,
74
76
} ;
75
77
78
+ if ( opts . sort ) {
79
+ const isMultisort = opts . sort [ 0 ] instanceof Array ;
80
+
81
+ this . sorts = (
82
+ isMultisort ? opts . sort : [ opts . sort ]
83
+ ) as SingleSort < Shard > [ ] ;
84
+ } else {
85
+ this . sorts = [ ] ;
86
+ }
87
+
88
+ if ( opts . tsKey ) {
89
+ this . sorts . unshift ( [ 'asc' , opts . tsKey ] ) ;
90
+ }
91
+
76
92
opts . __ptr = Math . max ( 0 , opts . __ptr ) ;
77
93
78
94
this . opts = opts ;
@@ -141,7 +157,7 @@ export default class Crystalizer<
141
157
}
142
158
143
159
focus ( seek : ShardSeekFn < Shard > ) {
144
- return this . buildNew ( { __focus : seek } ) ;
160
+ return this . buildNew ( { __focus : seek , __ptr : 0 } ) ;
145
161
}
146
162
147
163
with ( shards : Shard | Shard [ ] ) {
@@ -154,18 +170,21 @@ export default class Crystalizer<
154
170
}
155
171
if ( this . opts . tsKey ) {
156
172
shards [ i ] = {
157
- ...shards [ i ] ,
158
173
[ this . opts . tsKey ] : this . opts . __getTime ( ) ,
174
+ ...shards [ i ] ,
159
175
} ;
160
176
}
161
177
}
162
178
}
163
179
164
180
const limit = this . opts . __ptr == 0 ? Infinity : - this . opts . __ptr ;
165
181
182
+ const newShards = this . state . shards . slice ( 0 , limit ) . concat ( shards ) ;
183
+ this . sortMutate ( newShards ) ;
184
+
166
185
return this . buildNew ( {
167
186
__ptr : 0 ,
168
- __newShards : this . state . shards . slice ( 0 , limit ) . concat ( shards ) ,
187
+ __newShards : newShards ,
169
188
} ) ;
170
189
}
171
190
@@ -261,7 +280,8 @@ export default class Crystalizer<
261
280
) ;
262
281
}
263
282
264
- const ts = param ( this . opts . __getTime ( ) ) ;
283
+ const pastDistance = param ;
284
+ const ts = this . opts . __getTime ( ) - pastDistance ;
265
285
266
286
const index = shards . findIndex (
267
287
( shard ) => ( shard [ this . opts . tsKey ] as number ) >= ts ,
@@ -288,16 +308,10 @@ export default class Crystalizer<
288
308
}
289
309
290
310
private sortMutate ( shards : Shard [ ] ) : void {
291
- if ( ! this . opts . sort || ! this . opts . sort . length ) {
311
+ if ( ! this . sorts . length ) {
292
312
return ;
293
313
}
294
314
295
- const isMultisort = this . opts . sort [ 0 ] instanceof Array ;
296
-
297
- const sorts = (
298
- isMultisort ? this . opts . sort : [ this . opts . sort ]
299
- ) as SingleSort < Shard > [ ] ;
300
-
301
315
const getSortVal = (
302
316
shard : Shard ,
303
317
key : string | ( ( s : Shard ) => unknown ) ,
@@ -310,8 +324,8 @@ export default class Crystalizer<
310
324
} ;
311
325
312
326
shards . sort ( ( a : Shard , b : Shard ) => {
313
- for ( let i = 0 ; i < sorts . length ; i ++ ) {
314
- const [ dir , key ] = sorts [ i ] ;
327
+ for ( let i = 0 ; i < this . sorts . length ; i ++ ) {
328
+ const [ dir , key ] = this . sorts [ i ] ;
315
329
const l = dir == 'asc' ? a : b ;
316
330
const r = dir == 'asc' ? b : a ;
317
331
@@ -338,10 +352,6 @@ export default class Crystalizer<
338
352
if ( ! this . takeCache [ keepCount ] ) {
339
353
let { crystal, shards } = deepCopy ( this . state ) ;
340
354
341
- if ( this . opts . sort ) {
342
- this . sortMutate ( shards ) ;
343
- }
344
-
345
355
const ptr = this . getPtrIndex ( shards ) ;
346
356
347
357
if ( ptr != 0 ) {
0 commit comments