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

Improve synchronous script initialization #86

Merged
merged 8 commits into from
May 1, 2024

Conversation

stracker-phil
Copy link
Contributor

PR for the logic suggested in Issue #85

Changes:

  • await forceLoadCustomJS() does not start multiple concurrent loading operations; calling the function multiple times will load all classes once only.
  • New API method await cJS() which acts as a shorthand accessor for the fully initialized customJS object. The method has three signatures:
    • async cJS(): customJS
    • async cJS(moduleName: string): object
    • async cJS( Function ): customJS
  • This PR also introduces the new flag customJS.state._ready which is used internally but can also provide a public interface to evaluate the plugin's loading status

Usage samples:

  1. Check the flag state._ready
```dataviewjs
if (!customJS.state?._ready) {
    await forceLoadCustomJS()
}
customJS.MyModule.doSomething()
```
  1. Access the fully initialized customJS object
```dataviewjs
const modules = await cJS()
modules.MyModule.doSomething()
```
  1. Access a single module from the customJS object
```dataviewjs
const MyModule = await cJS('MyModule')
MyModule.doSomething()
```
  1. Run custom code via callback
```dataviewjs
await cJS( customJS => customJS.MyModule.doSomething() )
```

- Avoid concurrent calls to `loadClasses()`
- Add `cJS()` API for a more direct module access
- Avoid concurrent calls to `loadClasses()`
- Add `cJS()` API for a more direct module access
- Avoid concurrent calls to `loadClasses()`
- Add `cJS()` API for a more direct module access
- Avoid concurrent calls to `loadClasses()`
- Add `cJS()` API for a more direct module access
@saml-dev
Copy link
Owner

This looks great, thanks for contributing! I actually think this should be the default way to use CustomJS as I don't see any downsides. However, rather than breaking changes, I think a better way to proceed is to update the README to remove the existing examples and replace them with your new usage. We can also remove the section on forceLoadCustomJS entirely.

Can you update the PR with these changes?

@stracker-phil
Copy link
Contributor Author

I've made several updates to the README.md file, adding the new cJS() method and some other small details

@saml-dev saml-dev merged commit fab52ee into saml-dev:master May 1, 2024
1 of 2 checks passed
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