Skip to content

feat: Implement Collect pattern #15

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

Closed
wants to merge 2 commits into from
Closed

feat: Implement Collect pattern #15

wants to merge 2 commits into from

Conversation

EvanBoyle
Copy link
Member

Adds a <Collect> component to enable a map/reduce or fork/join pattern. Usage:

export const CollectWorkflow = createWorkflow<
  CollectWorkflowInputs,
  CollectWorkflowOutputs
>((props, render) => {
  const [NumberCollector, doubledNums] = createCollector<number>();
  const [StringCollector, doubledStrings] = createCollector<string>();

  return (
    <>
      <NumberCollector>
        {() => props.numbers.map(n => <DoubleNumber input={n} />)}
      </NumberCollector>
      <StringCollector>
        {() => props.strings.map(s => <DoubleString input={s} />)}
      </StringCollector>
      <SumNumbers numbers={doubledNums}>
        {sum => (
          <ConcatStrings strings={doubledStrings}>
            {str => render({ num: sum, str })}
          </ConcatStrings>
        )}
      </SumNumbers>
    </>
  );
});

A few notable things:

  1. Due to the fact that typescript handles type inference with JSX, the types need to be known at the time to component is instantiated, and you can't use information provided via props or children at the time it is used in markup. This requires using the createCollector HOC.
  2. I had to hack into workflow builder because we wrap up the children in a function by default. I exposed __rawChildren so that Collector can access the raw children directly. Things just work.

Personally, I don't think that the collector pattern with a HOC is too bad here. The nice thing is that it returns a workflow output promise that will contain it's results - so it basically just replaces having to declare an explicit workflow output which you would otherwise have to do.

@EvanBoyle EvanBoyle requested a review from jmoseley December 17, 2024 23:22
@EvanBoyle EvanBoyle changed the title Implement Collect pattern feat: Implement Collect pattern Dec 17, 2024
@jmoseley
Copy link
Contributor

After discussion, we concluded that this pattern is not quite right.

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.

2 participants