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

extract() #1

Closed
couzic opened this issue Sep 20, 2017 · 6 comments
Closed

extract() #1

couzic opened this issue Sep 20, 2017 · 6 comments

Comments

@couzic
Copy link
Owner

couzic commented Sep 20, 2017

Example:

const extract$: Observable<{a: A, c: C}> = store.extract({
   a: store.lens.focusOn('a'),
   c: store.lens.focusOn('b').focusOn('c')
})

extract$ only emits for new values of a or c

@couzic
Copy link
Owner Author

couzic commented Sep 20, 2017

Alternatively:

const extract$: Observable<{a: A, c: C}> = store.extract({
   a: state => state.a,
   c: state => state.b.c
})

@couzic
Copy link
Owner Author

couzic commented Sep 20, 2017

The second version is less type-safe, but more concise. Both alternatives are viable.

@couzic
Copy link
Owner Author

couzic commented Sep 20, 2017

The second version (state selectors) would also support:

const extract$: Observable<{a: A, c: C}> = store.extract({
   a: state => store.lens.focusOn('a').read(state),
   c: state => store.lens.focusOn('b').focusOn('c').read(state)
})

Which is quite verbose. However, if the read() function was properly bound to the lens, we could do:

const extract$: Observable<{a: A, c: C}> = store.extract({
   a: store.lens.focusOn('a').read,
   c: store.lens.focusOn('b').focusOn('c').read
})

Which is way more concise. Anyway, it appears that binding functions to the lens would be most useful in many cases.

@couzic
Copy link
Owner Author

couzic commented Sep 20, 2017

Binding reading functions to lenses is now an issue in immutable-lens:
couzic/immutable-lens#2

@couzic
Copy link
Owner Author

couzic commented Sep 21, 2017

Due to TypeScript compilator type inference limitations, the version with selectors would actually look like:

const extract$: Observable<{a: A, c: C}> = store.extract({
   a: (state: State) => state.a,
   c: (state: State) => state.b.c
})

Which is not ideal either. Maybe another version using lenses should be considered:

const extract$: Observable<{a: A, c: C}> = store.extract({
   a: store.lens.focusOn('a'),
   c: store.lens.focusOn('b').focusOn('c')
})

@couzic
Copy link
Owner Author

couzic commented Sep 21, 2017

Finally, extract() accepts both selectors and lenses

@couzic couzic closed this as completed Sep 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant