All files solid.js

100% Statements 208/208
94.94% Branches 150/158
100% Functions 21/21
100% Lines 175/175

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283    3x   3x   3x   3x 3x   134x   430x       220x 194x   63x 10x 10x 34x 34x     53x 53x 53x 86x 86x     63x       56x 56x 56x     3x   202x 176x 174x 174x 174x   6x 2x 2x   6x 6x 6x   174x     1x   1x       50x 50x 48x 48x 2x 46x 48x 48x 48x       7x 7x 7x 7x         47x 12x 12x 8x   8x   5x 5x     35x 35x 35x   35x   1x 2x   34x   1x 5x   33x   2x 2x 7x   31x   1x 5x   30x 24x 24x 17x 17x   24x 2x 22x 6x       24x 24x     24x 24x 24x 1x 2x   23x       24x       34x 34x 19x 9x 9x     10x 5x   4x 4x   4x 4x   4x 4x     4x 4x 5x 5x 5x 5x 5x     4x 5x 5x 5x 5x 4x 4x 4x       4x 9x 8x 8x   1x     1x 3x     5x 5x     5x 5x 12x   5x 5x 14x             7x 1x 6x 1x 1x   5x   5x   7x 7x 7x 7x 6x 6x 1x         12x   12x 4x 4x 4x 3x 3x 1x   2x 2x 2x     8x 12x     3x     9x     3x   3x 3x 3x 3x 3x 3x 3x 3x 3x 3x  
'use strict';
 
Object.defineProperty(exports, '__esModule', { value: true });
 
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
 
var S = _interopDefault(require('s-js'));
 
const SNODE = Symbol('solid-node'),
  SPROXY = Symbol('solid-proxy');
 
function wrap(value) { return value[SPROXY] || (value[SPROXY] = new Proxy(value, proxyTraps)); }
 
function isWrappable(obj) { return obj !== null && typeof obj === 'object'; }
 
function unwrap(item) {
  let result, unwrapped, v;
  if ((result = (item != null) && item._state)) return result;
  if (!isWrappable(item)) return item;
 
  if (Array.isArray(item)) {
    if (Object.isFrozen(item)) item = item.slice(0);
    for (let i = 0, l = item.length; i < l; i++) {
      v = item[i];
      if ((unwrapped = unwrap(v)) !== v) item[i] = unwrapped;
    }
  } else {
    if (Object.isFrozen(item)) item = Object.assign({}, item);
    let keys = Object.keys(item);
    for (let i = 0, l = keys.length; i < l; i++) {
      v = item[keys[i]];
      if ((unwrapped = unwrap(v)) !== v) item[keys[i]] = unwrapped;
    }
  }
  return item;
}
 
function getDataNodes(target) {
  let nodes = target[SNODE];
  if (!nodes) target[SNODE] = nodes = {};
  return nodes;
}
 
const proxyTraps = {
  get(target, property) {
    if (property === '_state') return target;
    if (property === SPROXY || property === SNODE) return;
    const value = target[property],
      wrappable = isWrappable(value);
    if (S.isListening() && typeof value !== 'function') {
      let nodes, node;
      if (wrappable && (nodes = getDataNodes(value))) {
        node = nodes._self || (nodes._self = S.makeDataNode());
        node.current();
      }
      nodes = getDataNodes(target);
      node = nodes[property] || (nodes[property] = S.makeDataNode());
      node.current();
    }
    return wrappable ? wrap(value) : value;
  },
 
  set() { return true; },
 
  deleteProperty() { return true; }
};
 
function setProperty(state, property, value) {
  value = unwrap(value);
  if (state[property] === value) return;
  const notify = Array.isArray(state) || !(property in state);
  if (value === void 0) {
    delete state[property];
  } else state[property] = value;
  let nodes = getDataNodes(state), node;
  (node = nodes[property]) && node.next();
  notify && (node = nodes._self) && node.next();
}
 
function mergeState(state, value) {
  const keys = Object.keys(value) || [];
  for (let i = 0; i < keys.length; i += 1) {
    const key = keys[i];
    setProperty(state, key, value[key]);
  }
}
 
function updatePath(current, path, traversed = []) {
  if (path.length === 1) {
    let value = path[0];
    if (typeof value === 'function') {
      value = value(wrap(current), traversed);
      // reconciled
      if (value === undefined) return;
    }
    mergeState(current, value);
    return;
  }
 
  const part = path.shift(),
    partType = typeof part,
    isArray = Array.isArray(current);
 
  if (Array.isArray(part)) {
    // Ex. update('data', [2, 23], 'label', l => l + ' !!!');
    for (let i = 0; i < part.length; i++) {
      updatePath(current, [part[i]].concat(path), traversed.concat([part[i]]));
    }
  } else if (isArray && partType === 'function') {
    // Ex. update('data', i => i.id === 42, 'label', l => l + ' !!!');
    for (let i = 0; i < current.length; i++) {
      if (part(current[i], i)) updatePath(current, [i].concat(path), traversed.concat([i]));
    }
  } else if (isArray && partType === 'object') {
    // Ex. update('data', { from: 3, to: 12, by: 2 }, 'label', l => l + ' !!!');
    const {from = 0, to = current.length - 1, by = 1} = part;
    for (let i = from; i <= to; i += by) {
      updatePath(current, [i].concat(path), traversed.concat([i]));
    }
  } else if (isArray && part === '*') {
    // Ex. update('data', '*', 'label', l => l + ' !!!');
    for (let i = 0; i < current.length; i++) {
      updatePath(current, [i].concat(path), traversed.concat([i]));
    }
  } else if (path.length === 1) {
    let value = path[0];
    if (typeof value === 'function') {
      const currentPart = current[part];
      value = value(isWrappable(currentPart) ? wrap(currentPart) : currentPart, traversed.concat([part]));
    }
    if (isWrappable(current[part]) && isWrappable(value) && !Array.isArray(value)) {
      mergeState(current[part], value);
    } else setProperty(current, part, value);
  } else updatePath(current[part], path, traversed.concat([part]));
}
 
