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

Add mathjs singleton and expose it via API #481

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Cube707
Copy link
Contributor

@Cube707 Cube707 commented Dec 5, 2024

This allows for modification of mathjs by the user, mostly to allow for the use of math.import to define custom functions.

See the new Advanced Examples/Customising mathjs for details.

closes #480


This is a first implementation, it works but I am sure there are better ways to implement the singleton.
Finding the right place in the lifecycle of the app just went over my head

I am also unsure if the MathjsInstance should be per-app or per file?

This allows for modification of `mathjs` by the user,
mostly to allow for the use of `math.import` to define custom functions
> [!Note]
> Please note the second parameter for `math.import`.
> Passing `silent:true` leads to the function <u>not</u> beeing updated on edit! (only on reload of meta-bind)
> Pass `override:true` to override the function everytime you switch between edit and viewing mode.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence is probably incorrect. You override the function every time the code block reruns, which is not necessarily when switching editor modes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point.

I also though about if the example should be in a more "playing around" friendly way. So just having it as override:true might be better and saves the troubles of having to add the note

@@ -13,12 +13,15 @@ export class MathVF extends AbstractViewField<unknown> {
expression?: EvalFunction;
expressionStr?: string;
hasError: boolean;
math: MathJsInstance;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you add this math member variable couldn't you get the singleton directly on line 54?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this and started this way because I expected to need multiple accesses, but than ended up not.

I was also unsure how this might change depending on the liftime of the singleton / multiple instances, so I left it being initialized by the constructor.

But fine to change if the singleton-pattern is staying

@mProjectsCode
Copy link
Owner

I am also unsure if the MathjsInstance should be per-app or per file?

This brings up an important point. The additions to mathjs only work once the code adding them has run. Code block execution order in a file is usually top to bottom, but that is not guaranteed. They can theoretically run in any order, so math view fields could run before the js-engine code block adding the needed mathjs extensions.

One approach could be to enable users to somehow register global mathjs extensions on plugin load.

@Cube707
Copy link
Contributor Author

Cube707 commented Dec 5, 2024

They can theoretically run in any order, so math view fields could run before the js-engine code block adding the needed mathjs extensions.

that would be super annoying if it happens randomly sometimes.

One approach could be to enable users to somehow register global mathjs extensions on plugin load

I also thought about adding a config option where the user could input a object to initialize the context with. This was just the quickest way to get a working version to test if it works at all.
I am also not sure how the object would be input as a config, JSON should work but I find it much easier to add stuff via a js-engine block than having to externally convert a JS function to JSON and copy-paste it in.

@mProjectsCode
Copy link
Owner

yeah, maybe a path to a JS file that get's run via js-engine at statup

@Cube707
Copy link
Contributor Author

Cube707 commented Dec 6, 2024

maybe a path to a JS file that get's run via js-engine at statup

I like the Idea, maybe identically to how Base Obsidian handles css snippets, just as JS snippets.

But it starts to feel more like it should be a Feature of JS-engine now?

@mProjectsCode
Copy link
Owner

But it starts to feel more like it should be a Feature of JS-engine now?

Yeah. Maybe it's better to just add a simple API to MB and the startup script thing to js-engine

@mProjectsCode
Copy link
Owner

After a few days of thought, I want to go with a simple API on the MB side and adding startup scripts to js-engine.

@mProjectsCode
Copy link
Owner

I just released statup scripts in js-engine 0.2.0

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.

giving access to mathjs.import
2 participants