|
| 1 | += Actors |
| 2 | + |
| 3 | +== Internet Computer |
| 4 | + |
| 5 | +- Canisters are memory isolated |
| 6 | +- Canisters can communicate with asynchronous messages |
| 7 | +- Canisters messenge candid values, the values are binary encoded |
| 8 | +- Canisters only process one message at a time, others wait |
| 9 | +- Canisters use callbacks to register code that needs to be done on given |
| 10 | +message processing |
| 11 | + |
| 12 | +== Motoko |
| 13 | + |
| 14 | +=== General |
| 15 | + |
| 16 | +- Actors are an abstraction/representation for canisters |
| 17 | +- The type of the actor is its functions |
| 18 | +- Its asynchronous functions indicate which messanges can be handled |
| 19 | +- Types of an actor and its functions can be translated to candid |
| 20 | +- The candid types define how the binary data passed is structured |
| 21 | + |
| 22 | +=== Actors vs Objects |
| 23 | + |
| 24 | +We can think of actors like sorts of objects. But there are notable differences: |
| 25 | + |
| 26 | +- Actors come with completely isolated state |
| 27 | +- Any interactivity with the outside goes through asynchronous messanges |
| 28 | +- Messages are processed one-at-a-time, no matter if issued in parallel |
| 29 | + |
| 30 | +=== Motoko Implementation |
| 31 | + |
| 32 | +- Message sending in Motoko means calling an async function of an actor |
| 33 | +- There is no blocking in the wait for the response, istead there is a |
| 34 | +promise/future, that is return to the caller by the callee, that resolves, once |
| 35 | +the message is handled |
| 36 | +- The promise/future is a placeholder, which can result in a result |
| 37 | +- Because there is no blocking in the time waiting for the promise to resolve, |
| 38 | +the caller can do anything, send other requests to any actor or whatever |
| 39 | +- The caller can wait for the result, when it resolves it will be available, |
| 40 | +otherwise its resolved value is stored for future access |
| 41 | + |
| 42 | +=== Shared functions and state |
| 43 | + |
| 44 | +- Actor messanging functions are called _shared_-functions, since they can be |
| 45 | +accessed from remote code, actors, etc. |
| 46 | +- Shared functions return futures/promises that resolve once handled by the |
| 47 | +called actor |
| 48 | +- The data transmitted is immutable, we call it _shared_-state |
| 49 | + |
| 50 | +[source,motko] |
| 51 | +---- |
| 52 | +actor { |
| 53 | + public shared func someFuncName() : async Text { |
| 54 | + return "I am some func returning some string" |
| 55 | + } |
| 56 | +} |
| 57 | +---- |
| 58 | + |
| 59 | +Since all functions of an actor have to be shared, we can obmit the `shared` |
| 60 | +keyword: |
| 61 | + |
| 62 | +[source,motko] |
| 63 | +---- |
| 64 | +actor { |
| 65 | + public shared func someFuncName() : async Text { |
| 66 | + return "I am some func returning some string" |
| 67 | + } |
| 68 | +} |
| 69 | +---- |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +// Questios: |
| 74 | + |
| 75 | +- Can multiple messanges we processed by one actor at the same time so the |
| 76 | +one-action-per-time only applies to one messange handler |
| 77 | + - Or does it apply to the whole actor and therfore only one messange can be |
| 78 | + called at one time |
0 commit comments