Closed dodyg closed 1 year ago
According to this PostEvent is only available via the worker itself.
I'm not sure where the misunderstanding lies here. Hopefully this clears it up.
In your window context
var worker = await workerService.GetWebWorker();
worker.OnMessage += (sender, msg) =>
{
// use msg.TargetName to get the message identifier used as the first parameter in ```SendEventToParents```
if (msg.TargetName == "progress")
{
PiProgress msgData = msg.GetData<PiProgress>();
piProgress = msgData.Progress;
StateHasChanged();
}
};
In your WebWorker context to send a message to the window that is listening
workerService.SendEventToParents("progress", new PiProgress { Progress = piProgress });
That code shows how a window can listen to messages from the worker and how to send messages to the window listening for those messages.
Another option is to use WebWorkerService.DedicatedWorkerParent
from inside a WebWorker.
WebWorkerService.DedicatedWorkerParent is equivalent to WebWorkerService.GetWebWorker except the returned value points to the Window context that created the WebWorker instead of a new WebWorker.
From inside a WebWorker context, to get an interface that allows calling a Service on the Window context from a WebWorker context just like a Window can do with a WebWorker.
// get a handle to the service interface ISomeWindowService in the Window context
var someWindowService = WebWorkerService.DedicatedWorkerParent.GetService<ISomeWindowService>();
// call a method on the window context
await someWindowService.SomeMethod();
Hopefully that helps. If you see a way to make the documentation more understandable please let me know. Thank you.
public interface ISomeWindowService
{
Task DoSomethingAsync();
}
public class SomeWindowService: ISomeWindowService
{
public async Task DoSomethingAsync()
{
//is there any mechanism that allows me to post a message from inside this method?
}
}
The window that created the WebWorker should already have a handle to the WebWorker but you can get the caller using a special parameter.
public interface ISomeWindowService
{
Task DoSomethingAsync(ServiceCallDispatcher caller);
}
public class SomeWindowService: ISomeWindowService
{
public async Task DoSomethingAsync(ServiceCallDispatcher caller)
{
// caller will be the WebWorker context that called the method.
// it must be named caller and be of type ServiceCallDispatcher but it can be the first, last, or even middle parameter
// the dispatcher auto inserts it when the method is called
// when calling DoSomethingAsync you would call it with a null in the caller parameter place. Ex. DoSomethingAsync(null)
// send message to OnMessage event handler of the caller (requires it is listening for it)
// web workers can attach a listener via WebWorkerService.DedicatedWorkerParent.OnMessage
// window can attach a listener after the worker is created via worker.OnMessage
caller.SendEvent("someevent", someEventData);
// OR
// using a service to call method on the caller context
var callerService = caller.GetService<ISomeServiceOnTheCaller>();
await callerService.SomeMethodInCallerContext();
}
}
That mechanism works in a WebWorker, SharedWebWorker, and Window contexts to get the caller context.
Thank you so much for your help. This is a brilliant library.
Thank you and glad to help. I will continue to work on improving the documentation. Some of these advanced features haven't quite made it into the docs yet but should be there soon.
It's not very clear from this whether it is possible to send a message from
IMathService
.According to this
PostEvent
is only available via the worker itself.