-
Notifications
You must be signed in to change notification settings - Fork 32
Remote Procedure Call RPC
Remote Procedure Call (RPC) is a mechanism which allows a program to invoke a function that is defined in another address space. If a user, A, writes a function and shares it with another user B, then B can invoke the shared function in A's program. The return value of the function is retrieved by B, which can then be used in B's program.
RPC is implemented using the Source Academy Module feature. The module is named 'rpc' and exposes three functions, namely connect, share and executeAfter.
The RPC also allows sharing of values in Source programs. These include numbers, strings, booleans, null and undefined. The sharing of arrays of arbitrary nesting is also allowed, along with lists and pairs which resolve into arrays themselves.
To start off, the functions mentioned above must be imported from the module 'rpc' using Source's import statement. The behaviour of the aforementioned functions is described as follows: -
-
share- Returns a promise that resolves to a token that uniquely identifies the shared data or function.shareaccepts data or functions as input and runs asynchronously. The obtained token, when shared to another user, can be supplied as input to theconnectfunction to retrieve the shared data or function.- Parameter:
input- Any value that occurs in a Source program. - Returns: A promise that resolves to a token that uniquely identifies the shared content.
- Parameter:
-
connect- Returns a promise that resolves to the content that is uniquely identified by the input token. The function throws an error if the input is invalid.connectexecutes asynchronously likeshare. -
executeAfter- Executes a call-back function after a promise is resolved. The value that the promise resolves into is supplied as input to the call-back function. This function must be used to deal with the returned promises ofconnectandshare. Using promises as normal values in a program withoutexecuteAftercan lead to undefined behaviour.- Parameter:
promise- A promise that resolves into some data or function. - Parameter:
call-back- A call-back function that executes after the promise is resolved. The resolved value is supplied as argument to this function. - Returns: The return value of the call-back, if any. Using this return value synchronously can lead to undefined behaviour.
- Parameter:
This system requires an understanding and familiarity with a broad range of topics. The following is a non-exhaustive but comprehensive list of topics and resources that should help a developer get started:
Since this project is written in Typescript, familiarity with Typescript/Javascript (JS) is required.
| Topic | Reference Resource |
|---|---|
| Source Academy Modules system | [https://github.com/source-academy/modules/wiki](Modules wiki) |
| Remote Procedure Calls | [https://en.wikipedia.org/wiki/Remote_procedure_call](Wikipedia Article) |
| How Javascript is executed by the browser | How Javascript works in the browser |
| Asynchronous and Concurrent program execution | Video by Hussein Nasser |
| Javascript Event Loop | The JavaScript Event Loop explained |
| Firestore Realtime Database | [https://firebase.google.com/docs/firestore/quickstart](Quickstart Guide) |
| JavaScript Promises and async-await | [https://javascript.info/async](Series of articles) |
- Home
- Overview
- System Implementation
-
Development Guide
- Getting Started
- Repository Structure
-
Creating a New Module
- Creating a Bundle
- Creating a Tab
- Writing Documentation
- Developer Documentation (TODO)
- Build System
- Source Modules
- FAQs
Try out Source Academy here.
Check out the Source Modules generated API documentation here.
