-
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.
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import {observable} from '../observable'; | ||
|
||
describe('observable()', () => { | ||
it('creates and object', () => { | ||
const obs = observable(123); | ||
expect(typeof obs).toBe('object'); | ||
}); | ||
it('if no initial value provided state equals undefined', () => { | ||
const obs = observable(); | ||
expect(obs.get()).toBe(void 0); | ||
}); | ||
it('sets state to the default specified value, .get() returns it', () => { | ||
const state = {}; | ||
const obs = observable(state); | ||
expect(obs.get()).toBe(state); | ||
}); | ||
it('returns mutated state', () => { | ||
const obs = observable(); | ||
const state = {}; | ||
obs.set(state); | ||
expect(obs.get()).toBe(state); | ||
}); | ||
it('.sub() returns an unsub function', () => { | ||
const obs = observable(); | ||
const unsub = obs.sub(() => {}); | ||
expect(typeof unsub).toBe('function'); | ||
}); | ||
it('calls listener on on state change', () => { | ||
const listener = jest.fn(); | ||
const obs = observable(); | ||
obs.sub(listener); | ||
obs.set(123); | ||
expect(listener).toHaveBeenCalledTimes(1); | ||
}); | ||
it('calls correct number of times', () => { | ||
const listener = jest.fn(); | ||
const obs = observable(); | ||
obs.sub(listener); | ||
obs.set(1); | ||
obs.set(2); | ||
obs.set(3); | ||
expect(listener).toHaveBeenCalledTimes(3); | ||
}); | ||
it('calls listener with the correct data', () => { | ||
const listener = jest.fn(); | ||
const obs = observable(); | ||
obs.sub(listener); | ||
obs.set(1); | ||
obs.set(2); | ||
obs.set(3); | ||
expect(listener.mock.calls).toEqual([[1], [2], [3]]); | ||
}); | ||
it('stops calling listener when unsubscribed', () => { | ||
const listener = jest.fn(); | ||
const obs = observable(); | ||
const unsub = obs.sub(listener); | ||
obs.set(1); | ||
obs.set(2); | ||
unsub(); | ||
obs.set(3); | ||
expect(listener.mock.calls).toEqual([[1], [2]]); | ||
}); | ||
}); |
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,31 @@ | ||
export type TObservalbeUnsub = (() => void); | ||
export type TObservableSet<T> = (state: T) => void; | ||
export interface IObservable<T> { | ||
get: () => T; | ||
set: TObservableSet<T>; | ||
sub: (listener: TObservableSet<T>) => TObservalbeUnsub; | ||
} | ||
|
||
export const observable: <T>(state?: T) => IObservable<T> = state => { | ||
let listeners = []; | ||
let currentState = state; | ||
|
||
const get = () => currentState; | ||
|
||
const set = state => { | ||
currentState = state; | ||
for (const listener of listeners) listener(currentState); | ||
}; | ||
|
||
const sub = listener => { | ||
listeners.push(listener); | ||
|
||
return () => (listeners = listeners.filter(item => item !== listener)); | ||
}; | ||
|
||
return { | ||
get, | ||
set, | ||
sub, | ||
}; | ||
}; |