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

Proof of concept custom raw charts with inject hook #330

Merged
merged 19 commits into from
Apr 22, 2022
Merged

Conversation

gffuma
Copy link
Contributor

@gffuma gffuma commented Nov 10, 2021

A proof of concept demo of custom raw charts.

This demo can load a new chart(s) and auto save it when you reload the pages using the caches api.

How does it work?

This demo expose d3 and rawgraphsCore on the window object thanks to this the custom charts can mark this dependencies as global and with the changes of rawgraphs/rawgraphs-charts#110 the baseline size of a chart including the svg icons is ~7KB.
This demo expose a global "stack" on window (RAWGRAPH_APP_INJECT_HOOK) this approach is similar to how google analytics, react dev tools and other libraries works.

When a new chart is loaded we append a script to head (this is how code splitting works), we wait them and we pop all the stack inside the inject hook. This approach is much better instead of exposing static prop Ex RAWGRAPH_CUSTOM_CHART = ... because we can executed scripts in parallel an wait for all them and them simply pop the stack.

For the purpose of the demo i use a naive approach to compute "the name of the script" i simply split the chart graph and take the first piece this is the name of our pack and i used to save it to the storage cache.

The only temp downside of this demo is that i disabled WebWorkers for simplicity, (we can still easy use web workers for core charts) to use WebWorker for custom charts we need to make also the chart imported a worker this need future investigation.

How can i try this???

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@gffuma
Copy link
Contributor Author

gffuma commented Nov 11, 2021

Got web workers work also for custom charts @bianchimro 😎

@gffuma
Copy link
Contributor Author

gffuma commented Nov 16, 2021

Thanks to the suggestions of @bianchimro and @osioalberto i write a tiny AMD loader that also works with WebWorkers.
So now we can simply load UMD / AMD bundled javascript.
As above we can optionally host d3 and raw core as dependencies, no more, other dependencies should be bundled (for now).
To identify an uploaded file i use the sha512 hash algorithm.
Thanks to these changes we can now load custom charts from npm or urls or a dropped file (all this formats suppose to be bundled as UMD / AMD).
I make the .rawgraphs protocol works with custom charts see rawgraphs/rawgraphs-core#26.
I also add an experimental feature (just for test if can works) to load a project from ?url=https://url-to-my-project.

Yes, as UMD you can load the charts itself using for example: https://cdn.jsdelivr.net/npm/@rawgraphs/rawgraphs-charts

I also notify the user to be careful to load third party javascript with a simple modal.
(A simple whitelist system is already in place so in future we can whitelist good charts from community).

To have a friendly url to try i upload the bundled version of https://github.com/gffuma/custom-rawcharts-template-test
here:

So if your starts the app from this branch and link the correct version of the core ... if you visit:
http://localhost:3000/?url=https://cdn.giova.fun/hello.rawgraphs

You will see:

The caution modal:

Schermata 2021-11-16 alle 21 50 43

... If you continue the custom chart:

Schermata 2021-11-16 alle 21 52 57

Schermata 2021-11-16 alle 21 50 57

Schermata 2021-11-16 alle 21 54 09

Schermata 2021-11-16 alle 21 54 30

@gffuma gffuma marked this pull request as ready for review April 22, 2022 12:51
@gffuma gffuma merged commit 319cdc2 into master Apr 22, 2022
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