Slava Pestov wrote:

> To be even more useful, the contents of the variable could be stored in a
> 'model' object. This way, other code could receive notifications when the
> model value changes using the existing framework, you could have a gadget
> that displays this value in some manner, etc.

I'm interested in two-way file/model links. If the file is changed, the model 
is updated. If the model is changed, the file is updated.

Let's do it.

----------------------------------------------------------------------

! Create a model

0 <model>

  ! Setup a link which updates the model when the file changes

  "/tmp/mod" over '[ "/tmp/mod" file->val , maybe-set ] on-modify

  ! Setup a link which updates the file when the model changes

  [ value>> "/tmp/mod" utf8 [ . ] with-file-writer ] over add-connection

----------------------------------------------------------------------

If I do:

        $ cat 10 > /tmp/mod

The model sitting on the stack in my listener changes.

If I do:

        20 over set-model

I can then check the file:

        ~$ cat /tmp/mod
        20

I use 'maybe-set' in the file->model link. This is used to prevent a loop: the 
model changes, so the file changes, so the model changes... 
Here's 'maybe-set':

        : maybe-set ( val mod -- ) dup value>> pick = [ 2drop ] [ set-model ] 
if ;

I wrote about the 'on-modify' word earlier today.

Finally, I added a 'model-changed' method for quotations:

        M: quotation model-changed call ;

Ed

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to