 M: model hashcode* drop model hashcode* ;
 
 : add-dependency ( dep model -- )
-    model-dependencies push ;
+    dependencies>> push ;
 
 : remove-dependency ( dep model -- )
-    model-dependencies delete ;
+    dependencies>> delete ;
 
 DEFER: add-connection
 

M: model model-activated drop ;

: ref-model ( model -- n )
    dup model-ref 1+ dup rot set-model-ref ;

: unref-model ( model -- n )
    dup model-ref 1- dup rot set-model-ref ;

: activate-model ( model -- )
    dup ref-model 1 = [
        dup model-dependencies
        [ dup activate-model dupd add-connection ] each
        model-activated
    ] [

GENERIC: model-changed ( model observer -- )

: add-connection ( observer model -- )
    dup connections>> empty? [ dup activate-model ] when
    connections>> push ;

: remove-connection ( observer model -- )
    [ connections>> delete ] keep
    dup connections>> empty? [ dup deactivate-model ] when
    drop ;

: with-locked-model ( model quot -- )
    swap
    t >>locked?
    slip
    f >>locked? drop ; inline

GENERIC: update-model ( model -- )

M: model update-model drop ;

: notify-connections ( model -- )
    dup connections>> [ model-changed ] with each ;

: set-model ( value model -- )
    dup locked?>> [
        2drop
    ] [
        dup [
            swap >>value
            [ update-model ] [ notify-connections ] bi
        ] with-locked-model
    ] if ;

: ((change-model)) ( model quot -- newvalue model )
    over >r >r value>> r> call r> ; inline

: change-model ( model quot -- )
    ((change-model)) set-model ; inline

: (change-model) ( model quot -- )
    ((change-model)) swap >>value drop ; inline

GENERIC: range-value ( model -- value )
GENERIC: range-page-value ( model -- value )
