{"id":13527258,"url":"https://github.com/googlearchive/observe-js","last_synced_at":"2025-12-17T23:14:51.804Z","repository":{"id":4246914,"uuid":"5372136","full_name":"googlearchive/observe-js","owner":"googlearchive","description":"A library for observing Arrays, Objects and PathValues","archived":true,"fork":false,"pushed_at":"2015-11-12T23:10:07.000Z","size":2263,"stargazers_count":1353,"open_issues_count":33,"forks_count":118,"subscribers_count":86,"default_branch":"master","last_synced_at":"2024-05-20T06:45:08.399Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/googlearchive.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-08-10T17:16:05.000Z","updated_at":"2024-05-14T16:42:37.000Z","dependencies_parsed_at":"2022-08-06T15:16:31.657Z","dependency_job_id":null,"html_url":"https://github.com/googlearchive/observe-js","commit_stats":null,"previous_names":["polymer/observe-js"],"tags_count":40,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googlearchive%2Fobserve-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googlearchive%2Fobserve-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googlearchive%2Fobserve-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googlearchive%2Fobserve-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/googlearchive","download_url":"https://codeload.github.com/googlearchive/observe-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219871970,"owners_count":16554474,"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-08-01T06:01:44.294Z","updated_at":"2025-09-27T08:31:06.707Z","avatar_url":"https://github.com/googlearchive.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"[![Analytics](https://ga-beacon.appspot.com/UA-39334307-2/Polymer/observe-js/README)](https://github.com/igrigorik/ga-beacon)\n\n## Learn the tech\n\n### Why observe-js?\n\nobserve-js is a library for observing changes in JavaScript data. It exposes a high-level API and uses [Object.observe](https://github.com/arv/ecmascript-object-observe) if available, and otherwise performs dirty-checking. observe-js requires ECMAScript 5.\n\n### Observable\n\nobserve-js implements a set of observers (PathObserver, ArrayObserver, ObjectObserver, CompoundObserver, ObserverTransform) which all implement the Observable interface:\n\n```JavaScript\n{\n  // Begins observation. Value changes will be reported by invoking |changeFn| with\n  // |opt_receiver| as the target, if provided. Returns the initial value of the observation.\n  open: function(changeFn, opt_receiver) {},\n\n  // Report any changes now (does nothing if there are no changes to report).\n  deliver: function() {},\n\n  // If there are changes to report, ignore them. Returns the current value of the observation.\n  discardChanges: function() {},\n\n  // Ends observation. Frees resources and drops references to observed objects.\n  close: function() {}\n}\n```\n\n### PathObserver\n\nPathObserver observes a \"value-at-a-path\" from a given object:\n\n```JavaScript\nvar obj = { foo: { bar: 'baz' } };\nvar defaultValue = 42;\nvar observer = new PathObserver(obj, 'foo.bar', defaultValue);\nobserver.open(function(newValue, oldValue) {\n  // respond to obj.foo.bar having changed value.\n});\n```\n\nPathObserver will report a change whenever the value obtained by the corresponding path expression (e.g. `obj.foo.bar`) would return a different value.\n\nPathObserver also exposes a `setValue` method which attempts to update the underlying value. Setting the value does not affect notification state (in other words, a caller sets the value but does not `discardChanges`, the `changeFn` will be notified of the change).\n\n```JavaScript\nobserver.setValue('boo');\nassert(obj.foo.bar == 'boo');\n```\n\nNotes:\n * If the path is ever unreachable, the value is considered to be `undefined` (unless you pass an overriding `defaultValue` to `new PathObserver(...)` as shown in the above example).\n * If the path is empty (e.g. `''`), it is said to be the empty path and its value is its root object.\n * PathObservation respects values on the prototype chain\n\n### ArrayObserver\n\nArrayObserver observes the index-positions of an Array and reports changes as the minimal set of \"splices\" which would have had the same effect.\n\n```JavaScript\nvar arr = [0, 1, 2, 4];\nvar observer = new ArrayObserver(arr);\nobserver.open(function(splices) {\n  // respond to changes to the elements of arr.\n  splices.forEach(function(splice) {\n    splice.index; // the index position that the change occurred.\n    splice.removed; // an array of values representing the sequence of removed elements\n    splice.addedCount; // the number of elements which were inserted.\n  });\n});\n```\n\nArrayObserver also exposes a utility function: `applySplices`. The purpose of `applySplices` is to transform a copy of an old state of an array into a copy of its current state, given the current state and the splices reported from the ArrayObserver.\n\n```JavaScript\nAraryObserver.applySplices = function(previous, current, splices) { }\n```\n\n### ObjectObserver\n\nObjectObserver observes the set of own-properties of an object and their values.\n\n```JavaScript\nvar myObj = { id: 1, foo: 'bar' };\nvar observer = new ObjectObserver(myObj);\nobserver.open(function(added, removed, changed, getOldValueFn) {\n  // respond to changes to the obj.\n  Object.keys(added).forEach(function(property) {\n    property; // a property which has been been added to obj\n    added[property]; // its value\n  });\n  Object.keys(removed).forEach(function(property) {\n    property; // a property which has been been removed from obj\n    getOldValueFn(property); // its old value\n  });\n  Object.keys(changed).forEach(function(property) {\n    property; // a property on obj which has changed value.\n    changed[property]; // its value\n    getOldValueFn(property); // its old value\n  });\n});\n```\n\n### CompoundObserver\n\nCompoundObserver allows simultaneous observation of multiple paths and/or Observables. It reports any and all changes in to the provided `changeFn` callback.\n\n```JavaScript\nvar obj = {\n  a: 1,\n  b: 2,\n};\n\nvar otherObj = { c: 3 };\n\nvar observer = new CompoundObserver();\nobserver.addPath(obj, 'a');\nobserver.addObserver(new PathObserver(obj, 'b'));\nobserver.addPath(otherObj, 'c');\nvar logTemplate = 'The %sth value before \u0026 after:';\nobserver.open(function(newValues, oldValues) {\n  // Use for-in to iterate which values have changed.\n  for (var i in oldValues) {\n    console.log(logTemplate, i, oldValues[i], newValues[i]);\n  }\n});\n```\n\n\n### ObserverTransform\n\nObserverTransform is used to dynamically transform observed value(s).\n\n```JavaScript\nvar obj = { value: 10 };\nvar observer = new PathObserver(obj, 'value');\nfunction getValue(value) { return value * 2 };\nfunction setValue(value) { return value / 2 };\n\nvar transform = new ObserverTransform(observer, getValue, setValue);\n\n// returns 20.\ntransform.open(function(newValue, oldValue) {\n  console.log('new: ' + newValue + ', old: ' + oldValue);\n});\n\nobj.value = 20;\ntransform.deliver(); // 'new: 40, old: 20'\ntransform.setValue(4); // obj.value === 2;\n```\n\nObserverTransform can also be used to reduce a set of observed values to a single value:\n\n```JavaScript\nvar obj = { a: 1, b: 2, c: 3 };\nvar observer = new CompoundObserver();\nobserver.addPath(obj, 'a');\nobserver.addPath(obj, 'b');\nobserver.addPath(obj, 'c');\nvar transform = new ObserverTransform(observer, function(values) {\n  var value = 0;\n  for (var i = 0; i \u003c values.length; i++)\n    value += values[i]\n  return value;\n});\n\n// returns 6.\ntransform.open(function(newValue, oldValue) {\n  console.log('new: ' + newValue + ', old: ' + oldValue);\n});\n\nobj.a = 2;\nobj.c = 10;\ntransform.deliver(); // 'new: 14, old: 6'\n```\n\n### Path objects\n\nA path is an ECMAScript expression consisting only of identifiers (`myVal`), member accesses (`foo.bar`) and key lookup with literal values (`arr[0]` `obj['str-value'].bar.baz`).\n\n`Path.get('foo.bar.baz')` returns a Path object which represents the path. Path objects have the following API:\n\n```JavaScript\n{\n  // Returns the current value of the path from the provided object. If eval() is available,\n  // a compiled getter will be used for better performance. Like PathObserver above, undefined\n  // is returned unless you provide an overriding defaultValue.\n  getValueFrom: function(obj, defaultValue) { },\n\n  // Attempts to set the value of the path from the provided object. Returns true IFF the path\n  // was reachable and set.\n  setValueFrom: function(obj, newValue) { }\n}\n```\n\nPath objects are interned (e.g. `assert(Path.get('foo.bar.baz') === Path.get('foo.bar.baz'));`) and are used internally to avoid excessive parsing of path strings. Observers which take path strings as arguments will also accept Path objects.\n\n## About delivery of changes\n\nobserve-js is intended for use in environments which implement Object.observe, but it supports use in environments which do not.\n\nIf `Object.observe` is present, and observers have changes to report, their callbacks will be invoked at the end of the current turn (microtask). In a browser environment, this is generally at the end of an event.\n\nIf `Object.observe` is absent, `Platform.performMicrotaskCheckpoint()` must be called to trigger delivery of changes. If `Object.observe` is implemented, `Platform.performMicrotaskCheckpoint()` has no effect.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgooglearchive%2Fobserve-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgooglearchive%2Fobserve-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgooglearchive%2Fobserve-js/lists"}