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 | - [sync](#sync)
|
65 | - [dump](#dump)
|
66 | - [process](#process)
|
67 |
|
68 | ### ObservedRemoveSet
|
69 |
|
70 | **Extends EventEmitter**
|
71 |
|
72 | Class representing an observed-remove set
|
73 |
|
74 | Implements all methods and iterators of the native `Set` object in addition to the following.
|
75 | See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set>
|
76 |
|
77 | **Parameters**
|
78 |
|
79 | - `entries` **Iterable<T>**
|
80 | - `options` **Options** (optional, default `{}`)
|
81 |
|
82 | #### sync
|
83 |
|
84 | Emit a 'publish' event containing a specified queue or all of the set's insertions and deletions.
|
85 |
|
86 | **Parameters**
|
87 |
|
88 | - `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()`)
|
89 |
|
90 | Returns **void**
|
91 |
|
92 | #### dump
|
93 |
|
94 | Return an array containing all of the set's insertions and deletions.
|
95 |
|
96 | 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>]**
|
97 |
|
98 | #### process
|
99 |
|
100 | Process an array of insertion and deletions.
|
101 |
|
102 | **Parameters**
|
103 |
|
104 | - `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
|
105 | - `skipFlush` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `false`)
|
106 |
|
107 | Returns **void**
|
108 |
|
109 | ## Map API
|
110 |
|
111 |
|
112 |
|
113 | #### Table of Contents
|
114 |
|
115 | - [ObservedRemoveMap](#observedremovemap)
|
116 | - [dump](#dump)
|
117 | - [sync](#sync)
|
118 | - [process](#process)
|
119 |
|
120 | ### ObservedRemoveMap
|
121 |
|
122 | **Extends EventEmitter**
|
123 |
|
124 | Class representing a Observed Remove Map
|
125 |
|
126 | Implements all methods and iterators of the native `Map` object in addition to the following.
|
127 | See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map>
|
128 |
|
129 | **Parameters**
|
130 |
|
131 | - `entries` **Iterable<\[K, V]>**
|
132 | - `options` **Options** (optional, default `{}`)
|
133 |
|
134 | #### dump
|
135 |
|
136 | Return an array containing all of the map's insertions and deletions.
|
137 |
|
138 | 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>]**
|
139 |
|
140 | #### sync
|
141 |
|
142 | Emit a 'publish' event containing a specified queue or all of the set's insertions and deletions.
|
143 |
|
144 | **Parameters**
|
145 |
|
146 | - `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()`)
|
147 |
|
148 | Returns **void**
|
149 |
|
150 | #### process
|
151 |
|
152 | Process an array of insertion and deletions.
|
153 |
|
154 | **Parameters**
|
155 |
|
156 | - `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
|
157 | - `skipFlush` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `false`)
|
158 |
|
159 | Returns **void**
|