├── .babelrc ├── .editorconfig ├── .eslintrc.yml ├── .gitignore ├── .npmignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── docs ├── Graph_index.js.html ├── Node_index.js.html ├── fonts │ ├── Lato-Regular.ttf │ ├── OFL.txt │ └── SourceCodePro-Regular.ttf ├── global.html ├── graph-crdt.module_Graph-Graph.html ├── graph-crdt.module_Graph.html ├── graph-crdt.module_Node-Node.html ├── graph-crdt.module_Node.html ├── index.html ├── scripts │ ├── linenumber.js │ └── prettify │ │ ├── Apache-License-2.0.txt │ │ ├── lang-css.js │ │ └── prettify.js ├── styles │ ├── ionicons.min.css │ ├── jsdoc-default.css │ ├── prettify-jsdoc.css │ └── prettify-tomorrow.css ├── test-helpers.js.html └── union_index.js.html ├── jsdoc.config.json ├── mocha.opts ├── package.json ├── src ├── Graph │ ├── index.js │ └── test.js ├── Node │ ├── index.js │ ├── merge.test.js │ └── test.js ├── index.js └── union │ ├── index.js │ └── test.js └── yarn.lock /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015", "stage-2"], 3 | "plugins": ["transform-runtime"] 4 | } 5 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | # global file settings 4 | [*] 5 | end_of_line = lf 6 | insert_final_newline = true 7 | charset = utf-8 8 | indent_style = space 9 | indent_size = 2 10 | trim_trailing_whitespace = true 11 | -------------------------------------------------------------------------------- /.eslintrc.yml: -------------------------------------------------------------------------------- 1 | env: 2 | browser: true 3 | node: true 4 | es6: true 5 | 6 | plugins: 7 | - babel 8 | 9 | extends: 10 | - eslint:recommended 11 | - llama 12 | - prettier 13 | 14 | parserOptions: 15 | sourceType: module 16 | 17 | parser: babel-eslint 18 | 19 | rules: 20 | lines-around-comment: off 21 | require-jsdoc: off 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | npm-debug.log 4 | dist/ 5 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # Only publish compiled code. 2 | src/ 3 | 4 | docs/ 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: node_js 3 | 4 | node_js: 5 | - 4 6 | - 5 7 | - 6 8 | 9 | script: 10 | - npm run test 11 | - npm run lint 12 | 13 | deploy: 14 | provider: npm 15 | email: Jesse_Gibson@me.com 16 | api_key: 17 | secure: D2i15mHlb2VWiRaCm/U0mEkhaoqtaKo7dMKglOkmbG1Cc1S7+JSfxQFyF2tNSQsgJNqIO3ik/l/09lm3nz1mV197/VMKyYKASsdlIg4elX3WaiyvMtQk82OUfNkCYrBskDaX3oqspByhiGfBXIrJqLQ/I2L1YJ/7rLVdx3t+/E2IYYZ6N/l4iWr/7Iq33Y+jy1mg4rm4R+UOS06QsRfMde4AxSGXjNGs9w6pm8C4cNgZx5rhHyJV6FJextofSCxp0idIOUtE8FeDDqpVccTUaD/phe128zWXnoN6pk7efxVgDlxhR94HCPXLfQBJyL+J3P5KjNNzz94IwTaVnuomwfnsxg7Wnrc2ZiVqNA/xV4CUphlpumiHi3Z7SfxIZno47pKbOh7UlCmEeNNdtFI88/YBrWaGEfDPFph6LQCgvXQbopbNYkUNIq2QA9vaXUG9r0NJr3o9WsoptAmfmrSkhP7RvrpqPOez6zlcxBX5OoryJTR21HIW8jP1Pn5+KRIynb3GJZF5dOHiiFJhZORr55IGslxddt0ze7INS+6N/9i6GBGZDA/5jqd+IWII+v2iB/rRDvafrqvOwQ8tTC1xtDpjYZJbTbSeQD36WQXmzENvGqHixa1mC8onZwEhIoP1VgmVpLx43HIMEQW54BkYwzhFIfP4q+0gwBHnt121gV8= 18 | on: 19 | tags: true 20 | repo: PsychoLlama/graph-crdt 21 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | `graph-crdt` uses [this changelog style](http://keepachangelog.com/en/0.3.0/), and versions after `0.1.0` follow [semver](http://semver.org/). 4 | 5 | ## v0.7.0 6 | ### Added 7 | - New `Node#snapshot` method. 8 | 9 | ## v0.6.2 10 | ### Fixed 11 | - Removed `Graph` and `Node` globals. 12 | 13 | ## v0.6.1 14 | ### Added 15 | - New documentation using JSDoc. 16 | 17 | ## v0.6.0 18 | ### Added 19 | - New `Node#setMetadata` method. 20 | 21 | ## v0.5.0 22 | ### Added 23 | - New `Node#overlap` method. 24 | - New `Graph#overlap` method. 25 | 26 | ## v0.4.0 27 | ### Added 28 | - New `Node#rebase` method. 29 | - New `Graph#rebase` method. 30 | 31 | ## v0.3.1 32 | ### Fixed 33 | - Prematurely resolved update merges caused by a `break` statement in the merge loop. Caused some updates to never fully merge. 34 | 35 | ## v0.3.0 36 | ### Changed 37 | - Replaced wall clocks with logical clocks. 38 | 39 | ### Removed 40 | - Deferred updates are gone! They don't make sense with Lamport time. 41 | - `Node#compare` is gone. 42 | 43 | ## v0.2.0 44 | ### Changed 45 | - Renamed `Graph#read` to `Graph#value`. 46 | 47 | ### Added 48 | - New `Graph#new` method, primarily for subclasses. 49 | 50 | ## v0.1.0 51 | ### Added 52 | - `Node#new` method, creates a new instance of the same type using the same configuration (such as `uid`). 53 | 54 | ### Changed 55 | - `Node#read` has been renamed to `Node#value`. 56 | 57 | ### Removed 58 | - `Node#clone` has been replaced in favor of `Node#new`, which does not copy properties. 59 | - `Graph#add` has been removed in favor of `Graph#merge`. 60 | 61 | ## v0.0.5 62 | ### Added 63 | - New `Node#clone` method which creates a copy of the node. 64 | 65 | ## v0.0.4 66 | ### Added 67 | - New `conflict` event on Node instances (only happens when a conflict overwrites the current value). 68 | 69 | ### Changed 70 | - `Node#compare` returns `"conflict"` on conflicts, instead of attempting to interpret them as `"update"` or `"history"`. 71 | 72 | ## v0.0.3 73 | ### Changed 74 | - Node `historical` event renamed to `history`. 75 | 76 | ### Added 77 | - New `Node#schedule` method schedules deferred updates. 78 | 79 | ### Fixed 80 | - Calling `Node#merge` wouldn't merge the incoming node's deferred updates. 81 | 82 | ## v0.0.2 83 | ### Fixed 84 | - Graph members are no longer globally mutated (allows graph time travel). 85 | 86 | ## v0.0.1 87 | ### Added 88 | - `Node` class 89 | - `Graph` class 90 | - Merge capabilities 91 | - `update`, `historical`, `deferred` events. 92 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2016 Jesse Gibson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Distributed Graph Engine 2 | [![Travis branch](https://img.shields.io/travis/PsychoLlama/graph-crdt/master.svg?style=flat-square)](https://travis-ci.org/PsychoLlama/graph-crdt) 3 | 4 | Designed for serializing arbitrary data structures, making offline edits, and seamlessly merging changes back in. All data is observable and event driven. 5 | 6 | For the technical, `graph-crdt` is a modified version of a [LWW-E-Set](https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type#LWW-Element-Set_(Last-Write-Wins-Element-Set)) with inline garbage collection using [lamport clocks](https://en.wikipedia.org/wiki/Lamport_timestamps) and JavaScript's [lexicographic comparison](https://en.wikipedia.org/wiki/Lexicographical_order) on deterministically serialized JSON for the predetermined conflict resolution bias. 7 | 8 | # Maintenance notice 9 | If it isn't obvious from the lack of recent commits, `graph-crdt` is **unmaintained**. 10 | 11 | I'm still pursuing the concepts through [the mytosis framework](https://github.com/PsychoLlama/mytosis) along with more ambitious ideas, but development has kinda paused there too. I hit some project burnout. 12 | 13 | # What for? 14 | This graph library aims to ease the complexity of synchronizing complex and interconnected state between peers, without assuming centralized authority. 15 | 16 | # How does it work? 17 | Truly offline systems **cannot** rely on any form of collaboration. They must (at some point) assume the editor is in complete isolation, such as a smartphone that lost cell service, or a server who's network is unreachable. 18 | 19 | You have a few options: 20 | - **Block writes**
21 | Probably the worst experience, block all writes until the network heals. This is essentially the same as losing socket connection to your database (Rethink, Neo4j, Redis, MySQL, etc.) 22 | 23 | - **Defer the updates**
24 | You allow writes on the offline machine, wait for the network to heal, then publish them. If not handled perfectly, you're susceptible to merge hell on an active production environment. 25 | 26 | - **Use a CRDT**
27 | CRDTs ([Convergent Replicated Data Types](https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type)) are similar to the option above, but come with additional guarantees: regardless of the order which updates are received in, every machine will arrive at the exact same result *every time*, and if implemented correctly, make merge conflicts impossible. 28 | 29 | > `graph-crdt` uses Lamport time to track state mutation and resolves concurrent edit conflicts using a deterministic sorting algorithm. 30 | 31 | This library opts for the latter, implementing a delta graph CvRDT. However, as great as they may seem, there are some cons (some specific to this library): 32 | 33 | - You need more data.
34 | Merges need a state integer on each field. 35 | 36 | - There is no "true" delete.
37 | You can remove the value, but some metadata has to stay around. 38 | 39 | - It only plays nice with other CRDTs.
40 | To merge two states, both must have the CRDT metadata (though this library allows you to upgrade nearly any data). 41 | 42 | ## Features 43 | - Commutative, idempotent, conflict-resolved `Node` unions. 44 | - Delta emission on `Node` and `Graph` unions. 45 | - Time travel (track and selectively apply deltas). 46 | 47 | ## [Documentation](https://psychollama.github.io/graph-crdt/) 48 | All the API docs [can be found here](https://psychollama.github.io/graph-crdt/). 49 | 50 | ## Roadmap 51 | 1. Node field tombstones. 52 | 2. Graph member tombstones. 53 | 3. Custom conflict resolvers. 54 | 4. A new data structure (this one is a surprise). 55 | 56 | ## Disclaimer 57 | Although I have working experience with decentralized systems (at [GunDB](//gundb.io)), I'm still a n00b. This library is my best understanding of CvRDTs and how they operate. I'm open to most suggestions. 58 | -------------------------------------------------------------------------------- /docs/Graph_index.js.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Graph/index.js - Postman Documentation 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 23 | 24 | 25 | 28 | 29 |
30 | 31 |

Graph/index.js

32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
40 |
41 |
/**
 42 |  * @module graph-crdt.Graph
 43 |  */
 44 | 
 45 | import Emitter from 'eventemitter3';
 46 | import Node from '../Node';
 47 | 
 48 | const nodes = Symbol('graph node container');
 49 | 
 50 | /**
 51 |  * Container and interface for groups of nodes.
 52 |  *
 53 |  * @class Graph
 54 |  */
 55 | export default class Graph extends Emitter {
 56 | 
 57 |   /**
 58 |    * Instantiates a graph without needing `new`.
 59 |    *
 60 |    * @returns {Graph} - A graph instance.
 61 |    */
 62 |   static create () {
 63 |     return new Graph();
 64 |   }
 65 | 
 66 |   /**
 67 |    * Imports a format compliant graph into a new one. It expects
 68 |    * nested nodes to use the node metadata format. Useful for
 69 |    * sending and importing graphs over the network.
 70 |    *
 71 |    * @param  {Object} object - The raw graph object.
 72 |    * @returns {Graph} - A new graph instance that consumes
 73 |    * the imported data.
 74 |    */
 75 |   static source (object) {
 76 | 
 77 |     // Create a new graph
 78 |     const graph = Graph.create();
 79 | 
 80 |     // For each node...
 81 |     Object.keys(object).forEach((key) => {
 82 | 
 83 |       let node = object[key];
 84 | 
 85 |       // Make sure it's a node.
 86 |       if (!(node instanceof Node)) {
 87 |         node = Node.source(node);
 88 |       }
 89 | 
 90 |       // Get it's unique ID.
 91 |       const { uid } = node.meta();
 92 | 
 93 |       // Add it to the new graph.
 94 |       graph[nodes][uid] = node;
 95 |     });
 96 | 
 97 |     return graph;
 98 |   }
 99 | 
100 |   constructor () {
101 |     super();
102 | 
103 |     this[nodes] = {};
104 |   }
105 | 
106 |   /**
107 |    * Return the unmodified value of a node lookup.
108 |    *
109 |    * @param  {String} key - The name/uid of the node.
110 |    * @returns {Node|null} - The node if found, otherwise `null`.
111 |    */
112 |   value (key) {
113 |     return this[nodes][key] || null;
114 |   }
115 | 
116 |   new () {
117 |     return new Graph();
118 |   }
119 | 
120 |   /**
121 |    * Replays all the changes in the graph as though they occurred
122 |    * after the events in the target graph.
123 |    * @param  {Graph} target - Preceding state.
124 |    * @return {Graph} - A new graph containing the rebased nodes.
125 |    */
126 |   rebase (target) {
127 |     const rebased = this.new();
128 | 
129 |     rebased.merge(target);
130 |     rebased.merge(this);
131 | 
132 |     for (const [id] of this) {
133 |       const existing = target.value(id);
134 | 
135 |       if (existing) {
136 |         rebased[nodes][id] = this.value(id).rebase(existing);
137 |       }
138 |     }
139 | 
140 |     return rebased;
141 |   }
142 | 
143 |   /**
144 |    * Figures out what fields are common to both graphs.
145 |    * @param  {Graph} target - Any other graph.
146 |    * @return {Graph} - The shared properties between both graphs.
147 |    */
148 |   overlap (target) {
149 |     const shared = this.new();
150 | 
151 |     for (const [key] of this) {
152 |       if (this.value(key) && target.value(key)) {
153 | 
154 |         // Calculate the node overlap.
155 |         const nodeSource = this.value(key);
156 |         const nodeTarget = target.value(key);
157 |         const overlap = nodeSource.overlap(nodeTarget);
158 | 
159 |         // Merge it into the new graph.
160 |         shared.merge({ [key]: overlap });
161 |       }
162 |     }
163 | 
164 |     return shared;
165 |   }
166 | 
167 |   /**
168 |    * Merge one graph with another (graph union operation).
169 |    *
170 |    * @param  {Object} graph - The graph to merge with.
171 |    * Items must be enumerable, and cannot be inherited from prototypes.
172 |    * @returns {Graph} - The `this` context.
173 |    */
174 |   merge (graph) {
175 | 
176 |     /** Ensure it's a graph. */
177 |     if (!(graph instanceof Graph)) {
178 |       graph = Graph.source(graph);
179 |     }
180 | 
181 |     const changes = {
182 |       update: this.new(),
183 |       history: this.new(),
184 |     };
185 | 
186 |     for (const [uid, node] of graph) {
187 |       let target = this.value(uid);
188 | 
189 |       if (!target) {
190 |         target = this[nodes][uid] = node.new();
191 |       }
192 | 
193 |       const { update, history } = target.merge(node);
194 | 
195 |       changes.update[nodes][uid] = update;
196 |       changes.history[nodes][uid] = history;
197 |     }
198 | 
199 |     this.emit('update', changes.update);
200 |     this.emit('history', changes.history);
201 | 
202 |     return changes;
203 |   }
204 | 
205 |   /**
206 |    * Iterates over every node in the graph.
207 |    * @return {Array} - Every yielded value is a key/value pair.
208 |    */
209 |   * [Symbol.iterator] () {
210 |     const object = this[nodes];
211 | 
212 |     for (const key in object) {
213 |       if (object.hasOwnProperty(key)) {
214 |         const value = this.value(key);
215 |         yield [key, value];
216 |       }
217 |     }
218 |   }
219 | 
220 |   /* Coercion interfaces */
221 | 
222 |   /**
223 |    * Used to serialize a graph (JSON.stringify calls this method).
224 |    *
225 |    * @private
226 |    * @returns {Object} - The hidden collection of nodes.
227 |    */
228 |   toJSON () {
229 |     return this[nodes];
230 |   }
231 | 
232 | }
233 | 
234 |
235 |
236 | 237 | 238 | 239 | 240 |
241 | 242 |
243 | 244 | 247 | 248 | 249 | 250 | 251 | 252 | -------------------------------------------------------------------------------- /docs/Node_index.js.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Node/index.js - Postman Documentation 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 23 | 24 | 25 | 28 | 29 |
30 | 31 |

Node/index.js

