# Code coverage report for lib/script/index.js

## Statements: 87.21% (75 / 86) Branches: 87.5% (21 / 24) Functions: 75.86% (22 / 29) Lines: 92.11% (70 / 76) Ignored: none

All files » lib/script/ » index.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    1941

    1
    291

    7

    20

    10

    24

    24

    23
    22
    22

    1

    23

    1
    215
    215
    77

    1

    159

    159

    76

    207

    1
    253
    253
    253
    253
    253

    207

    207

    207

    194
    186

    8

    8
    19

    8

    194

    187
    187

    1

    8
    23

    10

    1
    9

    28
    16

    12

    8

    8

    1
    9

    8

    8

    10
    10

    10
    7

    7

    8

    1

    1

    167
    167
    81
    8
    23
    13

    86

     var BindableReference = require("./ref");

    /**
     */

    function createClip (context) {
      return {
        context: context,
        get: function (path) {
          return this.context.get(path);
        },
        set: function (path, value) {
          return this.context.set(path, value);
        },
        bindTo: function (path, settable) {
          return new BindableReference(this, path, settable);
        },
        call: function (ctx, key, params) {

          var fn;

          if (!ctx) return;

          if (ctx.__isBindable) {
            fn = ctx.get(key);
            ctx = ctx;
          } else {
            fn = ctx[key];
          }

          if (fn) return fn.apply(ctx, params);
        }
      }
    }

    function wrapInRaf (cb, runner) {
      var updateable = { update: cb };
      return function () {
        runner.run(updateable);
      }
    }

    function boundScript (script) {

      var run = script.run, refs = script.refs;

      return {
        refs: refs,
        evaluate: function (context) {
          return run.call(createClip(context));
        },
        bind: function (runner, context, listener) {

          var clip = createClip(context),
          currentValue,
          locked = false;

          function now () {
            Iif (locked) return;
            locked = true;
            var oldValue = currentValue;
            listener(currentValue = run.call(clip), oldValue);
            locked = false;
          }

          var rafNow = wrapInRaf(now, runner);

          var dispose;

          if (!refs.length) return {
            dispose: function () {},
            now: now
          };

          if (refs.length === 1) {
            var dispose = context.bind(refs[0], rafNow).dispose;
          } else {

            var bindings = [];

            for (var i = refs.length; i--;) {
              bindings.push(context.bind(refs[i], rafNow));
            }

            dispose = function () {
              for (var i = bindings.length; i--;) bindings[i].dispose();
            }
          }

          return {
            dispose: dispose,
            now: function () {
              now();
              return this;
            }
          }
        }
      };
    };

    /**
     * scripts combined with strings. defined within attributes usually
     */

    function bufferedScript (values, runner) {

      var scripts = values.filter(function (value) {
        return typeof value !== "string";
      }).map(function (script) {
        return script;
      });

      function evaluate (clip) {
        return values.map(function (script) {

          if (typeof script === "string") {
            return script;
          }

          return script.run.call(clip);

        }).join("");
      }

      return {
        buffered: true,
        evaluate: function (context) {
          return evaluate(createClip(context));
        },
        bind: function (runner, context, listener) {

          var clip = createClip(context), bindings = [];

          function now () {
            listener(evaluate(clip));
          }

          var rafNow = wrapInRaf(now, runner);

          for (var i = scripts.length; i--;) {

            var script = scripts[i];
            Iif (!script.refs) continue;

            for (var j = script.refs.length; j--;) {
              var ref = script.refs[j];

              bindings.push(context.bind(ref, rafNow));
            }
          }

          return {
            now: now,
            dispose: function ()  {
              for (var i = bindings.length; i--;) bindings[i].dispose();
            }
          }
        }
      }
    }

    function staticScript (value, runner) {
      return {
        bind: function (context, listener) {
          return {
            now: function () {
              listener(value);
            },
            dispose: function () {

            }
          }
        }
      }
    }

    module.exports = function (value) {

      Iif (typeof value !== "object") return staticScript(value);
      if (value.length) {
        if (value.length === 1) return boundScript(value[0].value);
        return bufferedScript(value.map(function (v) {
          if (typeof v === "object") return v.value;
          return v;
        }));
      } else {
        return boundScript(value);
      }
    }