Skip to content

Enhance API compatibility: toJSON, toString#28

Open
dy wants to merge 3 commits intoWebReflection:mainfrom
dy:patch-1
Open

Enhance API compatibility: toJSON, toString#28
dy wants to merge 3 commits intoWebReflection:mainfrom
dy:patch-1

Conversation

@dy
Copy link
Copy Markdown

@dy dy commented Nov 17, 2025

Hi @WebReflection!

This adds toString and toJSON methods to make it more compatible with preact-signals API.

Lmk if you'd prefer casting result to string instead of {T}.

Added toString and toJSON methods to Signal class.
@WebReflection
Copy link
Copy Markdown
Owner

I've always been a bit skeptical about toJSON because an effect that re-produces JSON feels like a bit of a stretch ... do you have any obvious use case I am overlooking here?

@dy
Copy link
Copy Markdown
Author

dy commented Nov 18, 2025

Preact-signals have it added in preactjs/signals#320 due to 2 reports.
Not having toJSON makes it harder serializing structures that allow signals, eg. deep-signals.
I don't remember if that is a blocker, but it would require unnecessary glue code.
All other signals implementations support that.

@dy
Copy link
Copy Markdown
Author

dy commented Nov 18, 2025

An another compatibility issue here.
It seems effect ignores teardown function.

It would need wrapping effect like:

import {effect as _effect} from 'alien-signals'

export const effect = fn => {
  let teardown, dispose = _effect(() => (typeof teardown === 'function' && teardown(), teardown = fn()));
  return () => (typeof teardown === 'function' && teardown(), dispose());
}

@WebReflection
Copy link
Copy Markdown
Owner

but in practice you send JSON to servers and automatically sending new requests feels a bit weird to me ... anyway, mind amending the MR with your latest finding too? thanks!

@dy
Copy link
Copy Markdown
Author

dy commented Nov 18, 2025

Sure, added with test.

@dy
Copy link
Copy Markdown
Author

dy commented Feb 1, 2026

@WebReflection please merge if makes sense

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.

2 participants