|
| 1 | +## Seamless Communication Across Threads and Beyond |
| 2 | + |
| 3 | +In a multi-threaded architecture like Neo.mjs, efficient and seamless communication between different execution contexts |
| 4 | +(Web Workers, Main Thread, and even backend services) is paramount. The Neo.mjs Remote Procedure Call (RPC) Layer |
| 5 | +provides a powerful abstraction that simplifies this complex inter-thread and inter-process communication, allowing |
| 6 | +developers to invoke methods on remote objects as if they were local. |
| 7 | + |
| 8 | +### Abstracting Cross-Worker Communication |
| 9 | + |
| 10 | +At its core, the RPC Layer eliminates the need for developers to manually handle `postMessage` calls, message parsing, |
| 11 | +and promise resolution when communicating between Web Workers. Whether it's your App Worker interacting with the VDom |
| 12 | +Worker to update the UI, or with the Data Worker to fetch and process information, the RPC Layer provides a clean, |
| 13 | +promise-based API. |
| 14 | + |
| 15 | +**Benefit**: This abstraction significantly reduces boilerplate code and cognitive load. Developers can focus on the |
| 16 | +business logic of their application rather than the intricacies of message passing between threads. This leads to faster |
| 17 | +development, more readable code, and fewer errors related to inter-thread communication. |
| 18 | + |
| 19 | +### Extending to Backend Integration |
| 20 | + |
| 21 | +The power of the Neo.mjs RPC Layer extends beyond just inter-worker communication within the browser. It provides a |
| 22 | +consistent mechanism for interacting with backend services. This means the same patterns and mental model used for |
| 23 | +communicating with a Data Worker can be applied to making API calls to your server. |
| 24 | + |
| 25 | +**Benefit**: A unified approach to both frontend inter-thread communication and backend integration streamlines the |
| 26 | +development process. It reduces the learning curve for new team members and ensures consistency in how data and |
| 27 | +commands are exchanged across the entire application stack. |
| 28 | + |
| 29 | +### Key Advantages: |
| 30 | + |
| 31 | +* **Simplicity**: Invoke remote methods with a simple function call, receiving a promise that resolves with the result. |
| 32 | + The RPC Layer handles serialization, deserialization, and message routing automatically. |
| 33 | +* **Reduced Boilerplate**: Eliminates the need for manual message listeners, dispatchers, and complex state management |
| 34 | + around asynchronous operations. |
| 35 | +* **Improved Readability & Maintainability**: Code becomes cleaner and easier to understand, as the underlying |
| 36 | + communication mechanism is abstracted away. |
| 37 | +* **Performance**: Designed for efficiency, the RPC Layer ensures that inter-thread communication is as performant as |
| 38 | + possible, minimizing overhead and contributing to the overall responsiveness of Neo.mjs applications. |
| 39 | +* **Error Handling**: Provides robust error propagation across thread boundaries, making it easier to debug and handle |
| 40 | + issues that arise during remote method invocations. |
| 41 | + |
| 42 | +### Conceptual Example: Consistent API for Internal and External Calls |
| 43 | + |
| 44 | +Imagine calling a method on a data service defined within the Data Worker realm (e.g., `MyApp.data.UserService`) from your App Worker: |
| 45 | + |
| 46 | +```javascript readonly |
| 47 | +// In your App Worker code |
| 48 | +const userData = await MyApp.data.UserService.fetchUser(userId); |
| 49 | +console.log(userData); |
| 50 | +``` |
| 51 | + |
| 52 | +Now, consider triggering a backend request using the same RPC pattern, as seen in `apps/colors/view/ViewportController.mjs`: |
| 53 | + |
| 54 | +```javascript readonly |
| 55 | +// In apps/colors/view/ViewportController.mjs |
| 56 | +response = await Colors.backend.ColorService.read({ |
| 57 | + amountColors : stateProvider.getData('amountColors'), |
| 58 | + amountColumns: stateProvider.getData('amountColumns'), |
| 59 | + amountRows : stateProvider.getData('amountRows') |
| 60 | +}); |
| 61 | +``` |
| 62 | + |
| 63 | +Notice how the syntax for invoking a method on a backend service (`Colors.backend.ColorService.read`) is virtually |
| 64 | +identical to invoking a method on an internal worker service (`Neo.worker.Data.getService('UserService').fetchUser`). |
| 65 | +This consistency is a core strength of the Neo.mjs RPC Layer. |
| 66 | + |
| 67 | +Behind the scenes, for both internal and external remote calls, the RPC Layer handles: |
| 68 | +1. Serializing the method call and arguments. |
| 69 | +2. Sending a message to the target (worker or backend). |
| 70 | +3. The target receiving the message, invoking the method. |
| 71 | +4. Serializing the result and sending it back. |
| 72 | +5. Resolving the promise in the caller with the received data. |
| 73 | + |
| 74 | +This powerful abstraction is a cornerstone of Neo.mjs's multi-threaded architecture, enabling developers to build |
| 75 | +complex, highly responsive, and scalable applications with remarkable ease. |
0 commit comments