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

Named stores #2

Merged
merged 2 commits into from
Nov 2, 2015
Merged

Named stores #2

merged 2 commits into from
Nov 2, 2015

Conversation

10xjs
Copy link
Contributor

@10xjs 10xjs commented Nov 1, 2015

Specific store levels created by enhancers are difficult to access. By adding the ability to reference each level by name, composition using lift is much more accessible.

Use name('levelName')(enhancer) to add a name property to an enhanced store level. It works by enhancing the enhancer that lifts the store.

Use normalize(store) to flatten the store-parent hierarchy into a named map. The base store originally accessible at store.parent... .parent is accessible as default on the map.

Example Usage

const createNamedStores = compose(
  normalize,
  compose(
    name('ephemeral')(ephemeral),
    name('promise')(promise),
    name('devTools')(devTools)
  )(createStore)
);

const stores = createNamedStores(reducer, initialState);

results in:

{
  ephemeral: {
    ...
    parent: { ... },
    name: 'ephemeral'
  },
  promise: {
    ...
    parent: { ... },
    name: 'promise'
  },
  devTools: {
    ...
    parent: { ... },
    name: 'devTools'
  },
  default: {
    ...
  }
}

let current = store;
do {
stores[current.name || 'default'] = current;
} while ((current = current.parent));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want a shorter syntax, shave a line:

for (let current = store; current; current = current.parent) {
  stores[current.name || 'default'] = current;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure!

@izaakschroeder
Copy link
Owner

So shiny! 💎 Maybe incorporate the body of your PR into the commits for people looking at the log later. Also spelling on reulting. Great stuff!

@izaakschroeder izaakschroeder self-assigned this Nov 2, 2015
@10xjs 10xjs force-pushed the named-stores branch 2 times, most recently from 86ab386 to 553d854 Compare November 2, 2015 05:29
Neal Granger added 2 commits November 1, 2015 21:30
Specific store levels created by enhancers are difficult to access. By adding the ability to reference each level by name, composition using lift is much more accessible.

Use `name('levelName')(enhancer)` to add a name property to an enhanced store level. It works by enhancing the enhancer that lifts the store.

Use `normalize(store)` to flatten the store-parent hierarchy into a named map. The base store originally accessible at `store.parent... .parent` is accessible as `default` on the map.

**Example Usage**
```js
const createNamedStores = compose(
  normalize,
  compose(
    name('ephemeral')(ephemeral),
    name('promise')(promise),
    name('devTools')(devTools)
  )(createStore)
);

const stores = createNamedStores(reducer, initialState);
```
results in:
```js
{
  ephemeral: {
    ...
    parent: { ... },
    name: 'ephemeral'
  },
  promise: {
    ...
    parent: { ... },
    name: 'promise'
  },
  devTools: {
    ...
    parent: { ... },
    name: 'devTools'
  },
  default: {
    ...
  }
}
```
izaakschroeder added a commit that referenced this pull request Nov 2, 2015
Named stores allow for easier composition.
@izaakschroeder izaakschroeder merged commit 32fcf60 into izaakschroeder:master Nov 2, 2015
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

Successfully merging this pull request may close these issues.

2 participants