Repository navigation
[WIP] Refactors to support running in node #9440
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
Draft
davepagurek
wants to merge
13
commits into
main
Choose a base branch
from
wgpu-refactors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+235
−111
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
562730d
Add platform specific build
davepagurek e95c830
Try globalThis instead of window
davepagurek 4f6625e
Factor out uncaught error handling
davepagurek 6acba6c
Factor out script reading from FES
davepagurek 2ff5328
Move acceleration code under a window check
davepagurek e2669c2
Put more stuff behind window checks
davepagurek 1b5a84f
looks like another case of s/window/globalThis/g
davepagurek c0fda5f
oops we were referencing an assumed global p5
davepagurek 48d856e
Duck type canvas checks
davepagurek 105355f
Attempt to make some canvas setting apis
davepagurek 9406a12
Allow imagedata-shaped things too
davepagurek d6f3351
right, doc comments get attached to stuff and show up in the referenc…
davepagurek 7c8c8c4
Add a bit of scaffolding for testing node.js mode
limzykenneth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // import p5 from "../../dist/app.node.js"; | ||
| // import p5 from "../../lib/p5.node.esm.js"; | ||
| import webgpu from "../../dist/webgpu/index.js"; | ||
|
|
||
| p5.registerAddon(webgpu); | ||
|
|
||
| const sketch = function (p) { | ||
| p.setup = function () { | ||
| p.createCanvas(800, 600, p.WEBGPU); | ||
| }; | ||
| }; | ||
|
|
||
| new p5(sketch); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| Browser adapter. Everything p5 needs from its host environment should go | ||
| through this module, so that non-browser builds can swap in | ||
| alternate implementations instead. | ||
| */ | ||
|
|
||
| // make a promise that resolves when the document is ready | ||
| export const waitForDocumentReady = () => { | ||
| if (typeof document !== 'undefined') { | ||
| return new Promise((resolve, reject) => { | ||
| // if the page is ready, initialize p5 immediately | ||
| if (document.readyState === 'complete') { | ||
| resolve(); | ||
| // if the page is still loading, add an event listener | ||
| // and initialize p5 as soon as it finishes loading | ||
| } else { | ||
| window.addEventListener('load', resolve, false); | ||
| } | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| export const handleUncaughtErrors = callback => { | ||
| const handler = e => { | ||
| let error; | ||
| if (e instanceof ErrorEvent) { | ||
| error = e.error; | ||
| } else if (e instanceof PromiseRejectionEvent) { | ||
| error = e.reason; | ||
| if (!(error instanceof Error)) return; | ||
| } | ||
| if (error) callback(error); | ||
| }; | ||
| window.addEventListener('error', handler, false); | ||
| window.addEventListener('unhandledrejection', handler, false); | ||
| }; | ||
|
|
||
| export const getUserScript = () => { | ||
| // TODO: think of a more robust way to get the user's code. Refer to | ||
| // https://github.com/processing/p5.js/pull/7293. | ||
| const scripts = document.querySelectorAll('script'); | ||
| return scripts[scripts.length - 1]; | ||
| }; | ||
|
|
||
| export const isCanvasElement = data => data instanceof HTMLCanvasElement; | ||
|
|
||
| // TODO is this the right API? | ||
| export const markP5Canvas = (elt, id) => { | ||
| elt.id = id; | ||
| elt.classList.add('p5Canvas'); | ||
| }; | ||
|
|
||
| // TODO is this the right API? | ||
| export const setCanvasSize = (elt, w, h, density) => { | ||
| elt.width = w * density; | ||
| elt.height = h * density; | ||
| elt.style.width = `${w}px`; | ||
| elt.style.height = `${h}px`; | ||
| }; | ||
|
|
||
| // TODO is this the right API? | ||
| export const attachCanvas = (elt, userNode) => { | ||
| if (userNode) { | ||
| // user input node case | ||
| userNode.appendChild(elt); | ||
| } else { | ||
| //create main element | ||
| if (document.getElementsByTagName('main').length === 0) { | ||
| let m = document.createElement('main'); | ||
| document.body.appendChild(m); | ||
| } | ||
| //append canvas to main | ||
| document.getElementsByTagName('main')[0].appendChild(elt); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| Node adapter, used in place of `platform.js` by the p5.node build. | ||
| */ | ||
|
|
||
| // There is no document to wait for | ||
| export const waitForDocumentReady = () => Promise.resolve(); | ||
|
|
||
| export const handleUncaughtErrors = callback => { | ||
| // Node rethrows unhandled promise rejections as uncaught exceptions, so they | ||
| // are caught here too | ||
| process.on('uncaughtExceptionMonitor', error => { | ||
| if (error instanceof Error) callback(error); | ||
| }); | ||
| }; | ||
|
|
||
| // TODO: maybe read process.argv[1]? | ||
| export const getUserScript = () => undefined; | ||
|
|
||
| // This prop is added in the wgpu code so we can identify it here | ||
| export const isCanvasElement = data => data?.isP5WGPUCanvas === true; | ||
|
|
||
| // TODO is this the right API? | ||
| export const markP5Canvas = () => {}; | ||
|
|
||
| // TODO is this the right API? | ||
| // TODO resize the window and surface once ngpu supports it | ||
| export const setCanvasSize = (elt, w, h, density) => { | ||
| elt.width = w * density; | ||
| elt.height = h * density; | ||
| }; | ||
|
|
||
| // TODO is this the right API? | ||
| // No page to attach to on desktop | ||
| export const attachCanvas = () => {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea for is to use the
app.node.jsentry point instead of reusing this one. The other entry point simply don't have the global mode stuff. Would that solution work instead?If some kind of global mode sketch is preferred, I think the experimental new instance mode p5.ni.js probably can fit in for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that can work, but might not solve the whole problem, because currently the platform-specific calls are scattered kind of everywhere. Having one
#platformfile to import from works around that. An alternative could be to haveapp.node.jsput an object on the p5 global with those, so that the references everywhere in p5 could grab it from e.g.p5.platform.handleUncaughtErrors?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can still use the
#platformimport idea with theapp.node.jsapproach. The question is more around the intended usage pattern:app.node.jsis toimport p5 from "p5/node"and frontend appsimport p5 from "p5"which bundlers can treeshake.app.node.esm.jswon't be needed as node.js can import the transformedapp.node.jsfromp5/nodeexports and resolve normally from there. Unless the idea is to support kinda Deno style URL import where a singled bundled file would be more straightforward?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh I see. remind me, how does it work currently if you import p5 from an environment that does tree shaking? iirc we still serve built files to some degree to have things like the GLSL imports work? If so, is that system flexible enough to be used for node too, possibly just through an alternate entrypoint if we think initialization would be easier?
I think supporting something something like that would be good. I also wonder how possible it is for "true" global mode to work in node. Does
globalThiswork similarly in node where if you make afunction setup() {}it's accessible onglobalThislike in the browser? If we hit any weirdness then a p5.ni approach sounds like the right call and we can move that into the core.