Repository navigation
feat: add support for merging external events into Prompt.custom - #2205
Conversation
🦋 Changeset detectedLatest commit: 7938dac The changes in this PR will be included in the next version bump. This PR includes changesets to release 27 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
6b3813e to
5b3e6bc
Compare
|
here are a couple concrete examples of where this would be used:
|
| readonly receive?: (value: A, state: State) => Effect.Effect<Action<State, Output>, never, Environment> | ||
| }, | ||
| events?: Queue.Dequeue<A, never> | ||
| ): Prompt<Output> => { |
There was a problem hiding this comment.
- I would prefer avoiding a separate
receivehandler and instead merge the event types into the process input, and force the user to handle that logic there. Otherwise, we have two separate handlers that are doing the same thing. (required) - The overload that takes the events
Queueprobably should have theQueuepassed before the handlers so that the type of the events gets fixed before you specify the handlers. (maybe) - One other thing to consider is using an options object to pass the external events
Queueto "future proof" addition of further custom options. (maybe)
There was a problem hiding this comment.
I made the changes:
- remove the
receivecallback - combine the new
ProcessInputtaggedEnum with theTerminal.UserInputfor theprocess - moved the
eventsQueue to before the handler - added the typeTests
I've left out the option object since it kind of conflicts with the moving of the events Queue since I would expect the option key to be an optional arg at the end of a function call but would make the A type defined afterward. Happy to reconsider this if you think it would be better to look like:
const prompt = Prompt.custom(
{ count: 0 },
{
render: (state) => Effect.succeed(`Count: ${state.count}`),
process: (input, state) =>
Effect.succeed(
Match.value(input).pipe(
Match.tag("Input", () => Action.Submit({ value: state.count })),
Match.tag("Event", (input) => Action.NextFrame({ state: { count: state.count + input.value } })),
Match.exhaustive
)
),
clear: () => Effect.succeed("")
},
{
events: Queue.asDequeue(eventQueue)
}
)db73ea0 to
fcebe93
Compare
| 0, | ||
| { | ||
| render: () => Effect.succeed(""), | ||
| process: (_input, state) => Effect.succeed({ _tag: "Submit" as const, value: state }), |
There was a problem hiding this comment.
We are not doing anything currently here to ensure the type of _input is an untagged user input.
There was a problem hiding this comment.
There is an explicit expect in the "without events, process receives Terminal.UserInput" test above, but I've gone and added a Match against the output so there is also a type check against the types in action
fcebe93 to
ce6c548
Compare
ce6c548 to
7938dac
Compare
Bundle Size Analysis
|
Type
Description
I've been wanting a way to link in external data into the
Prompt.customin a way that triggers a re-render without needing to press a key stroke. In the current implementation I need to fake a key stroke usingprocess.stdinas a hack-y work around or build me own render loop from scratch. Ideally what I wanted was an API where I could just pass in a stream of events and be able to trigger the action with the next state:I've been digging around in the Prompt.ts for a while now so I already knew the
Terminal.readInputwas a Queue and that it shouldn't be too hard to just merge two Queues together.What I settled on was a type overload where there is the current implementation without the event doing the same thing (non-breaking) and the new type that adds the new
eventsQueue.Dequeued argumentand extends the handlers with the. This way adding the queue can be a progressive enhancement on an already functioning user input only prompt.receivedcallbackSome things I needed to mull over:
Promptisn't responsible for cleaning up theQueue, this needs to be handled by the userQueue.Dequeue<A, never>so the user is responsible outside of the Prompt to handle these causes.Related
I wanted to make an issue first but needed to open the repo to poke around in the types and next thing I know I was already halfway through implementing it 🤦♂️
Open to other suggestions or requests to change the approach.
Edit: Have changed the api a bit to put the event before the handler as well as removed the new received callback in favour of overloading the process with a taggedEnum when event us present.