Skip to content

Commit

Permalink
Fix $.update and $.update_pre for bigints
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocean-OS committed Dec 1, 2024
1 parent 4a85c41 commit 24113b4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
34 changes: 27 additions & 7 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -972,23 +972,43 @@ function get_parent_context(component_context) {
}

/**
* @param {Value<number>} signal
* @template {number|bigint} T
* @param {Value<T>} signal
* @param {1 | -1} [d]
* @returns {number}
* @returns {T}
*/
export function update(signal, d = 1) {
var value = +get(signal);
set(signal, value + d);
var value = get(signal);

if(typeof value === "bigint") {
//@ts-ignore
set(signal, value + BigInt(d));
}else {
//@ts-ignore
set(signal, +value + d);
}

return value;
}

/**
* @param {Value<number>} signal
* @template {number|bigint} T
* @param {Value<T>} signal
* @param {1 | -1} [d]
* @returns {number}
* @returns {T}
*/
export function update_pre(signal, d = 1) {
return set(signal, +get(signal) + d);
var value = get(signal);

if(typeof value === "bigint") {
//@ts-ignore
d = BigInt(d);
}else {
//@ts-ignore
value = +value;
}
//@ts-ignore
return set(signal, value + d);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions packages/svelte/tests/signals/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,4 +739,20 @@ describe('signals', () => {
assert.deepEqual(a.reactions, null);
};
});

test('bigint states update correctly', () => {
return () => {
const count = state(0n);

assert.doesNotThrow(() => $.update(count));
assert.equal($.get(count), 1n);
assert.doesNotThrow(() => $.update(count, -1));
assert.equal($.get(count), 0n);

assert.doesNotThrow(() => $.update_pre(count));
assert.equal($.get(count), 1n);
assert.doesNotThrow(() => $.update_pre(count, -1));
assert.equal($.get(count), 0n);
}
})
});

0 comments on commit 24113b4

Please sign in to comment.