Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
benStre committed Nov 22, 2023
1 parent c857dff commit 399586c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/manual/01 Introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ When you compare this code with the [example code](./02%20Important%20DATEX%20Co
you can see how the DATEX concepts are adopted in JavaScript in a very straightforward way.

To learn more about DATEX pointers in JavaScript, check out the chapter [Pointers](./03%20Pointers.md).
In the chapter [Functional Programming](./09%20Functional%20Programming.md), you can read more about `always` and other transform functions.

### Pointer synhronisation
### Pointer synchronisation

Check out the chapter [Pointer Synchronisation](./04%20Pointer%20Synchronisation.md) to understand
how pointers are synchronized between endpoints.
Expand Down
41 changes: 41 additions & 0 deletions docs/manual/09 Functional Programming.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,47 @@ c.val = 20;
> [!NOTE]
> The `always` transform function must always be synchronous and must not return a Promise

### Caching `always` output values

Since `always` functions are always required to be pure functions, it is possible to
cache the result of a calculation with given input values and return it at a later point in time.

This can be particularly useful when
* a calculation is very time-consuming
* the `always` function is triggered very often but returns a limited set of output values

To enable output caching, set `cache` to `true` in the `always` options that are passed in as a second parameter:

```ts
const n = $$(0);

const fibN = always(() => {
console.log("calculating fibonacci nr " + n)
return fibonacciNr(n.val);
}, {cache: true});

// triggered each time n/fibN is updated
effect(() => console.log(`fibonacci nr ${n} = ${fibN}`))

n.val = 42;
// -> "calculating fibonacci nr 42"
// -> "fibonacci nr 42 = 267914296"

n.val = 1;
// -> "calculating fibonacci nr 1"
// -> "fibonacci nr 1 = 1"

n.val = 42;
// calculation is not triggered again, cached result is used
// -> "fibonacci nr 42 = 267914296"
```

> [!WARNING]
> This feature is still experimental.
> It is not guaranteed that caches will be correctly used in every scenario.
> There is currently no way to limit the cache size, which could lead to memory leaks.
### The `always` template function

Instead of providing a JavaScript callback function, you can also provide a DATEX Script as a template string to an `always` function:
Expand Down

0 comments on commit 399586c

Please sign in to comment.