diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2025-02-25 00:37:33 -0500 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2025-02-26 01:54:44 -0500 |
| commit | f788ea84e25a4f7216ca0604aeb216346403b6ef (patch) | |
| tree | db62f5d1e15d871f8a73ce20b40cd53053d12f85 /ui/lib/iterator.test.js | |
| parent | f1b124a0423cdaf4d8a6bd62a2059722e9afdf2b (diff) | |
Track state on a per-session basis, rather than via globals.
Sorry about the thousand-line omnibus change; this is functionally a rewrite of the client's state tracking, flavoured to resemble the existing code as far as is possible, rather than something that can be parted out and committed in pieces.
Highlights:
* No more `store.writeable()`s. All state is now tracked using state runs or derivatives. State is still largely structured the way it was, but several bits of nested state have been rewritten to ensure that their properties are reactive just as much as their containers are.
* State is no longer global. `(app)/+layout` manages a stateful session, created via its load hook and started/stopped via component mount and destroy events. The session also tracks an event source for the current state, and feeds events into the state, broadly along the same lines as the previous stores-based approach.
Together these two changes fix up several rough spots integrating state with Svelte, and allow for the possibility of multiple states. This is a major step towards restartable states, and thus towards better connection management, which will require the ability to "start over" once a connection is restored.
Diffstat (limited to 'ui/lib/iterator.test.js')
| -rw-r--r-- | ui/lib/iterator.test.js | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/ui/lib/iterator.test.js b/ui/lib/iterator.test.js new file mode 100644 index 0000000..4c55358 --- /dev/null +++ b/ui/lib/iterator.test.js @@ -0,0 +1,93 @@ +import * as iter from './iterator.js'; +import { beforeEach, expect, test, describe, it, vi } from 'vitest'; + +describe('map', async () => { + it('applies the mapping function to each item', async () => { + const seq = Iterator.from([1, 2, 3, 5, 8]); + const mapped = iter.map(seq, (x) => 2 * x); + expect(Array.from(mapped)).toStrictEqual([2, 4, 6, 10, 16]); + }); +}); + +describe('reduce', async () => { + it('accumulates results and returns them', async () => { + const seq = Iterator.from([1, 2, 3, 4]); + const reduced = iter.reduce(seq, (sum, x) => sum + x, 0); + + // Good ol' triangle numbers. + expect(reduced).toStrictEqual(10); + }); +}); + +describe('chunkBy', async () => { + describe('with trivial operand functions', async () => { + it('yields nothing for an empty input', async () => { + const chunks = iter.chunkBy( + [], + (val) => val, + (last, next) => last === next + ); + + expect(Array.from(chunks)).toStrictEqual([]); + }); + + it('yields one chunk input for a singleton input', async () => { + const chunks = iter.chunkBy( + [37], + (val) => val, + (last, next) => last === next + ); + + expect(Array.from(chunks)).toStrictEqual([{ key: 37, chunk: [37] }]); + }); + + it('yields chunks of successive inputs', async () => { + const chunks = iter.chunkBy( + [37, 37, 28, 37], + (val) => val, + (last, next) => last === next + ); + + expect(Array.from(chunks)).toStrictEqual([ + { key: 37, chunk: [37, 37] }, + { key: 28, chunk: [28] }, + { + key: 37, + chunk: [37] + } + ]); + }); + }); + + describe('with a complex key function', async () => { + it('returns the key with each chunk', async () => { + const chunks = iter.chunkBy( + [37, 37, 28, 37], + (val) => val >> 1, + (last, next) => last === next + ); + + expect(Array.from(chunks)).toStrictEqual([ + { key: 18, chunk: [37, 37] }, + { key: 14, chunk: [28] }, + { key: 18, chunk: [37] } + ]); + }); + }); + + describe('with a complex coalesce function', async () => { + it('continues the chunk when specified', async () => { + const chunks = iter.chunkBy( + [36, 37, 28, 29, 30, 38], + (val) => val, + (last, next) => last + 1 === next + ); + + expect(Array.from(chunks)).toStrictEqual([ + { key: 37, chunk: [36, 37] }, + { key: 30, chunk: [28, 29, 30] }, + { key: 38, chunk: [38] } + ]); + }); + }); +}); |