32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
40 |
41 |
/**
 42 |  * @module graph-crdt.Node
 43 |  */
 44 | 
 45 | import Emitter from 'eventemitter3';
 46 | import { v4 as uuid } from 'uuid';
 47 | import { conflict } from '../union';
 48 | 
 49 | const node = Symbol('source object');
 50 | 
 51 | /**
 52 |  * Increments each state given an object of key/value pairs.
 53 |  * @private
 54 |  * @param  {Node} current - Current state.
 55 |  * @param  {Object} update - Key-value update pairs.
 56 |  * @return {Node} - Incremented update.
 57 |  */
 58 | const getIncrementedState = (current, update) => {
 59 |   const result = current.new();
 60 | 
 61 |   for (const field in update) {
 62 |     if (update.hasOwnProperty(field)) {
 63 |       const state = current.state(field) + 1;
 64 |       const value = update[field];
 65 | 
 66 |       result[node][field] = { value, state };
 67 |     }
 68 |   }
 69 | 
 70 |   return result;
 71 | };
 72 | 
 73 | /**
 74 |  * An observable object with conflict-resolution.
 75 |  *
 76 |  * @class Node
 77 |  * @param  {Object} [config] - Instance level configuration.
 78 |  * @param  {Object} [config.uid] - Override the randomly generated
 79 |  * node uid.
 80 |  */
 81 | export default class Node extends Emitter {
 82 | 
 83 |   /**
 84 |    * Creates a new Node instance without using
 85 |    * `new`.
 86 |    *
 87 |    * @param {Object} [config] - The constructor configuration object.
 88 |    * @returns {Node} - The new node instance.
 89 |    */
 90 |   static create (config) {
 91 |     return new Node(config);
 92 |   }
 93 | 
 94 |   /**
 95 |    * Turns a normal object into a Node instance.
 96 |    * Properties to be imported must be enumerable
 97 |    * and cannot be inherited via prototype.
 98 |    *
 99 |    * @param  {Object} object - Any enumerable object.
100 |    * @returns {Node} - A node interface constructed from the object.
101 |    */
102 |   static from (object) {
103 |     const instance = Node.create();
104 |     const state = 1;
105 | 
106 |     for (const key in object) {
107 |       if (object.hasOwnProperty(key)) {
108 |         const value = object[key];
109 |         instance[node][key] = { value, state };
110 |       }
111 |     }
112 | 
113 |     return instance;
114 |   }
115 | 
116 |   /**
117 |    * Take an object and use it as the data source for a new
118 |    * node. This method is used with properly formatted
119 |    * objects, such as stringified, then parsed node instances.
120 |    *
121 |    * @param  {Object} object - The preformatted object.
122 |    * @returns {Node} - A new node instance.
123 |    *
124 |    * @example
125 |    * const original = Node.create()
126 |    * original.merge({ data: 'intact' })
127 |    * const serialized = JSON.stringify(original)
128 |    * const parsed = JSON.parse(serialized)
129 |    *
130 |    * const node = Node.source(parsed)
131 |    * node.value('data') // 'intact'
132 |    */
133 |   static source (object) {
134 |     const instance = Node.create();
135 |     instance[node] = object;
136 | 
137 |     return instance;
138 |   }
139 | 
140 |   constructor (config = {}) {
141 | 
142 |     super();
143 | 
144 |     const uid = config.uid || uuid();
145 | 
146 |     this[node] = {
147 |       '@object': { uid },
148 |     };
149 |   }
150 | 
151 |   /**
152 |    * Read metadata, either on a field, or
153 |    * on the entire object.
154 |    *
155 |    * @param  {String} [field] - The property to read metadata from.
156 |    * @returns {Object|null} - If metadata is found, it returns the object.
157 |    * Otherwise `null` is given.
158 |    */
159 |   meta (field) {
160 | 
161 |     if (field === undefined) {
162 | 
163 |       /** Returns the object metadata if no field is specified. */
164 |       return this[node]['@object'];
165 |     }
166 | 
167 |     /** Returns the field given, and null if metadata isn't found. */
168 |     return this[node][field] || null;
169 |   }
170 | 
171 |   /**
172 |    * Show the value of a node's property.
173 |    *
174 |    * @param  {String} field - The name of the property
175 |    * to retrieve.
176 |    * @returns {Mixed} - The value of the property,
177 |    * or undefined if it doesn't exist. Cannot be called
178 |    * on reserved fields (like "@object").
179 |    */
180 |   value (field) {
181 |     if (field === '@object') {
182 |       return undefined;
183 |     }
184 | 
185 |     /** Gets the field metadata. */
186 |     const subject = this.meta(field);
187 | 
188 |    /**
189 |     * If the field exists, it returns the corresponding
190 |     * value, otherwise it returns undefined.
191 |     */
192 |     return subject ? subject.value : undefined;
193 |   }
194 | 
195 |   /**
196 |    * Get the current state of a property.
197 |    *
198 |    * @param  {String} field - Property name.
199 |    * @returns {Number} - Current lamport state (or 0).
200 |    */
201 |   state (field) {
202 | 
203 |     /** Get the field metadata. */
204 |     const meta = this.meta(field);
205 | 
206 |     return meta ? meta.state : 0;
207 |   }
208 | 
209 |   /**
210 |    * Sets metadata for a field, bumping the current state.
211 |    * @param  {String} field - A field to update.
212 |    * @param  {Object} metadata - What metadata the field should contain.
213 |    * @return {Object} - The metadata object (not the same one given).
214 |    */
215 |   setMetadata (field, metadata) {
216 |     const state = this.state(field) + 1;
217 | 
218 |     const update = this.new();
219 |     update[node][field] = { ...metadata, state };
220 | 
221 |     return this.merge(update);
222 |   }
223 | 
224 |   /**
225 |    * Takes the changes from the current node and plays them after the
226 |    * changes in the target node.
227 |    * Similar to git rebase, but without the conflicts.
228 |    * @param  {Node} target - Preceding state.
229 |    * @return {Node} - A new, rebased node.
230 |    */
231 |   rebase (target) {
232 |     const rebased = this.new();
233 | 
234 |     rebased.merge(target);
235 |     rebased.merge(this);
236 | 
237 |     // Bump state for older fields.
238 |     for (const [key] of this) {
239 |       if (target.state(key) >= this.state(key)) {
240 | 
241 |         // Avoids mutation of metadata.
242 |         rebased[node][key] = {
243 |           ...this.meta(key),
244 |           state: target.state(key) + 1,
245 |         };
246 |       }
247 |     }
248 | 
249 |     return rebased;
250 |   }
251 | 
252 |   /**
253 |    * Calculates the intersection between two nodes.
254 |    * @param  {Node} target - Any other node.
255 |    * @return {Node} - A collection of fields common to both nodes.
256 |    */
257 |   overlap (target) {
258 |     const shared = this.new();
259 | 
260 |     for (const [field] of this) {
261 |       if (this.state(field) && target.state(field)) {
262 |         shared[node][field] = this.meta(field);
263 |       }
264 |     }
265 | 
266 |     return shared;
267 |   }
268 | 
269 |   /**
270 |    * Merges an update into the current node.
271 |    *
272 |    * @param  {Node} incoming - The node to merge from.
273 |    * If a plain object is passed, it will be upgraded to a node
274 |    * using `Node.from`.
275 |    * @returns {Object} - A collection of changes caused by the merge.
276 |    */
277 |   merge (incoming) {
278 |     if (!(incoming instanceof Node)) {
279 |       incoming = getIncrementedState(this, incoming);
280 |     }
281 | 
282 |     /** Track all mutations. */
283 |     const changes = {
284 |       history: this.new(),
285 |       update: this.new(),
286 |     };
287 | 
288 |     for (const [field] of incoming) {
289 |       let forceUpdate = false;
290 | 
291 |       const current = this.meta(field);
292 |       const update = incoming.meta(field);
293 |       const state = {
294 |         incoming: incoming.state(field),
295 |         current: this.state(field),
296 |       };
297 | 
298 |       /** Handle conflicts. */
299 |       if (state.current === state.incoming) {
300 |         const winner = conflict(current, update);
301 | 
302 |         /** No further action needed. */
303 |         if (winner === current) {
304 |           continue;
305 |         }
306 | 
307 |         /** Replace the current value */
308 |         this.emit('conflict', update, current);
309 |         forceUpdate = true;
310 |       }
311 | 
312 |       if (state.current < state.incoming || forceUpdate) {
313 | 
314 |         /** Track overwritten values. */
315 |         if (current) {
316 |           changes.history[node][field] = current;
317 |         }
318 | 
319 |         changes.update[node][field] = update;
320 | 
321 |         /** Immediately apply updates. */
322 |         this[node][field] = update;
323 |       } else {
324 |         changes.history[node][field] = update;
325 |       }
326 |     }
327 | 
328 |     /** Only emit when there's a change. */
329 |     const changed = [...changes.update].length > 0;
330 |     const overwritten = [...changes.history].length > 0;
331 | 
332 |     if (overwritten) {
333 |       this.emit('history', changes.history);
334 |     }
335 |     if (changed) {
336 |       this.emit('update', changes.update);
337 |     }
338 | 
339 |     return changes;
340 |   }
341 | 
342 |   /**
343 |    * Creates an empty instance with the same configuration.
344 |    * @return {Node} - A new node instance with the same properties.
345 |    */
346 |   new () {
347 |     const { uid } = this.meta();
348 |     const clone = new Node({ uid });
349 | 
350 |     return clone;
351 |   }
352 | 
353 |   /**
354 |    * Takes a snapshot of the current state.
355 |    * @return {Object} - Every key and value (currently) in the node.
356 |    */
357 |   snapshot () {
358 |     const object = {};
359 | 
360 |     for (const [key, value] of this) {
361 |       object[key] = value;
362 |     }
363 | 
364 |     return object;
365 |   }
366 | 
367 |   /**
368 |    * Iterates over the node keys & values, ignoring metadata.
369 |    * @return {Array} - Each value yielded is a [key, value] pair.
370 |    */
371 |   * [Symbol.iterator] () {
372 |     const object = this[node];
373 |     const meta = '@object';
374 | 
375 |     /** Iterate over the source object. */
376 |     for (const key in object) {
377 | 
378 |       /** Ignore prototype values and node metadata. */
379 |       if (object.hasOwnProperty(key) && key !== meta) {
380 |         const value = this.value(key);
381 |         yield [key, value];
382 |       }
383 | 
384 |     }
385 |   }
386 | 
387 |   /* Coercion interfaces */
388 | 
389 |   /**
390 |    * Returns the node's uid. Not meant for end developers,
391 |    * instead used by JavaScript for type coercion.
392 |    *
393 |    * @private
394 |    * @returns {String} - The node's unique ID.
395 |    */
396 |   toString () {
397 |     const { uid } = this.meta();
398 | 
399 |     return uid;
400 |   }
401 | 
402 |   /**
403 |    * Returns the actual object, called when JSON needs something
404 |    * to stringify. Obviously dangerous as it exposes the object
405 |    * to untracked mutation. Please don't use it.
406 |    *
407 |    * @private
408 |    * @returns {Object} - The actual node object.
409 |    */
410 |   toJSON () {
411 |     return this[node];
412 |   }
413 | }
414 | 
415 |
416 |
417 | 418 | 419 | 420 | 421 |
422 | 423 |
424 | 425 | 428 | 429 | 430 | 431 | 432 | 433 | -------------------------------------------------------------------------------- /docs/fonts/Lato-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PsychoLlama/graph-crdt/e8e186541780acac07290b7e67a27def918d85e1/docs/fonts/Lato-Regular.ttf -------------------------------------------------------------------------------- /docs/fonts/OFL.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010-2014 by tyPoland Lukasz Dziedzic (team@latofonts.com) with Reserved Font Name "Lato" 2 | This Font Software is licensed under the SIL Open Font License, Version 1.1. 3 | This license is copied below, and is also available with a FAQ at: 4 | http://scripts.sil.org/OFL 5 | 6 | 7 | ----------------------------------------------------------- 8 | SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 9 | ----------------------------------------------------------- 10 | 11 | PREAMBLE 12 | The goals of the Open Font License (OFL) are to stimulate worldwide 13 | development of collaborative font projects, to support the font creation 14 | efforts of academic and linguistic communities, and to provide a free and 15 | open framework in which fonts may be shared and improved in partnership 16 | with others. 17 | 18 | The OFL allows the licensed fonts to be used, studied, modified and 19 | redistributed freely as long as they are not sold by themselves. The 20 | fonts, including any derivative works, can be bundled, embedded, 21 | redistributed and/or sold with any software provided that any reserved 22 | names are not used by derivative works. The fonts and derivatives, 23 | however, cannot be released under any other type of license. The 24 | requirement for fonts to remain under this license does not apply 25 | to any document created using the fonts or their derivatives. 26 | 27 | DEFINITIONS 28 | "Font Software" refers to the set of files released by the Copyright 29 | Holder(s) under this license and clearly marked as such. This may 30 | include source files, build scripts and documentation. 31 | 32 | "Reserved Font Name" refers to any names specified as such after the 33 | copyright statement(s). 34 | 35 | "Original Version" refers to the collection of Font Software components as 36 | distributed by the Copyright Holder(s). 37 | 38 | "Modified Version" refers to any derivative made by adding to, deleting, 39 | or substituting -- in part or in whole -- any of the components of the 40 | Original Version, by changing formats or by porting the Font Software to a 41 | new environment. 42 | 43 | "Author" refers to any designer, engineer, programmer, technical 44 | writer or other person who contributed to the Font Software. 45 | 46 | PERMISSION & CONDITIONS 47 | Permission is hereby granted, free of charge, to any person obtaining 48 | a copy of the Font Software, to use, study, copy, merge, embed, modify, 49 | redistribute, and sell modified and unmodified copies of the Font 50 | Software, subject to the following conditions: 51 | 52 | 1) Neither the Font Software nor any of its individual components, 53 | in Original or Modified Versions, may be sold by itself. 54 | 55 | 2) Original or Modified Versions of the Font Software may be bundled, 56 | redistributed and/or sold with any software, provided that each copy 57 | contains the above copyright notice and this license. These can be 58 | included either as stand-alone text files, human-readable headers or 59 | in the appropriate machine-readable metadata fields within text or 60 | binary files as long as those fields can be easily viewed by the user. 61 | 62 | 3) No Modified Version of the Font Software may use the Reserved Font 63 | Name(s) unless explicit written permission is granted by the corresponding 64 | Copyright Holder. This restriction only applies to the primary font name as 65 | presented to the users. 66 | 67 | 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font 68 | Software shall not be used to promote, endorse or advertise any 69 | Modified Version, except to acknowledge the contribution(s) of the 70 | Copyright Holder(s) and the Author(s) or with their explicit written 71 | permission. 72 | 73 | 5) The Font Software, modified or unmodified, in part or in whole, 74 | must be distributed entirely under this license, and must not be 75 | distributed under any other license. The requirement for fonts to 76 | remain under this license does not apply to any document created 77 | using the Font Software. 78 | 79 | TERMINATION 80 | This license becomes null and void if any of the above conditions are 81 | not met. 82 | 83 | DISCLAIMER 84 | THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 85 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF 86 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 87 | OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE 88 | COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 89 | INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL 90 | DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 91 | FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM 92 | OTHER DEALINGS IN THE FONT SOFTWARE. 93 | -------------------------------------------------------------------------------- /docs/fonts/SourceCodePro-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PsychoLlama/graph-crdt/e8e186541780acac07290b7e67a27def918d85e1/docs/fonts/SourceCodePro-Regular.ttf -------------------------------------------------------------------------------- /docs/global.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Global - Postman Documentation 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 23 | 24 | 25 | 28 | 29 |
30 | 31 |

Global

32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
40 | 41 |
42 | 43 |

44 | 45 |

46 | 47 | 48 |
49 | 50 |
51 |
52 | 53 | 54 | 55 | 56 | 57 | 58 |
59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 |
92 | 93 | 94 | 95 | 96 |
97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 |

Members

110 | 111 | 112 | 113 |

(constant) toObject

114 | 115 | 116 | 117 | 118 |
119 | Turns an entries iterable into an object. 120 |
121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 |
129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 |
Source:
156 |
159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 |
167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 |
182 | 183 |
184 | 185 | 186 | 187 | 188 |
189 | 190 |
191 | 192 | 195 | 196 | 197 | 198 | 199 | -------------------------------------------------------------------------------- /docs/graph-crdt.module_Graph-Graph.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Graph - Postman Documentation 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 23 | 24 | 25 | 28 | 29 |
30 | 31 |

Graph

32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
40 | 41 |
42 | 43 |

44 | .Graph~ 45 | 46 | Graph 47 |

48 | 49 | 50 |
51 | 52 |
53 |
54 | 55 | 56 | 57 | 58 | 59 |

new Graph()

60 | 61 | 62 | 63 | 64 | 65 |
66 | Container and interface for groups of nodes. 67 |
68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 |
82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 |
Source:
109 |
112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 |
120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 |
138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 |
157 | 158 |
159 | 160 | 161 | 162 | 163 |
164 | 165 |
166 | 167 | 170 | 171 | 172 | 173 | 174 | -------------------------------------------------------------------------------- /docs/graph-crdt.module_Graph.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Graph - Postman Documentation 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 23 | 24 | 25 | 28 | 29 |
30 | 31 |

Graph

32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
40 | 41 |
42 | 43 |
44 | 45 |
46 |
47 | 48 | 49 | 50 | 51 | 52 | 53 |
54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 |
Source:
81 |
84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 |
92 | 93 | 94 | 95 | 96 |
97 | 98 | 99 | 100 | 101 | 102 | 103 |

Classes

104 | 105 |
106 |
Graph
107 |
108 |
109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 |

Methods

119 | 120 | 121 | 122 | 123 | 124 | 125 |

(static) create() → {Graph}

126 | 127 | 128 | 129 | 130 | 131 |
132 | Instantiates a graph without needing `new`. 133 |
134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 |
148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 |
Source:
175 |
178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 |
186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 |
Returns:
200 | 201 | 202 |
203 | - A graph instance. 204 |
205 | 206 | 207 | 208 |
209 |
210 | Type 211 |
212 |
213 | 214 | Graph 215 | 216 | 217 |
218 |
219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 |

(static) source(object) → {Graph}

230 | 231 | 232 | 233 | 234 | 235 |
236 | Imports a format compliant graph into a new one. It expects 237 | nested nodes to use the node metadata format. Useful for 238 | sending and importing graphs over the network. 239 |
240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 |
Parameters:
250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 |
NameTypeDescription
object 278 | 279 | 280 | Object 281 | 282 | 283 | 284 | The raw graph object.
296 | 297 | 298 | 299 | 300 | 301 | 302 |
303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 |
Source:
330 |
333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 |
341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 |
Returns:
355 | 356 | 357 |
358 | - A new graph instance that consumes 359 | the imported data. 360 |
361 | 362 | 363 | 364 |
365 |
366 | Type 367 |
368 |
369 | 370 | Graph 371 | 372 | 373 |
374 |
375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 |

merge(graph) → {Graph}

386 | 387 | 388 | 389 | 390 | 391 |
392 | Merge one graph with another (graph union operation). 393 |
394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 |
Parameters:
404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 439 | 440 | 441 | 442 | 443 | 444 | 446 | 447 | 448 | 449 | 450 |
NameTypeDescription
graph 432 | 433 | 434 | Object 435 | 436 | 437 | 438 | The graph to merge with. 445 | Items must be enumerable, and cannot be inherited from prototypes.
451 | 452 | 453 | 454 | 455 | 456 | 457 |
458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 |
Source:
485 |
488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 |
496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 |
Returns:
510 | 511 | 512 |
513 | - The `this` context. 514 |
515 | 516 | 517 | 518 |
519 |
520 | Type 521 |
522 |
523 | 524 | Graph 525 | 526 | 527 |
528 |
529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 |

overlap(target) → {Graph}

540 | 541 | 542 | 543 | 544 | 545 |
546 | Figures out what fields are common to both graphs. 547 |
548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 |
Parameters:
558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 |
NameTypeDescription
target 586 | 587 | 588 | Graph 589 | 590 | 591 | 592 | Any other graph.
604 | 605 | 606 | 607 | 608 | 609 | 610 |
611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 |
Source:
638 |
641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 |
649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 |
Returns:
663 | 664 | 665 |
666 | - The shared properties between both graphs. 667 |
668 | 669 | 670 | 671 |
672 |
673 | Type 674 |
675 |
676 | 677 | Graph 678 | 679 | 680 |
681 |
682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 |

rebase(target) → {Graph}

693 | 694 | 695 | 696 | 697 | 698 |
699 | Replays all the changes in the graph as though they occurred 700 | after the events in the target graph. 701 |
702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 |
Parameters:
712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 |
NameTypeDescription
target 740 | 741 | 742 | Graph 743 | 744 | 745 | 746 | Preceding state.
758 | 759 | 760 | 761 | 762 | 763 | 764 |
765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 | 779 | 780 | 781 | 782 | 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 791 |
Source:
792 |
795 | 796 | 797 | 798 | 799 | 800 | 801 | 802 |
803 | 804 | 805 | 806 | 807 | 808 | 809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 |
Returns:
817 | 818 | 819 |
820 | - A new graph containing the rebased nodes. 821 |
822 | 823 | 824 | 825 |
826 |
827 | Type 828 |
829 |
830 | 831 | Graph 832 | 833 | 834 |
835 |
836 | 837 | 838 | 839 | 840 | 841 | 842 | 843 | 844 | 845 | 846 |

value(key) → {Node|null}

