"Fossies" - the Fresh Open Source Software Archive

Member "ember.js-3.15.0/packages/@ember/-internals/metal/lib/array_events.ts" (10 Dec 2019, 2549 Bytes) of package /linux/www/ember.js-3.15.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 import { peekMeta } from '@ember/-internals/meta';
    2 import { EMBER_METAL_TRACKED_PROPERTIES } from '@ember/canary-features';
    3 import { peekCacheFor } from './computed_cache';
    4 import { eachProxyArrayDidChange, eachProxyArrayWillChange } from './each_proxy_events';
    5 import { sendEvent } from './events';
    6 import { notifyPropertyChange } from './property_events';
    7 
    8 export function arrayContentWillChange<T extends object>(
    9   array: T,
   10   startIdx: number,
   11   removeAmt: number,
   12   addAmt: number
   13 ): T {
   14   // if no args are passed assume everything changes
   15   if (startIdx === undefined) {
   16     startIdx = 0;
   17     removeAmt = addAmt = -1;
   18   } else {
   19     if (removeAmt === undefined) {
   20       removeAmt = -1;
   21     }
   22 
   23     if (addAmt === undefined) {
   24       addAmt = -1;
   25     }
   26   }
   27 
   28   if (!EMBER_METAL_TRACKED_PROPERTIES) {
   29     eachProxyArrayWillChange(array, startIdx, removeAmt, addAmt);
   30   }
   31 
   32   sendEvent(array, '@array:before', [array, startIdx, removeAmt, addAmt]);
   33 
   34   return array;
   35 }
   36 
   37 export function arrayContentDidChange<T extends { length: number }>(
   38   array: T,
   39   startIdx: number,
   40   removeAmt: number,
   41   addAmt: number
   42 ): T {
   43   // if no args are passed assume everything changes
   44   if (startIdx === undefined) {
   45     startIdx = 0;
   46     removeAmt = addAmt = -1;
   47   } else {
   48     if (removeAmt === undefined) {
   49       removeAmt = -1;
   50     }
   51 
   52     if (addAmt === undefined) {
   53       addAmt = -1;
   54     }
   55   }
   56 
   57   let meta = peekMeta(array);
   58 
   59   if (addAmt < 0 || removeAmt < 0 || addAmt - removeAmt !== 0) {
   60     notifyPropertyChange(array, 'length', meta);
   61   }
   62 
   63   notifyPropertyChange(array, '[]', meta);
   64 
   65   if (!EMBER_METAL_TRACKED_PROPERTIES) {
   66     eachProxyArrayDidChange(array, startIdx, removeAmt, addAmt);
   67   }
   68 
   69   sendEvent(array, '@array:change', [array, startIdx, removeAmt, addAmt]);
   70 
   71   let cache = peekCacheFor(array);
   72   if (cache !== undefined) {
   73     let length = array.length;
   74     let addedAmount = addAmt === -1 ? 0 : addAmt;
   75     let removedAmount = removeAmt === -1 ? 0 : removeAmt;
   76     let delta = addedAmount - removedAmount;
   77     let previousLength = length - delta;
   78 
   79     let normalStartIdx = startIdx < 0 ? previousLength + startIdx : startIdx;
   80     if (cache.has('firstObject') && normalStartIdx === 0) {
   81       notifyPropertyChange(array, 'firstObject', meta);
   82     }
   83 
   84     if (cache.has('lastObject')) {
   85       let previousLastIndex = previousLength - 1;
   86       let lastAffectedIndex = normalStartIdx + removedAmount;
   87       if (previousLastIndex < lastAffectedIndex) {
   88         notifyPropertyChange(array, 'lastObject', meta);
   89       }
   90     }
   91   }
   92 
   93   return array;
   94 }