Skip to content

Commit 56c0b15

Browse files
committed
feat: adds old value as a second parameter to listeners
1 parent 27d301d commit 56c0b15

File tree

5 files changed

+23
-12
lines changed

5 files changed

+23
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-rv",
3-
"version": "0.2.5",
3+
"version": "0.3.0",
44
"description": "react-rv is a lightweight and efficient state management library for React that allows you to create reactive variables and subscribe to them with minimal overhead.",
55
"files": [
66
"dist"

src/rv.spec-d.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ describe('rv function', () => {
2727

2828
it('infers variable type in a subscription parameter', () => {
2929
const val = rv(2, {
30-
on: v => {
30+
on: (v, o) => {
3131
expectTypeOf(v).toBeNumber()
32+
expectTypeOf(o).toBeNumber()
3233
},
3334
})
3435

35-
val.on(v => {
36+
val.on((v, o) => {
3637
expectTypeOf(v).toBeNumber()
38+
expectTypeOf(o).toBeNumber()
3739
})
3840
})
3941

@@ -63,13 +65,15 @@ describe('rv function', () => {
6365

6466
it('infers variable type in a subscription parameter', () => {
6567
const val = rv.fn(() => 3, {
66-
on: v => {
68+
on: (v, o) => {
6769
expectTypeOf(v).toBeNumber()
70+
expectTypeOf(o).toBeNumber()
6871
},
6972
})
7073

71-
val.on(v => {
74+
val.on((v, o) => {
7275
expectTypeOf(v).toBeNumber()
76+
expectTypeOf(o).toBeNumber()
7377
})
7478
})
7579

src/rv.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('rv function', () => {
4949
val.on(listener)
5050

5151
val(3)
52-
expect(listener).toHaveBeenCalledExactlyOnceWith(3)
52+
expect(listener).toHaveBeenCalledExactlyOnceWith(3, 2)
5353
})
5454

5555
it('allows to pass the default listener', () => {
@@ -60,7 +60,7 @@ describe('rv function', () => {
6060
val.on(listener)
6161

6262
val(3)
63-
expect(listener).toHaveBeenCalledExactlyOnceWith(3)
63+
expect(listener).toHaveBeenCalledExactlyOnceWith(3, 2)
6464
})
6565

6666
it('does not call listeners if value does not change', () => {

src/rv.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const defaultEq = <T>(oldValue: T, newValue: T): boolean => oldValue === newValu
66
* Creates a reactive variable (RV), allowing value retrieval, updates, and subscriptions.
77
*
88
* @template T The type of the stored value.
9+
*
910
* @param val The initial value of the reactive variable.
1011
* @param options Optional configuration for the reactive variable.
1112
*
@@ -22,7 +23,7 @@ const defaultEq = <T>(oldValue: T, newValue: T): boolean => oldValue === newValu
2223
* eq: (oldValue, newValue) => newValue > oldValue && newValue >= 0,
2324
*
2425
* // define callback that's going to be run on every change
25-
* on: val => {}
26+
* on: (newValue, oldValue) => {}
2627
* })
2728
*
2829
* // alternatively, there's a handy function initializer
@@ -43,7 +44,7 @@ const defaultEq = <T>(oldValue: T, newValue: T): boolean => oldValue === newValu
4344
* })
4445
*
4546
* // you can also subscribe to value without using any hooks
46-
* const unsubscribe = positiveVar.on(newValue => console.log(newValue))
47+
* const unsubscribe = positiveVar.on((newValue, oldValue) => console.log(newValue, oldValue))
4748
*
4849
* positiveVar(4) // logs: 4
4950
*
@@ -72,9 +73,10 @@ export function rv<T>(val: T, options?: RvInitOptions<T>): Rv<T> {
7273

7374
if (eqfn(val, newValue)) return val
7475

76+
const oldValue = val
7577
val = newValue
7678

77-
listeners.forEach(cb => cb(newValue))
79+
listeners.forEach(cb => cb(newValue, oldValue))
7880

7981
return val
8082
}

src/types.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
* A function that listens to reactive variable updates.
33
*
44
* @template T The type of the value being listened to.
5+
*
56
* @param val The updated value of the reactive variable.
7+
* @param old The old value of the reactive variable.
68
*/
7-
export type Listener<T> = (val: T) => void
9+
export type Listener<T> = (val: T, old: T) => void
810

911
/**
1012
* A function that cleans up event listener.
@@ -15,6 +17,7 @@ export type CleanupFn = () => void
1517
* A function to compare old and new values for equality.
1618
*
1719
* @template T The type of values being compared.
20+
*
1821
* @param oldValue The previous value.
1922
* @param newValue The new value to compare against.
2023
*
@@ -81,7 +84,7 @@ export type RvInitOptions<T> = {
8184
*
8285
* @param val The new value of the reactive variable.
8386
*/
84-
on?: (val: T) => void
87+
on?: Listener<T>
8588
}
8689

8790
/**
@@ -92,6 +95,7 @@ export interface RvInit {
9295
* Creates a reactive variable (RV), allowing value retrieval, updates, and subscriptions.
9396
*
9497
* @template T The type of the stored value.
98+
*
9599
* @param val The initial value of the reactive variable.
96100
* @param options Optional configuration for the reactive variable.
97101
*
@@ -103,6 +107,7 @@ export interface RvInit {
103107
* The function is immediately executed to determine the initial value.
104108
*
105109
* @template T The type of the stored value.
110+
*
106111
* @param init A function that returns the initial value.
107112
* @param options Optional configuration for equality comparison and event listeners.
108113
*

0 commit comments

Comments
 (0)