AriaFallah / mobx-store

A data store with declarative querying, observable state, and easy undo/redo.
MIT License
282 stars 9 forks source link

Don't import all of lodash #11

Closed AriaFallah closed 8 years ago

AriaFallah commented 8 years ago

Addressing #9

This breaks the current API.

Instead of

store('numbers').filter((v) => v > 1)

it is now

import { filter } from 'lodash/fp'
store('numbers', filter((v) => v > 1))

For chaining, instead of

store('users').chain().sortBy('id').take(3).map('name').value()

it is now

import { sortBy, take, map } from 'lodash/fp'
store('users', [sortBy('id'), take(3), map('name')])

A caveat is that assign is not really great in lodash/fp https://github.com/lodash/lodash/issues/2275 so mobx-store now exports mutate

import mobxstore, { assign } from 'mobx-store'
const store = mobxstore()

store('time', assign([1, 2, 3]))
console.log(store('time')) // [1, 2, 3]

instead of

import mobxstore from 'mobx-store'
import { assign } from 'lodash/fp'
const store = mobxstore()

store('time', assign([1, 2, 3]))
console.log(store('time')) // []
jdalton commented 8 years ago

You could use:

var a = require('lodash/fp/assign');
a([1,2,3], [4,5,6])
// => { '0': 4, '1': 5, '2': 6 }

var a = require('lodash/fp/assign').convert({ 'immutable': false });
a([1,2,3], [4,5,6])
// => [ 4, 5, 6 ]
AriaFallah commented 8 years ago

@jdalton Ah thanks for the advice! I realized though that trying to mutate things using lodash/fp is not a great idea, and have adopted something a bit more appropriate now.

Thanks anyways 😄

jdalton commented 8 years ago

and have adopted something a bit more appropriate now

What did you adopt?

AriaFallah commented 8 years ago

@jdalton

Back when I was using .chain() and not lodash/fp I had this weird system of having an observable array behind the scenes, and then exposing a normal array wrapped in chain. This was because if I tried to wrap the observable in chain it didn't work at all. Then when the exposed chained array was mutated I would propagate the changes to the underlying observable. It was pretty hacky, but it worked.

When I switched to passing lodash/fp methods to flow at first I was confused on how to mutate the exposed array with lodash/fp in order to be able to pass on the changes to the underlying observable. I then had the epiphany that since I didn't need chain anymore I also didn't need the old hacky system.

I got rid of the middleman array, and made it so the store actually returned itself. Then it was trivial to directly mutate the store directly using the array prototype methods you would normally use.

store('x', [filter({ condition: true }), take(5)]) // read from the store using lodash/fp methods
store('x').push({ condition: false }) // mutate the store like you would an array

Does that make sense?

jdalton commented 8 years ago

Ah ok!