1 | # Observed-Remove Set and Map
|
2 |
|
3 | [![CircleCI](https://circleci.com/gh/wehriam/observed-remove.svg?style=svg)](https://circleci.com/gh/wehriam/observed-remove) [![npm version](https://badge.fury.io/js/observed-remove.svg)](http://badge.fury.io/js/observed-remove) [![codecov](https://codecov.io/gh/wehriam/observed-remove/branch/master/graph/badge.svg)](https://codecov.io/gh/wehriam/observed-remove)
|
4 |
|
5 | Eventually-consistent, conflict-free replicated data types (CRDT) [implemented](https://github.com/wehriam/observed-remove/blob/master/src/index.js) using native `Map` and `Set` objects.
|
6 |
|
7 | ```js
|
8 | const { ObservedRemoveSet } = require('observed-remove');
|
9 |
|
10 | const alice = new ObservedRemoveSet();
|
11 | const bob = new ObservedRemoveSet();
|
12 |
|
13 | alice.on('publish', (message) => {
|
14 | setTimeout(() => bob.process(message), Math.round(Math.random() * 1000));
|
15 | });
|
16 |
|
17 | bob.on('publish', (message) => {
|
18 | setTimeout(() => alice.process(message), Math.round(Math.random() * 1000));
|
19 | });
|
20 |
|
21 | alice.add('foo');
|
22 | bob.add('bar');
|
23 |
|
24 | // Later
|
25 |
|
26 | alice.has('bar'); // true
|
27 | bob.has('foo'); // true
|
28 | ```
|
29 |
|
30 | ```js
|
31 | const { ObservedRemoveMap } = require('observed-remove');
|
32 |
|
33 | const alice = new ObservedRemoveMap();
|
34 | const bob = new ObservedRemoveMap();
|
35 |
|
36 | alice.on('publish', (message) => {
|
37 | setTimeout(() => bob.process(message), Math.round(Math.random() * 1000));
|
38 | });
|
39 |
|
40 | bob.on('publish', (message) => {
|
41 | setTimeout(() => alice.process(message), Math.round(Math.random() * 1000));
|
42 | });
|
43 |
|
44 | alice.set('a', 1);
|
45 | bob.add('b', 2);
|
46 |
|
47 | // Later
|
48 |
|
49 | alice.get('b'); // 2
|
50 | bob.get('a'); // 1
|
51 | ```
|
52 |
|
53 | ## Install
|
54 |
|
55 | `yarn add observed-remove`
|
56 |
|
57 | ## Set API
|
58 |
|
59 |
|
60 |
|
61 | #### Table of Contents
|
62 |
|
63 | - [ObservedRemoveSet](#observedremoveset)
|
64 | - [Parameters](#parameters)
|
65 | - [sync](#sync)
|
66 | - [Parameters](#parameters-1)
|
67 | - [dump](#dump)
|
68 | - [process](#process)
|
69 | - [Parameters](#parameters-2)
|
70 |
|
71 | ### ObservedRemoveSet
|
72 |
|
73 | **Extends EventEmitter**
|
74 |
|
75 | Class representing an observed-remove set
|
76 |
|
77 | Implements all methods and iterators of the native `Set` object in addition to the following.
|
78 | See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set>
|
79 |
|
80 | #### Parameters
|
81 |
|
82 | - `entries` **Iterable<T>** Iterable of initial values (optional, default `[]`)
|
83 | - `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** (optional, default `{}`)
|
84 |
|
85 | #### sync
|
86 |
|
87 | Emit a 'publish' event containing a specified queue or all of the set's insertions and deletions.
|
88 |
|
89 | ##### Parameters
|
90 |
|
91 | - `queue` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<any>>** Array of insertions and deletions (optional, default `this.dump()`)
|
92 |
|
93 | Returns **void**
|
94 |
|
95 | #### dump
|
96 |
|
97 | Return an array containing all of the set's insertions and deletions.
|
98 |
|
99 | Returns **\[[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<any>, [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<any>]**
|
100 |
|
101 | #### process
|
102 |
|
103 | Process an array of insertion and deletions.
|
104 |
|
105 | ##### Parameters
|
106 |
|
107 | - `queue` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<any>>** Array of insertions and deletions
|
108 | - `skipFlush` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `false`)
|
109 |
|
110 | Returns **void**
|
111 |
|
112 | ## Map API
|
113 |
|
114 |
|
115 |
|
116 | #### Table of Contents
|
117 |
|
118 | - [ObservedRemoveMap](#observedremovemap)
|
119 | - [Parameters](#parameters)
|
120 | - [sync](#sync)
|
121 | - [Parameters](#parameters-1)
|
122 | - [dump](#dump)
|
123 |
|
124 | ### ObservedRemoveMap
|
125 |
|
126 | **Extends EventEmitter**
|
127 |
|
128 | Class representing a Observed Remove Map
|
129 |
|
130 | Implements all methods and iterators of the native `Map` object in addition to the following.
|
131 | See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map>
|
132 |
|
133 | #### Parameters
|
134 |
|
135 | - `entries` **Iterable<\[K, V]>**
|
136 | - `options` **Options** (optional, default `{}`)
|
137 |
|
138 | #### sync
|
139 |
|
140 | Emit a 'publish' event containing a specified queue or all of the set's insertions and deletions.
|
141 |
|
142 | ##### Parameters
|
143 |
|
144 | - `queue` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<any>>** Array of insertions and deletions (optional, default `this.dump()`)
|
145 |
|
146 | Returns **void**
|
147 |
|
148 | #### dump
|
149 |
|
150 | Return an array containing all of the map's insertions and deletions.
|
151 |
|
152 | Returns **\[[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<any>, [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<any>]**
|