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

Expose getSelectionKeys from new ./<internals | helpers | advanced> package entry-point. #275

Open
audunolsen opened this issue Aug 29, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@audunolsen
Copy link

audunolsen commented Aug 29, 2024

I am writing a custom React reducer hook that allows for firing callbacks when an event produces a matched state. It allows for passing a TS pattern. The gist of it to facilitate associating side effects with actions through a useEffect abstraction such that the reducer itself remains side effect free. Basic example;

useMatchedEvent(
    { progress: P.union('closed', 'opened') },
    function resetScroll(_, { progress }) {
      const max = dom.getMaxScroll(scrollerRef.current).inline;
      const left = progress === 'opened' ? 0 : max;

      scrollerRef.current?.scroll({ behavior: 'instant', left });
    },
  );

I'd like to add an internal implementation detail where the callback only fires on changes to the specific properties referenced in the pattern. Otherwise, as the state grows, a lot may fire the callback in this hook as any reducer result that returns a new object reference can pass through the pattern. This package's internal helper getSelectionKeys seem to be the exact solution I am looking for.

Exposing this through an additional entry point may prove useful for advanced usage, like the occasional abstraction.

Or is there other ways to achieve this with the functionality already exposed?

@audunolsen audunolsen added the enhancement New feature or request label Aug 29, 2024
@audunolsen
Copy link
Author

I decided to go for the slightly more explicit approach in my reducer abstraction. I added the option to form a pattern from a callback that has access to the previous state. This way I can guard against spammy reducer runs that produce new object references that aren't really relevant or meaningful.

useMatchedEvent(
    (prev) => ({
      activity: P.intersection(
        'swimming',
        P.when((val) => val !== prev.activity),
      ),
    }),
    (payload) => {
      console.log('Matched event', payload.type);
    },
  );

Anyway still wondering if some of the internal helpers could be exposed to allow for creating some powerful abstractions for those brave enough. I think getSelectionKeys — given that it does what I think it does — is really neat and could allow for a lot of cool functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant