{"id":15539114,"url":"https://github.com/patricklx/ember-cli-reduce-computed","last_synced_at":"2025-04-13T00:52:14.538Z","repository":{"id":25863137,"uuid":"29303022","full_name":"patricklx/ember-cli-reduce-computed","owner":"patricklx","description":null,"archived":false,"fork":false,"pushed_at":"2016-06-09T10:40:19.000Z","size":53,"stargazers_count":2,"open_issues_count":2,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-09T12:46:00.079Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/patricklx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-15T15:14:11.000Z","updated_at":"2015-07-29T07:12:33.000Z","dependencies_parsed_at":"2022-08-24T14:15:46.433Z","dependency_job_id":null,"html_url":"https://github.com/patricklx/ember-cli-reduce-computed","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patricklx%2Fember-cli-reduce-computed","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patricklx%2Fember-cli-reduce-computed/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patricklx%2Fember-cli-reduce-computed/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patricklx%2Fember-cli-reduce-computed/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/patricklx","download_url":"https://codeload.github.com/patricklx/ember-cli-reduce-computed/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248650435,"owners_count":21139672,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-02T12:09:11.863Z","updated_at":"2025-04-13T00:52:14.504Z","avatar_url":"https://github.com/patricklx.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ember-cli-reduce-computed\n\nthe ember reduced-computed and array-computed as addon\n\n##Addon Install\n`ember install:addon patricklx/ember-cli-reduce-computed`\n\nhowever the computed.* functions will still use the old implementation, \ntherefore you also need to install the addon [ember-cli-reduce-computed-macros](https://github.com/patricklx/ember-cli-reduce-computed-macros)\n\n\nthen in your app:\n\n`import install from 'ember-cli-reduce-computed';`\n`install()`\n\n\n## API difference\n- new callback: `propertyChanged`\n- item property changes are coalesced and applied asynchronously with `run.once`. \nThey are, however, applied immediately if `get` is called  for the reduceComputed property.\n- array changes are applied immediately.\n- if you need the index you need to specify `needIndex` in the `options`\n- if `undefined` is returned, the value will be recalculated on the next `get` \n\n## API Description\n  Creates a computed property which operates on dependent arrays and\n  is updated with \"one at a time\" semantics. When items are added or\n  removed from the dependent array(s) a reduce computed only operates\n  on the change instead of re-evaluating the entire array.\n  \n  If there are more than one arguments the first arguments are\n  considered to be dependent property keys. The last argument is\n  required to be an options object. The options object can have the\n  following four properties:\n  \n  `hasOwnInitialValue` - If this is set to true, the initialValue function is responsible for all preparations, e.g.: \n  adding items to the array, setting the initialValue. This can be used to optimize the calculation of the initial value.\n  \n  `initialValue` - A value or function that will be used as the initial\n  value for the computed. If this property is a function the result of calling\n  the function will be used as the initial value. This property is required.\n  \n  `initialize` - An optional initialize function. Typically this will be used\n  to set up state on the instanceMeta object.\n  \n  `removedItem` - A function that is called each time an element is removed\n  from the array.\n  \n  `addedItem` - A function that is called each time an element is added to\n  the array.\n  \n  `propertyChanged` - A function that is called each time an element's property changes, \n   if this function is not defined `removedItem` and `addedItem` will be called instead\n   \n   `flushedChanges` - A function that is called after all changes have been flushed. \n   It's called after items have been removed and/or added to the array and \n   after all itemProperty changes have been flushed.\n  \n  The `initialize` function has the following signature:\n  \n  ```javascript\n  function(initialValue, changeMeta, instanceMeta)\n  ```\n  \n  `initialValue` - The value of the `initialValue` property from the\n  options object.\n  \n  `changeMeta` - An object which contains meta information about the\n  computed. It contains the following properties:\n  \n   - `property` the computed property\n   - `propertyName` the name of the property on the object\n     \n  `instanceMeta` - An object that can be used to store meta\n  information needed for calculating your computed. For example a\n  unique computed might use this to store the number of times a given\n  element is found in the dependent array.\n  \n  The `removedItem` and `addedItem` functions both have the following signature:\n  \n  ```javascript\n  function(accumulatedValue, item, changeMeta, instanceMeta)\n  ```\n  \n  `accumulatedValue` - The value returned from the last time\n  `removedItem` or `addedItem` was called or `initialValue`.\n  `item` - the element added or removed from the array\n  `changeMeta` - An object which contains meta information about the\n  change. It contains the following properties:\n  - `property` the computed property\n  - `propertyName` the name of the property on the object\n  - `index` the index of the added or removed item\n  - `item` the added or removed item: this is exactly the same as\n      the second arg\n  - `arrayChanged` the array that triggered the change. Can be\n      useful when depending on multiple arrays.\n      \n  For property changes triggered on an item property change (when\n  depKey is something like `someArray.@each.someProperty`),\n  `changeMeta` will also contain the following property:\n  \n  `instanceMeta` - An object that can be used to store meta\n  information needed for calculating your computed. For example a\n  unique computed might use this to store the number of times a given\n  element is found in the dependent array.\n  \n  The `removedItem` and `addedItem` functions should return the accumulated\n  value. It is acceptable to not return anything (ie return undefined)\n  to invalidate the computation. This is generally not a good idea for\n  arrayComputed but it's used in eg max and min.\n  \n  Note that observers will be fired if either of these functions return a value\n  that differs from the accumulated value.  When returning an object that\n  mutates in response to array changes, for example an array that maps\n  everything from some other array (see `Ember.computed.map`), it is usually\n  important that the *same* array be returned to avoid accidentally triggering observers.\n  \n  Example\n  ```javascript\n  Ember.computed.max = function(dependentKey) {\n    return Ember.reduceComputed(dependentKey, {\n      initialValue: -Infinity,\n      addedItem: function(accumulatedValue, item, changeMeta, instanceMeta) {\n        return Math.max(accumulatedValue, item);\n      },\n      removedItem: function(accumulatedValue, item, changeMeta, instanceMeta) {\n        if (item \u003c accumulatedValue) {\n          return accumulatedValue;\n        }\n      }\n    });\n  };\n  ```\n  \n  Dependent keys may refer to `@this` to observe changes to the object itself,\n  which must be array-like, rather than a property of the object.  This is\n  mostly useful for array proxies, to ensure objects are retrieved via\n  `objectAtContent`.  This is how you could sort items by properties defined on an item controller.\n  \n  Example\n  ```javascript\n  App.PeopleController = Ember.ArrayController.extend({\n    itemController: 'person',\n    sortedPeople: Ember.computed.sort('@this.@each.reversedName', function(personA, personB) {\n      // `reversedName` isn't defined on Person, but we have access to it via\n      // the item controller App.PersonController.  If we'd used\n      // `content.@each.reversedName` above, we would be getting the objects\n      // directly and not have access to `reversedName`.\n      //\n      var reversedNameA = get(personA, 'reversedName');\n      var reversedNameB = get(personB, 'reversedName');\n      return Ember.compare(reversedNameA, reversedNameB);\n    })\n  });\n  App.PersonController = Ember.ObjectController.extend({\n    reversedName: function() {\n      return reverse(get(this, 'name'));\n    }.property('name')\n  });\n  ```\n  \n  Dependent keys whose values are not arrays are treated as regular\n  dependencies: when they change, the computed property is completely\n  recalculated.  It is sometimes useful to have dependent arrays with similar\n  semantics.  Dependent keys which end in `.[]` do not use \"one at a time\"\n  semantics.  When an item is added or removed from such a dependency, the\n  computed property is completely recomputed.\n  \n  When the computed property is completely recomputed, the `accumulatedValue`\n  is discarded, it starts with `initialValue` again, and each item is passed\n  to `addedItem` in turn.\n  \n  Example\n  ```javascript\n  Ember.Object.extend({\n    // When `string` is changed, `computed` is completely recomputed.\n    string: 'a string',\n    // When an item is added to `array`, `addedItem` is called.\n    array: [],\n    // When an item is added to `anotherArray`, `computed` is completely\n    // recomputed.\n    anotherArray: [],\n    computed: Ember.reduceComputed('string', 'array', 'anotherArray.[]', {\n      addedItem: addedItemCallback,\n      removedItem: removedItemCallback\n    })\n  });\n  ```\n\n\n## Installation\n\n* `git clone` this repository\n* `npm install`\n* `bower install`\n\n## Running\n\n* `ember server`\n* Visit your app at http://localhost:4200.\n\n## Running Tests\n\n* `ember test`\n* `ember test --server`\n\n## Building\n\n* `ember build`\n\nFor more information on using ember-cli, visit [http://www.ember-cli.com/](http://www.ember-cli.com/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatricklx%2Fember-cli-reduce-computed","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpatricklx%2Fember-cli-reduce-computed","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatricklx%2Fember-cli-reduce-computed/lists"}