{"version":3,"names":["View","pushIfMissing","removeIfExisting","identity","constructor","items","downstream","enterCallbacks","exitCallbacks","layersByItem","Map","isActiveGroup","safeAdd","item","wasNewItem","console","log","forEach","enterCallback","ea","newItemFromUpstream","safeRemove","gotRemoved","exitCallback","destroyItemFromUpstream","now","arr","push","size","length","enter","callback","exit","layer","partialBehavior","has","set","Layer","refineObject","layerForItem","get","isGlobal","beGlobal","beNotGlobal"],"sources":["view.js"],"sourcesContent":["import { pushIfMissing, removeIfExisting, identity } from './utils.js';\n\n/**\n *\n * @class View\n * @classdesc This is the class representing a view.\n */\nexport default class View {\n  constructor() {\n    this.items = [];\n    this.downstream = [];\n    this.enterCallbacks = [];\n    this.exitCallbacks = [];\n\n    this.layersByItem = new Map();\n  }\n  get isActiveGroup() { return true; }\n  \n  safeAdd(item) {\n    var wasNewItem = pushIfMissing(this.items, item);\n    if(wasNewItem) {\n      console.log('added to selection', item);\n      this.enterCallbacks.forEach(function(enterCallback) { enterCallback(item); });\n      this.downstream.forEach(function(ea) { ea.newItemFromUpstream(item); });\n    }\n  }\n\n  safeRemove(item) {\n    var gotRemoved = removeIfExisting(this.items, item);\n    if(gotRemoved) {\n      console.log('removed from selection', item);\n      this.exitCallbacks.forEach(function(exitCallback) { exitCallback(item); });\n      this.downstream.forEach(function(ea) { ea.destroyItemFromUpstream(item); });\n    }\n  }\n\n  // Get persistent version of the current state of the Selection.\n  /**\n   * Returns an Array of the objects that are currently in the set. This Array does not update automatically.\n   * @function View#now\n   * @return {Array}\n   */\n  now() {\n    var arr = [];\n    this.items.forEach(function(item) {\n      arr.push(item);\n    });\n\n    return arr;\n  }\n\n  /**\n   * Returns the current number of objects the set contains.\n   * @function View#size\n   * @return {Number}\n   */\n  size() { return this.now().length; }\n\n  /**\n   * Takes a callback that consumes a single parameter. This callback is called whenever an object is added to the reactive set with that very object.\n   * @function View#enter\n   * @param {View~enterCallback} callback - this is executed everytime an object is added to the set\n   * @return {View} The callee of this method.\n   */\n  enter(callback) {\n    this.enterCallbacks.push(callback);\n    this.now().forEach(function(item) {  callback(item); });\n\n    return this;\n  }\n\n  /**\n   * Similar to \\texttt{enter}, but the callback is called everytime an object is removed from the set.\n   * @function View#exit\n   * @param {View~exitCallback} callback\n   * @return {View} The callee of this method.\n   */\n  exit(callback) {\n    this.exitCallbacks.push(callback);\n\n    return this;\n  }\n\n  /**\n   * Define partial behavior attached to each object while it is contained in the set.\n   * @function View#layer\n   * @param {Object} partialBehavior - the mixin to be applied.\n   * @returns {View} The callee of this method.\n   */\n  // TODO: is this currently limited to 1 layer per item-view combination?\n  layer(partialBehavior) {\n    var layersByItem = this.layersByItem;\n\n    this.enter(function(item) {\n      // lazy initialization\n      if(!layersByItem.has(item)) {\n        layersByItem.set(item, new Layer().refineObject(item, partialBehavior));\n      }\n\n      var layerForItem = layersByItem.get(item);\n      if(!layerForItem.isGlobal()) {\n        layerForItem.beGlobal();\n      }\n    });\n\n    this.exit(function(item) {\n      var layerForItem = layersByItem.get(item);\n      if(layerForItem && layerForItem.isGlobal()) {\n        layerForItem.beNotGlobal();\n      }\n    });\n\n    return this;\n  }\n}\n\n/**\n * This callback is call whenever an item is added to this {@link View}.\n * @callback View~enterCallback\n * @param {Object} item - the item that was just added to the {@link View}.\n */\n\n/**\n * This callback is call whenever an item is removed from this {@link View}.\n * @callback View~exitCallback\n * @param {Object} item - the item that was just removed from the {@link View}.\n */\n"],"mappings":";;;iDAOqBA,IAAI;EAAA;EAAA;IAAA;MAPhBC,aAAa,YAAbA,aAAa;MAAEC,gBAAgB,YAAhBA,gBAAgB;MAAEC,QAAQ,YAARA,QAAQ;IAAA;IAAA;MAAA;MAAA;MAAA;QAAA;UAAA;QAAA;QAAA;UAARA,yCAAQ;UAAA;QAAA;QAAA;QAAA;MAAA;MAAA;QAAA;UAAA;QAAA;QAAA;UAA1BD,iDAAgB;UAAA;QAAA;QAAA;QAAA;MAAA;MAAA;QAAA;UAAA;QAAA;QAAA;UAA/BD,8CAAa;UAAA;QAAA;QAA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