Broadcast to all stacks?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Broadcast to all stacks?
Hi,
seems I got a knot in my brain again. Maybe someone can help?
I have a "mainStack" as a standalone. There's a number of other stacks, opened by the mainStack as required. These are plain LC stacks, no subStacks.
Now I want to dispatch/ send/ push/ whatever a message from the mainStack to every other open stack that has a handler to respond. How to do this?
I don't want to go lengths to achieve this - this should happen often, so it must be simple, fast & frugal. Using "topStack" doesn't work, this isn't necessarily the (only) target. I'm sure there's a simple solution, but it seems I'm in blackout here.
Any ideas? Thank you for any help!
Have fun!
seems I got a knot in my brain again. Maybe someone can help?
I have a "mainStack" as a standalone. There's a number of other stacks, opened by the mainStack as required. These are plain LC stacks, no subStacks.
Now I want to dispatch/ send/ push/ whatever a message from the mainStack to every other open stack that has a handler to respond. How to do this?
I don't want to go lengths to achieve this - this should happen often, so it must be simple, fast & frugal. Using "topStack" doesn't work, this isn't necessarily the (only) target. I'm sure there's a simple solution, but it seems I'm in blackout here.
Any ideas? Thank you for any help!
Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
-
- Posts: 253
- Joined: Wed Aug 19, 2015 4:29 pm
Re: Broadcast to all stacks?
Yes,
I could loop through the openStacks, ignore all that aren't mine, determine which of mine can respond & dispatch explicitly to each of those.
And thus create a "hickUp" each time this happens. Guess I should explain more:
My "mainStack" has a handler running that periodically calls itself (send "upDateClock" to me in myVar seconds). It sets a clock field, and checks if we still have internet, and sets some cProps accordingly.
Now I just want to dispatch (?) a msg from there, "into the blue", not to a certain stack.
So that any stack that has a suitable handler can respond - for instance, adjust an icon to show the IN status.
I'm taking much care in the "upDateClock" handler to be as unobtrusive as possible (watching the pendingMessages etc. ...). No way to do a lot of calculating, or sending more than 1 message. I'd rather scrap the whole idea.
So I need to "push" a message among the stacks, without knowing who'll receive it. Without polling from the receiver side. Is this possible? Any ideas?
Have fun!
I could loop through the openStacks, ignore all that aren't mine, determine which of mine can respond & dispatch explicitly to each of those.
And thus create a "hickUp" each time this happens. Guess I should explain more:
My "mainStack" has a handler running that periodically calls itself (send "upDateClock" to me in myVar seconds). It sets a clock field, and checks if we still have internet, and sets some cProps accordingly.
Now I just want to dispatch (?) a msg from there, "into the blue", not to a certain stack.
So that any stack that has a suitable handler can respond - for instance, adjust an icon to show the IN status.
I'm taking much care in the "upDateClock" handler to be as unobtrusive as possible (watching the pendingMessages etc. ...). No way to do a lot of calculating, or sending more than 1 message. I'd rather scrap the whole idea.
So I need to "push" a message among the stacks, without knowing who'll receive it. Without polling from the receiver side. Is this possible? Any ideas?
Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
-
- VIP Livecode Opensource Backer
- Posts: 9847
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Broadcast to all stacks?
The message path is very flexible, but there is no built-in way to do a broadcast.
A loop that uses dispatch is your best bet here, and you'd be in good company since that's pretty much what the IDE does for a similar need.
You could shorten the broadcast time a little by having stacks register themselves to the broadcaster when they open, so the broadcast has fewer things to loop through.
But even without that I think you'll find the loop pretty fast. I'd be surprised if it takes more than a few microseconds, maybe less unless you have a ginormous number of stack files open.
A loop that uses dispatch is your best bet here, and you'd be in good company since that's pretty much what the IDE does for a similar need.
You could shorten the broadcast time a little by having stacks register themselves to the broadcaster when they open, so the broadcast has fewer things to loop through.
But even without that I think you'll find the loop pretty fast. I'd be surprised if it takes more than a few microseconds, maybe less unless you have a ginormous number of stack files open.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- VIP Livecode Opensource Backer
- Posts: 9729
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Broadcast to all stacks?
What Richard said.
But I am confused. Your mainStack explicitly opens other stacks. Don't you therefore have a precise list of those stacks, to which you can send a message?
Craig
But I am confused. Your mainStack explicitly opens other stacks. Don't you therefore have a precise list of those stacks, to which you can send a message?
Craig
Re: Broadcast to all stacks?
Hi,
Craig, maintaining a list of stacks I have opened doesn't make much sense to me:
Such a list must be correct, else it causes mistakes - what if for whatever reason a stack in background dies quietly? So this list would need to be periodically verified. At each "Open" I had to add, at each "Close" to subtract. And then still I had only a list of stacks that I'd need to send messages to individually. That I can have already with "the openStacks".
Richard, ClipArtGuy, I tested your proposals, and it would actually work!
This gives me a list of stacks that I may have open (they all begin with "awa", and the mainStack end with "Main" ...). Getting this is about 1 ms on my machine, nothing compared to the ~ 140 ms my "upDateClock" already uses.
But, realizing that my idea of a "broadcast" may be impossible as I had envisioned it, I thought some more. And came to the conclusion that it would be nice to have, but that actually the topStack is the only one that really needs it. So:
must do it. "cNetStat" is the mode the program is in: "onLine mode" or "offLine mode". "cHaveNet" shows if we have internet, or not. The monitoring is done by the mainStack in the background, but the stack actually active should be able to respond if one of these parameters changes. And this works now.
Thx & have fun!
PS: I wonder if there wouldn't be demand for such a "broadcast" function?
Craig, maintaining a list of stacks I have opened doesn't make much sense to me:
Such a list must be correct, else it causes mistakes - what if for whatever reason a stack in background dies quietly? So this list would need to be periodically verified. At each "Open" I had to add, at each "Close" to subtract. And then still I had only a list of stacks that I'd need to send messages to individually. That I can have already with "the openStacks".
Richard, ClipArtGuy, I tested your proposals, and it would actually work!
Code: Select all
put the openStacks into myStax
repeat for each line L in myStax
if (L begins with "awa") and not (L ends with "Main") then
put L & CR after myVar
end if
end repeat
return char 1 to -2 of myVar
But, realizing that my idea of a "broadcast" may be impossible as I had envisioned it, I thought some more. And came to the conclusion that it would be nice to have, but that actually the topStack is the only one that really needs it. So:
Code: Select all
dispatch "ourNetStatus" to the topStack with \
(the cNetStat of this stack) & comma & (the cHaveNet of this stack)
Thx & have fun!
PS: I wonder if there wouldn't be demand for such a "broadcast" function?
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
-
- VIP Livecode Opensource Backer
- Posts: 9729
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Broadcast to all stacks?
Axwald.
It never occurred to me that a stack might quietly die. It is a matter of style, I suppose, whether keeping track of stacks that are explicitly opened or closed is an issue. But I know you can find a way that suits you.
Craig
It never occurred to me that a stack might quietly die. It is a matter of style, I suppose, whether keeping track of stacks that are explicitly opened or closed is an issue. But I know you can find a way that suits you.
Craig
Re: Broadcast to all stacks?
Craig,
this is meant to run on Win & Android, both. The same program.
On Win, it's easy - this is rock solid. Stacks rarely ever crash, as long as you use a stable version for development & test sufficiently. Basically all of my customers will have Win10 meanwhile, and I haven't had a reported crash for years.
On Android, it's different. I need to cover versions from 4.1 up, with widely different features/ bloatware. I need to cover various architectures, and for some of these, I'm forced to use LC versions less stable than desirable to compile with.
When I use "Test" & send my current (flawlessly running on Win ...) version to Android, I'm most often flabbergasted what all can go wrong. For fun & recreation, my newest, just found madness (on Android):
When my mainStack is active, I can switch to other apps, or have the tablet go to sleep - it just stays open, as desired.
When any other stack is active & I do this, Android will close my app ;-)
So, I cannot take anything as granted, and try to avoid anything that even remotely could, maybe, smell dubiously. Getting the openStacks via above mentioned function is clean. Manually keeping & maintaining a list of open stacks by far less, IMHO.
Thx, and have fun!
this is meant to run on Win & Android, both. The same program.
On Win, it's easy - this is rock solid. Stacks rarely ever crash, as long as you use a stable version for development & test sufficiently. Basically all of my customers will have Win10 meanwhile, and I haven't had a reported crash for years.
On Android, it's different. I need to cover versions from 4.1 up, with widely different features/ bloatware. I need to cover various architectures, and for some of these, I'm forced to use LC versions less stable than desirable to compile with.
When I use "Test" & send my current (flawlessly running on Win ...) version to Android, I'm most often flabbergasted what all can go wrong. For fun & recreation, my newest, just found madness (on Android):
When my mainStack is active, I can switch to other apps, or have the tablet go to sleep - it just stays open, as desired.
When any other stack is active & I do this, Android will close my app ;-)
So, I cannot take anything as granted, and try to avoid anything that even remotely could, maybe, smell dubiously. Getting the openStacks via above mentioned function is clean. Manually keeping & maintaining a list of open stacks by far less, IMHO.
Thx, and have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
-
- VIP Livecode Opensource Backer
- Posts: 9847
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Broadcast to all stacks?
Indeed there is. And since it takes only a few lines of code, we have it in just a minute's work whenever we need it.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- VIP Livecode Opensource Backer
- Posts: 76
- Joined: Tue Mar 05, 2013 7:00 pm
- Location: Berlin
- Contact:
Re: Broadcast to all stacks?
Hi axwald,
for me it sounds as if you are talking about the publish/subscribe pattern.
Andre Garzia has put together a simple lib for this, its called:
aagPubSubLib
Usage is explained in his book "LiveCode Advanced Application Architecture"
You can get it here:
https://andregarzia.com/livecode/
for me it sounds as if you are talking about the publish/subscribe pattern.
Andre Garzia has put together a simple lib for this, its called:
aagPubSubLib
Usage is explained in his book "LiveCode Advanced Application Architecture"
You can get it here:
https://andregarzia.com/livecode/
Re: Broadcast to all stacks?
This exactly. I came across Andre’s publish/subscribe library and was initially quite excited as a newcomer to LC, before I realised that most of the time this extra work isn’t really needed.mimu wrote: ↑Wed Oct 14, 2020 7:42 pmHi axwald,
for me it sounds as if you are talking about the publish/subscribe pattern.
Andre Garzia has put together a simple lib for this, its called:
aagPubSubLib
Usage is explained in his book "LiveCode Advanced Application Architecture"
You can get it here:
https://andregarzia.com/livecode/
But your use-case sounds like it fits the bill, you should definitely have a look at using an observer pattern like this...
Re: Broadcast to all stacks?
As a belated answer to the OP as well as for anyone searching for Publish-Subscribe functionality in LC (not always needed, but good to have if it is): https://github.com/stam66/skPubSub