milomg / reactively

MIT License
441 stars 23 forks source link

Well known Symbols in reactvely #14

Open kwangure opened 1 year ago

kwangure commented 1 year ago

This is a question. Did you explore well known symbols while ideating on Reactively? Symbol.toPrimitive is relevant to Reactively as a convinence so that you don't have to read with signal.value everywhere but the getter is called.

import { primitive } from "@reactively/core";

const counter = primitive(0);
const isEven = primitive(() => (counter & 1) == 0);
                                //  ^ no dot value
const prefix = primitive('isEven is');
const evenString = primitive(() => prefix + isEven) // isEven is true; 

I haven't looked much at HasReactive but it might have relevance there too. I was wondering whether you excluded it for performance or other reasons. I suppose there's an elegance to having one interface for all data types too.

For context, I was playing with Symbol.toPrimitive at https://github.com/kwangure/eventscript to see how reactive builtins might compose just because. There are probably gains to be had integrating with the Reactively class rather than handling my own reactivity and subscriptions.

milomg commented 1 year ago

This sounds awesome and I've never seen this trick before, the only drawback I can think of is strict equality is more confusing. I've mentioned this to @ryansolid to consider using in SolidJS as well.