Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for select() wrapper, proxying a source but not stopping it. #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions src/select.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Source } from './source'
import { SourceLike } from './types'


export function select<T>(source: SourceLike<T>) {
return new Source<T>(emit => {
const listener = (val: T) => emit(val)
source.get(listener)

return () => source.remove(listener)
})
}
9 changes: 9 additions & 0 deletions src/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Subject } from './subject'


export class State<T> extends Subject<T> {
constructor(initial: T) {
super()
this.set(initial)
}
}
88 changes: 88 additions & 0 deletions src/test/select.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { State } from '../state'
import { observe } from '../observe'
import { select } from '../select'


describe(select, () => {
test('proxies a source.', () => {
const src = new State(0)
const selected = select(src)

const cb = jest.fn()

observe($ => cb($(selected)!))

src.set(1)
expect(cb).toHaveBeenCalledWith(1)

src.set(2)
expect(cb).toHaveBeenCalledWith(2)
})

test('without select, higher order sources are stopped after switch.', () => {
const flag = new State(false)
const a = new State('a')
const b = new State('b')

const cb = jest.fn()
const switched = $ => $(flag) ? a : b

observe($ => cb($($(switched)!)))

expect(a.stopped).toBe(false)
expect(b.stopped).toBe(false)

flag.set(true)

expect(a.stopped).toBe(false)
expect(b.stopped).toBe(true)

flag.set(false)

expect(a.stopped).toBe(true)
expect(b.stopped).toBe(true)
})

test('with select, higher order sources are not stopped after switch.', () => {
const flag = new State(false)
const a = new State('a')
const b = new State('b')

const cb = jest.fn()
const switched = $ => $(flag) ? select(a) : select(b)

observe($ => cb($($(switched)!)))

expect(a.stopped).toBe(false)
expect(b.stopped).toBe(false)

flag.set(true)

expect(a.stopped).toBe(false)
expect(b.stopped).toBe(false)

flag.set(false)

expect(a.stopped).toBe(false)
expect(b.stopped).toBe(false)
})

test('removes its subscription when stopped.', () => {
const get = jest.fn()
const remove = jest.fn()
const stop = jest.fn()
const src = select({
get,
remove,
stop,
stops: jest.fn(),
stopped: false
})

expect(get).toHaveBeenCalled()

src.stop()
expect(remove).toHaveBeenCalled()
expect(stop).not.toHaveBeenCalled()
})
})
Loading