-
Notifications
You must be signed in to change notification settings - Fork 2
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
Suggestion: Trackers through arguments #1
Comments
Hey, Víctor! You've actively participated in this, so big thanks goes to you 🙂 🙌 Yeah! Actually this was my initial API: And it makes sense if you want to integrate this with, say, a custom // pseudocode
// state.js
import { $, _ } from 'rxjs-autorun';
export const State = v => {
const subj = new BehaviorSubject(v);
return Object.assign(subj, { get _: _.bind(null, subj), get $: $.bind(null, subj) });
// not sure it'l work, but you got the idea
}
// index.js
import { run } from 'rxjs-autorun';
import { State } from './state';
const state = State(0);
run(() => state.$ + ' 🐑'); // > 0 🐑
state.next(1); // > 1 🐑 Which wouldn't be possible with But maybe we could have both options available for the user? Would that be fine or confusing? |
Ahh, that makes sense, thank you for explaining.
Although I guess it wouldn't hurt (as users can just ignore one of both ways), it's not really needed. If anyone needs to use the suggested overload, it's fairly trivial to write a util function that exposes that API. I'll close this issue - feel free to reopen it if you think it's worth keeping it in. |
I think, you're right that it's a good alternative and it wouldn't hurt Also, I guess, there's a side-point that |
I like to add my 2ct here as well.
But an upside is that you don't have to import So I would very much argue in favor of passing |
Follow up to this discussion, I would also like to argue in favor of
() => `${${a}} 🐑` vs this: _ => `${_(a)} 🐑` (although the best option might be an added feature like this: () => rx`${a} 🐑` )
|
I want to try some integrations to see what are the limits of the two approaches. E.g. I want to try autorun with rxjs-proxify or a simple state based on it. Unlike const state = new State({ a: 1, b: { c: 2 } });
computed(() => state.a._ + state.b.c.$); // > 3
state.b.next({ c: 3 }); // > 4 Also, --
Nice! Maybe it's a candidate for an API. I also wonder if can we achieve this via external code now and would we be able to do that with Btw, @voliva did an interesting string template-based approach -- Now, back to the issue: currently, I see this change as not critical (unless it forbids tree shaking) And keep sharing your thoughts and findings! Cheers |
So, I've been typing-thinking. Guess, if we pass params as args, we can still achieve the separation: Warning: pseudocodish // shared.ts
import * as rxjsAutorun from 'rxjs-autorun';
export function unwrapComputed(){
const c = { _(){ throw 'ERR'; }, $(){ throw 'ERR'; } };
return [ o => c.$(o)
, o => c._(o)
, fn => rxjsAutorun.computed(($,_) => c.$ = $, c._ = _, fn())
]
} So that we can use it in 3rd party API: // state.ts
import { unwrapComputed } from './shared';
const [$,_, computed] = unwrapComputed();
const State = v => {
const subj = new BehaviorSubject(v);
return Object.assign(subj, { get _: _.bind(null, subj), get $: $.bind(null, subj) });
// not sure it'l work, but you got the idea
}
export { State, computed }; // < NOTE exporting computed from state And then: // index.js
import { State, computed } from './state';
const state = State(0);
computed(() => state.$ + ' 🐑'); // > 0 🐑
state.next(1); // > 1 🐑 But I suspect that in this case we are locked to Am I right in this assumption? What do you guys think? |
How about providing the
|
This is definitely an option! I'm holding this because Since the current approach works fine now, I'd like to postpone this decision until we got more practical cases. |
This is a cool and fun idea indeed. Thanks for this!
I'd like to suggest an alternative API that maybe would make more explicit what
$
and_
do, and make the implementation slightly easier.Instead of importing
$
and_
from the package, what if these two trackers could be passed as arguments torun
? So instead of:You could:
This would also let the consumer name
$
and_
as they want (in this case,track
andread
respectively), but they could also go with a smallert
andr
.The text was updated successfully, but these errors were encountered: