UNPKG

5.05 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 - [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
75Class representing an observed-remove set
76
77Implements all methods and iterators of the native `Set` object in addition to the following.
78See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set>
79
80#### Parameters
81
82- `entries` **Iterable&lt;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
87Emit 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)&lt;[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;any>>** Array of insertions and deletions (optional, default `this.dump()`)
92
93Returns **void**
94
95#### dump
96
97Return an array containing all of the set's insertions and deletions.
98
99Returns **\[[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>]**
100
101#### process
102
103Process an array of insertion and deletions.
104
105##### Parameters
106
107- `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
108- `skipFlush` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `false`)
109
110Returns **void**
111
112## Map API
113
114<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
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
128Class representing a Observed Remove Map
129
130Implements all methods and iterators of the native `Map` object in addition to the following.
131See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map>
132
133#### Parameters
134
135- `entries` **Iterable&lt;\[K, V]>**
136- `options` **Options** (optional, default `{}`)
137
138#### sync
139
140Emit 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)&lt;[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;any>>** Array of insertions and deletions (optional, default `this.dump()`)
145
146Returns **void**
147
148#### dump
149
150Return an array containing all of the map's insertions and deletions.
151
152Returns **\[[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>]**