Skip to content
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

[thi.ng/atom] - Extending classes for Observablehq #64

Open
den1k opened this issue Dec 26, 2018 · 7 comments
Open

[thi.ng/atom] - Extending classes for Observablehq #64

den1k opened this issue Dec 26, 2018 · 7 comments

Comments

@den1k
Copy link

den1k commented Dec 26, 2018

I extended umbrella/atom to work idiomatically with observable (e.g. viewof and reactive cells), result here - this now works with deeply nested objects!

extended Atom code here

But I'm struggling to get cursors to work, I think because I can't pass my the extended Atom class in the Cursor impl: https://github.com/thi-ng/umbrella/blob/master/packages/atom/src/cursor.ts#L97

If successful, I'd love to do the same for rstream and derived libs!

@den1k
Copy link
Author

den1k commented Dec 26, 2018

It looks like extending reactive parts of umbrella w/ the EventTarget interface will do the trick: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget - reactiveness comes from dispatchEvent which needs to run whenever values are updated, e.g. for update, reset, resetIn in atoms and next in streams and subscriptions.

Can this be done without overriding/redefining existing methods on Atom and Stream?

@den1k
Copy link
Author

den1k commented Dec 26, 2018

Some progress with Stream's but couldn't get Subscription's to work

https://beta.observablehq.com/d/d18d32f9c8545c65

@postspectacular
Copy link
Member

Hi @den1k - happy new year & apols for slower than usual response...

I have to admit from those examples, I'm not sure what you're trying to do here, maybe also because both examples are also broken due to JS errors in the notebook...

I guess, my main issue is that I'm still not familiar how observable's dataflow model works in detail (e.g. no idea what this viewof construct does). All I remember is that they're forming a DAG of promises to achieve reactivity... any pointers for further reading?

For atoms/views/cursors can't you just use .addWatch() and dispatch any required custom events from there (can you also explain what ? Likewise, for streams/subscriptions, why not just .subscribe() to (or .transform()) them and again add additional event logic there (instead of extending classes). You could publish some small wrapper function/syntax sugar in one of your notebooks and then re-use it...

In general, maybe the issues you're running into have to do with trying to bridge/shoehorn two different approaches into one? Your current solution seems overly complex to me - but again (see above), I'm really not too familiar with observable, so might misunderstand...

@den1k
Copy link
Author

den1k commented Jan 3, 2019

HNY! Fixed the first notebook... bundle.run is not very reliable.

The error in the second notebook is intentional, since I got stuck at that point.

Re: viewof here's a notebook explaining it:https://beta.observablehq.com/@mbostock/a-brief-introduction-to-viewof

You're right I could just use addWatch or subscribe/transform (and I tried that) but observable is all about seeing values update in realtime. Currently I can declare an atom or stream but would need to deref it to see changes. Or a separate cell with an add-watch, as well as one for every cursor and derived subscription. This gets very verbose over time.

By implementing EventTarget which I did successfully for atoms I can use viewof to get the atom, and otherwise automatically get the deref'd value of it, all while seeing values update reactively in the UI.

@den1k
Copy link
Author

den1k commented Jan 3, 2019

Here is Atom, extended: https://beta.observablehq.com/@den1k/atom

The main difference is that this.dispatchEvent (part of the EventTarget interface) is called during updates (e.g. a.resetIn). This triggers a UI update in observable.

So I'm wondering whether it would be problematic / undesired to add these to umbrella's reactive classes or if there's a way to optionally extend these classes since the above is a hack.

@xtuc
Copy link

xtuc commented Jan 16, 2019

HNY! Fixed the first notebook... bundle.run is not very reliable.

What's the issue you're seeing on bundle.run?

@den1k
Copy link
Author

den1k commented Jan 16, 2019

@xtuc not sure this is the same issue, but packages simply don't seem to resolve sometimes: https://talk.observablehq.com/t/bundle-run-errors-breaking-notebooks/1050

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

No branches or pull requests

3 participants