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

Extending the API (e.g. to support Immutable data structures) #1

Open
ide opened this issue Jan 16, 2017 · 0 comments
Open

Extending the API (e.g. to support Immutable data structures) #1

ide opened this issue Jan 16, 2017 · 0 comments

Comments

@ide
Copy link
Owner

ide commented Jan 16, 2017

We could make mux support non-standard data structures like the ones in Immutable.js by letting people write extensions. I prefer not to directly add support for Immutable.js to mux itself since everyone will want to add support for their own data structure library, so a way to write extensions seems best.

My current thinking is an API like this:

import { List, OrderedMap } from 'immutable';
import mux, { extendMux } from '@exponent/mux';

const muxWithImmutableSupport = extendMux(mux, async (mux, promises) => {
  // The `mux` function in the formal parameters is the copy of mux that you call from
  // your application. So if you've extended mux many times:
  //
  //   const muxWithImmutableAndBinaryTreeSupport = extendMux(
  //     extendMux(mux, handleImmutableObjects),
  //     handleBinaryTrees,
  //   );
  //
  // then `handleImmutableObject` should receive `muxWithImmutableAndBinaryTreeSupport`
  // so that if the Immutable objects contain binary trees they are recursively handled.

  // Should this run before or after mux has handled the standard JS data types?
  if (OrderedMap.isOrderedMap(promises)) {
    let keys = [...promises.keys()];
    let values = await Promise.all([...promises.values()].map(mux));
    return new OrderedMap(zip(keys, values));
  }

  // Do the same for other data types that your application uses...
  if (List.isList(promises)) {
    ...
  }

  // Don't worry about non-Immutable data types
  return promises;
});
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