847 | 848 | 849 | 850 | 851 | 852 |
853 | Return the unmodified value of a node lookup. 854 |
855 | 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 |
Parameters:
865 | 866 | 867 | 868 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877 | 878 | 879 | 880 | 881 | 882 | 883 | 884 | 885 | 886 | 887 | 888 | 889 | 890 | 891 | 892 | 900 | 901 | 902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 | 910 |
NameTypeDescription
key 893 | 894 | 895 | String 896 | 897 | 898 | 899 | The name/uid of the node.
911 | 912 | 913 | 914 | 915 | 916 | 917 |
918 | 919 | 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | 932 | 933 | 934 | 935 | 936 | 937 | 938 | 939 | 940 | 941 | 942 | 943 | 944 |
Source:
945 |
948 | 949 | 950 | 951 | 952 | 953 | 954 | 955 |
956 | 957 | 958 | 959 | 960 | 961 | 962 | 963 | 964 | 965 | 966 | 967 | 968 | 969 |
Returns:
970 | 971 | 972 |
973 | - The node if found, otherwise `null`. 974 |
975 | 976 | 977 | 978 |
979 |
980 | Type 981 |
982 |
983 | 984 | Node 985 | | 986 | 987 | null 988 | 989 | 990 |
991 |
992 | 993 | 994 | 995 | 996 | 997 | 998 | 999 | 1000 | 1001 | 1002 | 1003 |
1004 | 1005 |
1006 | 1007 | 1008 | 1009 | 1010 |
1011 | 1012 |
1013 | 1014 | 1017 | 1018 | 1019 | 1020 | 1021 | -------------------------------------------------------------------------------- /docs/graph-crdt.module_Node-Node.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Node - Postman Documentation 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 23 | 24 | 25 | 28 | 29 |
30 | 31 |

Node

32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
40 | 41 |
42 | 43 |

44 | .Node~ 45 | 46 | Node 47 |

48 | 49 | 50 |
51 | 52 |
53 |
54 | 55 | 56 | 57 | 58 | 59 |

new Node(configopt)

60 | 61 | 62 | 63 | 64 | 65 |
66 | An observable object with conflict-resolution. 67 |
68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 |
Parameters:
78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 115 | 116 | 117 | 126 | 127 | 128 | 129 | 130 | 193 | 194 | 195 | 196 | 197 |
NameTypeAttributesDescription
config 108 | 109 | 110 | Object 111 | 112 | 113 | 114 | 118 | 119 | <optional>
120 | 121 | 122 | 123 | 124 | 125 |
Instance level configuration. 131 |
Properties
132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 169 | 170 | 171 | 180 | 181 | 182 | 183 | 184 | 186 | 187 | 188 | 189 | 190 |
NameTypeAttributesDescription
uid 162 | 163 | 164 | Object 165 | 166 | 167 | 168 | 172 | 173 | <optional>
174 | 175 | 176 | 177 | 178 | 179 |
Override the randomly generated 185 | node uid.
191 | 192 |
198 | 199 | 200 | 201 | 202 | 203 | 204 |
205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 |
Source:
232 |
235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 |
243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 |
261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 |
280 | 281 |
282 | 283 | 284 | 285 | 286 |
287 | 288 |
289 | 290 | 293 | 294 | 295 | 296 | 297 | -------------------------------------------------------------------------------- /docs/graph-crdt.module_Node.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Node - Postman Documentation 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 23 | 24 | 25 | 28 | 29 |
30 | 31 |

Node

32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
40 | 41 |
42 | 43 |
44 | 45 |
46 |
47 | 48 | 49 | 50 | 51 | 52 | 53 |
54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 |
Source:
81 |
84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 |
92 | 93 | 94 | 95 | 96 |
97 | 98 | 99 | 100 | 101 | 102 | 103 |

Classes

104 | 105 |
106 |
Node
107 |
108 |
109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 |

Members

117 | 118 | 119 | 120 |

[undefined][undefined]

121 | 122 | 123 | 124 | 125 |
126 | Immediately apply updates. 127 |
128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 |
136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 |
Source:
163 |
166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 |
174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 |

Methods

185 | 186 | 187 | 188 | 189 | 190 | 191 |

(static) create(configopt) → {Node}

192 | 193 | 194 | 195 | 196 | 197 |
198 | Creates a new Node instance without using 199 | `new`. 200 |
201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 |
Parameters:
211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 248 | 249 | 250 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 |
NameTypeAttributesDescription
config 241 | 242 | 243 | Object 244 | 245 | 246 | 247 | 251 | 252 | <optional>
253 | 254 | 255 | 256 | 257 | 258 |
The constructor configuration object.
269 | 270 | 271 | 272 | 273 | 274 | 275 |
276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 |
Source:
303 |
306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 |
314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 |
Returns:
328 | 329 | 330 |
331 | - The new node instance. 332 |
333 | 334 | 335 | 336 |
337 |
338 | Type 339 |
340 |
341 | 342 | Node 343 | 344 | 345 |
346 |
347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 |

(static) from(object) → {Node}

358 | 359 | 360 | 361 | 362 | 363 |
364 | Turns a normal object into a Node instance. 365 | Properties to be imported must be enumerable 366 | and cannot be inherited via prototype. 367 |
368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 |
Parameters:
378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 |
NameTypeDescription
object 406 | 407 | 408 | Object 409 | 410 | 411 | 412 | Any enumerable object.
424 | 425 | 426 | 427 | 428 | 429 | 430 |
431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 |
Source:
458 |
461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 |
469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 |
Returns:
483 | 484 | 485 |
486 | - A node interface constructed from the object. 487 |
488 | 489 | 490 | 491 |
492 |
493 | Type 494 |
495 |
496 | 497 | Node 498 | 499 | 500 |
501 |
502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 |

(static) source(object) → {Node}

513 | 514 | 515 | 516 | 517 | 518 |
519 | Take an object and use it as the data source for a new 520 | node. This method is used with properly formatted 521 | objects, such as stringified, then parsed node instances. 522 |
523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 |
Parameters:
533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 |
NameTypeDescription
object 561 | 562 | 563 | Object 564 | 565 | 566 | 567 | The preformatted object.
579 | 580 | 581 | 582 | 583 | 584 | 585 |
586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 |
Source:
613 |
616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 |
624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 |
Returns:
638 | 639 | 640 |
641 | - A new node instance. 642 |
643 | 644 | 645 | 646 |
647 |
648 | Type 649 |
650 |
651 | 652 | Node 653 | 654 | 655 |
656 |
657 | 658 | 659 | 660 | 661 |
Example
662 | 663 |
const original = Node.create()
 664 | original.merge({ data: 'intact' })
 665 | const serialized = JSON.stringify(original)
 666 | const parsed = JSON.parse(serialized)
 667 | 
 668 | const node = Node.source(parsed)
 669 | node.value('data') // 'intact'
670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 |

merge(incoming) → {Object}

679 | 680 | 681 | 682 | 683 | 684 |
685 | Merges an update into the current node. 686 |
687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 |
Parameters:
697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 732 | 733 | 734 | 735 | 736 | 737 | 740 | 741 | 742 | 743 | 744 |
NameTypeDescription
incoming 725 | 726 | 727 | Node 728 | 729 | 730 | 731 | The node to merge from. 738 | If a plain object is passed, it will be upgraded to a node 739 | using `Node.from`.
745 | 746 | 747 | 748 | 749 | 750 | 751 |
752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 |
Source:
779 |
782 | 783 | 784 | 785 | 786 | 787 | 788 | 789 |
790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 | 798 | 799 | 800 | 801 | 802 | 803 |
Returns:
804 | 805 | 806 |
807 | - A collection of changes caused by the merge. 808 |
809 | 810 | 811 | 812 |
813 |
814 | Type 815 |
816 |
817 | 818 | Object 819 | 820 | 821 |
822 |
823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 |

meta(fieldopt) → {Object|null}

834 | 835 | 836 | 837 | 838 | 839 |
840 | Read metadata, either on a field, or 841 | on the entire object. 842 |
843 | 844 | 845 | 846 | 847 | 848 | 849 | 850 | 851 | 852 |
Parameters:
853 | 854 | 855 | 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | 866 | 867 | 868 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877 | 878 | 879 | 880 | 881 | 882 | 890 | 891 | 892 | 901 | 902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 | 910 |
NameTypeAttributesDescription
field 883 | 884 | 885 | String 886 | 887 | 888 | 889 | 893 | 894 | <optional>
895 | 896 | 897 | 898 | 899 | 900 |
The property to read metadata from.
911 | 912 | 913 | 914 | 915 | 916 | 917 |
918 | 919 | 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | 932 | 933 | 934 | 935 | 936 | 937 | 938 | 939 | 940 | 941 | 942 | 943 | 944 |
Source:
945 |
948 | 949 | 950 | 951 | 952 | 953 | 954 | 955 |
956 | 957 | 958 | 959 | 960 | 961 | 962 | 963 | 964 | 965 | 966 | 967 | 968 | 969 |
Returns:
970 | 971 | 972 |
973 | - If metadata is found, it returns the object. 974 | Otherwise `null` is given. 975 |
976 | 977 | 978 | 979 |
980 |
981 | Type 982 |
983 |
984 | 985 | Object 986 | | 987 | 988 | null 989 | 990 | 991 |
992 |
993 | 994 | 995 | 996 | 997 | 998 | 999 | 1000 | 1001 | 1002 | 1003 |

new() → {Node}

1004 | 1005 | 1006 | 1007 | 1008 | 1009 |
1010 | Creates an empty instance with the same configuration. 1011 |
1012 | 1013 | 1014 | 1015 | 1016 | 1017 | 1018 | 1019 | 1020 | 1021 | 1022 | 1023 | 1024 | 1025 |
1026 | 1027 | 1028 | 1029 | 1030 | 1031 | 1032 | 1033 | 1034 | 1035 | 1036 | 1037 | 1038 | 1039 | 1040 | 1041 | 1042 | 1043 | 1044 | 1045 | 1046 | 1047 | 1048 | 1049 | 1050 | 1051 | 1052 |
Source:
1053 |
1056 | 1057 | 1058 | 1059 | 1060 | 1061 | 1062 | 1063 |
1064 | 1065 | 1066 | 1067 | 1068 | 1069 | 1070 | 1071 | 1072 | 1073 | 1074 | 1075 | 1076 | 1077 |
Returns:
1078 | 1079 | 1080 |
1081 | - A new node instance with the same properties. 1082 |
1083 | 1084 | 1085 | 1086 |
1087 |
1088 | Type 1089 |
1090 |
1091 | 1092 | Node 1093 | 1094 | 1095 |
1096 |
1097 | 1098 | 1099 | 1100 | 1101 | 1102 | 1103 | 1104 | 1105 | 1106 | 1107 |

overlap(target) → {Node}

1108 | 1109 | 1110 | 1111 | 1112 | 1113 |
1114 | Calculates the intersection between two nodes. 1115 |
1116 | 1117 | 1118 | 1119 | 1120 | 1121 | 1122 | 1123 | 1124 | 1125 |
Parameters:
1126 | 1127 | 1128 | 1129 | 1130 | 1131 | 1132 | 1133 | 1134 | 1135 | 1136 | 1137 | 1138 | 1139 | 1140 | 1141 | 1142 | 1143 | 1144 | 1145 | 1146 | 1147 | 1148 | 1149 | 1150 | 1151 | 1152 | 1153 | 1161 | 1162 | 1163 | 1164 | 1165 | 1166 | 1167 | 1168 | 1169 | 1170 | 1171 |
NameTypeDescription
target 1154 | 1155 | 1156 | Node 1157 | 1158 | 1159 | 1160 | Any other node.
1172 | 1173 | 1174 | 1175 | 1176 | 1177 | 1178 |
1179 | 1180 | 1181 | 1182 | 1183 | 1184 | 1185 | 1186 | 1187 | 1188 | 1189 | 1190 | 1191 | 1192 | 1193 | 1194 | 1195 | 1196 | 1197 | 1198 | 1199 | 1200 | 1201 | 1202 | 1203 | 1204 | 1205 |
Source:
1206 |
1209 | 1210 | 1211 | 1212 | 1213 | 1214 | 1215 | 1216 |
1217 | 1218 | 1219 | 1220 | 1221 | 1222 | 1223 | 1224 | 1225 | 1226 | 1227 | 1228 | 1229 | 1230 |
Returns:
1231 | 1232 | 1233 |
1234 | - A collection of fields common to both nodes. 1235 |
1236 | 1237 | 1238 | 1239 |
1240 |
1241 | Type 1242 |
1243 |
1244 | 1245 | Node 1246 | 1247 | 1248 |
1249 |
1250 | 1251 | 1252 | 1253 | 1254 | 1255 | 1256 | 1257 | 1258 | 1259 | 1260 |

rebase(target) → {Node}

1261 | 1262 | 1263 | 1264 | 1265 | 1266 |
1267 | Takes the changes from the current node and plays them after the 1268 | changes in the target node. 1269 | Similar to git rebase, but without the conflicts. 1270 |
1271 | 1272 | 1273 | 1274 | 1275 | 1276 | 1277 | 1278 | 1279 | 1280 |
Parameters:
1281 | 1282 | 1283 | 1284 | 1285 | 1286 | 1287 | 1288 | 1289 | 1290 | 1291 | 1292 | 1293 | 1294 | 1295 | 1296 | 1297 | 1298 | 1299 | 1300 | 1301 | 1302 | 1303 | 1304 | 1305 | 1306 | 1307 | 1308 | 1316 | 1317 | 1318 | 1319 | 1320 | 1321 | 1322 | 1323 | 1324 | 1325 | 1326 |
NameTypeDescription
target 1309 | 1310 | 1311 | Node 1312 | 1313 | 1314 | 1315 | Preceding state.
1327 | 1328 | 1329 | 1330 | 1331 | 1332 | 1333 |
1334 | 1335 | 1336 | 1337 | 1338 | 1339 | 1340 | 1341 | 1342 | 1343 | 1344 | 1345 | 1346 | 1347 | 1348 | 1349 | 1350 | 1351 | 1352 | 1353 | 1354 | 1355 | 1356 | 1357 | 1358 | 1359 | 1360 |
Source:
1361 |
1364 | 1365 | 1366 | 1367 | 1368 | 1369 | 1370 | 1371 |
1372 | 1373 | 1374 | 1375 | 1376 | 1377 | 1378 | 1379 | 1380 | 1381 | 1382 | 1383 | 1384 | 1385 |
Returns:
1386 | 1387 | 1388 |
1389 | - A new, rebased node. 1390 |
1391 | 1392 | 1393 | 1394 |
1395 |
1396 | Type 1397 |
1398 |
1399 | 1400 | Node 1401 | 1402 | 1403 |
1404 |
1405 | 1406 | 1407 | 1408 | 1409 | 1410 | 1411 | 1412 | 1413 | 1414 | 1415 |

setMetadata(field, metadata) → {Object}

1416 | 1417 | 1418 | 1419 | 1420 | 1421 |
1422 | Sets metadata for a field, bumping the current state. 1423 |
1424 | 1425 | 1426 | 1427 | 1428 | 1429 | 1430 | 1431 | 1432 | 1433 |
Parameters:
1434 | 1435 | 1436 | 1437 | 1438 | 1439 | 1440 | 1441 | 1442 | 1443 | 1444 | 1445 | 1446 | 1447 | 1448 | 1449 | 1450 | 1451 | 1452 | 1453 | 1454 | 1455 | 1456 | 1457 | 1458 | 1459 | 1460 | 1461 | 1469 | 1470 | 1471 | 1472 | 1473 | 1474 | 1475 | 1476 | 1477 | 1478 | 1479 | 1480 | 1481 | 1482 | 1483 | 1484 | 1492 | 1493 | 1494 | 1495 | 1496 | 1497 | 1498 | 1499 | 1500 | 1501 | 1502 |
NameTypeDescription
field 1462 | 1463 | 1464 | String 1465 | 1466 | 1467 | 1468 | A field to update.
metadata 1485 | 1486 | 1487 | Object 1488 | 1489 | 1490 | 1491 | What metadata the field should contain.
1503 | 1504 | 1505 | 1506 | 1507 | 1508 | 1509 |
1510 | 1511 | 1512 | 1513 | 1514 | 1515 | 1516 | 1517 | 1518 | 1519 | 1520 | 1521 | 1522 | 1523 | 1524 | 1525 | 1526 | 1527 | 1528 | 1529 | 1530 | 1531 | 1532 | 1533 | 1534 | 1535 | 1536 |
Source:
1537 |
1540 | 1541 | 1542 | 1543 | 1544 | 1545 | 1546 | 1547 |
1548 | 1549 | 1550 | 1551 | 1552 | 1553 | 1554 | 1555 | 1556 | 1557 | 1558 | 1559 | 1560 | 1561 |
Returns:
1562 | 1563 | 1564 |
1565 | - The metadata object (not the same one given). 1566 |
1567 | 1568 | 1569 | 1570 |
1571 |
1572 | Type 1573 |
1574 |
1575 | 1576 | Object 1577 | 1578 | 1579 |
1580 |
1581 | 1582 | 1583 | 1584 | 1585 | 1586 | 1587 | 1588 | 1589 | 1590 | 1591 |

snapshot() → {Object}

1592 | 1593 | 1594 | 1595 | 1596 | 1597 |
1598 | Takes a snapshot of the current state. 1599 |
1600 | 1601 | 1602 | 1603 | 1604 | 1605 | 1606 | 1607 | 1608 | 1609 | 1610 | 1611 | 1612 | 1613 |
1614 | 1615 | 1616 | 1617 | 1618 | 1619 | 1620 | 1621 | 1622 | 1623 | 1624 | 1625 | 1626 | 1627 | 1628 | 1629 | 1630 | 1631 | 1632 | 1633 | 1634 | 1635 | 1636 | 1637 | 1638 | 1639 | 1640 |
Source:
1641 |
1644 | 1645 | 1646 | 1647 | 1648 | 1649 | 1650 | 1651 |
1652 | 1653 | 1654 | 1655 | 1656 | 1657 | 1658 | 1659 | 1660 | 1661 | 1662 | 1663 | 1664 | 1665 |
Returns:
1666 | 1667 | 1668 |
1669 | - Every key and value (currently) in the node. 1670 |
1671 | 1672 | 1673 | 1674 |
1675 |
1676 | Type 1677 |
1678 |
1679 | 1680 | Object 1681 | 1682 | 1683 |
1684 |
1685 | 1686 | 1687 | 1688 | 1689 | 1690 | 1691 | 1692 | 1693 | 1694 | 1695 |

state(field) → {Number}

1696 | 1697 | 1698 | 1699 | 1700 | 1701 |
1702 | Get the current state of a property. 1703 |
1704 | 1705 | 1706 | 1707 | 1708 | 1709 | 1710 | 1711 | 1712 | 1713 |
Parameters:
1714 | 1715 | 1716 | 1717 | 1718 | 1719 | 1720 | 1721 | 1722 | 1723 | 1724 | 1725 | 1726 | 1727 | 1728 | 1729 | 1730 | 1731 | 1732 | 1733 | 1734 | 1735 | 1736 | 1737 | 1738 | 1739 | 1740 | 1741 | 1749 | 1750 | 1751 | 1752 | 1753 | 1754 | 1755 | 1756 | 1757 | 1758 | 1759 |
NameTypeDescription
field 1742 | 1743 | 1744 | String 1745 | 1746 | 1747 | 1748 | Property name.
1760 | 1761 | 1762 | 1763 | 1764 | 1765 | 1766 |
1767 | 1768 | 1769 | 1770 | 1771 | 1772 | 1773 | 1774 | 1775 | 1776 | 1777 | 1778 | 1779 | 1780 | 1781 | 1782 | 1783 | 1784 | 1785 | 1786 | 1787 | 1788 | 1789 | 1790 | 1791 | 1792 | 1793 |
Source:
1794 |
1797 | 1798 | 1799 | 1800 | 1801 | 1802 | 1803 | 1804 |
1805 | 1806 | 1807 | 1808 | 1809 | 1810 | 1811 | 1812 | 1813 | 1814 | 1815 | 1816 | 1817 | 1818 |
Returns:
1819 | 1820 | 1821 |
1822 | - Current lamport state (or 0). 1823 |
1824 | 1825 | 1826 | 1827 |
1828 |
1829 | Type 1830 |
1831 |
1832 | 1833 | Number 1834 | 1835 | 1836 |
1837 |
1838 | 1839 | 1840 | 1841 | 1842 | 1843 | 1844 | 1845 | 1846 | 1847 | 1848 |

