Namespace: SpawnDev.SpawnJS.JSObjects
Inherits: EventTarget
Source: JSObjects/SharedWorker.cs
MDN Reference: SharedWorker on MDN
The SharedWorker interface represents a specific kind of worker that can be accessed from several browsing contexts, such as several windows, iframes or even workers. They implement an interface different than dedicated workers and have a different global scope, SharedWorkerGlobalScope. https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker
| Signature | Description |
|---|---|
SharedWorker(SpawnJSObjectReference _ref) |
Deserialization constructor |
SharedWorker(string url) |
The SharedWorker() constructor creates a SharedWorker object that executes the script at the specified URL. This script must obey the same-origin policy. A string representing the URL of the script the worker will execute. It must obey the same-origin policy. |
SharedWorker(string url, string name) |
The SharedWorker() constructor creates a SharedWorker object that executes the script at the specified URL. This script must obey the same-origin policy. A string representing the URL of the script the worker will execute. It must obey the same-origin policy. A string specifying an identifying name for the SharedWorkerGlobalScope representing the scope of the worker, which is useful for creating new instances of the same SharedWorker and debugging. |
SharedWorker(string url, SharedWorkerOptions options) |
A string representing the URL of the script the worker will execute. It must obey the same-origin policy. |
| Property | Type | Access | Description |
|---|---|---|---|
Port |
MessagePort |
get | Returns a MessagePort object used to communicate with and control the shared worker. |
| Event | Type | Description |
|---|---|---|
OnError |
ActionEvent<Event> |
Fires when an error occurs in the shared worker. |
SpawnDev.SpawnJS Mapping Note: The JavaScript examples below show the standard Web API usage. In SpawnDev.SpawnJS, the same API is available with C# conventions:
- Properties and methods use PascalCase (e.g.,
readyStatebecomesReadyState)- Events use ActionEvent with
+=/-=(e.g.,addEventListener("click", fn)becomesOnClick += handler)- Async methods return
Task<T>instead ofPromise<T>- Objects should be disposed with
usingstatements- Access via
JS.Get<SharedWorker>(...)or constructornew SharedWorker(...)
const myWorker = new SharedWorker("worker.js");myWorker.port.start();[first, second].forEach((input) => {
input.onchange = () => {
myWorker.port.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};
});
myWorker.port.onmessage = (e) => {
result1.textContent = e.data;
console.log("Message received from worker");
};