UNPKG

5.23 kBMarkdownView Raw
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
5Eventually-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
8const { ObservedRemoveSet } = require('observed-remove');
9
10const alice = new ObservedRemoveSet();
11const bob = new ObservedRemoveSet();
12
13alice.on('publish', (message) => {
14 setTimeout(() => bob.process(message), Math.round(Math.random() * 1000));
15});
16
17bob.on('publish', (message) => {
18 setTimeout(() => alice.process(message), Math.round(Math.random() * 1000));
19});
20
21alice.add('foo');
22bob.add('bar');
23
24// Later
25
26alice.has('bar'); // true
27bob.has('foo'); // true
28```
29
30```js
31const { ObservedRemoveMap } = require('observed-remove');
32
33const alice = new ObservedRemoveMap();
34const bob = new ObservedRemoveMap();
35
36alice.on('publish', (message) => {
37 setTimeout(() => bob.process(message), Math.round(Math.random() * 1000));
38});
39
40bob.on('publish', (message) => {
41 setTimeout(() => alice.process(message), Math.round(Math.random() * 1000));
42});
43
44alice.set('a', 1);
45bob.add('b', 2);
46
47// Later
48
49alice.get('b'); // 2
50bob.get('a'); // 1
51```
52
53## Install
54
55`yarn add observed-remove`
56
57## Set API
58
59<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
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
72Class representing an observed-remove set
73
74Implements all methods and iterators of the native `Set` object in addition to the following.
75See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set>
76
77**Parameters**
78
79- `entries` **Iterable&lt;T>**
80- `options` **Options** (optional, default `{}`)
81
82#### sync
83
84Emit 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)&lt;[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;any>>** Array of insertions and deletions (optional, default `this.dump()`)
89
90Returns **void**
91
92#### dump
93
94Return an array containing all of the set's insertions and deletions.
95
96Returns **\[[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;any>, [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;any>]**
97
98#### process
99
100Process an array of insertion and deletions.
101
102**Parameters**
103
104- `queue` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;any>>** Array of insertions and deletions
105- `skipFlush` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `false`)
106
107Returns **void**
108
109## Map API
110
111<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
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
124Class representing a Observed Remove Map
125
126Implements all methods and iterators of the native `Map` object in addition to the following.
127See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map>
128
129**Parameters**
130
131- `entries` **Iterable&lt;\[K, V]>**
132- `options` **Options** (optional, default `{}`)
133
134#### dump
135
136Return an array containing all of the map's insertions and deletions.
137
138Returns **\[[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;any>, [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;any>]**
139
140#### sync
141
142Emit 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)&lt;[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;any>>** Array of insertions and deletions (optional, default `this.dump()`)
147
148Returns **void**
149
150#### process
151
152Process an array of insertion and deletions.
153
154**Parameters**
155
156- `queue` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;any>>** Array of insertions and deletions
157- `skipFlush` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `false`)
158
159Returns **void**