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

gather: add transformKey function #33

Merged
merged 1 commit into from
Feb 28, 2017
Merged
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ function TodoList (sources) {
And how do we process fetched data?
---

It's a quite common use case when a collection is built from fetched data. Usually it comes in a form of items' state snapshot. `Collection.gather` takes a stream of those snapshots and turns into a stream of collections. It takes `Collection` and `sources` arguments, just as `Collection` does, plus `itemState$` and an optional `idAttribute` argument, which defaults to `'id'`.
It's a quite common use case when a collection is built from fetched data. Usually it comes in a form of items' state snapshot. `Collection.gather` takes a stream of those snapshots and turns into a stream of collections. It takes `Collection` and `sources` arguments, just as `Collection` does, plus the snapshots stream `itemState$`, an optional `idAttribute` argument, which defaults to `'id'`, and an optional `transformKey` function for converting source keys.

```js
const tasks$ = Collection.gather(Task, sources, fetchedTasks$, 'uid')
const tasks$ = Collection.gather(Task, sources, fetchedTasks$, 'uid', key => `${key}$`) // converts 'props' in snapshots to 'props$' in sources
```

It uses a set of rules:
Expand Down
10 changes: 5 additions & 5 deletions examples/taskrunner/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function taskView ([{status, text}, editing]) {
);
}

function Task ({DOM, props}) {
function Task ({DOM, props$}) {
const delete$ = DOM
.select('.delete')
.events('click');
Expand All @@ -47,11 +47,11 @@ function Task ({DOM, props}) {
const edit$ = editing$.map(editing => changeText$.filter(() => editing)).flatten();

return {
DOM: xs.combine(props, editing$).map(taskView),
complete$: props.map(({status}) => status === 'complete'),
DOM: xs.combine(props$, editing$).map(taskView),
complete$: props$.map(({status}) => status === 'complete'),
delete$,
edit$,
HTTP: props.map(({id}) => ({
HTTP: props$.map(({id}) => ({
url: `/tasks/${id}`
}))
};
Expand Down Expand Up @@ -131,7 +131,7 @@ export default function TaskRunner ({DOM, HTTP}) {
)
.startWith([]);

const tasks$ = Collection.gather(Task, {DOM}, tasksState$);
const tasks$ = Collection.gather(Task, {DOM}, tasksState$, 'id', key => `${key}$`);

const addTaskClick$ = DOM
.select('.add-task')
Expand Down
6 changes: 4 additions & 2 deletions src/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function makeCollection (externalSA = xsAdapter) {
};

// convert a stream of items' sources snapshots into a stream of collections
Collection.gather = function gather (component, sources, sourceItems$, idAttribute = 'id') {
Collection.gather = function gather (component, sources, sourceItems$, idAttribute = 'id', transformKey = null) {
function makeDestroyable (component) {
return (sources) => ({
...component(sources),
Expand Down Expand Up @@ -196,9 +196,11 @@ function makeCollection (externalSA = xsAdapter) {
.compose(dropRepeats(compareJSON))
.remember();

const sourceKey = transformKey ? transformKey(key) : key;

return {
...sources,
[key]: convert(stream$, xsAdapter, externalSA)
[sourceKey]: convert(stream$, xsAdapter, externalSA)
};
}, {
_destroy$
Expand Down
18 changes: 18 additions & 0 deletions test/gather-diversity-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,22 @@ describe('Collection.gather with different stream libs', () => {
}
});
});

it('uses transformKey function to transform source keys', (done) => {
const itemState$ = O.of([
{id: 0, props: {foo: 'bar'}}
]);

function MockedComponent (sources) {
assert.ok(sources.props$);
done();
}

const collection$ = Collection.gather(MockedComponent, {}, itemState$, 'id', key => `${key}$`);
collection$.subscribe({
next () {},
error (err) { done(err); },
complete () {}
});
});
});
18 changes: 18 additions & 0 deletions test/gather-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,22 @@ describe('Collection.gather', () => {
}
});
});

it('uses transformKey function to transform source keys', (done) => {
const itemState$ = xs.of([
{id: 0, props: {foo: 'bar'}}
]);

function MockedComponent (sources) {
assert.ok(sources.props$);
done();
}

const collection$ = Collection.gather(MockedComponent, {}, itemState$, 'id', key => `${key}$`);
collection$.addListener({
next () {},
error (err) { done(err); },
complete () {}
});
});
});