Netflix / falcor-router

A Falcor JavaScript DataSource which creates a Virtual JSON Graph document on your app server.
http://netflix.github.io/falcor
Apache License 2.0
104 stars 46 forks source link

in route handler, buffer synchronous observable emissions #222

Open jameslaneconkling opened 6 years ago

jameslaneconkling commented 6 years ago

The following router implementation using observables produces inefficient, disaggregated service calls

const router = new Router([
    {
      route: 'people[{integers:indices}]',
      get: ({ indices }) => {
        return from(indices).pipe(
          map((index) => ({
            path: ['people', index],
            value: { $type: 'ref', value: ['peopleById', `_${index}`] }, // fake index -> id mapping
          }))
        );
      },
    },
    {
      route: 'peopleById[{keys:ids}].name',
      get: ({ ids }) => {
        console.log('ids', ids);
        return from(ids).pipe(
          map((id) => ({
            path: ['peopleById', id, 'name'],
            value: `Person #${id}`,
          }))
        );
      },
    }
]).subscribe();

// > ids [ '_1' ]
// > ids [ '_2' ]
// > ids [ '_3' ]
// > ids [ '_4' ]
// > ids [ '_5' ]

Adding a bufferTime(0) operator correctly keeps synchronous emissions aggregated

const router = new Router([
    {
      route: 'people[{integers:indices}]',
      get: ({ indices }) => {
        return from(indices).pipe(
          map((index) => ({
            path: ['people', index],
            value: { $type: 'ref', value: ['peopleById', `_${index}`] },
          })),
          bufferTime(0),
        );
      },
    },
    ...
]).subscribe();

Should buffering of synchronous observable responses be the default?