logo logo
Elm Multiview Source

Elm Multiview consists of two scripts: _elm.awk and _elm.sh.

_elm.awk

_elm.awk ↗︎ generates the logic described in the welcome which updates currentView and renders all the nodes.

BEGIN {
    count = 0;
}

/currentView = 0/ {
    views[count++] = $2;
}

END {
    print "  if (args && args['nodes']) {"
    print ""
    print "     //"
    print "     // when caller passes 'nodes' assume using multi-views"
    print "     //"
    print ""
    print "     return _Platform_initialize("
    print "        flagDecoder,"
    print "        args,"
    print "        impl.init,"
    print "        impl.update,"
    print "        impl.subscriptions,"
    print "  "
    print "        function(sendToApp, initialModel) {"
    print "           var view = impl.view;"
    print "  "
    print "           var domNodes = args && args['nodes'] ? args['nodes'] : _Debug_crash(0);"
    print "           var curNodes = domNodes.map(n => _VirtualDom_virtualize(n));"
    print "           var len      = curNodes.length;"
    print "  "
    print "           return _Browser_makeAnimator(initialModel, function(model)"
    print "           {"
    print "             for (var i = 0; i < len; i++) { //console.log('@@@', i);"
    print "               var domNode  = domNodes[i];"
    print "               var currNode = curNodes[i];"
    print ""
    print "               //"
    print "               // we don't know what module is running so"
    print "               // set 'currentView' in all of them"
    print "               //"
    print ""
    for (i = 0; i < count; i++) 
        print "               " views[i] " = i;"
    print "  "
    print "               var nextNode = view(model);"
    print "               var patches = _VirtualDom_diff(currNode, nextNode);"
    print "  "
    print "               domNodes[i] = _VirtualDom_applyPatches(domNode, currNode, patches, sendToApp);"
    print "               curNodes[i] = nextNode;"
    print "             }"
    print "           });"
    print "        }"
    print "     );"
    print "  }"
}

_elm.sh

_elm.sh ↗︎ injects the code generated by _elm.awk into the final output using ex.

The version in the repository includes superfluous details to deal with temporary files so only the relevent portion is shown below.

function edit() {
    awk -f _elm.awk $1 > elm-multiview.added
    ex $1 <<EOF 
/console.warn
d
/var _Browser_element
.+1
r elm-multiview.added
w
q
EOF
}