-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: subscriptions (server-streams) + observable (#22)
* subscriptions v0.8.0 * fix lint errors * add datatypes export to package json * make observable a fixture instead of a top-level thing * fix package.json, return on stream done rather than explicitly checking value
- Loading branch information
Showing
15 changed files
with
463 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Observable } from './observable'; | ||
import { describe, expect, test, vitest } from 'vitest'; | ||
|
||
describe('Observable', () => { | ||
test('should set initial value correctly', () => { | ||
const initialValue = 10; | ||
const observable = new Observable(initialValue); | ||
expect(observable.value).toBe(initialValue); | ||
}); | ||
|
||
test('should update value correctly', () => { | ||
const observable = new Observable(10); | ||
const newValue = 20; | ||
observable.set(() => newValue); | ||
expect(observable.value).toBe(newValue); | ||
}); | ||
|
||
test('should notify listeners when value changes', () => { | ||
const observable = new Observable(10); | ||
const listener = vitest.fn(); | ||
observable.observe(listener); | ||
expect(listener).toHaveBeenCalledTimes(1); | ||
|
||
const newValue = 20; | ||
observable.set(() => newValue); | ||
|
||
expect(listener).toHaveBeenCalledTimes(2); | ||
expect(listener).toHaveBeenCalledWith(newValue); | ||
}); | ||
|
||
test('should unsubscribe from notifications', () => { | ||
const observable = new Observable(10); | ||
const listener = vitest.fn(); | ||
const unsubscribe = observable.observe(listener); | ||
expect(listener).toHaveBeenCalledTimes(1); | ||
|
||
const newValue = 20; | ||
observable.set(() => newValue); | ||
|
||
expect(listener).toHaveBeenCalledTimes(2); | ||
expect(listener).toHaveBeenCalledWith(newValue); | ||
|
||
unsubscribe(); | ||
|
||
const anotherValue = 30; | ||
observable.set(() => anotherValue); | ||
|
||
expect(listener).toHaveBeenCalledTimes(2); // should not be called again after unsubscribing | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Represents an observable value that can be subscribed to for changes. | ||
* This should only be used in tests | ||
* @template T - The type of the value being observed. | ||
*/ | ||
export class Observable<T> { | ||
value: T; | ||
private listeners: Set<(val: T) => void>; | ||
|
||
constructor(initialValue: T) { | ||
this.value = initialValue; | ||
this.listeners = new Set(); | ||
} | ||
|
||
/** | ||
* Gets the current value of the observable. | ||
*/ | ||
get() { | ||
return this.value; | ||
} | ||
|
||
/** | ||
* Sets the current value of the observable. All listeners will get an update with this value. | ||
* @param newValue - The new value to set. | ||
*/ | ||
set(tx: (preValue: T) => T) { | ||
const newValue = tx(this.value); | ||
this.value = newValue; | ||
this.listeners.forEach((listener) => listener(newValue)); | ||
} | ||
|
||
/** | ||
* Subscribes to changes in the observable value. | ||
* @param listener - A callback function that will be called when the value changes. | ||
* @returns A function that can be called to unsubscribe from further notifications. | ||
*/ | ||
observe(listener: (val: T) => void) { | ||
this.listeners.add(listener); | ||
listener(this.get()); | ||
return () => this.listeners.delete(listener); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.