Skip to content

Add post function to templateHandler - #15

Open
jannikluhn wants to merge 1 commit into
mainfrom
feat/template-post-handler
Open

jannikluhn wants to merge 1 commit into
mainfrom
feat/template-post-handler

Conversation

@jannikluhn

Copy link
Copy Markdown
Contributor

Closes #7

…into one

`SignalHandler` has had `post` all along, and the template Handler's own doc
comment sold the workaround: `{ ...templateHandler(options), post }` is a
Handler. It is, and it is worse than it reads.

The spread drops the payload type. `TPayload` reaches `session` and `data`, but
a `post` written outside the call is typed by the map it lands in, which is
`Record<string, SignalHandler>` and so `Signal<unknown>`. The Operator has to
re-annotate the signal by hand, naming a type the other options never made them
name, and an annotation that is wrong is an assertion about a payload nothing
checked. The spread also works only because `handle` never touches `this`,
which nothing enforces and nothing tests, so an implementation detail had
become part of the contract.

So `post` is an option, carried to the Worker as written. Nothing wraps it and
nothing calls it from here: the same argument that keeps `session` and `data`
outside the try/catch keeps this outside it too, because putting our sentence
about a template on somebody else's bug is what that argument is against. The
Worker already records it as `the post phase failed: ...`. The spread still
works and is now the long way round.

It is spread into the returned object rather than assigned, because
`exactOptionalPropertyTypes` refuses an explicit `undefined` against `post?`,
and because a Handler given no post phase must not carry the property at all:
the Worker asks `handler.post !== undefined` before running one, so the two
spellings are not the same Handler. That absence is the assertion with teeth
and it is the first of the new tests.

The rest are the two outcomes a template Handler can reach, through a real
worker: `failed: false` after a Run that ran, and `failed: true` for a template
that did not render and so produced no Run at all, which is the case a post
phase is written for, a Signal never being retried. And a throwing post phase,
whose words reach the Signal log as its own, saying nothing about a template
that rendered perfectly well.

`scripts/check-package.ts` carries the option in the annotated `main.ts` and
reads `signal.payload.userId` in it, so the narrowing is proved from the
installed package and not only in this tree.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

Add post function to templateHandler

1 participant