UNPKG

4.49 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
99#### process
100
101Process an array of insertion and deletions.
102
103##### Parameters
104
105- `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
106- `skipFlush` (optional, default `false`)
107
108Returns **void**
109
110## Map API
111
112<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
113
114#### Table of Contents
115
116- [ObservedRemoveMap](#observedremovemap)
117 - [Parameters](#parameters)
118 - [sync](#sync)
119 - [Parameters](#parameters-1)
120 - [dump](#dump)
121
122### ObservedRemoveMap
123
124**Extends EventEmitter**
125
126Class representing a Observed Remove Map
127
128Implements all methods and iterators of the native `Map` object in addition to the following.
129See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map>
130
131#### Parameters
132
133- `entries`
134- `options` (optional, default `{}`)
135
136#### sync
137
138Emit a 'publish' event containing a specified queue or all of the set's insertions and deletions.
139
140##### Parameters
141
142- `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()`)
143
144Returns **void**
145
146#### dump
147
148Return an array containing all of the map's insertions and deletions.