{"id":18901076,"url":"https://github.com/regular/ssb-review-reduce","last_synced_at":"2026-03-03T18:30:25.917Z","repository":{"id":57368813,"uuid":"150729498","full_name":"regular/ssb-review-reduce","owner":"regular","description":null,"archived":false,"fork":false,"pushed_at":"2019-09-02T15:24:25.000Z","size":65,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-10T09:37:42.444Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/regular.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-28T11:18:23.000Z","updated_at":"2019-09-02T15:24:23.000Z","dependencies_parsed_at":"2022-09-12T03:01:29.764Z","dependency_job_id":null,"html_url":"https://github.com/regular/ssb-review-reduce","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/regular%2Fssb-review-reduce","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/regular%2Fssb-review-reduce/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/regular%2Fssb-review-reduce/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/regular%2Fssb-review-reduce/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/regular","download_url":"https://codeload.github.com/regular/ssb-review-reduce/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239886003,"owners_count":19713409,"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-11-08T08:55:07.029Z","updated_at":"2026-03-03T18:30:25.850Z","avatar_url":"https://github.com/regular.png","language":"JavaScript","readme":"# ssb-review-reduce\n\nA view into a reduce function for [ssb-revisions](https://github.com/regular/ssb-revisions).\nThis is more or less a drop-in replacement for flumeview-reduce for scuttlebutt applications that need mutable documents.\n\nSee also: [ssb-review-level](https://github.com/regular/ssb-review-level)\n\n## What's different? (in respect to flumeview-reduce)\n\n- In case a message is a revision of a prior message (e.g. it has revisionRoot and revisionBranch properties), your map function is called twice: once for the old value, once for the new value. (your map function typically does not care whether it is called for the old or new value. However, if it does, this information is provided in the third argument: true for new, false for old).\n\n- These two values are then both provided to your reduce function, along with the flumelog sequence numbers of the messages representing the old and new vlues: \n\n``` js\nreduce(acumulator, new_value, new_seq, old_value, old_seq)\n```\n\n(your reduce functon typically does not care about the sequence numbers)\n\n- in streaming mode, tuples (arrays of length two) of `[new_value, old_value]` are emitted, where `old_value==null` for original messages.\n\n## Example\n\n``` js\nssb.revisions.use('view', Reduce(\n  1, // version\n  function reduce(acc, item, seq, old_item, old_seq) {\n    return {\n      sum: acc.sum + item - (old_item ? old_item : 0),\n      squareSum: acc.squareSum + item*item - (old_item ? old_item * old_item : 0)\n    }\n  },\n  function map(kv, seq, is_new) {\n    return kv.value.content.value\n  },\n  null, { sum: 0, squareSum: 0 } // codec, initial state\n))\n\nssb.publish({type: 'number', value: 10}, function(err) {\n  ssb.publish({type: 'number', value: 30}, function(err, msg) {\n    ssb.publish({\n      type: 'number',\n      revisionRoot: msg.key,\n      revisionBranch: msg.key,\n      value: 20\n    }, function(err, msg2) {\n      revisions.view.get(function (err, value) {\n        t.deepEqual(value, { sum: 30, squareSum: 500 }, 'reduces state')\n      })\n    })\n  })\n})\n```\n\nAnother examples is [here](https://github.com/regular/ssb-revisions/blob/master/indexes/stats.js)\n\nThe rest of this Readme is adapted from flumeview-reduce.\n\n## ReviewReduce(version, reduce, map?, codec?, initialState?) =\u003e Review\n\nconstruct a view from this reduce function. `version` should be a number,\nand must be provided. If you make a breaking change to either `reduce` or `map`\nthen increment `version` and the view will be rebuilt.\n\n`map` is optional. If map is applied, then each item in the log is passed to `map`\nand then if the returned value is not null, it is passed to reduce.\n\n``` js\nvar _old_value = old_value \u0026\u0026 map(old_value, old_seq, false)\nvar _value = map(value, seq, true)\nif(_value != null)\n  state = reduce(state, _value, seq, _old_value, old_seq)\n```\n\nusing a `map` function is useful, because it enables efficiently streaming the realtime\nchanges in the state to a remote client.\n\nthen, pass the view to `ssb.revisions.use(name, view)`\nand you'll have access to the view methods on `ssb.revisions[name]`\n\n`codec` (optional) - specify the codec to use in the event your log uses the filesystem.\n`initialState` (optional) - declare an initial state for your reducer. This will be ignored if a persisted state is found.\n\n## ssb.revisions[name].get(cb)\n\nget the current state of the reduce. This will wait until the view is up to date, if necessary.\n\n## ssb.revisions[name].stream({live: boolean}) =\u003e PullSource\n\nCreates a [pull-stream](https://github.com/pull-stream/pull-stream) whose:\n- first value is the current state of the view,\n- following values are not the view state, but the _values_ (they're had your `map` applied, but the `reducer` hasn't been applied yet).\n- each item starting from the second is an array with two entries: [new_value, old_value]. The old value is supplied when an object was updated with a newer revision (see example above), otherwise the 2nd entry is null.\n\nThis is a light-weight for a remote client to keep up to date with the view - get a snapshot, and then update it themselves. This way we don't need to send a massive view every time there's a new log entry.\n\n## Stores\n\n`flumeview-reduce` currently includes several _store_ implementations,\nthis is how the actual data is persisted. the current implementations are\n\n* 'store/fs' - store in a file.\n* 'store/local-storage' - `localStorage`, in a browser\n* 'store/remote' - a meta store that keeps a local copy of a remote view.\n\nto set a store, you must set up flumeview-reduce via the lower level dependency injection api.\n\n``` js\nvar createReduce = require('ssb-review-reduce/inject')\n\nvar Reduce = createReduce(Store)\n\n//then use Reduce normally\n\nvar view = ssb.revisions.use('view', Reduce(version, reduce, map)) //etc\n\n//since remote is most interesting\n\nvar Remote = require('ssb-review-reduce/store/remote')\nfunction get (opts, cb) {\n  //call the get method on the remote copy of the view\n  view.get(opts, cb)\n}\nvar RemoteReduce = createReduce(Remote(get, Store, codec))\n\nvar remoteView = _ssb.review.use('view', Reduce(version, reduce, map)) //etc\n//make sure you pass the exact same reduce and map functions to the remote view!\n```\n\n## License\n\nMIT\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fregular%2Fssb-review-reduce","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fregular%2Fssb-review-reduce","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fregular%2Fssb-review-reduce/lists"}