1

How can I show information from external processes in N2O framefork.

Code like this:

tokyoWeather() ->
     timer:sleep(10000),
     Data = getTokyoWeater(),
     ??? SHOW_ON_PAGES,
     tokyoWeather().
lisbonWeather() ->
     timer:sleep(10000),
     Data = getLisbonWeater(),
     ??? SHOW_ON_PAGES,
     lisbonWeather().

Page1:

body() ->
    [ #panel { id=TokyoWeather text= <<>> },
      #panel { id=LisbonWeather, text= <<>> },
      ...].

Page2:

body() ->
    [ #panel { id=TokyoWeather text= <<>> },
      #panel { id=LisbonWeather, text= <<>> },
      ...].

Thanks in advance.

John Davis
  • 11
  • 3

2 Answers2

2

Use wf:reg and wf:flush functions. Example: /samples/apps/review/src/index.erl

  1. Register unique name for a page-process:

    23: event(init) -> wf:reg(room)...

  2. Updating DOM:

    60: wf:insert_bottom(history, Terms)

  3. Flushing changes to browser using page-process name:

    62: wf:flush(room)

m2k
  • 21
  • 5
1

Documentation suggests using wf:reg/1 and wf:flush. However, this requires you to register process into a pool, which is handy for when you want to manipulate multiple processes at once, but quite redundant otherwise.

Another interesting function the N2O book mentions is wf:flush/1. Its description claims that it does something completely different from wf:flush/0, while in reality it does not. It really just collects the wf actions placed into a "buffer" and sends them via wf:send/2 to a registered supposedly websocket processes. Meanwhile the websocket process sits in a timed receive loop.

After some N2O's code inspection, there is a pretty simple solution, in my opinion:

  1. Starting at process that can render actions(e.g. the websocket process), spawn your desired function and pass the parent process id as one of its parameters

    async_render() ->
      Pid = self(),
      spawn(fun() -> update(Pid) end).
    
  2. Perform your wf actions in now asynchronous function, then retrieve the queued actions, clear the buffer, and finally send the a tuple of {flush, Actions} to your parent process.

    update(Pid) ->
      %% Your wf:insert..., update, etc. code here
      Actions = wf:actions(),
      wf:actions([]),
      Pid ! {flush, Actions}.
    

The rest is done by N2O for you :)

Pavel Kozlovsky
  • 123
  • 1
  • 8