value(field) → {Mixed}

1849 | 1850 | 1851 | 1852 | 1853 | 1854 |
1855 | Show the value of a node's property. 1856 |
1857 | 1858 | 1859 | 1860 | 1861 | 1862 | 1863 | 1864 | 1865 | 1866 |
Parameters:
1867 | 1868 | 1869 | 1870 | 1871 | 1872 | 1873 | 1874 | 1875 | 1876 | 1877 | 1878 | 1879 | 1880 | 1881 | 1882 | 1883 | 1884 | 1885 | 1886 | 1887 | 1888 | 1889 | 1890 | 1891 | 1892 | 1893 | 1894 | 1902 | 1903 | 1904 | 1905 | 1906 | 1907 | 1909 | 1910 | 1911 | 1912 | 1913 |
NameTypeDescription
field 1895 | 1896 | 1897 | String 1898 | 1899 | 1900 | 1901 | The name of the property 1908 | to retrieve.
1914 | 1915 | 1916 | 1917 | 1918 | 1919 | 1920 |
1921 | 1922 | 1923 | 1924 | 1925 | 1926 | 1927 | 1928 | 1929 | 1930 | 1931 | 1932 | 1933 | 1934 | 1935 | 1936 | 1937 | 1938 | 1939 | 1940 | 1941 | 1942 | 1943 | 1944 | 1945 | 1946 | 1947 |
Source:
1948 |
1951 | 1952 | 1953 | 1954 | 1955 | 1956 | 1957 | 1958 |
1959 | 1960 | 1961 | 1962 | 1963 | 1964 | 1965 | 1966 | 1967 | 1968 | 1969 | 1970 | 1971 | 1972 |
Returns:
1973 | 1974 | 1975 |
1976 | - The value of the property, 1977 | or undefined if it doesn't exist. Cannot be called 1978 | on reserved fields (like "@object"). 1979 |
1980 | 1981 | 1982 | 1983 |
1984 |
1985 | Type 1986 |
1987 |
1988 | 1989 | Mixed 1990 | 1991 | 1992 |
1993 |
1994 | 1995 | 1996 | 1997 | 1998 | 1999 | 2000 | 2001 | 2002 | 2003 | 2004 | 2005 |
2006 | 2007 |
2008 | 2009 | 2010 | 2011 | 2012 |
2013 | 2014 |
2015 | 2016 | 2019 | 2020 | 2021 | 2022 | 2023 | -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Home - Postman Documentation 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 23 | 24 | 25 | 28 | 29 |
30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
50 |

Distributed Graph Engine

Travis branch

51 |
52 |

graph-crdt is a work in progress with frequent breaking changes.

53 |
54 |

Designed for serializing arbitrary data structures, making offline edits, and seamlessly merging changes back in. All data is observable and event driven.

55 |

What for?

This graph library aims to ease the complexity of synchronizing complex and interconnected state between peers, without assuming centralized authority.

56 |

How does it work?

Truly offline systems cannot rely on any form of collaboration. They must (at some point) assume the editor is in complete isolation, such as a smartphone that lost cell service, or a server who's network is unreachable.

57 |

You have a few options:

58 |
    59 |
  • Block writes
    60 | Probably the worst experience, block all writes until the network heals. This is essentially the same as losing socket connection to your database (Rethink, Neo4j, Redis, MySQL, etc.)

    61 |
  • 62 |
  • Defer the updates
    63 | You allow writes on the offline machine, wait for the network to heal, then publish them. If not handled perfectly, you're susceptible to merge hell on an active production environment.

    64 |
  • 65 |
  • Use a CRDT
    66 | CRDTs (Convergent Replicated Data Types) are similar to the option above, but come with additional guarantees: regardless of the order which updates are received in, every machine will arrive at the exact same result every time, and if implemented correctly, make merge conflicts impossible*.

    67 |
  • 68 |
69 |

70 | *graph-crdt uses Lamport time to track state mutation and resolves concurrent edit conflicts using a deterministic sorting algorithm. 71 |

72 |

This library opts for the latter, implementing a delta graph CvRDT. However, as great as they may seem, there are some cons (some specific to this library):

73 |
    74 |
  • You need more data.
    75 | Merges need a state integer on each field.

    76 |
  • 77 |
  • There is no "true" delete.
    78 | You can remove the value, but some metadata has to stay around.

    79 |
  • 80 |
  • It only plays nice with other CRDTs.
    81 | To merge two states, both must have the CRDT metadata (though this library allows you to upgrade nearly any data).

    82 |
  • 83 |
84 |

Features

    85 |
  • Commutative, idempotent, conflict-resolved Node unions.
  • 86 |
  • Delta emission on Node and Graph unions.
  • 87 |
  • Time travel (track and selectively apply deltas).
  • 88 |
89 |

Documentation

All the API docs can be found here.

90 |

Roadmap

    91 |
  1. Node field tombstones.
  2. 92 |
  3. Graph member tombstones.
  4. 93 |
  5. Custom conflict resolvers.
  6. 94 |
  7. A new data structure (this one is a surprise).
  8. 95 |
96 |

Disclaimer

Although I have working experience with decentralized systems (at GunDB), I'm still a n00b. This library is my best understanding of CvRDTs and how they operate. I'm open to most suggestions.

