/**
  This function is called just after an object property has changed.
  It will notify any observers and clear caches among other things.

  Normally you will not need to call this method directly but if for some
  reason you can't directly watch a property you can invoke this method
  manually.

  @method notifyPropertyChange
  @for @ember/object
  @param {Object} obj The object with the property that will change
  @param {String} keyName The property key (or path) that will change.
  @param {Meta} meta The objects meta.
  @return {void}
  @since 3.1.0
  @public
*/
function notifyPropertyChange(obj: object, keyName: string, _meta?: Meta | null): void {
  let meta = _meta === undefined ? peekMeta(obj) : _meta;

  if (meta !== null && (meta.isInitializing() || meta.isPrototypeMeta(obj))) {
    return;
  }

  let possibleDesc = descriptorFor(obj, keyName, meta);

  if (possibleDesc !== undefined && typeof possibleDesc.didChange === 'function') {
    possibleDesc.didChange(obj, keyName);
  }

  if (meta !== null && meta.peekWatching(keyName) > 0) {
    dependentKeysDidChange(obj, keyName, meta);
    chainsDidChange(obj, keyName, meta);
    notifyObservers(obj, keyName, meta);
  }

  if (PROPERTY_DID_CHANGE in obj) {
    obj[PROPERTY_DID_CHANGE](keyName);
  }

  if (meta !== null) {
    if (meta.isSourceDestroying()) {
      return;
    }
    markObjectAsDirty(obj, keyName, meta);
  }

  if (DEBUG) {
    assertNotRendered(obj, keyName);
  }
}
Beispiel #2
0
  ACTIVE_OBSERVERS.forEach((activeObservers, target) => {
    let meta = peekMeta(target);

    if (meta && (meta.isSourceDestroying() || meta.isMetaDestroyed())) {
      ACTIVE_OBSERVERS.delete(target);
      return;
    }

    activeObservers.forEach((observer, eventName) => {
      if (!observer.tag.validate(observer.lastRevision)) {
        let sendObserver = () => {
          try {
            sendEvent(target, eventName, [target, observer.path]);
          } finally {
            observer.tag = getChainTagsForKey(target, observer.path);
            observer.lastRevision = observer.tag.value();
          }
        };

        if (shouldSchedule) {
          schedule('actions', sendObserver);
        } else {
          // TODO: we need to schedule eagerly in exactly one location (_internalReset),
          // for query params. We should get rid of this ASAP
          sendObserver();
        }
      }
    });
  });
Beispiel #3
0
export function hasListeners(obj: object, eventName: string): boolean {
  let meta = peekMeta(obj);
  if (meta === null) {
    return false;
  }
  let matched = meta.matchingListeners(eventName);
  return matched !== undefined && matched.length > 0;
}
Beispiel #4
0
export function arrayContentDidChange<T extends { length: number }>(
  array: T,
  startIdx: number,
  removeAmt: number,
  addAmt: number
): T {
  // if no args are passed assume everything changes
  if (startIdx === undefined) {
    startIdx = 0;
    removeAmt = addAmt = -1;
  } else {
    if (removeAmt === undefined) {
      removeAmt = -1;
    }

    if (addAmt === undefined) {
      addAmt = -1;
    }
  }

  let meta = peekMeta(array);

  if (addAmt < 0 || removeAmt < 0 || addAmt - removeAmt !== 0) {
    notifyPropertyChange(array, 'length', meta);
  }

  notifyPropertyChange(array, '[]', meta);

  if (!EMBER_METAL_TRACKED_PROPERTIES) {
    eachProxyArrayDidChange(array, startIdx, removeAmt, addAmt);
  }

  sendEvent(array, '@array:change', [array, startIdx, removeAmt, addAmt]);

  let cache = peekCacheFor(array);
  if (cache !== undefined) {
    let length = array.length;
    let addedAmount = addAmt === -1 ? 0 : addAmt;
    let removedAmount = removeAmt === -1 ? 0 : removeAmt;
    let delta = addedAmount - removedAmount;
    let previousLength = length - delta;

    let normalStartIdx = startIdx < 0 ? previousLength + startIdx : startIdx;
    if (cache.has('firstObject') && normalStartIdx === 0) {
      notifyPropertyChange(array, 'firstObject', meta);
    }

    if (cache.has('lastObject')) {
      let previousLastIndex = previousLength - 1;
      let lastAffectedIndex = normalStartIdx + removedAmount;
      if (previousLastIndex < lastAffectedIndex) {
        notifyPropertyChange(array, 'lastObject', meta);
      }
    }
  }

  return array;
}
Beispiel #5
0
 function SETTER_FUNCTION(this: object, value: any | undefined | null): void {
   let m = peekMeta(this)!;
   if (m.isInitializing() || m.isPrototypeMeta(this)) {
     m.writeValues(name, value);
   } else {
     assert(
       `You must use set() to set the \`${name}\` property (of ${this}) to \`${value}\`.`,
       false
     );
   }
 }
Beispiel #6
0
 /**
   @method detect
   @param obj
   @return {Boolean}
   @private
 */
 detect(obj: any): boolean {
   if (typeof obj !== 'object' || obj === null) {
     return false;
   }
   if (obj instanceof Mixin) {
     return _detect(obj, this);
   }
   let meta = peekMeta(obj);
   if (meta === undefined) {
     return false;
   }
   return meta.hasMixin(this);
 }
Beispiel #7
0
  function IGETTER_FUNCTION(this: any): void {
    let meta = peekMeta(this);
    let val;
    if (meta !== null) {
      val = meta.readInheritedValue('values', name);
      if (val === UNDEFINED) {
        let proto = Object.getPrototypeOf(this);
        return proto === null ? undefined : proto[name];
      }
    }

    return val;
  }
Beispiel #8
0
 arrayDidChange(content: T[] | EmberArray<T>, idx: number, _removedCnt: number, addedCnt: number) {
   let keys = this._keys;
   if (!keys) {
     return;
   }
   let lim = addedCnt > 0 ? idx + addedCnt : -1;
   let meta = peekMeta(this);
   for (let key in keys) {
     if (lim > 0) {
       addObserverForContentKey(content, key, this, idx, lim);
     }
     notifyPropertyChange(this, key, meta);
   }
 }
Beispiel #9
0
export function unwatchPath(obj: any, keyPath: string, meta?: Meta): void {
  let m = meta === undefined ? peekMeta(obj) : meta;
  if (m === null) {
    return;
  }
  let counter = m.peekWatching(keyPath);

  if (counter > 0) {
    m.writeWatching(keyPath, counter - 1);
    if (counter === 1) {
      m.writableChains(makeChainNode).remove(keyPath);
    }
  }
}
Beispiel #10
0
export function descriptorForProperty(obj: object, keyName: string, _meta?: Meta | null) {
  assert('Cannot call `descriptorFor` on null', obj !== null);
  assert('Cannot call `descriptorFor` on undefined', obj !== undefined);
  assert(
    `Cannot call \`descriptorFor\` on ${typeof obj}`,
    typeof obj === 'object' || typeof obj === 'function'
  );

  let meta = _meta === undefined ? peekMeta(obj) : _meta;

  if (meta !== null) {
    return meta.peekDescriptors(keyName);
  }
}