Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/kida/.size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ export default [
gzip: true,
path: 'dist/index.production.js',
import: '*',
limit: '5.5 kB',
limit: '5.45 kB',
modifyRolldownConfig
},
{
name: 'All publics (Brotli)',
path: 'dist/index.production.js',
import: '*',
limit: '5.1 kB',
limit: '5.05 kB',
modifyRolldownConfig
},
{
Expand Down
114 changes: 0 additions & 114 deletions packages/kida/src/utils.spec.ts

This file was deleted.

33 changes: 0 additions & 33 deletions packages/kida/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,39 +39,6 @@ export function boolean<T>($signal: Accessor<T>) {
return computed(() => Boolean($signal()))
}

/**
* Create a signal with the value of the source that changed last.
* A change is a change of value: writing a value equal to the current one
* changes nothing. The last source wins on the first read and whenever
* several sources changed since the previous read, which is what reading
* without a subscription or writing inside a `batch` amounts to.
* @param sources - Sources to merge.
* @returns A signal with the value of the most recently changed source.
*/
/* @__NO_SIDE_EFFECTS__ */
export function latest<T>(...sources: Accessor<T>[]) {
const prev: T[] = []
let value: T

return computed(() => {
// The first run has nothing to compare against, so every source counts as
// changed and the last one wins
const init = !prev.length

// Every source is read on every run: skipping one drops its subscription
for (let i = 0, len = sources.length; i < len; i++) {
const next = sources[i]()

if (init || next !== prev[i]) {
prev[i] = next
value = next
}
}

return value
})
}

/**
* Compose multiple destroy functions into a single destroy function.
* @param destroys - Destroy functions to compose.
Expand Down
100 changes: 100 additions & 0 deletions packages/store/src/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import {
expect
} from 'vitest'
import {
batch,
effect,
signal
} from 'kida'
import {
interval,
latest,
previous,
signalNodeAssign
} from './utils.js'
Expand Down Expand Up @@ -54,6 +56,104 @@ describe('store', () => {
})
})

describe('latest', () => {
it('should return the last source value on the first read', () => {
const $a = signal('a')
const $b = signal('b')
const $latest = latest($a, $b)

expect($latest()).toBe('b')
})

it('should return the last source value on the first read when it is undefined', () => {
const $a = signal<string | undefined>('a')
const $b = signal<string | undefined>(undefined)
const $latest = latest($a, $b)

expect($latest()).toBeUndefined()
})

it('should return the value of the source that changed last', () => {
const $a = signal('a')
const $b = signal('b')
const $latest = latest($a, $b)
const listener = vi.fn()
const off = effect(() => {
listener($latest())
})

$a('a1')
$b('b1')
$a('a2')

expect(listener.mock.calls).toEqual([['b'], ['a1'], ['b1'], ['a2']])

off()
})

it('should return the last changed source when several sources changed in a batch', () => {
const $a = signal('a')
const $b = signal('b')
const $latest = latest($a, $b)
const off = effect(() => {
$latest()
})

batch(() => {
$b('b1')
$a('a1')
})

expect($latest()).toBe('b1')

off()
})

it('should return the last changed source when several sources changed between reads', () => {
const $a = signal('a')
const $b = signal('b')
const $latest = latest($a, $b)

expect($latest()).toBe('b')

$b('b1')
$a('a1')

expect($latest()).toBe('b1')
})

it('should keep the value when a source is written with an equal value', () => {
const $a = signal('a')
const $b = signal('b')
const $latest = latest($a, $b)

expect($latest()).toBe('b')

$a('a')

expect($latest()).toBe('b')
})

it('should keep the value when nothing changed since the last read', () => {
const $a = signal('a')
const $b = signal('b')
const $latest = latest($a, $b)
const off = effect(() => {
$latest()
})

$a('a1')

expect($latest()).toBe('a1')

// Losing the last subscriber purges the deps and forces the next read
// to recompute with no source changed
off()

expect($latest()).toBe('a1')
})
})

describe('interval', () => {
beforeEach(() => {
vi.useFakeTimers()
Expand Down
33 changes: 33 additions & 0 deletions packages/store/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,39 @@ export function previous<T>($signal: Accessor<T>): Accessor<T | undefined> {
})
}

/**
* Create a signal with the value of the source that changed last.
* A change is a change of value: writing a value equal to the current one
* changes nothing. The last source wins on the first read and whenever
* several sources changed since the previous read, which is what reading
* without a subscription or writing inside a `batch` amounts to.
* @param sources - Sources to merge.
* @returns A signal with the value of the most recently changed source.
*/
/* @__NO_SIDE_EFFECTS__ */
export function latest<T>(...sources: Accessor<T>[]) {
const prev: T[] = []
let value: T

return computed(() => {
// The first run has nothing to compare against, so every source counts as
// changed and the last one wins
const init = !prev.length

// Every source is read on every run: skipping one drops its subscription
for (let i = 0, len = sources.length; i < len; i++) {
const next = sources[i]()

if (init || next !== prev[i]) {
prev[i] = next
value = next
}
}

return value
})
}

/**
* Create a signal that increments on an interval while mounted.
* @param ms - Interval duration in milliseconds.
Expand Down
Loading