function createState(state = {}) {
  state = unwrap(state);
  const wrappedState = wrap(state);
 
  function setState() {
    const args = arguments;
    S.freeze(() => {
      if (Array.isArray(args[0])) {
        for (let i = 0; i < args.length; i += 1) {
          updatePath(state, args[i]);
        }
      } else updatePath(state, Array.prototype.slice.call(args));
    });
  }
 
  return [wrappedState, setState];
}
 
function applyState(target, parent, property, merge, key) {
  let previous = parent[property];
  if (target === previous) return;
  if (!isWrappable(target) || (previous == null)) {
    (target !== previous) && setProperty(parent, property, target);
    return;
  }
 
  if (Array.isArray(target)) {
    if (target.length && previous.length && (!merge || (key && target[0][key] != null))) {
      let i, j, start, end, newEnd, item, newIndicesNext, keyVal,
        temp = new Array(target.length),
        newIndices = new Map();
      // skip common prefix and suffix
      for (start = 0, end = Math.min(previous.length, target.length); start < end && (previous[start] === target[start] || key && previous[start][key] === target[start][key]); start++) {
        applyState(target[start], previous, start, merge, key);
      }
      for (end = previous.length - 1, newEnd = target.length - 1; end >= 0 && newEnd >= 0 && (previous[end] === target[newEnd] || key && previous[end][key] === target[newEnd][key]); end--, newEnd--) {
        temp[newEnd] = previous[end];
      }
      // prepare a map of all indices in target
      newIndicesNext = new Array(newEnd + 1);
      for (j = newEnd; j >= start; j--) {
        item = target[j];
        keyVal = key ? item[key] : item;
        i = newIndices.get(keyVal);
        newIndicesNext[j] = i === undefined ? -1 : i;
        newIndices.set(keyVal, j);
      }
      // step through all old items to check reuse
      for (i = start; i <= end; i++) {
        item = previous[i];
        keyVal = key ? item[key] : item;
        j = newIndices.get(keyVal);
        if (j !== undefined && j !== -1) {
          temp[j] = previous[i];
          j = newIndicesNext[j];
          newIndices.set(keyVal, j);
        }
      }
      // set all the new values
      for (j = start; j < target.length; j++) {
        if (temp.hasOwnProperty(j)) {
          setProperty(previous, j, temp[j]);
          applyState(target[j], previous, j, merge, key);
        }
        else setProperty(previous, j, target[j]);
      }
    } else {
      for (let i = 0, len = target.length; i < len; i++) {
        applyState(target[i], previous, i, merge, key);
      }
    }
    if (previous.length > target.length) setProperty(previous, 'length', target.length);
    return;
  }
 
  const targetKeys = Object.keys(target);
  for (let i = 0, len = targetKeys.length; i < len; i++) {
    applyState(target[targetKeys[i]], previous, targetKeys[i], merge, key);
  }
  const previousKeys = Object.keys(previous);
  for (let i = 0, len = previousKeys.length; i < len; i++) {
    if (target[previousKeys[i]] === undefined) setProperty(previous, previousKeys[i], undefined);
  }
}
 
// Diff method for setState
function reconcile(path, options = {}) {
  let value;
  if (Array.isArray(path)) {
    value = path.pop();
  } else if (typeof path === 'object') {
    value = path;
    path = undefined;
  } else {
    path = Array.prototype.slice.call(arguments, 0, -1),
    value = arguments[arguments.length - 1];
    options = {};
  }
  const { merge, key = 'id' } = options;
  return state => {
    state = unwrap(state);
    if (path) {
      for (let i = 0; i < path.length - 1; i += 1) state = state[path[i]];
      applyState(value, state, path[path.length - 1], merge, key);
    } else applyState(value, { state }, 'state', merge, key);
  };
}
 
function createSignal(value, comparator) {
  const d = S.makeDataNode(value);
  let setter;
  if (comparator) {
    let age = -1;
    setter = v => {
      if (!comparator(value, v)) {
        const time = d.clock().time();
        if (time === age) {
          throw new Error(`Conflicting value update: ${v} is not the same as ${value}`);
        }
        age = time;
        value = v;
        d.next(v);
      }
    };
  } else setter = d.next.bind(d);
  return [d.current.bind(d), setter];
}
 
function createMemo(fn, seed) { return S(fn, seed); }
 
function createEffect(fn, deps, defer) {
  deps ? S.on(deps, fn, undefined, defer) : S.makeComputationNode(fn);
}
 
const { root: createRoot, cleanup: onCleanup, sample, freeze } = S;
 
exports.createRoot = createRoot;
exports.onCleanup = onCleanup;
exports.sample = sample;
exports.freeze = freeze;
exports.createState = createState;
exports.unwrap = unwrap;
exports.reconcile = reconcile;
exports.createSignal = createSignal;
exports.createMemo = createMemo;
exports.createEffect = createEffect;