97 |
98 | 99 | 100 | 101 | 102 | 103 | 104 |
105 | 106 |
107 | 108 | 111 | 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /docs/scripts/linenumber.js: -------------------------------------------------------------------------------- 1 | /*global document */ 2 | (function() { 3 | var source = document.getElementsByClassName('prettyprint source linenums'); 4 | var i = 0; 5 | var lineNumber = 0; 6 | var lineId; 7 | var lines; 8 | var totalLines; 9 | var anchorHash; 10 | 11 | if (source && source[0]) { 12 | anchorHash = document.location.hash.substring(1); 13 | lines = source[0].getElementsByTagName('li'); 14 | totalLines = lines.length; 15 | 16 | for (; i < totalLines; i++) { 17 | lineNumber++; 18 | lineId = 'line' + lineNumber; 19 | lines[i].id = lineId; 20 | if (lineId === anchorHash) { 21 | lines[i].className += ' selected'; 22 | } 23 | } 24 | } 25 | })(); 26 | -------------------------------------------------------------------------------- /docs/scripts/prettify/Apache-License-2.0.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /docs/scripts/prettify/lang-css.js: -------------------------------------------------------------------------------- 1 | PR.registerLangHandler(PR.createSimpleLexer([["pln",/^[\t\n\f\r ]+/,null," \t\r\n "]],[["str",/^"(?:[^\n\f\r"\\]|\\(?:\r\n?|\n|\f)|\\[\S\s])*"/,null],["str",/^'(?:[^\n\f\r'\\]|\\(?:\r\n?|\n|\f)|\\[\S\s])*'/,null],["lang-css-str",/^url\(([^"')]*)\)/i],["kwd",/^(?:url|rgb|!important|@import|@page|@media|@charset|inherit)(?=[^\w-]|$)/i,null],["lang-css-kw",/^(-?(?:[_a-z]|\\[\da-f]+ ?)(?:[\w-]|\\\\[\da-f]+ ?)*)\s*:/i],["com",/^\/\*[^*]*\*+(?:[^*/][^*]*\*+)*\//],["com", 2 | /^(?:<\!--|--\>)/],["lit",/^(?:\d+|\d*\.\d+)(?:%|[a-z]+)?/i],["lit",/^#[\da-f]{3,6}/i],["pln",/^-?(?:[_a-z]|\\[\da-f]+ ?)(?:[\w-]|\\\\[\da-f]+ ?)*/i],["pun",/^[^\s\w"']+/]]),["css"]);PR.registerLangHandler(PR.createSimpleLexer([],[["kwd",/^-?(?:[_a-z]|\\[\da-f]+ ?)(?:[\w-]|\\\\[\da-f]+ ?)*/i]]),["css-kw"]);PR.registerLangHandler(PR.createSimpleLexer([],[["str",/^[^"')]+/]]),["css-str"]); 3 | -------------------------------------------------------------------------------- /docs/scripts/prettify/prettify.js: -------------------------------------------------------------------------------- 1 | var q=null;window.PR_SHOULD_USE_CONTINUATION=!0; 2 | (function(){function L(a){function m(a){var f=a.charCodeAt(0);if(f!==92)return f;var b=a.charAt(1);return(f=r[b])?f:"0"<=b&&b<="7"?parseInt(a.substring(1),8):b==="u"||b==="x"?parseInt(a.substring(2),16):a.charCodeAt(1)}function e(a){if(a<32)return(a<16?"\\x0":"\\x")+a.toString(16);a=String.fromCharCode(a);if(a==="\\"||a==="-"||a==="["||a==="]")a="\\"+a;return a}function h(a){for(var f=a.substring(1,a.length-1).match(/\\u[\dA-Fa-f]{4}|\\x[\dA-Fa-f]{2}|\\[0-3][0-7]{0,2}|\\[0-7]{1,2}|\\[\S\s]|[^\\]/g),a= 3 | [],b=[],o=f[0]==="^",c=o?1:0,i=f.length;c122||(d<65||j>90||b.push([Math.max(65,j)|32,Math.min(d,90)|32]),d<97||j>122||b.push([Math.max(97,j)&-33,Math.min(d,122)&-33]))}}b.sort(function(a,f){return a[0]-f[0]||f[1]-a[1]});f=[];j=[NaN,NaN];for(c=0;ci[0]&&(i[1]+1>i[0]&&b.push("-"),b.push(e(i[1])));b.push("]");return b.join("")}function y(a){for(var f=a.source.match(/\[(?:[^\\\]]|\\[\S\s])*]|\\u[\dA-Fa-f]{4}|\\x[\dA-Fa-f]{2}|\\\d+|\\[^\dux]|\(\?[!:=]|[()^]|[^()[\\^]+/g),b=f.length,d=[],c=0,i=0;c=2&&a==="["?f[c]=h(j):a!=="\\"&&(f[c]=j.replace(/[A-Za-z]/g,function(a){a=a.charCodeAt(0);return"["+String.fromCharCode(a&-33,a|32)+"]"}));return f.join("")}for(var t=0,s=!1,l=!1,p=0,d=a.length;p=5&&"lang-"===b.substring(0,5))&&!(o&&typeof o[1]==="string"))c=!1,b="src";c||(r[f]=b)}i=d;d+=f.length;if(c){c=o[1];var j=f.indexOf(c),k=j+c.length;o[2]&&(k=f.length-o[2].length,j=k-c.length);b=b.substring(5);B(l+i,f.substring(0,j),e,p);B(l+i+j,c,C(b,c),p);B(l+i+k,f.substring(k),e,p)}else p.push(l+i,b)}a.e=p}var h={},y;(function(){for(var e=a.concat(m), 9 | l=[],p={},d=0,g=e.length;d=0;)h[n.charAt(k)]=r;r=r[1];n=""+r;p.hasOwnProperty(n)||(l.push(r),p[n]=q)}l.push(/[\S\s]/);y=L(l)})();var t=m.length;return e}function u(a){var m=[],e=[];a.tripleQuotedStrings?m.push(["str",/^(?:'''(?:[^'\\]|\\[\S\s]|''?(?=[^']))*(?:'''|$)|"""(?:[^"\\]|\\[\S\s]|""?(?=[^"]))*(?:"""|$)|'(?:[^'\\]|\\[\S\s])*(?:'|$)|"(?:[^"\\]|\\[\S\s])*(?:"|$))/,q,"'\""]):a.multiLineStrings?m.push(["str",/^(?:'(?:[^'\\]|\\[\S\s])*(?:'|$)|"(?:[^"\\]|\\[\S\s])*(?:"|$)|`(?:[^\\`]|\\[\S\s])*(?:`|$))/, 10 | q,"'\"`"]):m.push(["str",/^(?:'(?:[^\n\r'\\]|\\.)*(?:'|$)|"(?:[^\n\r"\\]|\\.)*(?:"|$))/,q,"\"'"]);a.verbatimStrings&&e.push(["str",/^@"(?:[^"]|"")*(?:"|$)/,q]);var h=a.hashComments;h&&(a.cStyleComments?(h>1?m.push(["com",/^#(?:##(?:[^#]|#(?!##))*(?:###|$)|.*)/,q,"#"]):m.push(["com",/^#(?:(?:define|elif|else|endif|error|ifdef|include|ifndef|line|pragma|undef|warning)\b|[^\n\r]*)/,q,"#"]),e.push(["str",/^<(?:(?:(?:\.\.\/)*|\/?)(?:[\w-]+(?:\/[\w-]+)+)?[\w-]+\.h|[a-z]\w*)>/,q])):m.push(["com",/^#[^\n\r]*/, 11 | q,"#"]));a.cStyleComments&&(e.push(["com",/^\/\/[^\n\r]*/,q]),e.push(["com",/^\/\*[\S\s]*?(?:\*\/|$)/,q]));a.regexLiterals&&e.push(["lang-regex",/^(?:^^\.?|[!+-]|!=|!==|#|%|%=|&|&&|&&=|&=|\(|\*|\*=|\+=|,|-=|->|\/|\/=|:|::|;|<|<<|<<=|<=|=|==|===|>|>=|>>|>>=|>>>|>>>=|[?@[^]|\^=|\^\^|\^\^=|{|\||\|=|\|\||\|\|=|~|break|case|continue|delete|do|else|finally|instanceof|return|throw|try|typeof)\s*(\/(?=[^*/])(?:[^/[\\]|\\[\S\s]|\[(?:[^\\\]]|\\[\S\s])*(?:]|$))+\/)/]);(h=a.types)&&e.push(["typ",h]);a=(""+a.keywords).replace(/^ | $/g, 12 | "");a.length&&e.push(["kwd",RegExp("^(?:"+a.replace(/[\s,]+/g,"|")+")\\b"),q]);m.push(["pln",/^\s+/,q," \r\n\t\xa0"]);e.push(["lit",/^@[$_a-z][\w$@]*/i,q],["typ",/^(?:[@_]?[A-Z]+[a-z][\w$@]*|\w+_t\b)/,q],["pln",/^[$_a-z][\w$@]*/i,q],["lit",/^(?:0x[\da-f]+|(?:\d(?:_\d+)*\d*(?:\.\d*)?|\.\d\+)(?:e[+-]?\d+)?)[a-z]*/i,q,"0123456789"],["pln",/^\\[\S\s]?/,q],["pun",/^.[^\s\w"-$'./@\\`]*/,q]);return x(m,e)}function D(a,m){function e(a){switch(a.nodeType){case 1:if(k.test(a.className))break;if("BR"===a.nodeName)h(a), 13 | a.parentNode&&a.parentNode.removeChild(a);else for(a=a.firstChild;a;a=a.nextSibling)e(a);break;case 3:case 4:if(p){var b=a.nodeValue,d=b.match(t);if(d){var c=b.substring(0,d.index);a.nodeValue=c;(b=b.substring(d.index+d[0].length))&&a.parentNode.insertBefore(s.createTextNode(b),a.nextSibling);h(a);c||a.parentNode.removeChild(a)}}}}function h(a){function b(a,d){var e=d?a.cloneNode(!1):a,f=a.parentNode;if(f){var f=b(f,1),g=a.nextSibling;f.appendChild(e);for(var h=g;h;h=g)g=h.nextSibling,f.appendChild(h)}return e} 14 | for(;!a.nextSibling;)if(a=a.parentNode,!a)return;for(var a=b(a.nextSibling,0),e;(e=a.parentNode)&&e.nodeType===1;)a=e;d.push(a)}var k=/(?:^|\s)nocode(?:\s|$)/,t=/\r\n?|\n/,s=a.ownerDocument,l;a.currentStyle?l=a.currentStyle.whiteSpace:window.getComputedStyle&&(l=s.defaultView.getComputedStyle(a,q).getPropertyValue("white-space"));var p=l&&"pre"===l.substring(0,3);for(l=s.createElement("LI");a.firstChild;)l.appendChild(a.firstChild);for(var d=[l],g=0;g=0;){var h=m[e];A.hasOwnProperty(h)?window.console&&console.warn("cannot override language handler %s",h):A[h]=a}}function C(a,m){if(!a||!A.hasOwnProperty(a))a=/^\s*=o&&(h+=2);e>=c&&(a+=2)}}catch(w){"console"in window&&console.log(w&&w.stack?w.stack:w)}}var v=["break,continue,do,else,for,if,return,while"],w=[[v,"auto,case,char,const,default,double,enum,extern,float,goto,int,long,register,short,signed,sizeof,static,struct,switch,typedef,union,unsigned,void,volatile"], 18 | "catch,class,delete,false,import,new,operator,private,protected,public,this,throw,true,try,typeof"],F=[w,"alignof,align_union,asm,axiom,bool,concept,concept_map,const_cast,constexpr,decltype,dynamic_cast,explicit,export,friend,inline,late_check,mutable,namespace,nullptr,reinterpret_cast,static_assert,static_cast,template,typeid,typename,using,virtual,where"],G=[w,"abstract,boolean,byte,extends,final,finally,implements,import,instanceof,null,native,package,strictfp,super,synchronized,throws,transient"], 19 | H=[G,"as,base,by,checked,decimal,delegate,descending,dynamic,event,fixed,foreach,from,group,implicit,in,interface,internal,into,is,lock,object,out,override,orderby,params,partial,readonly,ref,sbyte,sealed,stackalloc,string,select,uint,ulong,unchecked,unsafe,ushort,var"],w=[w,"debugger,eval,export,function,get,null,set,undefined,var,with,Infinity,NaN"],I=[v,"and,as,assert,class,def,del,elif,except,exec,finally,from,global,import,in,is,lambda,nonlocal,not,or,pass,print,raise,try,with,yield,False,True,None"], 20 | J=[v,"alias,and,begin,case,class,def,defined,elsif,end,ensure,false,in,module,next,nil,not,or,redo,rescue,retry,self,super,then,true,undef,unless,until,when,yield,BEGIN,END"],v=[v,"case,done,elif,esac,eval,fi,function,in,local,set,then,until"],K=/^(DIR|FILE|vector|(de|priority_)?queue|list|stack|(const_)?iterator|(multi)?(set|map)|bitset|u?(int|float)\d*)/,N=/\S/,O=u({keywords:[F,H,w,"caller,delete,die,do,dump,elsif,eval,exit,foreach,for,goto,if,import,last,local,my,next,no,our,print,package,redo,require,sub,undef,unless,until,use,wantarray,while,BEGIN,END"+ 21 | I,J,v],hashComments:!0,cStyleComments:!0,multiLineStrings:!0,regexLiterals:!0}),A={};k(O,["default-code"]);k(x([],[["pln",/^[^]*(?:>|$)/],["com",/^<\!--[\S\s]*?(?:--\>|$)/],["lang-",/^<\?([\S\s]+?)(?:\?>|$)/],["lang-",/^<%([\S\s]+?)(?:%>|$)/],["pun",/^(?:<[%?]|[%?]>)/],["lang-",/^]*>([\S\s]+?)<\/xmp\b[^>]*>/i],["lang-js",/^]*>([\S\s]*?)(<\/script\b[^>]*>)/i],["lang-css",/^]*>([\S\s]*?)(<\/style\b[^>]*>)/i],["lang-in.tag",/^(<\/?[a-z][^<>]*>)/i]]), 22 | ["default-markup","htm","html","mxml","xhtml","xml","xsl"]);k(x([["pln",/^\s+/,q," \t\r\n"],["atv",/^(?:"[^"]*"?|'[^']*'?)/,q,"\"'"]],[["tag",/^^<\/?[a-z](?:[\w-.:]*\w)?|\/?>$/i],["atn",/^(?!style[\s=]|on)[a-z](?:[\w:-]*\w)?/i],["lang-uq.val",/^=\s*([^\s"'>]*(?:[^\s"'/>]|\/(?=\s)))/],["pun",/^[/<->]+/],["lang-js",/^on\w+\s*=\s*"([^"]+)"/i],["lang-js",/^on\w+\s*=\s*'([^']+)'/i],["lang-js",/^on\w+\s*=\s*([^\s"'>]+)/i],["lang-css",/^style\s*=\s*"([^"]+)"/i],["lang-css",/^style\s*=\s*'([^']+)'/i],["lang-css", 23 | /^style\s*=\s*([^\s"'>]+)/i]]),["in.tag"]);k(x([],[["atv",/^[\S\s]+/]]),["uq.val"]);k(u({keywords:F,hashComments:!0,cStyleComments:!0,types:K}),["c","cc","cpp","cxx","cyc","m"]);k(u({keywords:"null,true,false"}),["json"]);k(u({keywords:H,hashComments:!0,cStyleComments:!0,verbatimStrings:!0,types:K}),["cs"]);k(u({keywords:G,cStyleComments:!0}),["java"]);k(u({keywords:v,hashComments:!0,multiLineStrings:!0}),["bsh","csh","sh"]);k(u({keywords:I,hashComments:!0,multiLineStrings:!0,tripleQuotedStrings:!0}), 24 | ["cv","py"]);k(u({keywords:"caller,delete,die,do,dump,elsif,eval,exit,foreach,for,goto,if,import,last,local,my,next,no,our,print,package,redo,require,sub,undef,unless,until,use,wantarray,while,BEGIN,END",hashComments:!0,multiLineStrings:!0,regexLiterals:!0}),["perl","pl","pm"]);k(u({keywords:J,hashComments:!0,multiLineStrings:!0,regexLiterals:!0}),["rb"]);k(u({keywords:w,cStyleComments:!0,regexLiterals:!0}),["js"]);k(u({keywords:"all,and,by,catch,class,else,extends,false,finally,for,if,in,is,isnt,loop,new,no,not,null,of,off,on,or,return,super,then,true,try,unless,until,when,while,yes", 25 | hashComments:3,cStyleComments:!0,multilineStrings:!0,tripleQuotedStrings:!0,regexLiterals:!0}),["coffee"]);k(x([],[["str",/^[\S\s]+/]]),["regex"]);window.prettyPrintOne=function(a,m,e){var h=document.createElement("PRE");h.innerHTML=a;e&&D(h,e);E({g:m,i:e,h:h});return h.innerHTML};window.prettyPrint=function(a){function m(){for(var e=window.PR_SHOULD_USE_CONTINUATION?l.now()+250:Infinity;p=0){var k=k.match(g),f,b;if(b= 26 | !k){b=n;for(var o=void 0,c=b.firstChild;c;c=c.nextSibling)var i=c.nodeType,o=i===1?o?b:c:i===3?N.test(c.nodeValue)?b:o:o;b=(f=o===b?void 0:o)&&"CODE"===f.tagName}b&&(k=f.className.match(g));k&&(k=k[1]);b=!1;for(o=n.parentNode;o;o=o.parentNode)if((o.tagName==="pre"||o.tagName==="code"||o.tagName==="xmp")&&o.className&&o.className.indexOf("prettyprint")>=0){b=!0;break}b||((b=(b=n.className.match(/\blinenums\b(?::(\d+))?/))?b[1]&&b[1].length?+b[1]:!0:!1)&&D(n,b),d={g:k,h:n,i:b},E(d))}}p h2 > a { 158 | display: block; 159 | width: 100%; 160 | height: 100%; 161 | color: #fff !important; 162 | font-weight: 700; 163 | margin: 0; 164 | padding: 10px 10px 6px 10px; 165 | } 166 | 167 | nav h2, 168 | nav h3 { 169 | margin: 0; 170 | font-size: 13px; 171 | text-transform: uppercase; 172 | letter-spacing: 1px; 173 | font-weight: 700; 174 | line-height: 24px; 175 | color: #fff !important; 176 | } 177 | 178 | nav h3 { 179 | padding: 10px 10px 6px 10px; 180 | } 181 | nav ul { 182 | font-family: 'Lato'; 183 | font-size: 100%; 184 | line-height: 17px; 185 | padding: 0; 186 | margin: 0; 187 | list-style-type: none; 188 | } 189 | 190 | nav ul a { 191 | font-family: 'Lato'; 192 | line-height: 18px; 193 | padding: 0; 194 | display: block; 195 | font-size: 12px; 196 | transition: padding 0.2s, color 0.2s, font-weight 0.2s; 197 | } 198 | 199 | nav ul a:hover { 200 | color: #fff; 201 | text-decoration: none; 202 | font-weight: bold; 203 | } 204 | 205 | nav > ul { 206 | padding: 0; 207 | } 208 | 209 | nav > ul > li > a { 210 | margin: 0; 211 | color: #999; 212 | padding: 4px 10px 4px 10px; 213 | font-weight: 500; 214 | } 215 | 216 | nav ul ul { 217 | margin-bottom: 10px 218 | } 219 | 220 | nav ul ul a { 221 | color: hsl(207, 1%, 60%); 222 | border-left: 1px solid hsl(207, 10%, 86%); 223 | } 224 | 225 | nav ul ul a, 226 | nav ul ul a:active { 227 | padding-left: 20px 228 | } 229 | 230 | footer { 231 | color: hsl(0, 0%, 28%); 232 | margin-left: 250px; 233 | display: block; 234 | padding: 30px; 235 | font-size: 80%; 236 | color: #999; 237 | text-transform: lowercase; 238 | font-style: italic; 239 | } 240 | 241 | .ancestors { 242 | color: #999 243 | } 244 | 245 | .ancestors a { 246 | color: #999 !important; 247 | text-decoration: none; 248 | } 249 | 250 | .clear { 251 | clear: both 252 | } 253 | 254 | .important { 255 | font-weight: bold; 256 | color: #950B02; 257 | } 258 | 259 | .yes-def { 260 | text-indent: -1000px; 261 | } 262 | 263 | .type-signature { 264 | color: #aaa; 265 | } 266 | 267 | .signature { 268 | font-family: 'Source Code Pro'; 269 | font-size: 80%; 270 | } 271 | 272 | .name { 273 | font-family: 'Source Code Pro'; 274 | font-size: 110%; 275 | font-weight: bold; 276 | color: #ff6429; 277 | padding-top: 30px; 278 | } 279 | 280 | .details { 281 | margin: 0; 282 | color: #999; 283 | } 284 | 285 | .details .tag-source a { 286 | color: #999; 287 | } 288 | .details dt { 289 | width: 120px; 290 | float: left; 291 | padding-left: 10px; 292 | } 293 | 294 | .details dd { 295 | margin-left: 70px 296 | } 297 | 298 | .details ul { 299 | margin: 0 300 | } 301 | 302 | .details ul { 303 | list-style-type: none 304 | } 305 | 306 | .details li { 307 | margin-left: 30px 308 | } 309 | 310 | .details pre.prettyprint { 311 | margin: 0 312 | } 313 | 314 | .details .object-value { 315 | padding-top: 0 316 | } 317 | 318 | .description { 319 | margin-bottom: 1em; 320 | margin-top: 1em; 321 | } 322 | 323 | .code-caption { 324 | font-style: italic; 325 | font-size: 107%; 326 | margin: 0; 327 | } 328 | 329 | .prettyprint { 330 | font-size: 13px; 331 | border: 1px solid #ddd; 332 | border-radius: 3px; 333 | box-shadow: 0 1px 3px hsla(0, 0%, 0%, 0.05); 334 | overflow: auto; 335 | } 336 | 337 | .prettyprint.source { 338 | width: inherit 339 | } 340 | 341 | .prettyprint code { 342 | font-size: 100%; 343 | line-height: 18px; 344 | display: block; 345 | margin: 0 30px; 346 | background-color: #fff; 347 | color: #4D4E53; 348 | } 349 | 350 | .prettyprint > code { 351 | padding: 15px 352 | } 353 | 354 | .prettyprint .linenums code { 355 | padding: 0 15px 356 | } 357 | 358 | .prettyprint .linenums li:first-of-type code { 359 | padding-top: 15px 360 | } 361 | 362 | .prettyprint code span.line { 363 | display: inline-block 364 | } 365 | 366 | .prettyprint.linenums { 367 | padding-left: 70px; 368 | -webkit-user-select: none; 369 | -moz-user-select: none; 370 | -ms-user-select: none; 371 | user-select: none; 372 | } 373 | 374 | .prettyprint.linenums ol { 375 | padding-left: 0 376 | } 377 | 378 | .prettyprint.linenums li { 379 | border-left: 3px #ddd solid 380 | } 381 | 382 | .prettyprint.linenums li.selected, .prettyprint.linenums li.selected * { 383 | background-color: lightyellow 384 | } 385 | 386 | .prettyprint.linenums li * { 387 | -webkit-user-select: text; 388 | -moz-user-select: text; 389 | -ms-user-select: text; 390 | user-select: text; 391 | } 392 | 393 | .params, .props { 394 | border-spacing: 0; 395 | border: 1px solid #ddd; 396 | border-collapse: collapse; 397 | border-radius: 3px; 398 | box-shadow: 0 1px 3px rgba(0,0,0,0.1); 399 | width: 100%; 400 | font-size: 14px; 401 | } 402 | 403 | .params .name, .props .name, .name code { 404 | color: #4D4E53; 405 | font-family: 'Source Code Pro'; 406 | font-size: 100%; 407 | } 408 | 409 | .params td, .params th, .props td, .props th { 410 | margin: 0px; 411 | text-align: left; 412 | vertical-align: top; 413 | padding: 10px; 414 | display: table-cell; 415 | } 416 | 417 | .params td { 418 | border-top: 1px solid #eee 419 | } 420 | 421 | .params thead tr, .props thead tr { 422 | background-color: #fff; 423 | font-weight: bold; 424 | } 425 | 426 | .params .params thead tr, .props .props thead tr { 427 | background-color: #fff; 428 | font-weight: bold; 429 | } 430 | 431 | .params td.description > p:first-child, .props td.description > p:first-child { 432 | margin-top: 0; 433 | padding-top: 0; 434 | } 435 | 436 | .params td.description > p:last-child, .props td.description > p:last-child { 437 | margin-bottom: 0; 438 | padding-bottom: 0; 439 | } 440 | 441 | dl.param-type { 442 | border-bottom: 1px solid hsl(0, 0%, 87%); 443 | margin-bottom: 30px; 444 | padding-bottom: 30px; 445 | } 446 | 447 | .param-type dt, .param-type dd { 448 | display: inline-block 449 | } 450 | 451 | .param-type dd { 452 | font-family: 'Source Code Pro'; 453 | font-size: 80%; 454 | } 455 | 456 | .disabled { 457 | color: #454545 458 | } 459 | 460 | /* navicon button */ 461 | .navicon-button { 462 | display: none; 463 | position: relative; 464 | padding: 2.0625rem 1.5rem; 465 | transition: 0.25s; 466 | cursor: pointer; 467 | user-select: none; 468 | opacity: .8; 469 | } 470 | .navicon-button .navicon:before, .navicon-button .navicon:after { 471 | transition: 0.25s; 472 | } 473 | .navicon-button:hover { 474 | transition: 0.5s; 475 | opacity: 1; 476 | } 477 | .navicon-button:hover .navicon:before, .navicon-button:hover .navicon:after { 478 | transition: 0.25s; 479 | } 480 | .navicon-button:hover .navicon:before { 481 | top: .825rem; 482 | } 483 | .navicon-button:hover .navicon:after { 484 | top: -.825rem; 485 | } 486 | 487 | /* navicon */ 488 | .navicon { 489 | position: relative; 490 | width: 2.5em; 491 | height: .3125rem; 492 | background: #000; 493 | transition: 0.3s; 494 | border-radius: 2.5rem; 495 | } 496 | .navicon:before, .navicon:after { 497 | display: block; 498 | content: ""; 499 | height: .3125rem; 500 | width: 2.5rem; 501 | background: #000; 502 | position: absolute; 503 | z-index: -1; 504 | transition: 0.3s 0.25s; 505 | border-radius: 1rem; 506 | } 507 | .navicon:before { 508 | top: .625rem; 509 | } 510 | .navicon:after { 511 | top: -.625rem; 512 | } 513 | 514 | /* open */ 515 | .nav-trigger:checked + label:not(.steps) .navicon:before, 516 | .nav-trigger:checked + label:not(.steps) .navicon:after { 517 | top: 0 !important; 518 | } 519 | 520 | .nav-trigger:checked + label .navicon:before, 521 | .nav-trigger:checked + label .navicon:after { 522 | transition: 0.5s; 523 | } 524 | 525 | /* Minus */ 526 | .nav-trigger:checked + label { 527 | transform: scale(0.75); 528 | } 529 | 530 | /* × and + */ 531 | .nav-trigger:checked + label.plus .navicon, 532 | .nav-trigger:checked + label.x .navicon { 533 | background: transparent; 534 | } 535 | 536 | .nav-trigger:checked + label.plus .navicon:before, 537 | .nav-trigger:checked + label.x .navicon:before { 538 | transform: rotate(-45deg); 539 | background: #FFF; 540 | } 541 | 542 | .nav-trigger:checked + label.plus .navicon:after, 543 | .nav-trigger:checked + label.x .navicon:after { 544 | transform: rotate(45deg); 545 | background: #FFF; 546 | } 547 | 548 | .nav-trigger:checked + label.plus { 549 | transform: scale(0.75) rotate(45deg); 550 | } 551 | 552 | .nav-trigger:checked ~ nav { 553 | left: 0 !important; 554 | } 555 | 556 | .nav-trigger:checked ~ .overlay { 557 | display: block; 558 | } 559 | 560 | .nav-trigger { 561 | position: fixed; 562 | top: 0; 563 | clip: rect(0, 0, 0, 0); 564 | } 565 | 566 | .overlay { 567 | display: none; 568 | position: fixed; 569 | top: 0; 570 | bottom: 0; 571 | left: 0; 572 | right: 0; 573 | width: 100%; 574 | height: 100%; 575 | background: hsla(0, 0%, 0%, 0.5); 576 | z-index: 1; 577 | } 578 | 579 | @media only screen and (min-width: 320px) and (max-width: 680px) { 580 | body { 581 | overflow-x: hidden; 582 | } 583 | 584 | nav { 585 | top: 0; 586 | right: 0; 587 | bottom: 0; 588 | left: -250px; 589 | z-index: 3; 590 | transition: left 0.2s; 591 | } 592 | 593 | .navicon-button { 594 | display: inline-block; 595 | position: fixed; 596 | top: 1.5em; 597 | right: 0; 598 | z-index: 2; 599 | } 600 | 601 | #main { 602 | width: 100%; 603 | min-width: 360px; 604 | margin-left: 0; 605 | padding: 0 25px; 606 | } 607 | 608 | #main h1.page-title { 609 | margin: 1em 0; 610 | } 611 | 612 | #main section { 613 | padding: 0; 614 | } 615 | 616 | footer { 617 | margin-left: 0; 618 | } 619 | } 620 | -------------------------------------------------------------------------------- /docs/styles/prettify-jsdoc.css: -------------------------------------------------------------------------------- 1 | /* JSDoc prettify.js theme */ 2 | 3 | /* plain text */ 4 | .pln { 5 | color: #000000; 6 | font-weight: normal; 7 | font-style: normal; 8 | } 9 | 10 | /* string content */ 11 | .str { 12 | color: hsl(104, 100%, 24%); 13 | font-weight: normal; 14 | font-style: normal; 15 | } 16 | 17 | /* a keyword */ 18 | .kwd { 19 | color: #000000; 20 | font-weight: bold; 21 | font-style: normal; 22 | } 23 | 24 | /* a comment */ 25 | .com { 26 | font-weight: normal; 27 | font-style: italic; 28 | } 29 | 30 | /* a type name */ 31 | .typ { 32 | color: #000000; 33 | font-weight: normal; 34 | font-style: normal; 35 | } 36 | 37 | /* a literal value */ 38 | .lit { 39 | color: #006400; 40 | font-weight: normal; 41 | font-style: normal; 42 | } 43 | 44 | /* punctuation */ 45 | .pun { 46 | color: #000000; 47 | font-weight: bold; 48 | font-style: normal; 49 | } 50 | 51 | /* lisp open bracket */ 52 | .opn { 53 | color: #000000; 54 | font-weight: bold; 55 | font-style: normal; 56 | } 57 | 58 | /* lisp close bracket */ 59 | .clo { 60 | color: #000000; 61 | font-weight: bold; 62 | font-style: normal; 63 | } 64 | 65 | /* a markup tag name */ 66 | .tag { 67 | color: #006400; 68 | font-weight: normal; 69 | font-style: normal; 70 | } 71 | 72 | /* a markup attribute name */ 73 | .atn { 74 | color: #006400; 75 | font-weight: normal; 76 | font-style: normal; 77 | } 78 | 79 | /* a markup attribute value */ 80 | .atv { 81 | color: #006400; 82 | font-weight: normal; 83 | font-style: normal; 84 | } 85 | 86 | /* a declaration */ 87 | .dec { 88 | color: #000000; 89 | font-weight: bold; 90 | font-style: normal; 91 | } 92 | 93 | /* a variable name */ 94 | .var { 95 | color: #000000; 96 | font-weight: normal; 97 | font-style: normal; 98 | } 99 | 100 | /* a function name */ 101 | .fun { 102 | color: #000000; 103 | font-weight: bold; 104 | font-style: normal; 105 | } 106 | 107 | /* Specify class=linenums on a pre to get line numbering */ 108 | ol.linenums { 109 | margin-top: 0; 110 | margin-bottom: 0; 111 | } 112 | -------------------------------------------------------------------------------- /docs/styles/prettify-tomorrow.css: -------------------------------------------------------------------------------- 1 | /* Tomorrow Theme */ 2 | /* Original theme - https://github.com/chriskempson/tomorrow-theme */ 3 | /* Pretty printing styles. Used with prettify.js. */ 4 | /* SPAN elements with the classes below are added by prettyprint. */ 5 | /* plain text */ 6 | .pln { 7 | color: #4d4d4c; } 8 | 9 | @media screen { 10 | /* string content */ 11 | .str { 12 | color: hsl(104, 100%, 24%); } 13 | 14 | /* a keyword */ 15 | .kwd { 16 | color: hsl(240, 100%, 50%); } 17 | 18 | /* a comment */ 19 | .com { 20 | color: hsl(0, 0%, 60%); } 21 | 22 | /* a type name */ 23 | .typ { 24 | color: hsl(240, 100%, 32%); } 25 | 26 | /* a literal value */ 27 | .lit { 28 | color: hsl(240, 100%, 40%); } 29 | 30 | /* punctuation */ 31 | .pun { 32 | color: #000000; } 33 | 34 | /* lisp open bracket */ 35 | .opn { 36 | color: #000000; } 37 | 38 | /* lisp close bracket */ 39 | .clo { 40 | color: #000000; } 41 | 42 | /* a markup tag name */ 43 | .tag { 44 | color: #c82829; } 45 | 46 | /* a markup attribute name */ 47 | .atn { 48 | color: #f5871f; } 49 | 50 | /* a markup attribute value */ 51 | .atv { 52 | color: #3e999f; } 53 | 54 | /* a declaration */ 55 | .dec { 56 | color: #f5871f; } 57 | 58 | /* a variable name */ 59 | .var { 60 | color: #c82829; } 61 | 62 | /* a function name */ 63 | .fun { 64 | color: #4271ae; } } 65 | /* Use higher contrast and text-weight for printable form. */ 66 | @media print, projection { 67 | .str { 68 | color: #060; } 69 | 70 | .kwd { 71 | color: #006; 72 | font-weight: bold; } 73 | 74 | .com { 75 | color: #600; 76 | font-style: italic; } 77 | 78 | .typ { 79 | color: #404; 80 | font-weight: bold; } 81 | 82 | .lit { 83 | color: #044; } 84 | 85 | .pun, .opn, .clo { 86 | color: #440; } 87 | 88 | .tag { 89 | color: #006; 90 | font-weight: bold; } 91 | 92 | .atn { 93 | color: #404; } 94 | 95 | .atv { 96 | color: #060; } } 97 | /* Style */ 98 | /* 99 | pre.prettyprint { 100 | background: white; 101 | font-family: Consolas, Monaco, 'Andale Mono', monospace; 102 | font-size: 12px; 103 | line-height: 1.5; 104 | border: 1px solid #ccc; 105 | padding: 10px; } 106 | */ 107 | 108 | /* Specify class=linenums on a pre to get line numbering */ 109 | ol.linenums { 110 | margin-top: 0; 111 | margin-bottom: 0; } 112 | 113 | /* IE indents via margin-left */ 114 | li.L0, 115 | li.L1, 116 | li.L2, 117 | li.L3, 118 | li.L4, 119 | li.L5, 120 | li.L6, 121 | li.L7, 122 | li.L8, 123 | li.L9 { 124 | /* */ } 125 | 126 | /* Alternate shading for lines */ 127 | li.L1, 128 | li.L3, 129 | li.L5, 130 | li.L7, 131 | li.L9 { 132 | /* */ } 133 | -------------------------------------------------------------------------------- /docs/test-helpers.js.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | test-helpers.js - Postman Documentation 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 23 | 24 | 25 | 28 | 29 |
30 | 31 |

test-helpers.js

32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
40 |
41 |
/**
42 |  * Turns an entries iterable into an object.
43 |  * @param  {Mixed} iterable - Anything with a Symbol.iterator.
44 |  * @return {Object} - Iterated entry entries spread onto a new object.
45 |  */
46 | export const toObject = (iterable) => {
47 |   const entries = [...iterable];
48 | 
49 |   return entries.reduce((object, [key, value]) => {
50 |     const copy = Object.assign({
51 |       [key]: value,
52 |     }, object);
53 | 
54 |     return copy;
55 |   }, {});
56 | };
57 | 
58 |
59 |
60 | 61 | 62 | 63 | 64 |
65 | 66 |
67 | 68 |
69 | Documentation generated at Mon Apr 17 2017 18:41:21 GMT-0600 (MDT) 70 |
71 | 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /docs/union_index.js.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | union/index.js - Postman Documentation 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 23 | 24 | 25 | 28 | 29 |
30 | 31 |

union/index.js

32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
40 |
41 |
/**
42 |  * Utilities for deterministically merging node attributes.
43 |  *
44 |  * @private
45 |  * @module graph-crdt/src/union
46 |  */
47 | 
48 | /**
49 |  * Deterministically resolves merge conflicts
50 |  * on JSON-compatible data.
51 |  *
52 |  * @param  {Object} field1 - Current state field metadata.
53 |  * @param  {Object} field2 - Update field metadata.
54 |  * @returns {Object} - The greater value, or if they're
55 |  * equal, the current state.
56 |  */
57 | export function conflict (field1, field2) {
58 | 
59 |   /** Turn the values into comparable strings. */
60 |   const string = {
61 |     current: JSON.stringify(field1.value),
62 |     update: JSON.stringify(field2.value),
63 |   };
64 | 
65 |   /** Are the values equal? */
66 |   const equal = (string.current === string.update);
67 | 
68 |   /** Is our current value greater? */
69 |   const greater = (string.current > string.update);
70 | 
71 |   /** Return the winning value. */
72 |   return (equal || greater) ? field1 : field2;
73 | }
74 | 
75 |
76 |
77 | 78 | 79 | 80 | 81 |
82 | 83 |
84 | 85 |
86 | Documentation generated at Fri Apr 21 2017 17:51:20 GMT-0600 (MDT) 87 |
88 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /jsdoc.config.json: -------------------------------------------------------------------------------- 1 | { 2 | "source": { 3 | "include": ["src/"], 4 | "exclude": ["node_modules/"], 5 | "includePattern": ".*\\.js$", 6 | "excludePattern": "(node_modules|docs)$" 7 | }, 8 | "templates": { 9 | "cleverLinks": true 10 | }, 11 | "opts": { 12 | "template": "./node_modules/postman-jsdoc-theme", 13 | "recurse": true, 14 | "private": false, 15 | "destination": "./docs/", 16 | "readme": "./README.md" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /mocha.opts: -------------------------------------------------------------------------------- 1 | --recursive 2 | --check-leaks 3 | --compilers js:babel-register 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "graph-crdt", 3 | "version": "0.7.0", 4 | "description": "A Delta State Graph CRDT variant", 5 | "main": "dist/index.js", 6 | "scripts": { 7 | "prepublish": "npm run build", 8 | "preversion": "npm run build", 9 | "build": "npm run build:js && npm run build:doc", 10 | "build:js": "babel src/ -d dist/ --ignore **/*spec.js", 11 | "build:doc": "jsdoc --configure jsdoc.config.json", 12 | "test": "mocha 'src/**/*test.js' --opts ./mocha.opts", 13 | "lint": "eslint src/", 14 | "precommit": "lint-staged", 15 | "prettier": "prettier --single-quote --trailing-comma all" 16 | }, 17 | "lint-staged": { 18 | "*.js": [ 19 | "npm run prettier -- --write", 20 | "git add" 21 | ] 22 | }, 23 | "keywords": [ 24 | "graph", 25 | "crdt", 26 | "strong-eventual-consistency", 27 | "conflict-resolution", 28 | "distributed", 29 | "observable" 30 | ], 31 | "author": "Jesse Gibson (http://psychollama.io/)", 32 | "devDependencies": { 33 | "babel-cli": "^6.18.0", 34 | "babel-eslint": "^7.2.3", 35 | "babel-plugin-transform-runtime": "^6.15.0", 36 | "babel-preset-es2015": "^6.18.0", 37 | "babel-preset-stage-0": "^6.16.0", 38 | "babel-preset-stage-2": "^6.22.0", 39 | "babel-register": "^6.18.0", 40 | "eslint": "^3.11.1", 41 | "eslint-config-llama": "^3.0.0", 42 | "eslint-config-prettier": "^2.3.0", 43 | "eslint-plugin-babel": "^4.0.0", 44 | "expect": "^1.20.2", 45 | "husky": "^0.14.3", 46 | "jsdoc": "^3.4.0", 47 | "lint-staged": "^4.0.2", 48 | "mocha": "^3.2.0", 49 | "postman-jsdoc-theme": "0.0.2", 50 | "prettier": "^1.5.3" 51 | }, 52 | "dependencies": { 53 | "babel-runtime": "^6.20.0", 54 | "eventemitter3": "^2.0.1", 55 | "uuid": "^3.0.0" 56 | }, 57 | "repository": { 58 | "type": "git", 59 | "url": "git+https://github.com/PsychoLlama/graph-crdt.git" 60 | }, 61 | "license": "MIT", 62 | "bugs": { 63 | "url": "https://github.com/PsychoLlama/graph-crdt/issues" 64 | }, 65 | "homepage": "https://github.com/PsychoLlama/graph-crdt#readme" 66 | } 67 | -------------------------------------------------------------------------------- /src/Graph/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module graph-crdt.Graph 3 | */ 4 | 5 | import Emitter from 'eventemitter3'; 6 | import Node from '../Node'; 7 | 8 | const nodes = Symbol('graph node container'); 9 | 10 | /** 11 | * Container and interface for groups of nodes. 12 | * 13 | * @class Graph 14 | */ 15 | export default class Graph extends Emitter { 16 | /** 17 | * Instantiates a graph without needing `new`. 18 | * 19 | * @returns {Graph} - A graph instance. 20 | */ 21 | static create() { 22 | return new Graph(); 23 | } 24 | 25 | /** 26 | * Imports a format compliant graph into a new one. It expects 27 | * nested nodes to use the node metadata format. Useful for 28 | * sending and importing graphs over the network. 29 | * 30 | * @param {Object} object - The raw graph object. 31 | * @returns {Graph} - A new graph instance that consumes 32 | * the imported data. 33 | */ 34 | static source(object) { 35 | // Create a new graph 36 | const graph = Graph.create(); 37 | 38 | // For each node... 39 | Object.keys(object).forEach(key => { 40 | let node = object[key]; 41 | 42 | // Make sure it's a node. 43 | if (!(node instanceof Node)) { 44 | node = Node.source(node); 45 | } 46 | 47 | // Get it's unique ID. 48 | const { uid } = node.meta(); 49 | 50 | // Add it to the new graph. 51 | graph[nodes][uid] = node; 52 | }); 53 | 54 | return graph; 55 | } 56 | 57 | constructor() { 58 | super(); 59 | 60 | this[nodes] = {}; 61 | } 62 | 63 | /** 64 | * Return the unmodified value of a node lookup. 65 | * 66 | * @param {String} key - The name/uid of the node. 67 | * @returns {Node|null} - The node if found, otherwise `null`. 68 | */ 69 | value(key) { 70 | return this[nodes][key] || null; 71 | } 72 | 73 | new() { 74 | return new Graph(); 75 | } 76 | 77 | /** 78 | * Replays all the changes in the graph as though they occurred 79 | * after the events in the target graph. 80 | * @param {Graph} target - Preceding state. 81 | * @return {Graph} - A new graph containing the rebased nodes. 82 | */ 83 | rebase(target) { 84 | const rebased = this.new(); 85 | 86 | rebased.merge(target); 87 | rebased.merge(this); 88 | 89 | for (const [id] of this) { 90 | const existing = target.value(id); 91 | 92 | if (existing) { 93 | rebased[nodes][id] = this.value(id).rebase(existing); 94 | } 95 | } 96 | 97 | return rebased; 98 | } 99 | 100 | /** 101 | * Figures out what fields are common to both graphs. 102 | * @param {Graph} target - Any other graph. 103 | * @return {Graph} - The shared properties between both graphs. 104 | */ 105 | overlap(target) { 106 | const shared = this.new(); 107 | 108 | for (const [key] of this) { 109 | if (this.value(key) && target.value(key)) { 110 | // Calculate the node overlap. 111 | const nodeSource = this.value(key); 112 | const nodeTarget = target.value(key); 113 | const overlap = nodeSource.overlap(nodeTarget); 114 | 115 | // Merge it into the new graph. 116 | shared.merge({ [key]: overlap }); 117 | } 118 | } 119 | 120 | return shared; 121 | } 122 | 123 | /** 124 | * Merge one graph with another (graph union operation). 125 | * 126 | * @param {Object} graph - The graph to merge with. 127 | * Items must be enumerable, and cannot be inherited from prototypes. 128 | * @returns {Graph} - The `this` context. 129 | */ 130 | merge(graph) { 131 | /** Ensure it's a graph. */ 132 | if (!(graph instanceof Graph)) { 133 | graph = Graph.source(graph); 134 | } 135 | 136 | const changes = { 137 | update: this.new(), 138 | history: this.new(), 139 | }; 140 | 141 | for (const [uid, node] of graph) { 142 | let target = this.value(uid); 143 | 144 | if (!target) { 145 | target = this[nodes][uid] = node.new(); 146 | } 147 | 148 | const { update, history } = target.merge(node); 149 | 150 | changes.update[nodes][uid] = update; 151 | changes.history[nodes][uid] = history; 152 | } 153 | 154 | this.emit('update', changes.update); 155 | this.emit('history', changes.history); 156 | 157 | return changes; 158 | } 159 | 160 | /** 161 | * Iterates over every node in the graph. 162 | * @return {Array} - Every yielded value is a key/value pair. 163 | */ 164 | *[Symbol.iterator]() { 165 | const object = this[nodes]; 166 | 167 | for (const key in object) { 168 | if (object.hasOwnProperty(key)) { 169 | const value = this.value(key); 170 | yield [key, value]; 171 | } 172 | } 173 | } 174 | 175 | /* Coercion interfaces */ 176 | 177 | /** 178 | * Used to serialize a graph (JSON.stringify calls this method). 179 | * 180 | * @private 181 | * @returns {Object} - The hidden collection of nodes. 182 | */ 183 | toJSON() { 184 | return this[nodes]; 185 | } 186 | } 187 | -------------------------------------------------------------------------------- /src/Graph/test.js: -------------------------------------------------------------------------------- 1 | /* eslint-env mocha */ 2 | import expect, { spyOn, createSpy } from 'expect'; 3 | import Graph from '../Graph'; 4 | import Node from '../Node'; 5 | 6 | describe('Graph static method', () => { 7 | describe('source()', () => { 8 | it("uses the input as it's data source", () => { 9 | const node = Node.create({ uid: 'member' }); 10 | const graph = Graph.create(); 11 | node.merge({ data: true }); 12 | graph.merge({ [node]: node }); 13 | 14 | const copy = Graph.source(graph.toJSON()); 15 | expect(copy.value('member').value('data')).toBe(true); 16 | }); 17 | 18 | it('sources nested POJOs into nodes', () => { 19 | const node = Node.create(); 20 | node.merge({ data: true }); 21 | const copy = JSON.parse(JSON.stringify(node)); 22 | 23 | const graph = Graph.source({ 24 | // "copy" is a plain object. 25 | placeholder: copy, 26 | }); 27 | 28 | const [key] = [...graph].map(([key]) => key); 29 | expect(graph.value(key)).toBeA(Node); 30 | }); 31 | }); 32 | }); 33 | 34 | describe('A graph', () => { 35 | let graph; 36 | 37 | beforeEach(() => { 38 | graph = new Graph(); 39 | }); 40 | 41 | it('is initialized empty', () => { 42 | const keys = [...graph].map(([key]) => key); 43 | 44 | expect(keys.length).toBe(0); 45 | }); 46 | 47 | it('returns the nodes when `toJSON` is called', () => { 48 | const node = Node.create({ uid: 'unique id' }); 49 | node.merge({ data: 'intact' }); 50 | graph.merge({ [node]: node }); 51 | 52 | const string = JSON.stringify(graph); 53 | expect(string).toContain('unique id'); 54 | expect(string).toContain('intact'); 55 | }); 56 | 57 | describe('iterator()', () => { 58 | it('lists all the node indices', () => { 59 | const first = Node.create({ uid: 'first' }); 60 | const second = Node.create({ uid: 'second' }); 61 | 62 | graph.merge({ 63 | [first]: first, 64 | [second]: second, 65 | }); 66 | 67 | const entries = [...graph]; 68 | 69 | expect(entries).toEqual([['first', first], ['second', second]]); 70 | }); 71 | }); 72 | 73 | describe('read()', () => { 74 | let node; 75 | 76 | beforeEach(() => { 77 | node = Node.create({ uid: 'dave' }); 78 | graph.merge({ [node]: node }); 79 | }); 80 | 81 | it('returns existing nodes', () => { 82 | const result = graph.value(node.toString()); 83 | 84 | expect(result.meta()).toContain({ 85 | uid: String(node), 86 | }); 87 | }); 88 | 89 | it('returns null for non-existent nodes', () => { 90 | const result = graph.value('potato'); 91 | expect(result).toBe(null); 92 | }); 93 | }); 94 | 95 | describe('merge()', () => { 96 | let node1, node2, subgraph; 97 | 98 | beforeEach(() => { 99 | node1 = Node.create(); 100 | node2 = Node.create(); 101 | 102 | subgraph = Graph.source({ 103 | [node1]: node1, 104 | [node2]: node2, 105 | }); 106 | }); 107 | 108 | it('ensures the subgraph is a Graph instance', () => { 109 | const { uid } = node1.meta(); 110 | node1.merge({ 111 | value: 'preserved', 112 | }); 113 | 114 | graph.merge({ 115 | [uid]: node1, 116 | }); 117 | 118 | const result = graph.value(uid); 119 | expect(result.snapshot()).toEqual(node1.snapshot()); 120 | }); 121 | 122 | it('adds all the items in the subgraph', () => { 123 | graph.merge(subgraph); 124 | 125 | const keys = [...graph].map(([key]) => key); 126 | expect(keys).toContain(node1.toString()); 127 | expect(keys).toContain(node2.toString()); 128 | }); 129 | 130 | it('assumes sub-objects are already formatted', () => { 131 | node1.merge({ data: 'preserved' }); 132 | 133 | graph.merge({ 134 | [node1]: node1.toJSON(), 135 | }); 136 | 137 | const result = graph.value(node1.toString()); 138 | expect(result.value('data')).toBe('preserved'); 139 | }); 140 | 141 | it('adds node copies, not originals', () => { 142 | const { uid } = node1.meta(); 143 | node1.merge({ isNode1: true }); 144 | 145 | graph.merge({ 146 | [node1]: node1, 147 | }); 148 | 149 | const copied = graph.value(uid); 150 | expect(copied.snapshot()).toEqual({ isNode1: true }); 151 | expect(copied).toNotBe(node1); 152 | }); 153 | 154 | it('returns the update delta', () => { 155 | const { update } = graph.merge({ 156 | [node2]: node2, 157 | }); 158 | 159 | expect([...update]).toEqual([[String(node2), node2]]); 160 | }); 161 | 162 | it('emits an `update` delta graph on change', () => { 163 | const spy = createSpy(); 164 | graph.on('update', spy); 165 | 166 | const { update } = graph.merge({ 167 | [node1]: node1, 168 | }); 169 | 170 | expect(spy).toHaveBeenCalledWith(update); 171 | }); 172 | 173 | it('returns the history delta', () => { 174 | const data = new Node({ uid: 'existing' }); 175 | const update = new Node({ uid: 'existing' }); 176 | data.merge({ old: true }); 177 | data.meta('old').state = 1; 178 | update.merge({ old: false }); 179 | update.meta('old').state = 2; 180 | 181 | graph.merge({ [data]: data }); 182 | const { history } = graph.merge({ [update]: update }); 183 | 184 | const node = history.value('existing'); 185 | expect(node).toBeA(Node); 186 | expect(node.snapshot()).toEqual({ old: true }); 187 | }); 188 | 189 | it('emits a `history` graph delta', () => { 190 | const spy = createSpy(); 191 | graph.on('history', spy); 192 | 193 | const node = new Node({ uid: 'node' }); 194 | const update = new Node({ uid: 'node' }); 195 | graph.merge({ [node]: node }); 196 | 197 | const { history } = graph.merge({ [update]: update }); 198 | expect(spy).toHaveBeenCalledWith(history); 199 | }); 200 | 201 | it('uses Graph#new to create deltas', () => { 202 | const spy = spyOn(graph, 'new').andCall(() => { 203 | const graph = new Graph(); 204 | graph.viaNew = true; 205 | 206 | return graph; 207 | }); 208 | 209 | const { update, history } = graph.merge({}); 210 | 211 | expect(update).toContain({ viaNew: true }); 212 | expect(history).toContain({ viaNew: true }); 213 | 214 | spy.restore(); 215 | }); 216 | }); 217 | 218 | describe('new()', () => { 219 | let node; 220 | 221 | beforeEach(() => { 222 | node = new Node(); 223 | }); 224 | 225 | it('returns a new graph', () => { 226 | graph.merge({ [node]: node }); 227 | 228 | const copy = graph.new(); 229 | expect(copy).toBeA(Graph); 230 | 231 | // Must be a copy. 232 | expect(copy).toNotBe(graph); 233 | }); 234 | }); 235 | 236 | describe('rebase()', () => { 237 | let target; 238 | 239 | beforeEach(() => { 240 | target = new Graph(); 241 | }); 242 | 243 | it('returns a graph', () => { 244 | const result = graph.rebase(target); 245 | 246 | expect(result).toBeA(Graph); 247 | expect(result).toNotBe(graph); 248 | expect(result).toNotBe(target); 249 | }); 250 | 251 | it('contains all the state of the target', () => { 252 | const node = Node.from({ existing: true }); 253 | target.merge({ [node]: node }); 254 | 255 | const result = graph.rebase(target); 256 | 257 | expect([...result]).toEqual([...target]); 258 | }); 259 | 260 | it('contains all the graph state', () => { 261 | const node = Node.from({ existing: true }); 262 | graph.merge({ [node]: node }); 263 | 264 | const result = graph.rebase(target); 265 | 266 | expect([...result]).toEqual([...graph]); 267 | }); 268 | 269 | it('rebases every node', () => { 270 | const node = Node.from({ existing: true }); 271 | graph.merge({ [node]: node }); 272 | target.merge({ [node]: node }); 273 | const spy = spyOn(graph.value(node), 'rebase').andCallThrough(); 274 | 275 | graph.rebase(target); 276 | 277 | expect(spy).toHaveBeenCalled(); 278 | }); 279 | 280 | it('uses the rebased node in the new graph', () => { 281 | const node = new Node(); 282 | 283 | const current = node.new(); 284 | const old = node.new(); 285 | 286 | old.merge({ old: true }); 287 | current.merge({ old: false }); 288 | 289 | graph.merge({ [node]: current }); 290 | target.merge({ [node]: old }); 291 | 292 | const result = graph.rebase(target).value(node.meta().uid); 293 | 294 | expect(result.value('old')).toBe(false); 295 | expect(result.state('old')).toBe(2); 296 | }); 297 | }); 298 | 299 | describe('overlap()', () => { 300 | let target; 301 | 302 | beforeEach(() => { 303 | target = new Graph(); 304 | }); 305 | 306 | it('returns a new graph', () => { 307 | const result = graph.overlap(target); 308 | 309 | expect(result).toBeA(Graph); 310 | expect(result).toNotBe(graph); 311 | expect(result).toNotBe(target); 312 | }); 313 | 314 | it('contains no nodes if the target is empty', () => { 315 | const node = new Node(); 316 | graph.merge({ [node]: node }); 317 | const result = graph.overlap(target); 318 | 319 | expect([...result]).toEqual([]); 320 | }); 321 | 322 | it('contains no nodes if the source is empty', () => { 323 | const node = new Node(); 324 | target.merge({ [node]: node }); 325 | const result = graph.overlap(target); 326 | 327 | expect([...result]).toEqual([]); 328 | }); 329 | 330 | it('contains nodes shared by both graphs', () => { 331 | const node1 = new Node(); 332 | const node2 = new Node({ uid: String(node1) }); 333 | 334 | graph.merge({ [node1]: node1 }); 335 | target.merge({ [node2]: node2 }); 336 | 337 | const result = graph.overlap(target); 338 | 339 | expect([...result]).toEqual([...graph]); 340 | }); 341 | 342 | it('only contains overlapping node fields', () => { 343 | const node1 = new Node({ uid: 'tweets' }); 344 | const node2 = new Node({ uid: 'tweets' }); 345 | 346 | node1.merge({ node1: true, shared: true }); 347 | node2.merge({ node2: true, shared: true }); 348 | 349 | graph.merge({ [node1]: node1 }); 350 | target.merge({ [node2]: node2 }); 351 | 352 | const result = graph.overlap(target); 353 | 354 | const { uid } = node1.meta(); 355 | expect([...result.value(uid)]).toEqual([['shared', true]]); 356 | }); 357 | 358 | it('uses the values from the source graph', () => { 359 | const node1 = new Node({ uid: 'node' }); 360 | const node2 = new Node({ uid: 'node' }); 361 | 362 | node1.merge({ shared: 'source' }); 363 | node2.merge({ shared: 'target' }); 364 | 365 | graph.merge({ [node1]: node1 }); 366 | target.merge({ [node2]: node2 }); 367 | 368 | const result = graph.overlap(target); 369 | 370 | const { uid } = node1.meta(); 371 | expect(result.value(uid).value('shared')).toBe('source'); 372 | }); 373 | }); 374 | }); 375 | -------------------------------------------------------------------------------- /src/Node/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module graph-crdt.Node 3 | */ 4 | 5 | import Emitter from 'eventemitter3'; 6 | import { v4 as uuid } from 'uuid'; 7 | import { conflict } from '../union'; 8 | 9 | const node = Symbol('source object'); 10 | 11 | /** 12 | * Increments each state given an object of key/value pairs. 13 | * @private 14 | * @param {Node} current - Current state. 15 | * @param {Object} update - Key-value update pairs. 16 | * @return {Node} - Incremented update. 17 | */ 18 | const getIncrementedState = (current, update) => { 19 | const result = current.new(); 20 | 21 | for (const field in update) { 22 | if (update.hasOwnProperty(field)) { 23 | const state = current.state(field) + 1; 24 | const value = update[field]; 25 | 26 | result[node][field] = { value, state }; 27 | } 28 | } 29 | 30 | return result; 31 | }; 32 | 33 | /** 34 | * An observable object with conflict-resolution. 35 | * 36 | * @class Node 37 | * @param {Object} [config] - Instance level configuration. 38 | * @param {Object} [config.uid] - Override the randomly generated 39 | * node uid. 40 | */ 41 | export default class Node extends Emitter { 42 | /** 43 | * Creates a new Node instance without using 44 | * `new`. 45 | * 46 | * @param {Object} [config] - The constructor configuration object. 47 | * @returns {Node} - The new node instance. 48 | */ 49 | static create(config) { 50 | return new Node(config); 51 | } 52 | 53 | /** 54 | * Turns a normal object into a Node instance. 55 | * Properties to be imported must be enumerable 56 | * and cannot be inherited via prototype. 57 | * 58 | * @param {Object} object - Any enumerable object. 59 | * @returns {Node} - A node interface constructed from the object. 60 | */ 61 | static from(object) { 62 | const instance = Node.create(); 63 | const state = 1; 64 | 65 | for (const key in object) { 66 | if (object.hasOwnProperty(key)) { 67 | const value = object[key]; 68 | instance[node][key] = { value, state }; 69 | } 70 | } 71 | 72 | return instance; 73 | } 74 | 75 | /** 76 | * Take an object and use it as the data source for a new 77 | * node. This method is used with properly formatted 78 | * objects, such as stringified, then parsed node instances. 79 | * 80 | * @param {Object} object - The preformatted object. 81 | * @returns {Node} - A new node instance. 82 | * 83 | * @example 84 | * const original = Node.create() 85 | * original.merge({ data: 'intact' }) 86 | * const serialized = JSON.stringify(original) 87 | * const parsed = JSON.parse(serialized) 88 | * 89 | * const node = Node.source(parsed) 90 | * node.value('data') // 'intact' 91 | */ 92 | static source(object) { 93 | const instance = Node.create(); 94 | instance[node] = object; 95 | 96 | return instance; 97 | } 98 | 99 | constructor(config = {}) { 100 | super(); 101 | 102 | const uid = config.uid || uuid(); 103 | 104 | this[node] = { 105 | '@object': { uid }, 106 | }; 107 | } 108 | 109 | /** 110 | * Read metadata, either on a field, or 111 | * on the entire object. 112 | * 113 | * @param {String} [field] - The property to read metadata from. 114 | * @returns {Object|null} - If metadata is found, it returns the object. 115 | * Otherwise `null` is given. 116 | */ 117 | meta(field) { 118 | if (field === undefined) { 119 | /** Returns the object metadata if no field is specified. */ 120 | return this[node]['@object']; 121 | } 122 | 123 | /** Returns the field given, and null if metadata isn't found. */ 124 | return this[node][field] || null; 125 | } 126 | 127 | /** 128 | * Show the value of a node's property. 129 | * 130 | * @param {String} field - The name of the property 131 | * to retrieve. 132 | * @returns {Mixed} - The value of the property, 133 | * or undefined if it doesn't exist. Cannot be called 134 | * on reserved fields (like "@object"). 135 | */ 136 | value(field) { 137 | if (field === '@object') { 138 | return undefined; 139 | } 140 | 141 | /** Gets the field metadata. */ 142 | const subject = this.meta(field); 143 | 144 | /** 145 | * If the field exists, it returns the corresponding 146 | * value, otherwise it returns undefined. 147 | */ 148 | return subject ? subject.value : undefined; 149 | } 150 | 151 | /** 152 | * Get the current state of a property. 153 | * 154 | * @param {String} field - Property name. 155 | * @returns {Number} - Current lamport state (or 0). 156 | */ 157 | state(field) { 158 | /** Get the field metadata. */ 159 | const meta = this.meta(field); 160 | 161 | return meta ? meta.state : 0; 162 | } 163 | 164 | /** 165 | * Sets metadata for a field, bumping the current state. 166 | * @param {String} field - A field to update. 167 | * @param {Object} metadata - What metadata the field should contain. 168 | * @return {Object} - The metadata object (not the same one given). 169 | */ 170 | setMetadata(field, metadata) { 171 | const state = this.state(field) + 1; 172 | 173 | const update = this.new(); 174 | update[node][field] = { ...metadata, state }; 175 | 176 | return this.merge(update); 177 | } 178 | 179 | /** 180 | * Takes the changes from the current node and plays them after the 181 | * changes in the target node. 182 | * Similar to git rebase, but without the conflicts. 183 | * @param {Node} target - Preceding state. 184 | * @return {Node} - A new, rebased node. 185 | */ 186 | rebase(target) { 187 | const rebased = this.new(); 188 | 189 | rebased.merge(target); 190 | rebased.merge(this); 191 | 192 | // Bump state for older fields. 193 | for (const [key] of this) { 194 | if (target.state(key) >= this.state(key)) { 195 | // Avoids mutation of metadata. 196 | rebased[node][key] = { 197 | ...this.meta(key), 198 | state: target.state(key) + 1, 199 | }; 200 | } 201 | } 202 | 203 | return rebased; 204 | } 205 | 206 | /** 207 | * Calculates the intersection between two nodes. 208 | * @param {Node} target - Any other node. 209 | * @return {Node} - A collection of fields common to both nodes. 210 | */ 211 | overlap(target) { 212 | const shared = this.new(); 213 | 214 | for (const [field] of this) { 215 | if (this.state(field) && target.state(field)) { 216 | shared[node][field] = this.meta(field); 217 | } 218 | } 219 | 220 | return shared; 221 | } 222 | 223 | /** 224 | * Merges an update into the current node. 225 | * 226 | * @param {Node} incoming - The node to merge from. 227 | * If a plain object is passed, it will be upgraded to a node 228 | * using `Node.from`. 229 | * @returns {Object} - A collection of changes caused by the merge. 230 | */ 231 | merge(incoming) { 232 | if (!(incoming instanceof Node)) { 233 | incoming = getIncrementedState(this, incoming); 234 | } 235 | 236 | /** Track all mutations. */ 237 | const changes = { 238 | history: this.new(), 239 | update: this.new(), 240 | }; 241 | 242 | for (const [field] of incoming) { 243 | let forceUpdate = false; 244 | 245 | const current = this.meta(field); 246 | const update = incoming.meta(field); 247 | const state = { 248 | incoming: incoming.state(field), 249 | current: this.state(field), 250 | }; 251 | 252 | /** Handle conflicts. */ 253 | if (state.current === state.incoming) { 254 | const winner = conflict(current, update); 255 | 256 | /** No further action needed. */ 257 | if (winner === current) { 258 | continue; 259 | } 260 | 261 | /** Replace the current value */ 262 | this.emit('conflict', update, current); 263 | forceUpdate = true; 264 | } 265 | 266 | if (state.current < state.incoming || forceUpdate) { 267 | /** Track overwritten values. */ 268 | if (current) { 269 | changes.history[node][field] = current; 270 | } 271 | 272 | changes.update[node][field] = update; 273 | 274 | /** Immediately apply updates. */ 275 | this[node][field] = update; 276 | } else { 277 | changes.history[node][field] = update; 278 | } 279 | } 280 | 281 | /** Only emit when there's a change. */ 282 | const changed = [...changes.update].length > 0; 283 | const overwritten = [...changes.history].length > 0; 284 | 285 | if (overwritten) { 286 | this.emit('history', changes.history); 287 | } 288 | if (changed) { 289 | this.emit('update', changes.update); 290 | } 291 | 292 | return changes; 293 | } 294 | 295 | /** 296 | * Creates an empty instance with the same configuration. 297 | * @return {Node} - A new node instance with the same properties. 298 | */ 299 | new() { 300 | const { uid } = this.meta(); 301 | const clone = new Node({ uid }); 302 | 303 | return clone; 304 | } 305 | 306 | /** 307 | * Takes a snapshot of the current state. 308 | * @return {Object} - Every key and value (currently) in the node. 309 | */ 310 | snapshot() { 311 | const object = {}; 312 | 313 | for (const [key, value] of this) { 314 | object[key] = value; 315 | } 316 | 317 | return object; 318 | } 319 | 320 | /** 321 | * Iterates over the node keys & values, ignoring metadata. 322 | * @return {Array} - Each value yielded is a [key, value] pair. 323 | */ 324 | *[Symbol.iterator]() { 325 | const object = this[node]; 326 | const meta = '@object'; 327 | 328 | /** Iterate over the source object. */ 329 | for (const key in object) { 330 | /** Ignore prototype values and node metadata. */ 331 | if (object.hasOwnProperty(key) && key !== meta) { 332 | const value = this.value(key); 333 | yield [key, value]; 334 | } 335 | } 336 | } 337 | 338 | /* Coercion interfaces */ 339 | 340 | /** 341 | * Returns the node's uid. Not meant for end developers, 342 | * instead used by JavaScript for type coercion. 343 | * 344 | * @private 345 | * @returns {String} - The node's unique ID. 346 | */ 347 | toString() { 348 | const { uid } = this.meta(); 349 | 350 | return uid; 351 | } 352 | 353 | /** 354 | * Returns the actual object, called when JSON needs something 355 | * to stringify. Obviously dangerous as it exposes the object 356 | * to untracked mutation. Please don't use it. 357 | * 358 | * @private 359 | * @returns {Object} - The actual node object. 360 | */ 361 | toJSON() { 362 | return this[node]; 363 | } 364 | } 365 | -------------------------------------------------------------------------------- /src/Node/merge.test.js: -------------------------------------------------------------------------------- 1 | /* eslint-env mocha */ 2 | import expect, { createSpy } from 'expect'; 3 | import Node from './index'; 4 | 5 | describe('A node merge', () => { 6 | let node; 7 | 8 | beforeEach(() => { 9 | node = new Node(); 10 | }); 11 | 12 | it('namespaces to avoid conflicts', () => { 13 | node.merge({ read: 'not a function' }); 14 | expect(node.value).toBeA(Function); 15 | }); 16 | 17 | it('converts POJOs into Nodes', () => { 18 | node.merge({ data: 'success' }); 19 | expect(node.value('data')).toBe('success'); 20 | }); 21 | 22 | it('uses the same node ID for new change nodes', () => { 23 | const { update, history } = node.merge({}); 24 | const { uid } = node.meta(); 25 | 26 | expect(update.meta()).toContain({ uid }); 27 | expect(history.meta()).toContain({ uid }); 28 | }); 29 | 30 | describe('within operating state bounds', () => { 31 | it('adds all new properties', () => { 32 | const update = Node.from({ data: true }); 33 | node.merge(update); 34 | 35 | const keys = [...node].map(([key]) => key); 36 | expect(keys).toContain('data'); 37 | }); 38 | 39 | it('updates existing properties', () => { 40 | const incoming = Node.from({ data: false }); 41 | node.merge(incoming); 42 | expect(node.value('data')).toBe(false); 43 | 44 | incoming.merge({ data: true }); 45 | node.merge(incoming); 46 | 47 | expect(node.value('data')).toBe(true); 48 | }); 49 | 50 | it('emits `update` after updates', () => { 51 | const spy = createSpy(); 52 | node.on('update', spy); 53 | 54 | node.merge({ data: 'yep' }); 55 | 56 | const [value] = spy.calls[0].arguments; 57 | expect(value).toBeA(Node); 58 | 59 | const object = value.snapshot(); 60 | expect(object).toEqual({ data: 'yep' }); 61 | }); 62 | 63 | it('returns the updates', () => { 64 | const { update } = node.merge({ data: 'yep' }); 65 | expect(update).toBeA(Node); 66 | 67 | const object = update.snapshot(); 68 | expect(object).toEqual({ 69 | data: 'yep', 70 | }); 71 | }); 72 | 73 | it('does not emit `update` without updates', () => { 74 | const spy = createSpy(); 75 | node.on('update', spy); 76 | 77 | // No properties. 78 | node.merge({}); 79 | 80 | expect(spy).toNotHaveBeenCalled(); 81 | }); 82 | 83 | it('does not emit `update` for the same update', () => { 84 | const spy = createSpy(); 85 | node.on('update', spy); 86 | 87 | const update = Node.from({ update: true }); 88 | 89 | node.merge(update); 90 | node.merge(update); 91 | 92 | expect(spy.calls.length).toBe(1); 93 | }); 94 | 95 | it('merges other nodes', () => { 96 | const weather = new Node({ uid: 'weather' }); 97 | weather.merge({ condition: 'sunny', temp: 65 }); 98 | 99 | const change = new Node({ uid: 'weather' }); 100 | change.merge(weather); 101 | change.merge({ temp: 70 }); 102 | 103 | weather.merge(change); 104 | 105 | expect(weather.state('temp')).toBe(2); 106 | expect(weather.value('temp')).toBe(70); 107 | }); 108 | }); 109 | 110 | describe('in historical state', () => { 111 | let incoming; 112 | 113 | beforeEach(() => { 114 | incoming = Node.create(); 115 | }); 116 | 117 | it('does not overwrite more recent properties', () => { 118 | // Stale update. 119 | incoming.merge({ hello: 'Mars' }); 120 | incoming.meta('hello').state = 1; 121 | 122 | const update = Node.from({ hello: 'World' }); 123 | update.meta('hello').state = 2; 124 | 125 | node.merge(update); 126 | node.merge(incoming); 127 | 128 | expect(node.value('hello')).toBe('World'); 129 | }); 130 | 131 | it('adds new properties', () => { 132 | // Really old state, but it's new to `node`. 133 | incoming.merge({ success: true }); 134 | incoming.meta('success').state = 10; 135 | 136 | node.merge(incoming); 137 | 138 | expect(node.value('success')).toBe(true); 139 | }); 140 | 141 | it('returns the history', () => { 142 | incoming.merge({ old: true }); 143 | incoming.meta('old').state = 1; 144 | node.merge({ old: false }); 145 | node.meta('old').state = 2; 146 | 147 | const { history } = node.merge(incoming); 148 | 149 | expect(history).toBeA(Node); 150 | const object = history.snapshot(); 151 | 152 | expect(object).toEqual({ old: true }); 153 | }); 154 | 155 | it('includes overwritten values in history', () => { 156 | node.merge({ old: true }); 157 | const { history } = node.merge({ old: false }); 158 | 159 | const object = history.snapshot(); 160 | expect(object).toEqual({ old: true }); 161 | }); 162 | 163 | it('emits `history` if updates are outdated', () => { 164 | incoming.merge({ data: 'old state' }); 165 | incoming.meta('data').state = 1; 166 | 167 | node.merge({ data: 'new state' }); 168 | node.meta('data').state = 2; 169 | 170 | const spy = createSpy(); 171 | node.on('history', spy); 172 | 173 | node.merge(incoming); 174 | const [history] = spy.calls[0].arguments; 175 | 176 | expect(history).toBeA(Node); 177 | 178 | const object = history.snapshot(); 179 | expect(object).toEqual({ data: 'old state' }); 180 | }); 181 | 182 | it('does not emit `history` without stale updates', () => { 183 | const spy = createSpy(); 184 | node.on('history', spy); 185 | 186 | incoming.merge({ 'new property': 'yeah' }); 187 | incoming.meta('new property').state = 1; 188 | 189 | node.merge({ hello: 'Earth' }); 190 | 191 | node.merge(incoming); 192 | 193 | expect(spy).toNotHaveBeenCalled(); 194 | }); 195 | }); 196 | 197 | describe('conflict', () => { 198 | let conflict; 199 | 200 | beforeEach(() => { 201 | conflict = new Node(); 202 | 203 | node.merge({ value: '9' }); 204 | conflict.merge({ value: '5' }); 205 | 206 | // Same state. 207 | node.meta('value').state = conflict.meta('value').state; 208 | }); 209 | 210 | it('is ignored if it loses', () => { 211 | const { update, history } = node.merge(conflict); 212 | 213 | expect(update.snapshot()).toEqual({}); 214 | expect(history.snapshot()).toEqual({}); 215 | }); 216 | 217 | it('triggers an update if it wins', () => { 218 | node.meta('value').value = '1'; 219 | 220 | const { update } = node.merge(conflict); 221 | 222 | expect(update.snapshot()).toEqual({ value: '5' }); 223 | }); 224 | 225 | it('emits `conflict` if it wins', () => { 226 | const spy = createSpy(); 227 | node.on('conflict', spy); 228 | 229 | const current = node.meta('value'); 230 | const update = conflict.meta('value'); 231 | 232 | current.value = '1'; 233 | 234 | node.merge(conflict); 235 | 236 | expect(spy).toHaveBeenCalled(); 237 | expect(spy).toHaveBeenCalledWith(update, current); 238 | }); 239 | 240 | it('does not emit `conflict` if it loses', () => { 241 | const spy = createSpy(); 242 | node.on('conflict', spy); 243 | 244 | node.merge(conflict); 245 | 246 | expect(spy).toNotHaveBeenCalled(); 247 | }); 248 | }); 249 | }); 250 | -------------------------------------------------------------------------------- /src/Node/test.js: -------------------------------------------------------------------------------- 1 | /* eslint-env mocha */ 2 | import expect, { createSpy } from 'expect'; 3 | import Node from './index'; 4 | 5 | describe('Node static method', () => { 6 | describe('"from"', () => { 7 | it('turns a POJO into a node', () => { 8 | const node = Node.from({}); 9 | 10 | expect(node).toBeA(Node); 11 | }); 12 | 13 | it('imports the properties from the target object', () => { 14 | const date = new Date('1 Jan 1980'); 15 | const node = Node.from({ 16 | name: 'Sam', 17 | birthday: date, 18 | }); 19 | 20 | expect(node.value('name')).toBe('Sam'); 21 | expect(node.value('birthday')).toBe(date); 22 | }); 23 | 24 | it('provides an initial state', () => { 25 | const node = Node.from({ name: 'Alvin' }); 26 | 27 | expect(node.state('name')).toBe(1); 28 | }); 29 | }); 30 | 31 | describe('"source"', () => { 32 | it('creates a node that draws from an object', () => { 33 | const node = Node.create(); 34 | node.merge({ data: 'intact' }); 35 | const string = JSON.stringify(node); 36 | const object = JSON.parse(string); 37 | const copy = Node.source(object); 38 | expect(copy.value('data')).toBe('intact'); 39 | }); 40 | }); 41 | }); 42 | 43 | describe('Node', () => { 44 | let node; 45 | 46 | beforeEach(() => { 47 | node = Node.create(); 48 | }); 49 | 50 | /* Generic tests */ 51 | it('does not have properties upon creation', () => { 52 | const keys = [...node].map(([key]) => key); 53 | expect(keys.length).toBe(0); 54 | }); 55 | 56 | it('returns the uid when `toString` is called', () => { 57 | const result = node.toString(); 58 | const { uid } = node.meta(); 59 | expect(result).toBe(uid); 60 | }); 61 | 62 | it('returns the node when `toJSON` is called', () => { 63 | node.merge({ 'json worked': true }); 64 | const result = JSON.stringify(node); 65 | 66 | expect(result).toContain('json worked'); 67 | }); 68 | 69 | /* Not so generic tests */ 70 | describe('uid', () => { 71 | it('exists on creation', () => { 72 | const { uid } = node.meta(); 73 | expect(uid).toNotBe(undefined); 74 | }); 75 | 76 | it('is unique', () => { 77 | const { uid: uid1 } = Node.create().meta(); 78 | const { uid: uid2 } = Node.create().meta(); 79 | 80 | expect(uid1).toNotBe(uid2); 81 | }); 82 | 83 | it('uses the configuration provided', () => { 84 | const node = Node.create({ uid: 'custom' }); 85 | const { uid } = node.meta(); 86 | expect(uid).toBe('custom'); 87 | }); 88 | }); 89 | 90 | describe('state()', () => { 91 | it('returns 0 when there is no property', () => { 92 | const state = node.state('no such key'); 93 | expect(state).toBe(0); 94 | }); 95 | }); 96 | 97 | describe('meta()', () => { 98 | it('returns null if no values exist', () => { 99 | const result = node.meta('no such key'); 100 | expect(result).toBe(null); 101 | }); 102 | 103 | it('returns the metadata if the field exists', () => { 104 | node.merge({ name: 'Steve' }); 105 | const result = node.meta('name'); 106 | expect(result).toBeAn(Object); 107 | }); 108 | }); 109 | 110 | describe('Symbol.iterator()', () => { 111 | it('skips the node metadata field', () => { 112 | node.merge({ name: 'John' }); 113 | const keys = [...node].map(([key]) => key); 114 | 115 | // Should not contain '@object'. 116 | expect(keys).toEqual(['name']); 117 | }); 118 | 119 | it('includes key/value pairs for every field', () => { 120 | node.merge({ 121 | name: 'Stewart', 122 | tier: 'premium', 123 | }); 124 | 125 | const pairs = [...node]; 126 | 127 | expect(pairs).toEqual([['name', 'Stewart'], ['tier', 'premium']]); 128 | }); 129 | }); 130 | 131 | describe('value()', () => { 132 | it('returns undefined if the key cannot be found', () => { 133 | const result = node.value('no such key'); 134 | expect(result).toBe(undefined); 135 | }); 136 | 137 | it('returns undefined if called on reserved fields', () => { 138 | // Please, never do this in your code. 139 | node.meta().value = 'failure!'; 140 | 141 | const result = node.value('@object'); 142 | expect(result).toBe(undefined); 143 | }); 144 | }); 145 | 146 | describe('new()', () => { 147 | it('creates a node with the same ID', () => { 148 | const { uid } = node.meta(); 149 | const copy = node.new(); 150 | 151 | expect(copy.meta()).toContain({ uid }); 152 | }); 153 | 154 | it('does not carry over any properties', () => { 155 | node.merge({ original: true }); 156 | const copy = node.new(); 157 | 158 | expect(copy.snapshot()).toEqual({}); 159 | }); 160 | }); 161 | 162 | describe('setMetadata()', () => { 163 | it('updates the field metadata', () => { 164 | const update = { 165 | value: 'jack', 166 | lastChanged: 1491531671735, 167 | }; 168 | 169 | node.setMetadata('username', update); 170 | 171 | expect(node.meta('username')).toContain(update); 172 | }); 173 | 174 | it('sets the state', () => { 175 | const update = { 176 | value: 6.28, 177 | logs: 'address', 178 | }; 179 | 180 | node.setMetadata('pressure', update); 181 | 182 | expect(node.state('pressure')).toBe(1); 183 | }); 184 | 185 | it('bumps the state if the field existed before', () => { 186 | node.merge({ temp: 31 }); 187 | 188 | const update = { 189 | value: 30, 190 | encoding: 'KELVIN', 191 | }; 192 | 193 | node.setMetadata('temp', update); 194 | 195 | expect(node.state('temp')).toBe(2); 196 | }); 197 | 198 | it('ignores state if specified in the metadata', () => { 199 | node.setMetadata('dangerous', { state: 6 }); 200 | 201 | expect(node.state('dangerous')).toBe(1); 202 | }); 203 | 204 | it('does not mutate the metadata object', () => { 205 | const update = { state: 5 }; 206 | 207 | node.setMetadata('field', update); 208 | 209 | expect(update.state).toBe(5); 210 | expect(node.meta('field')).toNotBe(update); 211 | }); 212 | 213 | it('returns the merge delta', () => { 214 | const result = node.setMetadata('speed', { value: 150 }); 215 | 216 | expect(result.update).toBeA(Node); 217 | expect(result.history).toBeA(Node); 218 | }); 219 | 220 | it('emits "update"', () => { 221 | const spy = createSpy(); 222 | node.on('update', spy); 223 | 224 | node.setMetadata('field', { value: 'update!' }); 225 | 226 | expect(spy).toHaveBeenCalled(); 227 | const [update] = spy.calls[0].arguments; 228 | 229 | expect(update).toBeA(Node); 230 | expect(update.meta().uid).toBe(node.meta().uid); 231 | expect([...update]).toEqual([['field', 'update!']]); 232 | }); 233 | 234 | it('emits "history"', () => { 235 | const spy = createSpy(); 236 | node.on('history', spy); 237 | 238 | node.merge({ field: 'history' }); 239 | node.setMetadata('field', 'update'); 240 | 241 | expect(spy).toHaveBeenCalled(); 242 | const [update] = spy.calls[0].arguments; 243 | 244 | expect(update).toBeA(Node); 245 | expect([...update]).toEqual([['field', 'history']]); 246 | }); 247 | }); 248 | 249 | describe('rebase()', () => { 250 | let target; 251 | 252 | beforeEach(() => { 253 | target = new Node(); 254 | }); 255 | 256 | it('returns a new node', () => { 257 | const result = node.rebase(); 258 | 259 | expect(result).toBeA(Node); 260 | expect(result).toNotBe(node); 261 | }); 262 | 263 | it('returns the same node id', () => { 264 | const { uid } = node.rebase(target).meta(); 265 | 266 | expect(uid).toBe(node.meta().uid); 267 | }); 268 | 269 | it('does not change state if the node is empty', () => { 270 | node.merge({ initial: true }); 271 | const result = node.rebase(target); 272 | 273 | expect(result.state('initial')).toBe(1); 274 | }); 275 | 276 | it('does not change state if the fields do not overlap', () => { 277 | node.merge({ initial: true }); 278 | target.merge({ different: true }); 279 | target.meta('different').state = 2001; 280 | const result = node.rebase(target); 281 | 282 | expect(result.state('initial')).toBe(1); 283 | }); 284 | 285 | it('increments the state for overlapping fields', () => { 286 | node.merge({ existing: 'should replace' }); 287 | target.merge({ existing: 'replace me', different: true }); 288 | const result = node.rebase(target); 289 | 290 | expect(result.state('existing')).toBe(2); 291 | expect(result.state('different')).toBe(1); 292 | }); 293 | 294 | it('does not mutate field metadata', () => { 295 | node.merge({ old: false }); 296 | target.merge({ old: true }); 297 | 298 | const result = node.rebase(target); 299 | 300 | expect(result.meta('old')) 301 | .toNotBe(node.meta('old')) 302 | .toNotBe(target.meta('old')) 303 | .toNotContain({ state: 1 }); 304 | }); 305 | 306 | it('does not change fields which are already greater', () => { 307 | const state = 20000; 308 | node.merge({ old: false }); 309 | node.meta('old').state = state; 310 | target.merge({ old: true }); 311 | 312 | const result = node.rebase(target); 313 | 314 | expect(result.state('old')).toBe(state); 315 | }); 316 | }); 317 | 318 | describe('overlap()', () => { 319 | let target; 320 | 321 | beforeEach(() => { 322 | target = new Node(); 323 | }); 324 | 325 | it('returns a new node', () => { 326 | const result = node.overlap(target); 327 | 328 | expect(result).toBeA(Node); 329 | expect(result).toNotBe(node); 330 | expect(result).toNotBe(target); 331 | }); 332 | 333 | it('has the same id', () => { 334 | const { uid } = node.overlap(target).meta(); 335 | 336 | expect(uid).toBe(node.meta().uid); 337 | }); 338 | 339 | it('has no fields if the target is empty', () => { 340 | node.merge({ existing: true }); 341 | const result = node.overlap(target); 342 | 343 | expect([...result]).toEqual([]); 344 | }); 345 | 346 | it('ignores additional properties in the target', () => { 347 | node.merge({ existing: true }); 348 | target.merge({ existing: true, different: true }); 349 | const result = node.overlap(target); 350 | 351 | expect([...result]).toEqual([...node]); 352 | }); 353 | 354 | it('ignores additional properties in the source', () => { 355 | node.merge({ shared: true, extra: true }); 356 | target.merge({ shared: true }); 357 | const result = node.overlap(target); 358 | 359 | expect([...result]).toEqual([...target]); 360 | }); 361 | 362 | it('is identical when compared against itself', () => { 363 | node.merge({ exhausted: 'field name creativity' }); 364 | const result = node.overlap(node); 365 | 366 | expect([...result]).toEqual([...node]); 367 | }); 368 | 369 | it('contains the values from the source node', () => { 370 | node.merge({ shared: 'node value' }); 371 | target.merge({ shared: 'target value' }); 372 | 373 | const result = node.overlap(target); 374 | 375 | expect(result.meta('shared')).toBe(node.meta('shared')); 376 | }); 377 | }); 378 | 379 | describe('snapshot()', () => { 380 | it('returns an object', () => { 381 | const result = node.snapshot(); 382 | 383 | expect(result).toBeAn(Object); 384 | }); 385 | 386 | it('contains every key/value pair in the node', () => { 387 | const state = { name: 'Living Room Lamp', state: 'ON' }; 388 | node.merge(state); 389 | 390 | const result = node.snapshot(); 391 | 392 | expect(result).toEqual(state); 393 | }); 394 | }); 395 | }); 396 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import Graph from './Graph'; 2 | import Node from './Node'; 3 | 4 | export { Graph, Node }; 5 | -------------------------------------------------------------------------------- /src/union/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Utilities for deterministically merging node attributes. 3 | * 4 | * @private 5 | * @module graph-crdt/src/union 6 | */ 7 | 8 | /** 9 | * Deterministically resolves merge conflicts 10 | * on JSON-compatible data. 11 | * 12 | * @param {Object} field1 - Current state field metadata. 13 | * @param {Object} field2 - Update field metadata. 14 | * @returns {Object} - The greater value, or if they're 15 | * equal, the current state. 16 | */ 17 | export function conflict(field1, field2) { 18 | /** Turn the values into comparable strings. */ 19 | const string = { 20 | current: JSON.stringify(field1.value), 21 | update: JSON.stringify(field2.value), 22 | }; 23 | 24 | /** Are the values equal? */ 25 | const equal = string.current === string.update; 26 | 27 | /** Is our current value greater? */ 28 | const greater = string.current > string.update; 29 | 30 | /** Return the winning value. */ 31 | return equal || greater ? field1 : field2; 32 | } 33 | -------------------------------------------------------------------------------- /src/union/test.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable require-jsdoc*/ 2 | import { describe, it, beforeEach } from 'mocha'; 3 | import expect from 'expect'; 4 | import { conflict } from './index'; 5 | const { createSpy } = expect; 6 | 7 | describe('A union', () => { 8 | describe('conflict', () => { 9 | let current, update, result, inverse; 10 | const state = 10; 11 | 12 | beforeEach(() => { 13 | current = { state }; 14 | update = { state }; 15 | }); 16 | 17 | function setup() { 18 | // Try it in both orders. 19 | result = conflict(current, update); 20 | inverse = conflict(update, current); 21 | } 22 | 23 | it('should resolve to the greater object uid', () => { 24 | current.value = { edge: 'abc' }; 25 | update.value = { edge: 'def' }; 26 | setup(); 27 | expect(result).toBe(update); 28 | expect(inverse).toBe(update); 29 | }); 30 | 31 | it('should return the first arg if uids are equal', () => { 32 | current.value = { edge: 'equal' }; 33 | update.value = { edge: 'equal' }; 34 | setup(); 35 | expect(result).toBe(current); 36 | expect(inverse).toBe(update); 37 | }); 38 | 39 | it('should favor objects over strings', () => { 40 | current.value = { edge: 'def' }; 41 | update.value = 'abc'; 42 | setup(); 43 | expect(result).toBe(current); 44 | expect(inverse).toBe(current); 45 | }); 46 | 47 | it('should favor numbers over strings', () => { 48 | current.value = '5'; 49 | update.value = 5; 50 | setup(); 51 | expect(result).toBe(update); 52 | expect(inverse).toBe(update); 53 | }); 54 | 55 | it('should resolve to larger values', () => { 56 | current.value = 'def'; 57 | update.value = 'abc'; 58 | setup(); 59 | expect(result).toBe(current); 60 | expect(inverse).toBe(current); 61 | }); 62 | 63 | it('should return the first arg if both are equivalent', () => { 64 | current.value = 'equal'; 65 | update.value = 'equal'; 66 | setup(); 67 | expect(result).toBe(current); 68 | expect(inverse).toBe(update); 69 | }); 70 | 71 | it('should toString objects to compare', () => { 72 | const spy = createSpy().andReturn('abc'); 73 | current.value = { toJSON: spy }; 74 | update.value = { toJSON: () => 'def' }; 75 | setup(); 76 | expect(result).toBe(update); 77 | expect(inverse).toBe(update); 78 | expect(spy).toHaveBeenCalled(); 79 | }); 80 | 81 | it('should favor edges over strings of the same value', () => { 82 | current.value = { edge: 'equal' }; 83 | update.value = 'equal'; 84 | setup(); 85 | expect(result).toBe(current); 86 | expect(inverse).toBe(current); 87 | }); 88 | }); 89 | }); 90 | --------------------------------------------------------------------------------