Skip to content

Commit 4fbf100

Browse files
committed
fix: tests and fixes
1 parent b1b8726 commit 4fbf100

File tree

2 files changed

+262
-460
lines changed

2 files changed

+262
-460
lines changed

src/index.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type Keep<Shard> =
1212
| ['none']
1313
| ['count', number]
1414
| ['first', ShardSeekFn<Shard>]
15-
| ['since', (now: number) => number]
15+
| ['since', number]
1616
| ['min', Keep<Shard>[]]
1717
| ['max', Keep<Shard>[]];
1818

@@ -58,6 +58,8 @@ export default class Crystalizer<
5858
[count: number]: { crystal: Crystal; shards: Shard[] };
5959
} = {};
6060

61+
private sorts: SingleSort<Shard>[];
62+
6163
constructor(_opts: UserOpts<Crystal, Shard> | Opts<Crystal, Shard>) {
6264
let opts: Opts<Crystal, Shard> = {
6365
keep: ['all'],
@@ -73,6 +75,20 @@ export default class Crystalizer<
7375
crystal: deepCopy(opts.initial),
7476
};
7577

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+
7692
opts.__ptr = Math.max(0, opts.__ptr);
7793

7894
this.opts = opts;
@@ -141,7 +157,7 @@ export default class Crystalizer<
141157
}
142158

143159
focus(seek: ShardSeekFn<Shard>) {
144-
return this.buildNew({ __focus: seek });
160+
return this.buildNew({ __focus: seek, __ptr: 0 });
145161
}
146162

147163
with(shards: Shard | Shard[]) {
@@ -154,18 +170,21 @@ export default class Crystalizer<
154170
}
155171
if (this.opts.tsKey) {
156172
shards[i] = {
157-
...shards[i],
158173
[this.opts.tsKey]: this.opts.__getTime(),
174+
...shards[i],
159175
};
160176
}
161177
}
162178
}
163179

164180
const limit = this.opts.__ptr == 0 ? Infinity : -this.opts.__ptr;
165181

182+
const newShards = this.state.shards.slice(0, limit).concat(shards);
183+
this.sortMutate(newShards);
184+
166185
return this.buildNew({
167186
__ptr: 0,
168-
__newShards: this.state.shards.slice(0, limit).concat(shards),
187+
__newShards: newShards,
169188
});
170189
}
171190

@@ -261,7 +280,8 @@ export default class Crystalizer<
261280
);
262281
}
263282

264-
const ts = param(this.opts.__getTime());
283+
const pastDistance = param;
284+
const ts = this.opts.__getTime() - pastDistance;
265285

266286
const index = shards.findIndex(
267287
(shard) => (shard[this.opts.tsKey] as number) >= ts,
@@ -288,16 +308,10 @@ export default class Crystalizer<
288308
}
289309

290310
private sortMutate(shards: Shard[]): void {
291-
if (!this.opts.sort || !this.opts.sort.length) {
311+
if (!this.sorts.length) {
292312
return;
293313
}
294314

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-
301315
const getSortVal = (
302316
shard: Shard,
303317
key: string | ((s: Shard) => unknown),
@@ -310,8 +324,8 @@ export default class Crystalizer<
310324
};
311325

312326
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];
315329
const l = dir == 'asc' ? a : b;
316330
const r = dir == 'asc' ? b : a;
317331

@@ -338,10 +352,6 @@ export default class Crystalizer<
338352
if (!this.takeCache[keepCount]) {
339353
let { crystal, shards } = deepCopy(this.state);
340354

341-
if (this.opts.sort) {
342-
this.sortMutate(shards);
343-
}
344-
345355
const ptr = this.getPtrIndex(shards);
346356

347357
if (ptr != 0) {

0 commit comments

Comments
 (0)