Fork me on GitHub
#membrane
<
2022-04-10
>
zimablue19:04:39

Just random chat, ignore if you're busy - I was wondering how much membrane assumes that it's a 'global singleton', and how hard it would be to persuade it not to be. From memory of reading the webgl.cljs file, it sets up event handlers, and from memory of using it/reading docs, I think it does think it's a single loop, to be set up once at top level. Because it's targetting stuff that writes pixels, it seems like it might be useful/easy to write it such that it can be it's own primitive "windowing system", like instead of passing it a single canvas once, you pass it a "pixel selector/writer/event emitter" (a canvas + an offset/width/height) multiple times, and each instance just runs independantly, returning some sort of state one could use to start/stop/kll individual windows. Sort of motivated by my current dev flow for a problem was to implement this myself INSIDE a single global membrane app using offset and a 2-vec of two states/handlers, so that I could always see the before/after of my changes. Thniking further actually if you passed in a top-level transparency or pixel-write-modification or two-pixel-merger (colour inversion? highlight differences?) you could actually draw your two different versions of your app on top of eachother

phronmophobic19:04:28

As a whole, membrane does not assume a global singleton. Most of the backends explicitly allow multiple apps that can run simultaneously. It might be that webgl makes some global assumptions. I don't quite remember. If it does, it wouldn't be that hard to fix.

phronmophobic20:04:57

> Thniking further actually if you passed in a top-level transparency or pixel-write-modification or two-pixel-merger (colour inversion? highlight differences?) you could actually draw your two different versions of your app on top of eachother Assuming you had the graphics code available, this shouldn't be that hard. The transparency version should be pretty easy

zimablue20:04:50

skimming the file/memory there's quite a few globals -> ctx, paint-style, event-handlers, font-cache, images so I don't think it would be totally trivial

phronmophobic20:04:14

None of those are global in the sense that having multiple apps would stomp on each-other: • context and paint style are dynamic bindings and are dynamically rebound for each draw • all the event handlers are partially applied with the context for a specific app, https://github.com/phronmophobic/membrane/blob/master/src/membrane/webgl.cljs#L576. • The font cache is global, but it's append only. Having more fonts available shouldn't break anyone and you don't have to worry about a font changing out from under you. • I'm not sure about images.

zimablue21:04:34

Bindings set per call being safe is only true until asynchrony though I guess

phronmophobic21:04:46

> Bindings set per call being safe is only true until asynchrony though I guess I don't think that applies here. The dynamic binding is only used for drawing and there aren't any asynchronous draw calls. I'm having trouble imagining a scenario where that would be unsafe.

zimablue22:04:58

Sorry was maybe a bit pedantic

zimablue11:04:56

@U7RJTCH6J reading this again, is there a good example of "stopping" or "running multiple" apps? if not don't worry I'll work it out through the source code

phronmophobic18:04:20

• For webgl, I don't think there's good support for "stopping" an app. As long as your app doesn't trigger any background timers, then an easy way to approximate would to just remove the target canvas and replace it with a new canvas+app if necessary • I don't think you need to do anything special to run multiple apps. The one big caveat is that you probably want to have a separate canvas for each app. The main reason is that when app redraws, it will first clear the full canvas. It would be possible to change things so that each app doesn't clear its canvas, but I think it's easier to provide a separate canvas for each app and create a new canvas that combines the canvas elements from each app using https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage . • I'm not exactly sure what you're trying to do, but in most cases, you probably don't want to run multiple apps. Instead, you can create a new component that embeds multiple versions of what would have been your app's main component.

zimablue20:04:34

thanks, that's very helpful information