Skip to content

keySelector composition#78

Closed
toomuchdesign wants to merge 7 commits intomasterfrom
key-selector-composition
Closed

keySelector composition#78
toomuchdesign wants to merge 7 commits intomasterfrom
key-selector-composition

Conversation

@toomuchdesign
Copy link
Copy Markdown
Owner

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

Feature

What is the current behaviour? (You can also link to an open issue here)

#73

What is the new behaviour?

Try to provide some additional optional runtime context information to keySelector function to allow keySelectors autogeneration based on provided inputSelectors and resultFunc.

Does this PR introduce a breaking change? (What changes might users need to make in their application due to this PR?)

Hopefully not.

Other information:

Please check if the PR fulfills these requirements:

  • Tests for the changes have been added
  • Docs have been added / updated

@coveralls
Copy link
Copy Markdown

coveralls commented Jun 9, 2019

Coverage Status

Coverage decreased (-1.09%) to 98.907% when pulling 484c1e7 on key-selector-composition into 3422d90 on master.

@toomuchdesign toomuchdesign changed the title combineKeySelectors utility keySelectorCreator option Jun 9, 2019
@toomuchdesign toomuchdesign force-pushed the key-selector-composition branch from 0677371 to 770daca Compare June 12, 2019 21:16
@toomuchdesign toomuchdesign force-pushed the key-selector-composition branch from 770daca to 5d78aaf Compare June 12, 2019 22:10
@@ -0,0 +1,22 @@
function combineKeySelectors({inputSelectors = [], keySelector}) {
const keySelectors = inputSelectors
.filter(entry => entry.hasOwnProperty('keySelector'))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it will more performant if you will filter inputSelectors by typeof keySelector === 'function' here.

@toomuchdesign toomuchdesign force-pushed the key-selector-composition branch from c77a340 to 484c1e7 Compare June 18, 2019 06:56
@toomuchdesign
Copy link
Copy Markdown
Owner Author

toomuchdesign commented Jun 18, 2019

Before considering this PR mergeable, I'd like to address the following issue. I write it down here to not keep everything floating in my head.

keySelector argument

implicit keySelector vs. explicit extraKeySelector

In an early iteration, keySelector was provided as argument to keySelectorCreator function (along with inputSelectors and resultFunc) and used to produce an optional extra chunk of cache key:

export const getMyData = createCachedSelector(
  // inputSelectors and resultFunc
)(
  keySelector,
  {
    keySelectorCreator: ({keySelector}) => {},
  }
);

I tried avoiding this implicitness by not considering keySelector when providing a keySelectorCreator and directly supplying an extraKeySelector prop

export const getMyData = createCachedSelector(
  // inputSelectors and resultFunc
)(
  undefined,
  {
    keySelectorCreator: new KeySelectorCombiner({
      extraKeySelector: () => 'extra'
    }),
  }
);

Even though I find this solution more straightforward, the need of passing some useless keySelector argument looks suboptimal in terms of design

TS Typings

On top of that if keySelector is considered required in TS typings, and make it optional implies considerable typing declarations changes.

A new polymorphic signature for createCachedSelector?

I considered providing a second optional signature for createCachedSelector consisting of a single option object so that the 2 following example produce the same result:

// Current default API
(
  keySelector,
  {...options}
)
// "option only" API
(
  {
   keySelector,
    ...options,
  }
)

The signature consisting of keySelector function + option object could be removed in favour or keySelector function (simple mode) OR option object (advanced).

Even if it solves the optional cacheKey API issue, I'm not sure at all that introducing polymorphic API's is a good idea on the long run.

Of course such a signature would also increase types complexity.

Release options.keySelectorCreator first (#82)

Since finding a solution to all this might take time, I'm considering making a first release with the only part of this PR which looks solid: options.keySelectorCreator.

options.keySelectorCreator would expect a function with the following signature.

type KeySelectorCreator<S, C, D> = ({
  inputSelectors: D,
  resultFunc: C,
  selector: S,
}) => KeySelector<S>

Users might temporarily provide their own keySelectorCreator implementation.

@toomuchdesign toomuchdesign mentioned this pull request Jun 18, 2019
2 tasks
@toomuchdesign toomuchdesign force-pushed the key-selector-composition branch from 3b5824c to 648688a Compare June 18, 2019 20:45
@toomuchdesign toomuchdesign changed the title keySelectorCreator option keySelector composition Jun 19, 2019
@toomuchdesign
Copy link
Copy Markdown
Owner Author

Most of the open points of this PR are now solved:

  • v3.3.0 enabled keySelector composition shipping keySelectorCreator option plus documentation.
  • v3.4.0 introduced single argument signature that alleviates the keySelector vs. explicit extraKeySelector issue

We'll open a new PR in case we decided to include a KeySelectorCombiner utility.

@toomuchdesign toomuchdesign deleted the key-selector-composition branch February 1, 2020 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants