Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

@tangle/reduce

Package Overview
Dependencies
Maintainers
2
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tangle/reduce

reduce tangles into their current state


Version published
Weekly downloads
163
increased by526.92%
Maintainers
2
Weekly downloads
 
Created
Source

@tangle/reduce

Takes in a collection of tangle nodes which contain operational transforms, and reduces them into some final transform(s).

The reduce starts with root node(s) and traverses the tangle graphs, successively concatenating the transforms in each node until the tip(s) of the graph are reached, where we return the final state(s)

Example Usage

//     A       (root node)
//    / \                                       |
//   B   C     (two concurrent nodes)           |  causality
//    \ /                                       v
//     D       (leading tip)

const nodes = [
  {
    key: 'A',
    previous: null,
    title: { set: 'spec gathering' },
    attendees: { mix: 1, luandro: 1 }
  },
  {
    key: 'B',
    previous: ['A'],
    attendees: { luandro: -1, cherese: 1 }
  },
  {
    key: 'C',
    previous: ['A'],
    attendees: { luandro: -1 }
  },
  {
    key: 'D',
    previous: ['B', 'C'],
    title: { set: 'Tangle Spec v1 meeting' },
  },
]
const reduce = require('@tangle/reduce')
const Srategy = require('@tangle/strategy')

const strategy = Compose({
  title: require('@tangle/simple-set')(),
  attendees: require('@tangle/overwrite')()
})

const result = reduce(nodes, strategy)
// => {
//   D: {
//     title: { set: 'Tangle Spec v1 meeting' },
//     attendees: { mix: 1. luandro: -2, cherese: 1 }
//   }
// }

Here the only key in our result is D, as there's only one leading tip. If we reduced [A, B, C] there would be two tips [B, C] and an accumulated transform state for each.

Note you could use strategy.reify to convert this accumulated transformation into a 'real' state:

strategy.reify(result['D'])
// => {
//   title: 'Tangle Spec v1 meeting',
//   attendees: ['cherese', 'mix']
// }

API

reduce(nodes, strategy, opts) => tips

where:

nodes Array is a collection of tangle nodes, where each expected to include:

  • node.key - a unique identified for the node
  • "backlinks"
    • an Array of keys for nodes which this node extended from (or null if it's a root node)
    • default location: node.previous (can be over-ridden, see opts)
  • "transformation"
    • an object which containing the transformation which the strategy describes
    • default location: node (can be over-ridden, see opts)

strategy Object defines how to reduce nodes. Produced by @tangle/strategy

opts Object (optional) is an object which lets you to customise the reduce:

  • opts.getTransformation = fn(node): Object

    • fetch the part of the node which contains the transformation
    • default: node => node
    • NOTE - the strategy will automatically pick out properties defined in the strategy and ignore all others
  • opts.getBacklinks = fn(node): Array

    • fetch the part of the node which contains the backlinks to the nodes it followed in the tangle
    • default: node => node.previous
  • opts.isValid = fn(state, node): Boolean

    • determine whether the next node node should be included in the tangle
    • state is the state in the tangle immediately before node, and is an object { graph, accT }
      • graph is an instance of @tangle/graph, which has a bunch of helper functions for querying the tangle
      • accT is the accumulated transform right before node
    • NOTE if a node is not valid, all nodes downstream of that node and considered invalid, and will not be included in the reduce

Keywords

FAQs

Package last updated on 13 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc