1 |
|
2 | # Lib0 [![Build Status](https://travis-ci.com/dmonad/lib0.svg?branch=main)](https://travis-ci.com/dmonad/lib0)
|
3 | > Monorepo of isomorphic utility functions
|
4 |
|
5 | This library is meant to replace all global JavaScript functions with isomorphic module imports. Additionally, it implements several performance-oriented utility modules. Most noteworthy are the binary encoding/decoding modules **[lib0/encoding]** / **[lib0/decoding]**, the randomized testing framework **[lib0/testing]**, the fast Pseudo Random Number Generator **[lib0/PRNG]**, the small socket.io alternative **[lib0/websocket]**, and the logging module **[lib0/logging]** that allows colorized logging in all environments. Lib0 has only one dependency, which is also from the author of lib0. If lib0 is transpiled with rollup or webpack, very little code is produced because of the way that it is written. All exports are pure and are removed by transpilers that support dead code elimination. Here is an example of how dead code elemination and mangling optimizes code from lib0:
|
6 |
|
7 | ```js
|
8 | // How the code is optimized by transpilers:
|
9 |
|
10 | // lib0/json.js
|
11 | export const stringify = JSON.stringify
|
12 | export const parse = JSON.parse
|
13 |
|
14 | // index.js
|
15 | import * as json from 'lib0/json'
|
16 | export const f = (arg1, arg2) => json.stringify(arg1) + json.stringify(arg2)
|
17 |
|
18 | // compiled with rollup and uglifyjs:
|
19 | const s=JSON.stringify,f=(a,b)=>s(a)+s(b)
|
20 | export {f}
|
21 | ```
|
22 |
|
23 | ## Performance resources
|
24 |
|
25 | Each function in this library is tested thoroughly and is not deoptimized by v8 (except some logging and comparison functions that can't be implemented without deoptimizations). This library implements its own test suite that is designed for randomized testing and inspecting performance issues.
|
26 |
|
27 | * `node --trace-deopt` and `node --trace-opt`
|
28 | * https://youtu.be/IFWulQnM5E0 Good intro video
|
29 | * https://github.com/thlorenz/v8-perf
|
30 | * https://github.com/thlorenz/deoptigate - A great tool for investigating deoptimizations
|
31 | * https://github.com/vhf/v8-bailout-reasons - Description of some deopt messages
|
32 |
|
33 | ## Code style
|
34 |
|
35 | The code style might be a bit different from what you are used to. Stay open. Most of the design choices have been thought through. The purpose of this code style is to create code that is optimized by the compiler and that results in small code bundles when used with common module bundlers. Keep that in mind when reading the library.
|
36 |
|
37 | * No polymorphism!
|
38 | * Modules should only export pure functions and constants. This way the module bundler can eliminate dead code. The statement `const x = someCondition ? A : B` cannot be eleminated, because it is tied to a condition.
|
39 | * Use Classes for structuring data. Classes are well supported by jsdoc and are immediately optimized by the compiler. I.e. prefer `class Coord { constructor (x, y) { this.x = x; this.y = y} }` instead of `{ x: x, y: y }`, because the compiler needs to be assured that the order of properties does not change. `{ y: y, x: x }` has a different hidden class than `{ x: x, y: y }`, which will lead to code deoptimizations if their use is alternated.
|
40 | * The user of your module should never create data objects with the `new` keyword. Prefer exporting factory functions like `const createCoordinate = (x, y) => new Coord(x, y)`.
|
41 | * The use of class methods is discouraged, because method names can't be mangled or removed by dead code elimination.
|
42 | * The only acceptable use of methods is when two objects implement functionality differently.
|
43 | E.g. `class Duck { eat () { swallow() } }` and `class Cow { eat () { chew() } }` have the
|
44 | same signature, but implement it differently.
|
45 | * Prefer `const` variable declarations. Use `let` only in loops. `const` always leads to easier code.
|
46 | * Keep the potential execution stack small and compartmentalized. Nobody wants to debug spaghetti code.
|
47 | * Give proper names to your functions and ask yourself if you would know what the function does if you saw it in the execution stack.
|
48 | * Avoid recursion. There is a stack limit in most browsers and not every recursive function is optimized by the compiler.
|
49 | * Semicolons are superfluous. Lint with https://standardjs.com/
|
50 |
|
51 | ## Using lib0
|
52 |
|
53 | `lib0` contains isomorphic modules that work in nodejs, the browser, and other environments. It exports modules as the `commonjs` and the new `esm module` format.
|
54 |
|
55 | If possible,
|
56 |
|
57 | **ESM module**
|
58 | ```js
|
59 | import module from 'lib0/[module]' // automatically resolves to lib0/[module].js
|
60 | ```
|
61 |
|
62 | **CommonJS**
|
63 | ```js
|
64 | require('lib0/[module]') // automatically resolves to lib0/dist/[module].cjs
|
65 | ```
|
66 |
|
67 | **Manual**
|
68 |
|
69 | Automatically resolving to `commonjs` and `esm modules` is implemented using *conditional exports* which is available in `node>=v12`. If support for older versions is required, then it is recommended to define the location of the module manually:
|
70 |
|
71 | ```js
|
72 | import module from 'lib0/[module].js'
|
73 | // require('lib0/dist/[module].cjs')
|
74 | ```
|
75 |
|
76 | ## Modules
|
77 |
|
78 | <details><summary><b>[lib0/array]</b> Utility module to work with Arrays.</summary>
|
79 | <pre>import * as array from 'lib0/array'</pre>
|
80 | <dl>
|
81 | <b><code>array.last(arr: ArrayLike<L>): L</code></b><br>
|
82 | <dd><p>Return the last element of an array. The element must exist</p></dd>
|
83 | <b><code>array.create(): Array<C></code></b><br>
|
84 | <b><code>array.copy(a: Array<D>): Array<D></code></b><br>
|
85 | <b><code>array.appendTo(dest: Array<M>, src: Array<M>)</code></b><br>
|
86 | <dd><p>Append elements from src to dest</p></dd>
|
87 | <b><code>array.from(arraylike: ArrayLike<T>|Iterable<T>): T</code></b><br>
|
88 | <dd><p>Transforms something array-like to an actual Array.</p></dd>
|
89 | <b><code>array.every(arr: ARR, f: function(ITEM, number, ARR):boolean): boolean</code></b><br>
|
90 | <dd><p>True iff condition holds on every element in the Array.</p></dd>
|
91 | <b><code>array.some(arr: ARR, f: function(S, number, ARR):boolean): boolean</code></b><br>
|
92 | <dd><p>True iff condition holds on some element in the Array.</p></dd>
|
93 | <b><code>array.equalFlat(a: ArrayLike<ELEM>, b: ArrayLike<ELEM>): boolean</code></b><br>
|
94 | <b><code>array.flatten(arr: Array<Array<ELEM>>): Array<ELEM></code></b><br>
|
95 | <b><code>array.isArray</code></b><br>
|
96 | <b><code>array.unique(arr: Array<T>): Array<T></code></b><br>
|
97 | <b><code>array.uniqueBy(arr: ArrayLike<T>, mapper: function(T):M): Array<T></code></b><br>
|
98 | </dl>
|
99 | </details>
|
100 | <details><summary><b>[lib0/binary]</b> Binary data constants.</summary>
|
101 | <pre>import * as binary from 'lib0/binary'</pre>
|
102 | <dl>
|
103 | <b><code>binary.BIT1: number</code></b><br>
|
104 | <dd><p>n-th bit activated.</p></dd>
|
105 | <b><code>binary.BIT2</code></b><br>
|
106 | <b><code>binary.BIT3</code></b><br>
|
107 | <b><code>binary.BIT4</code></b><br>
|
108 | <b><code>binary.BIT5</code></b><br>
|
109 | <b><code>binary.BIT6</code></b><br>
|
110 | <b><code>binary.BIT7</code></b><br>
|
111 | <b><code>binary.BIT8</code></b><br>
|
112 | <b><code>binary.BIT9</code></b><br>
|
113 | <b><code>binary.BIT10</code></b><br>
|
114 | <b><code>binary.BIT11</code></b><br>
|
115 | <b><code>binary.BIT12</code></b><br>
|
116 | <b><code>binary.BIT13</code></b><br>
|
117 | <b><code>binary.BIT14</code></b><br>
|
118 | <b><code>binary.BIT15</code></b><br>
|
119 | <b><code>binary.BIT16</code></b><br>
|
120 | <b><code>binary.BIT17</code></b><br>
|
121 | <b><code>binary.BIT18</code></b><br>
|
122 | <b><code>binary.BIT19</code></b><br>
|
123 | <b><code>binary.BIT20</code></b><br>
|
124 | <b><code>binary.BIT21</code></b><br>
|
125 | <b><code>binary.BIT22</code></b><br>
|
126 | <b><code>binary.BIT23</code></b><br>
|
127 | <b><code>binary.BIT24</code></b><br>
|
128 | <b><code>binary.BIT25</code></b><br>
|
129 | <b><code>binary.BIT26</code></b><br>
|
130 | <b><code>binary.BIT27</code></b><br>
|
131 | <b><code>binary.BIT28</code></b><br>
|
132 | <b><code>binary.BIT29</code></b><br>
|
133 | <b><code>binary.BIT30</code></b><br>
|
134 | <b><code>binary.BIT31</code></b><br>
|
135 | <b><code>binary.BIT32</code></b><br>
|
136 | <b><code>binary.BITS0: number</code></b><br>
|
137 | <dd><p>First n bits activated.</p></dd>
|
138 | <b><code>binary.BITS1</code></b><br>
|
139 | <b><code>binary.BITS2</code></b><br>
|
140 | <b><code>binary.BITS3</code></b><br>
|
141 | <b><code>binary.BITS4</code></b><br>
|
142 | <b><code>binary.BITS5</code></b><br>
|
143 | <b><code>binary.BITS6</code></b><br>
|
144 | <b><code>binary.BITS7</code></b><br>
|
145 | <b><code>binary.BITS8</code></b><br>
|
146 | <b><code>binary.BITS9</code></b><br>
|
147 | <b><code>binary.BITS10</code></b><br>
|
148 | <b><code>binary.BITS11</code></b><br>
|
149 | <b><code>binary.BITS12</code></b><br>
|
150 | <b><code>binary.BITS13</code></b><br>
|
151 | <b><code>binary.BITS14</code></b><br>
|
152 | <b><code>binary.BITS15</code></b><br>
|
153 | <b><code>binary.BITS16</code></b><br>
|
154 | <b><code>binary.BITS17</code></b><br>
|
155 | <b><code>binary.BITS18</code></b><br>
|
156 | <b><code>binary.BITS19</code></b><br>
|
157 | <b><code>binary.BITS20</code></b><br>
|
158 | <b><code>binary.BITS21</code></b><br>
|
159 | <b><code>binary.BITS22</code></b><br>
|
160 | <b><code>binary.BITS23</code></b><br>
|
161 | <b><code>binary.BITS24</code></b><br>
|
162 | <b><code>binary.BITS25</code></b><br>
|
163 | <b><code>binary.BITS26</code></b><br>
|
164 | <b><code>binary.BITS27</code></b><br>
|
165 | <b><code>binary.BITS28</code></b><br>
|
166 | <b><code>binary.BITS29</code></b><br>
|
167 | <b><code>binary.BITS30</code></b><br>
|
168 | <b><code>binary.BITS31: number</code></b><br>
|
169 | <b><code>binary.BITS32: number</code></b><br>
|
170 | </dl>
|
171 | </details>
|
172 | <details><summary><b>[lib0/broadcastchannel]</b> Helpers for cross-tab communication using broadcastchannel with LocalStorage fallback.</summary>
|
173 | <pre>import * as broadcastchannel from 'lib0/broadcastchannel'</pre>
|
174 |
|
175 | <pre class="prettyprint source lang-js"><code>// In browser window A:
|
176 | broadcastchannel.subscribe('my events', data => console.log(data))
|
177 | broadcastchannel.publish('my events', 'Hello world!') // => A: 'Hello world!' fires synchronously in same tab
|
178 |
|
179 | // In browser window B:
|
180 | broadcastchannel.publish('my events', 'hello from tab B') // => A: 'hello from tab B'
|
181 | </code></pre>
|
182 | <dl>
|
183 | <b><code>broadcastchannel.subscribe(room: string, f: function(any, any):any)</code></b><br>
|
184 | <dd><p>Subscribe to global <code>publish</code> events.</p></dd>
|
185 | <b><code>broadcastchannel.unsubscribe(room: string, f: function(any, any):any)</code></b><br>
|
186 | <dd><p>Unsubscribe from <code>publish</code> global events.</p></dd>
|
187 | <b><code>broadcastchannel.publish(room: string, data: any, origin: any)</code></b><br>
|
188 | <dd><p>Publish data to all subscribers (including subscribers on this tab)</p></dd>
|
189 | </dl>
|
190 | </details>
|
191 | <details><summary><b>[lib0/buffer]</b> Utility functions to work with buffers (Uint8Array).</summary>
|
192 | <pre>import * as buffer from 'lib0/buffer'</pre>
|
193 | <dl>
|
194 | <b><code>buffer.createUint8ArrayFromLen(len: number)</code></b><br>
|
195 | <b><code>buffer.createUint8ArrayViewFromArrayBuffer(buffer: ArrayBuffer, byteOffset: number, length: number)</code></b><br>
|
196 | <dd><p>Create Uint8Array with initial content from buffer</p></dd>
|
197 | <b><code>buffer.createUint8ArrayFromArrayBuffer(buffer: ArrayBuffer)</code></b><br>
|
198 | <dd><p>Create Uint8Array with initial content from buffer</p></dd>
|
199 | <b><code>buffer.toBase64</code></b><br>
|
200 | <b><code>buffer.fromBase64</code></b><br>
|
201 | <b><code>buffer.copyUint8Array(uint8Array: Uint8Array): Uint8Array</code></b><br>
|
202 | <dd><p>Copy the content of an Uint8Array view to a new ArrayBuffer.</p></dd>
|
203 | <b><code>buffer.encodeAny(data: any): Uint8Array</code></b><br>
|
204 | <dd><p>Encode anything as a UInt8Array. It's a pun on typescripts's <code>any</code> type.
|
205 | See encoding.writeAny for more information.</p></dd>
|
206 | <b><code>buffer.decodeAny(buf: Uint8Array): any</code></b><br>
|
207 | <dd><p>Decode an any-encoded value.</p></dd>
|
208 | </dl>
|
209 | </details>
|
210 | <details><summary><b>[lib0/cache]</b> An implementation of a map which has keys that expire.</summary>
|
211 | <pre>import * as cache from 'lib0/cache'</pre>
|
212 | <dl>
|
213 | <b><code>new cache.Cache(timeout: number)</code></b><br>
|
214 | <b><code>cache.removeStale(cache: module:cache.Cache<K, V>): number</code></b><br>
|
215 | <b><code>cache.set(cache: module:cache.Cache<K, V>, key: K, value: V)</code></b><br>
|
216 | <b><code>cache.get(cache: module:cache.Cache<K, V>, key: K): V | undefined</code></b><br>
|
217 | <b><code>cache.refreshTimeout(cache: module:cache.Cache<K, V>, key: K)</code></b><br>
|
218 | <b><code>cache.getAsync(cache: module:cache.Cache<K, V>, key: K): V | Promise<V> | undefined</code></b><br>
|
219 | <dd><p>Works well in conjunktion with setIfUndefined which has an async init function.
|
220 | Using getAsync & setIfUndefined ensures that the init function is only called once.</p></dd>
|
221 | <b><code>cache.remove(cache: module:cache.Cache<K, V>, key: K)</code></b><br>
|
222 | <b><code>cache.setIfUndefined(cache: module:cache.Cache<K, V>, key: K, init: function():Promise<V>, removeNull: boolean): Promise<V> | V</code></b><br>
|
223 | <b><code>cache.create(timeout: number)</code></b><br>
|
224 | </dl>
|
225 | </details>
|
226 | <details><summary><b>[lib0/component]</b> Web components.</summary>
|
227 | <pre>import * as component from 'lib0/component'</pre>
|
228 | <dl>
|
229 | <b><code>component.registry: CustomElementRegistry</code></b><br>
|
230 | <b><code>component.define(name: string, constr: any, opts: ElementDefinitionOptions)</code></b><br>
|
231 | <b><code>component.whenDefined(name: string): Promise<CustomElementConstructor></code></b><br>
|
232 | <b><code>new component.Lib0Component(state: S)</code></b><br>
|
233 | <b><code>component.Lib0Component#state: S|null</code></b><br>
|
234 | <b><code>component.Lib0Component#setState(state: S, forceStateUpdate: boolean)</code></b><br>
|
235 | <b><code>component.Lib0Component#updateState(stateUpdate: any)</code></b><br>
|
236 | <b><code>component.createComponent(name: string, cnf: module:component~CONF<T>): Class<module:component.Lib0Component></code></b><br>
|
237 | <b><code>component.createComponentDefiner(definer: function)</code></b><br>
|
238 | <b><code>component.defineListComponent</code></b><br>
|
239 | <b><code>component.defineLazyLoadingComponent</code></b><br>
|
240 | </dl>
|
241 | </details>
|
242 | <details><summary><b>[lib0/conditions]</b> Often used conditions.</summary>
|
243 | <pre>import * as conditions from 'lib0/conditions'</pre>
|
244 | <dl>
|
245 | <b><code>conditions.undefinedToNull</code></b><br>
|
246 | </dl>
|
247 | </details>
|
248 | <details><summary><b>[lib0/crypto]</b> </summary>
|
249 | <pre>import * as crypto from 'lib0/crypto'</pre>
|
250 | <dl>
|
251 | <b><code>y(data: string | Uint8Array): Uint8Array</code></b><br>
|
252 | <b><code>ymmetricKey(secret: string | Uint8Array, salt: string | Uint8Array, opts: Object, opts.extractable: boolean, opts.usages: Array<'sign'|'verify'|'encrypt'|'decrypt'>): PromiseLike<CryptoKey></code></b><br>
|
253 | <b><code>ymmetricKey()</code></b><br>
|
254 | <b><code>eAsymmetricKey(opts: Object, opts.extractable: boolean, opts.usages: Array<'sign'|'verify'|'encrypt'|'decrypt'>)</code></b><br>
|
255 | <b><code>eAsymmetricKey()</code></b><br>
|
256 | <b><code>ey(key: CryptoKey)</code></b><br>
|
257 | <b><code>ey()</code></b><br>
|
258 | <b><code>ymmetricKey(jwk: any, opts: Object, opts.extractable: boolean, opts.usages: Array<'sign'|'verify'|'encrypt'|'decrypt'>)</code></b><br>
|
259 | <b><code>ymmetricKey()</code></b><br>
|
260 | <b><code>symmetricKey(jwk: any, opts: Object, opts.extractable: boolean, opts.usages: Array<'sign'|'verify'|'encrypt'|'decrypt'>)</code></b><br>
|
261 | <b><code>symmetricKey()</code></b><br>
|
262 | <b><code>(data: Uint8Array, key: CryptoKey): PromiseLike<Uint8Array></code></b><br>
|
263 | <b><code>()</code></b><br>
|
264 | <b><code>(data: Uint8Array, key: CryptoKey): PromiseLike<Uint8Array></code></b><br>
|
265 | <b><code>()</code></b><br>
|
266 | <b><code>(data: Uint8Array, privateKey: CryptoKey): PromiseLike<Uint8Array></code></b><br>
|
267 | <b><code>()</code></b><br>
|
268 | <b><code>(signature: Uint8Array, data: Uint8Array, publicKey: CryptoKey): PromiseLike<boolean></code></b><br>
|
269 | <b><code>()</code></b><br>
|
270 | </dl>
|
271 | </details>
|
272 | <details><summary><b>[lib0/decoding]</b> Efficient schema-less binary decoding with support for variable length encoding.</summary>
|
273 | <pre>import * as decoding from 'lib0/decoding'</pre>
|
274 |
|
275 | <p>Use [lib0/decoding] with [lib0/encoding]. Every encoding function has a corresponding decoding function.</p>
|
276 | <p>Encodes numbers in little-endian order (least to most significant byte order)
|
277 | and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
|
278 | which is also used in Protocol Buffers.</p>
|
279 | <pre class="prettyprint source lang-js"><code>// encoding step
|
280 | const encoder = encoding.createEncoder()
|
281 | encoding.writeVarUint(encoder, 256)
|
282 | encoding.writeVarString(encoder, 'Hello world!')
|
283 | const buf = encoding.toUint8Array(encoder)
|
284 | </code></pre>
|
285 | <pre class="prettyprint source lang-js"><code>// decoding step
|
286 | const decoder = decoding.createDecoder(buf)
|
287 | decoding.readVarUint(decoder) // => 256
|
288 | decoding.readVarString(decoder) // => 'Hello world!'
|
289 | decoding.hasContent(decoder) // => false - all data is read
|
290 | </code></pre>
|
291 | <dl>
|
292 | <b><code>new decoding.Decoder(uint8Array: Uint8Array)</code></b><br>
|
293 | <dd><p>A Decoder handles the decoding of an Uint8Array.</p></dd>
|
294 | <b><code>decoding.Decoder#arr: Uint8Array</code></b><br>
|
295 | <dd><p>Decoding target.</p></dd>
|
296 | <b><code>decoding.Decoder#pos: number</code></b><br>
|
297 | <dd><p>Current decoding position.</p></dd>
|
298 | <b><code>decoding.createDecoder(uint8Array: Uint8Array): module:decoding.Decoder</code></b><br>
|
299 | <b><code>decoding.hasContent(decoder: module:decoding.Decoder): boolean</code></b><br>
|
300 | <b><code>decoding.clone(decoder: module:decoding.Decoder, newPos: number): module:decoding.Decoder</code></b><br>
|
301 | <dd><p>Clone a decoder instance.
|
302 | Optionally set a new position parameter.</p></dd>
|
303 | <b><code>decoding.readUint8Array(decoder: module:decoding.Decoder, len: number): Uint8Array</code></b><br>
|
304 | <dd><p>Create an Uint8Array view of the next <code>len</code> bytes and advance the position by <code>len</code>.</p>
|
305 | <p>Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
|
306 | Use <code>buffer.copyUint8Array</code> to copy the result into a new Uint8Array.</p></dd>
|
307 | <b><code>decoding.readVarUint8Array(decoder: module:decoding.Decoder): Uint8Array</code></b><br>
|
308 | <dd><p>Read variable length Uint8Array.</p>
|
309 | <p>Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
|
310 | Use <code>buffer.copyUint8Array</code> to copy the result into a new Uint8Array.</p></dd>
|
311 | <b><code>decoding.readTailAsUint8Array(decoder: module:decoding.Decoder): Uint8Array</code></b><br>
|
312 | <dd><p>Read the rest of the content as an ArrayBuffer</p></dd>
|
313 | <b><code>decoding.skip8(decoder: module:decoding.Decoder): number</code></b><br>
|
314 | <dd><p>Skip one byte, jump to the next position.</p></dd>
|
315 | <b><code>decoding.readUint8(decoder: module:decoding.Decoder): number</code></b><br>
|
316 | <dd><p>Read one byte as unsigned integer.</p></dd>
|
317 | <b><code>decoding.readUint16(decoder: module:decoding.Decoder): number</code></b><br>
|
318 | <dd><p>Read 2 bytes as unsigned integer.</p></dd>
|
319 | <b><code>decoding.readUint32(decoder: module:decoding.Decoder): number</code></b><br>
|
320 | <dd><p>Read 4 bytes as unsigned integer.</p></dd>
|
321 | <b><code>decoding.readUint32BigEndian(decoder: module:decoding.Decoder): number</code></b><br>
|
322 | <dd><p>Read 4 bytes as unsigned integer in big endian order.
|
323 | (most significant byte first)</p></dd>
|
324 | <b><code>decoding.peekUint8(decoder: module:decoding.Decoder): number</code></b><br>
|
325 | <dd><p>Look ahead without incrementing the position
|
326 | to the next byte and read it as unsigned integer.</p></dd>
|
327 | <b><code>decoding.peekUint16(decoder: module:decoding.Decoder): number</code></b><br>
|
328 | <dd><p>Look ahead without incrementing the position
|
329 | to the next byte and read it as unsigned integer.</p></dd>
|
330 | <b><code>decoding.peekUint32(decoder: module:decoding.Decoder): number</code></b><br>
|
331 | <dd><p>Look ahead without incrementing the position
|
332 | to the next byte and read it as unsigned integer.</p></dd>
|
333 | <b><code>decoding.readVarUint(decoder: module:decoding.Decoder): number</code></b><br>
|
334 | <dd><p>Read unsigned integer (32bit) with variable length.
|
335 | 1/8th of the storage is used as encoding overhead.</p>
|
336 | <ul>
|
337 | <li>numbers < 2^7 is stored in one bytlength</li>
|
338 | <li>numbers < 2^14 is stored in two bylength</li>
|
339 | </ul></dd>
|
340 | <b><code>decoding.readVarInt(decoder: module:decoding.Decoder): number</code></b><br>
|
341 | <dd><p>Read signed integer (32bit) with variable length.
|
342 | 1/8th of the storage is used as encoding overhead.</p>
|
343 | <ul>
|
344 | <li>numbers < 2^7 is stored in one bytlength</li>
|
345 | <li>numbers < 2^14 is stored in two bylength</li>
|
346 | </ul></dd>
|
347 | <b><code>decoding.peekVarUint(decoder: module:decoding.Decoder): number</code></b><br>
|
348 | <dd><p>Look ahead and read varUint without incrementing position</p></dd>
|
349 | <b><code>decoding.peekVarInt(decoder: module:decoding.Decoder): number</code></b><br>
|
350 | <dd><p>Look ahead and read varUint without incrementing position</p></dd>
|
351 | <b><code>decoding.readVarString</code></b><br>
|
352 | <b><code>decoding.peekVarString(decoder: module:decoding.Decoder): string</code></b><br>
|
353 | <dd><p>Look ahead and read varString without incrementing position</p></dd>
|
354 | <b><code>decoding.readFromDataView(decoder: module:decoding.Decoder, len: number): DataView</code></b><br>
|
355 | <b><code>decoding.readFloat32(decoder: module:decoding.Decoder)</code></b><br>
|
356 | <b><code>decoding.readFloat64(decoder: module:decoding.Decoder)</code></b><br>
|
357 | <b><code>decoding.readBigInt64(decoder: module:decoding.Decoder)</code></b><br>
|
358 | <b><code>decoding.readBigUint64(decoder: module:decoding.Decoder)</code></b><br>
|
359 | <b><code>decoding.readAny(decoder: module:decoding.Decoder)</code></b><br>
|
360 | <b><code>new decoding.RleDecoder(uint8Array: Uint8Array, reader: function(module:decoding.Decoder):T)</code></b><br>
|
361 | <dd><p>T must not be null.</p></dd>
|
362 | <b><code>decoding.RleDecoder#s: T|null</code></b><br>
|
363 | <dd><p>Current state</p></dd>
|
364 | <b><code>decoding.RleDecoder#read()</code></b><br>
|
365 | <b><code>decoding.RleDecoder#s: T</code></b><br>
|
366 | <b><code>new decoding.IntDiffDecoder(uint8Array: Uint8Array, start: number)</code></b><br>
|
367 | <b><code>decoding.IntDiffDecoder#s: number</code></b><br>
|
368 | <dd><p>Current state</p></dd>
|
369 | <b><code>decoding.IntDiffDecoder#read(): number</code></b><br>
|
370 | <b><code>new decoding.RleIntDiffDecoder(uint8Array: Uint8Array, start: number)</code></b><br>
|
371 | <b><code>decoding.RleIntDiffDecoder#s: number</code></b><br>
|
372 | <dd><p>Current state</p></dd>
|
373 | <b><code>decoding.RleIntDiffDecoder#read(): number</code></b><br>
|
374 | <b><code>decoding.RleIntDiffDecoder#s: number</code></b><br>
|
375 | <b><code>new decoding.UintOptRleDecoder(uint8Array: Uint8Array)</code></b><br>
|
376 | <b><code>decoding.UintOptRleDecoder#s: number</code></b><br>
|
377 | <b><code>decoding.UintOptRleDecoder#read()</code></b><br>
|
378 | <b><code>decoding.UintOptRleDecoder#s: number</code></b><br>
|
379 | <b><code>new decoding.IncUintOptRleDecoder(uint8Array: Uint8Array)</code></b><br>
|
380 | <b><code>decoding.IncUintOptRleDecoder#s: number</code></b><br>
|
381 | <b><code>decoding.IncUintOptRleDecoder#read()</code></b><br>
|
382 | <b><code>new decoding.IntDiffOptRleDecoder(uint8Array: Uint8Array)</code></b><br>
|
383 | <b><code>decoding.IntDiffOptRleDecoder#s: number</code></b><br>
|
384 | <b><code>decoding.IntDiffOptRleDecoder#read(): number</code></b><br>
|
385 | <b><code>new decoding.StringDecoder(uint8Array: Uint8Array)</code></b><br>
|
386 | <b><code>decoding.StringDecoder#spos: number</code></b><br>
|
387 | <b><code>decoding.StringDecoder#read(): string</code></b><br>
|
388 | <b><code>decoding.RleDecoder#arr: Uint8Array</code></b><br>
|
389 | <dd><p>Decoding target.</p></dd>
|
390 | <b><code>decoding.RleDecoder#pos: number</code></b><br>
|
391 | <dd><p>Current decoding position.</p></dd>
|
392 | <b><code>decoding.IntDiffDecoder#arr: Uint8Array</code></b><br>
|
393 | <dd><p>Decoding target.</p></dd>
|
394 | <b><code>decoding.IntDiffDecoder#pos: number</code></b><br>
|
395 | <dd><p>Current decoding position.</p></dd>
|
396 | <b><code>decoding.RleIntDiffDecoder#arr: Uint8Array</code></b><br>
|
397 | <dd><p>Decoding target.</p></dd>
|
398 | <b><code>decoding.RleIntDiffDecoder#pos: number</code></b><br>
|
399 | <dd><p>Current decoding position.</p></dd>
|
400 | <b><code>decoding.UintOptRleDecoder#arr: Uint8Array</code></b><br>
|
401 | <dd><p>Decoding target.</p></dd>
|
402 | <b><code>decoding.UintOptRleDecoder#pos: number</code></b><br>
|
403 | <dd><p>Current decoding position.</p></dd>
|
404 | <b><code>decoding.IncUintOptRleDecoder#arr: Uint8Array</code></b><br>
|
405 | <dd><p>Decoding target.</p></dd>
|
406 | <b><code>decoding.IncUintOptRleDecoder#pos: number</code></b><br>
|
407 | <dd><p>Current decoding position.</p></dd>
|
408 | <b><code>decoding.IntDiffOptRleDecoder#arr: Uint8Array</code></b><br>
|
409 | <dd><p>Decoding target.</p></dd>
|
410 | <b><code>decoding.IntDiffOptRleDecoder#pos: number</code></b><br>
|
411 | <dd><p>Current decoding position.</p></dd>
|
412 | </dl>
|
413 | </details>
|
414 | <details><summary><b>[lib0/diff]</b> Efficient diffs.</summary>
|
415 | <pre>import * as diff from 'lib0/diff'</pre>
|
416 | <dl>
|
417 | <b><code>diff.simpleDiffString(a: string, b: string): module:diff~SimpleDiff<string></code></b><br>
|
418 | <dd><p>Create a diff between two strings. This diff implementation is highly
|
419 | efficient, but not very sophisticated.</p></dd>
|
420 | <b><code>diff.simpleDiff</code></b><br>
|
421 | <b><code>diff.simpleDiffArray(a: Array<T>, b: Array<T>, compare: function(T, T):boolean): module:diff~SimpleDiff<Array<T>></code></b><br>
|
422 | <dd><p>Create a diff between two arrays. This diff implementation is highly
|
423 | efficient, but not very sophisticated.</p>
|
424 | <p>Note: This is basically the same function as above. Another function was created so that the runtime
|
425 | can better optimize these function calls.</p></dd>
|
426 | <b><code>diff.simpleDiffStringWithCursor(a: string, b: string, cursor: number)</code></b><br>
|
427 | <dd><p>Diff text and try to diff at the current cursor position.</p></dd>
|
428 | </dl>
|
429 | </details>
|
430 | <details><summary><b>[lib0/dom]</b> Utility module to work with the DOM.</summary>
|
431 | <pre>import * as dom from 'lib0/dom'</pre>
|
432 | <dl>
|
433 | <b><code>dom.doc: Document</code></b><br>
|
434 | <b><code>dom.createElement</code></b><br>
|
435 | <b><code>dom.createDocumentFragment</code></b><br>
|
436 | <b><code>dom.createTextNode</code></b><br>
|
437 | <b><code>dom.domParser</code></b><br>
|
438 | <b><code>dom.emitCustomEvent</code></b><br>
|
439 | <b><code>dom.setAttributes</code></b><br>
|
440 | <b><code>dom.setAttributesMap</code></b><br>
|
441 | <b><code>dom.fragment</code></b><br>
|
442 | <b><code>dom.append</code></b><br>
|
443 | <b><code>dom.remove</code></b><br>
|
444 | <b><code>dom.addEventListener</code></b><br>
|
445 | <b><code>dom.removeEventListener</code></b><br>
|
446 | <b><code>dom.addEventListeners</code></b><br>
|
447 | <b><code>dom.removeEventListeners</code></b><br>
|
448 | <b><code>dom.element</code></b><br>
|
449 | <b><code>dom.canvas</code></b><br>
|
450 | <b><code>dom.text</code></b><br>
|
451 | <b><code>dom.pairToStyleString</code></b><br>
|
452 | <b><code>dom.pairsToStyleString</code></b><br>
|
453 | <b><code>dom.mapToStyleString</code></b><br>
|
454 | <b><code>dom.querySelector</code></b><br>
|
455 | <b><code>dom.querySelectorAll</code></b><br>
|
456 | <b><code>dom.getElementById</code></b><br>
|
457 | <b><code>dom.parseFragment</code></b><br>
|
458 | <b><code>dom.childNodes: any</code></b><br>
|
459 | <b><code>dom.parseElement</code></b><br>
|
460 | <b><code>dom.replaceWith</code></b><br>
|
461 | <b><code>dom.insertBefore</code></b><br>
|
462 | <b><code>dom.appendChild</code></b><br>
|
463 | <b><code>dom.ELEMENT_NODE</code></b><br>
|
464 | <b><code>dom.TEXT_NODE</code></b><br>
|
465 | <b><code>dom.CDATA_SECTION_NODE</code></b><br>
|
466 | <b><code>dom.COMMENT_NODE</code></b><br>
|
467 | <b><code>dom.DOCUMENT_NODE</code></b><br>
|
468 | <b><code>dom.DOCUMENT_TYPE_NODE</code></b><br>
|
469 | <b><code>dom.DOCUMENT_FRAGMENT_NODE</code></b><br>
|
470 | <b><code>dom.checkNodeType(node: any, type: number)</code></b><br>
|
471 | <b><code>dom.isParentOf(parent: Node, child: HTMLElement)</code></b><br>
|
472 | </dl>
|
473 | </details>
|
474 | <details><summary><b>[lib0/encoding]</b> Efficient schema-less binary encoding with support for variable length encoding.</summary>
|
475 | <pre>import * as encoding from 'lib0/encoding'</pre>
|
476 |
|
477 | <p>Use [lib0/encoding] with [lib0/decoding]. Every encoding function has a corresponding decoding function.</p>
|
478 | <p>Encodes numbers in little-endian order (least to most significant byte order)
|
479 | and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
|
480 | which is also used in Protocol Buffers.</p>
|
481 | <pre class="prettyprint source lang-js"><code>// encoding step
|
482 | const encoder = encoding.createEncoder()
|
483 | encoding.writeVarUint(encoder, 256)
|
484 | encoding.writeVarString(encoder, 'Hello world!')
|
485 | const buf = encoding.toUint8Array(encoder)
|
486 | </code></pre>
|
487 | <pre class="prettyprint source lang-js"><code>// decoding step
|
488 | const decoder = decoding.createDecoder(buf)
|
489 | decoding.readVarUint(decoder) // => 256
|
490 | decoding.readVarString(decoder) // => 'Hello world!'
|
491 | decoding.hasContent(decoder) // => false - all data is read
|
492 | </code></pre>
|
493 | <dl>
|
494 | <b><code>new encoding.Encoder()</code></b><br>
|
495 | <dd><p>A BinaryEncoder handles the encoding to an Uint8Array.</p></dd>
|
496 | <b><code>encoding.Encoder#bufs: Array<Uint8Array></code></b><br>
|
497 | <b><code>encoding.createEncoder(): module:encoding.Encoder</code></b><br>
|
498 | <b><code>encoding.length(encoder: module:encoding.Encoder): number</code></b><br>
|
499 | <dd><p>The current length of the encoded data.</p></dd>
|
500 | <b><code>encoding.toUint8Array(encoder: module:encoding.Encoder): Uint8Array</code></b><br>
|
501 | <dd><p>Transform to Uint8Array.</p></dd>
|
502 | <b><code>encoding.verifyLen(encoder: module:encoding.Encoder, len: number)</code></b><br>
|
503 | <dd><p>Verify that it is possible to write <code>len</code> bytes wtihout checking. If
|
504 | necessary, a new Buffer with the required length is attached.</p></dd>
|
505 | <b><code>encoding.write(encoder: module:encoding.Encoder, num: number)</code></b><br>
|
506 | <dd><p>Write one byte to the encoder.</p></dd>
|
507 | <b><code>encoding.set(encoder: module:encoding.Encoder, pos: number, num: number)</code></b><br>
|
508 | <dd><p>Write one byte at a specific position.
|
509 | Position must already be written (i.e. encoder.length > pos)</p></dd>
|
510 | <b><code>encoding.writeUint8(encoder: module:encoding.Encoder, num: number)</code></b><br>
|
511 | <dd><p>Write one byte as an unsigned integer.</p></dd>
|
512 | <b><code>encoding.setUint8(encoder: module:encoding.Encoder, pos: number, num: number)</code></b><br>
|
513 | <dd><p>Write one byte as an unsigned Integer at a specific location.</p></dd>
|
514 | <b><code>encoding.writeUint16(encoder: module:encoding.Encoder, num: number)</code></b><br>
|
515 | <dd><p>Write two bytes as an unsigned integer.</p></dd>
|
516 | <b><code>encoding.setUint16(encoder: module:encoding.Encoder, pos: number, num: number)</code></b><br>
|
517 | <dd><p>Write two bytes as an unsigned integer at a specific location.</p></dd>
|
518 | <b><code>encoding.writeUint32(encoder: module:encoding.Encoder, num: number)</code></b><br>
|
519 | <dd><p>Write two bytes as an unsigned integer</p></dd>
|
520 | <b><code>encoding.writeUint32BigEndian(encoder: module:encoding.Encoder, num: number)</code></b><br>
|
521 | <dd><p>Write two bytes as an unsigned integer in big endian order.
|
522 | (most significant byte first)</p></dd>
|
523 | <b><code>encoding.setUint32(encoder: module:encoding.Encoder, pos: number, num: number)</code></b><br>
|
524 | <dd><p>Write two bytes as an unsigned integer at a specific location.</p></dd>
|
525 | <b><code>encoding.writeVarUint(encoder: module:encoding.Encoder, num: number)</code></b><br>
|
526 | <dd><p>Write a variable length unsigned integer. Max encodable integer is 2^53.</p></dd>
|
527 | <b><code>encoding.writeVarInt(encoder: module:encoding.Encoder, num: number)</code></b><br>
|
528 | <dd><p>Write a variable length integer.</p>
|
529 | <p>We use the 7th bit instead for signaling that this is a negative number.</p></dd>
|
530 | <b><code>encoding.writeVarString</code></b><br>
|
531 | <b><code>encoding.writeBinaryEncoder(encoder: module:encoding.Encoder, append: module:encoding.Encoder)</code></b><br>
|
532 | <dd><p>Write the content of another Encoder.</p></dd>
|
533 | <b><code>encoding.writeUint8Array(encoder: module:encoding.Encoder, uint8Array: Uint8Array)</code></b><br>
|
534 | <dd><p>Append fixed-length Uint8Array to the encoder.</p></dd>
|
535 | <b><code>encoding.writeVarUint8Array(encoder: module:encoding.Encoder, uint8Array: Uint8Array)</code></b><br>
|
536 | <dd><p>Append an Uint8Array to Encoder.</p></dd>
|
537 | <b><code>encoding.writeOnDataView(encoder: module:encoding.Encoder, len: number): DataView</code></b><br>
|
538 | <dd><p>Create an DataView of the next <code>len</code> bytes. Use it to write data after
|
539 | calling this function.</p>
|
540 | <pre class="prettyprint source lang-js"><code>// write float32 using DataView
|
541 | const dv = writeOnDataView(encoder, 4)
|
542 | dv.setFloat32(0, 1.1)
|
543 | // read float32 using DataView
|
544 | const dv = readFromDataView(encoder, 4)
|
545 | dv.getFloat32(0) // => 1.100000023841858 (leaving it to the reader to find out why this is the correct result)
|
546 | </code></pre></dd>
|
547 | <b><code>encoding.writeFloat32(encoder: module:encoding.Encoder, num: number)</code></b><br>
|
548 | <b><code>encoding.writeFloat64(encoder: module:encoding.Encoder, num: number)</code></b><br>
|
549 | <b><code>encoding.writeBigInt64(encoder: module:encoding.Encoder, num: bigint)</code></b><br>
|
550 | <b><code>encoding.writeBigUint64(encoder: module:encoding.Encoder, num: bigint)</code></b><br>
|
551 | <b><code>encoding.writeAny(encoder: module:encoding.Encoder, data: undefined|null|number|bigint|boolean|string|Object<string,any>|Array<any>|Uint8Array)</code></b><br>
|
552 | <dd><p>Encode data with efficient binary format.</p>
|
553 | <p>Differences to JSON:
|
554 | • Transforms data to a binary format (not to a string)
|
555 | • Encodes undefined, NaN, and ArrayBuffer (these can't be represented in JSON)
|
556 | • Numbers are efficiently encoded either as a variable length integer, as a
|
557 | 32 bit float, as a 64 bit float, or as a 64 bit bigint.</p>
|
558 | <p>Encoding table:</p>
|
559 | <table>
|
560 | <thead>
|
561 | <tr>
|
562 | <th>Data Type</th>
|
563 | <th>Prefix</th>
|
564 | <th>Encoding Method</th>
|
565 | <th>Comment</th>
|
566 | </tr>
|
567 | </thead>
|
568 | <tbody>
|
569 | <tr>
|
570 | <td>undefined</td>
|
571 | <td>127</td>
|
572 | <td></td>
|
573 | <td>Functions, symbol, and everything that cannot be identified is encoded as undefined</td>
|
574 | </tr>
|
575 | <tr>
|
576 | <td>null</td>
|
577 | <td>126</td>
|
578 | <td></td>
|
579 | <td></td>
|
580 | </tr>
|
581 | <tr>
|
582 | <td>integer</td>
|
583 | <td>125</td>
|
584 | <td>writeVarInt</td>
|
585 | <td>Only encodes 32 bit signed integers</td>
|
586 | </tr>
|
587 | <tr>
|
588 | <td>float32</td>
|
589 | <td>124</td>
|
590 | <td>writeFloat32</td>
|
591 | <td></td>
|
592 | </tr>
|
593 | <tr>
|
594 | <td>float64</td>
|
595 | <td>123</td>
|
596 | <td>writeFloat64</td>
|
597 | <td></td>
|
598 | </tr>
|
599 | <tr>
|
600 | <td>bigint</td>
|
601 | <td>122</td>
|
602 | <td>writeBigInt64</td>
|
603 | <td></td>
|
604 | </tr>
|
605 | <tr>
|
606 | <td>boolean (false)</td>
|
607 | <td>121</td>
|
608 | <td></td>
|
609 | <td>True and false are different data types so we save the following byte</td>
|
610 | </tr>
|
611 | <tr>
|
612 | <td>boolean (true)</td>
|
613 | <td>120</td>
|
614 | <td></td>
|
615 | <td>- 0b01111000 so the last bit determines whether true or false</td>
|
616 | </tr>
|
617 | <tr>
|
618 | <td>string</td>
|
619 | <td>119</td>
|
620 | <td>writeVarString</td>
|
621 | <td></td>
|
622 | </tr>
|
623 | <tr>
|
624 | <td>object<string,any></td>
|
625 | <td>118</td>
|
626 | <td>custom</td>
|
627 | <td>Writes {length} then {length} key-value pairs</td>
|
628 | </tr>
|
629 | <tr>
|
630 | <td>array<any></td>
|
631 | <td>117</td>
|
632 | <td>custom</td>
|
633 | <td>Writes {length} then {length} json values</td>
|
634 | </tr>
|
635 | <tr>
|
636 | <td>Uint8Array</td>
|
637 | <td>116</td>
|
638 | <td>writeVarUint8Array</td>
|
639 | <td>We use Uint8Array for any kind of binary data</td>
|
640 | </tr>
|
641 | </tbody>
|
642 | </table>
|
643 | <p>Reasons for the decreasing prefix:
|
644 | We need the first bit for extendability (later we may want to encode the
|
645 | prefix with writeVarUint). The remaining 7 bits are divided as follows:
|
646 | [0-30] the beginning of the data range is used for custom purposes
|
647 | (defined by the function that uses this library)
|
648 | [31-127] the end of the data range is used for data encoding by
|
649 | lib0/encoding.js</p></dd>
|
650 | <b><code>new encoding.RleEncoder(writer: function(module:encoding.Encoder, T):void)</code></b><br>
|
651 | <dd><p>Now come a few stateful encoder that have their own classes.</p></dd>
|
652 | <b><code>encoding.RleEncoder#s: T|null</code></b><br>
|
653 | <dd><p>Current state</p></dd>
|
654 | <b><code>encoding.RleEncoder#write(v: T)</code></b><br>
|
655 | <b><code>new encoding.IntDiffEncoder(start: number)</code></b><br>
|
656 | <dd><p>Basic diff decoder using variable length encoding.</p>
|
657 | <p>Encodes the values [3, 1100, 1101, 1050, 0] to [3, 1097, 1, -51, -1050] using writeVarInt.</p></dd>
|
658 | <b><code>encoding.IntDiffEncoder#s: number</code></b><br>
|
659 | <dd><p>Current state</p></dd>
|
660 | <b><code>encoding.IntDiffEncoder#write(v: number)</code></b><br>
|
661 | <b><code>new encoding.RleIntDiffEncoder(start: number)</code></b><br>
|
662 | <dd><p>A combination of IntDiffEncoder and RleEncoder.</p>
|
663 | <p>Basically first writes the IntDiffEncoder and then counts duplicate diffs using RleEncoding.</p>
|
664 | <p>Encodes the values [1,1,1,2,3,4,5,6] as [1,1,0,2,1,5] (RLE([1,0,0,1,1,1,1,1]) ⇒ RleIntDiff[1,1,0,2,1,5])</p></dd>
|
665 | <b><code>encoding.RleIntDiffEncoder#s: number</code></b><br>
|
666 | <dd><p>Current state</p></dd>
|
667 | <b><code>encoding.RleIntDiffEncoder#write(v: number)</code></b><br>
|
668 | <b><code>new encoding.UintOptRleEncoder()</code></b><br>
|
669 | <dd><p>Optimized Rle encoder that does not suffer from the mentioned problem of the basic Rle encoder.</p>
|
670 | <p>Internally uses VarInt encoder to write unsigned integers. If the input occurs multiple times, we write
|
671 | write it as a negative number. The UintOptRleDecoder then understands that it needs to read a count.</p>
|
672 | <p>Encodes [1,2,3,3,3] as [1,2,-3,3] (once 1, once 2, three times 3)</p></dd>
|
673 | <b><code>encoding.UintOptRleEncoder#s: number</code></b><br>
|
674 | <b><code>encoding.UintOptRleEncoder#write(v: number)</code></b><br>
|
675 | <b><code>encoding.UintOptRleEncoder#toUint8Array()</code></b><br>
|
676 | <b><code>new encoding.IncUintOptRleEncoder()</code></b><br>
|
677 | <dd><p>Increasing Uint Optimized RLE Encoder</p>
|
678 | <p>The RLE encoder counts the number of same occurences of the same value.
|
679 | The IncUintOptRle encoder counts if the value increases.
|
680 | I.e. 7, 8, 9, 10 will be encoded as [-7, 4]. 1, 3, 5 will be encoded
|
681 | as [1, 3, 5].</p></dd>
|
682 | <b><code>encoding.IncUintOptRleEncoder#s: number</code></b><br>
|
683 | <b><code>encoding.IncUintOptRleEncoder#write(v: number)</code></b><br>
|
684 | <b><code>encoding.IncUintOptRleEncoder#toUint8Array()</code></b><br>
|
685 | <b><code>new encoding.IntDiffOptRleEncoder()</code></b><br>
|
686 | <dd><p>A combination of the IntDiffEncoder and the UintOptRleEncoder.</p>
|
687 | <p>The count approach is similar to the UintDiffOptRleEncoder, but instead of using the negative bitflag, it encodes
|
688 | in the LSB whether a count is to be read. Therefore this Encoder only supports 31 bit integers!</p>
|
689 | <p>Encodes [1, 2, 3, 2] as [3, 1, 6, -1] (more specifically [(1 << 1) | 1, (3 << 0) | 0, -1])</p>
|
690 | <p>Internally uses variable length encoding. Contrary to normal UintVar encoding, the first byte contains:</p>
|
691 | <ul>
|
692 | <li>1 bit that denotes whether the next value is a count (LSB)</li>
|
693 | <li>1 bit that denotes whether this value is negative (MSB - 1)</li>
|
694 | <li>1 bit that denotes whether to continue reading the variable length integer (MSB)</li>
|
695 | </ul>
|
696 | <p>Therefore, only five bits remain to encode diff ranges.</p>
|
697 | <p>Use this Encoder only when appropriate. In most cases, this is probably a bad idea.</p></dd>
|
698 | <b><code>encoding.IntDiffOptRleEncoder#s: number</code></b><br>
|
699 | <b><code>encoding.IntDiffOptRleEncoder#write(v: number)</code></b><br>
|
700 | <b><code>encoding.IntDiffOptRleEncoder#toUint8Array()</code></b><br>
|
701 | <b><code>new encoding.StringEncoder()</code></b><br>
|
702 | <dd><p>Optimized String Encoder.</p>
|
703 | <p>Encoding many small strings in a simple Encoder is not very efficient. The function call to decode a string takes some time and creates references that must be eventually deleted.
|
704 | In practice, when decoding several million small strings, the GC will kick in more and more often to collect orphaned string objects (or maybe there is another reason?).</p>
|
705 | <p>This string encoder solves the above problem. All strings are concatenated and written as a single string using a single encoding call.</p>
|
706 | <p>The lengths are encoded using a UintOptRleEncoder.</p></dd>
|
707 | <b><code>encoding.StringEncoder#sarr: Array<string></code></b><br>
|
708 | <b><code>encoding.StringEncoder#write(string: string)</code></b><br>
|
709 | <b><code>encoding.StringEncoder#toUint8Array()</code></b><br>
|
710 | <b><code>encoding.RleEncoder#bufs: Array<Uint8Array></code></b><br>
|
711 | <b><code>encoding.IntDiffEncoder#bufs: Array<Uint8Array></code></b><br>
|
712 | <b><code>encoding.RleIntDiffEncoder#bufs: Array<Uint8Array></code></b><br>
|
713 | </dl>
|
714 | </details>
|
715 | <details><summary><b>[lib0/map]</b> Isomorphic module to work access the environment (query params, env variables).</summary>
|
716 | <pre>import * as map from 'lib0/environment'</pre>
|
717 | <dl>
|
718 | <b><code>map.isNode</code></b><br>
|
719 | <b><code>map.isBrowser</code></b><br>
|
720 | <b><code>map.isMac</code></b><br>
|
721 | <b><code>map.hasParam</code></b><br>
|
722 | <b><code>map.getParam</code></b><br>
|
723 | <b><code>map.getVariable</code></b><br>
|
724 | <b><code>map.getConf(name: string): string|null</code></b><br>
|
725 | <b><code>map.hasConf</code></b><br>
|
726 | <b><code>map.production</code></b><br>
|
727 | <b><code>map.supportsColor</code></b><br>
|
728 | </dl>
|
729 | </details>
|
730 | <details><summary><b>[lib0/error]</b> Error helpers.</summary>
|
731 | <pre>import * as error from 'lib0/error'</pre>
|
732 | <dl>
|
733 | <b><code>error.create(s: string): Error</code></b><br>
|
734 | <b><code>error.methodUnimplemented(): never</code></b><br>
|
735 | <b><code>error.unexpectedCase(): never</code></b><br>
|
736 | </dl>
|
737 | </details>
|
738 | <details><summary><b>[lib0/eventloop]</b> Utility module to work with EcmaScript's event loop.</summary>
|
739 | <pre>import * as eventloop from 'lib0/eventloop'</pre>
|
740 | <dl>
|
741 | <b><code>eventloop.enqueue(f: function():void)</code></b><br>
|
742 | <b><code>eventloop#destroy()</code></b><br>
|
743 | <b><code>eventloop.timeout(timeout: number, callback: function): module:eventloop~TimeoutObject</code></b><br>
|
744 | <b><code>eventloop.interval(timeout: number, callback: function): module:eventloop~TimeoutObject</code></b><br>
|
745 | <b><code>eventloop.Animation</code></b><br>
|
746 | <b><code>eventloop.animationFrame(cb: function(number):void): module:eventloop~TimeoutObject</code></b><br>
|
747 | <b><code>eventloop.idleCallback(cb: function): module:eventloop~TimeoutObject</code></b><br>
|
748 | <dd><p>Note: this is experimental and is probably only useful in browsers.</p></dd>
|
749 | <b><code>eventloop.createDebouncer(timeout: number): function(function():void):void</code></b><br>
|
750 | </dl>
|
751 | </details>
|
752 | <details><summary><b>[lib0/function]</b> Common functions and function call helpers.</summary>
|
753 | <pre>import * as function from 'lib0/function'</pre>
|
754 | <dl>
|
755 | <b><code>function.callAll(fs: Array<function>, args: Array<any>)</code></b><br>
|
756 | <dd><p>Calls all functions in <code>fs</code> with args. Only throws after all functions were called.</p></dd>
|
757 | <b><code>function.nop</code></b><br>
|
758 | <b><code>function.apply(f: function():T): T</code></b><br>
|
759 | <b><code>function.id(a: A): A</code></b><br>
|
760 | <b><code>function.equalityStrict(a: T, b: T): boolean</code></b><br>
|
761 | <b><code>function.equalityFlat(a: Array<T>|object, b: Array<T>|object): boolean</code></b><br>
|
762 | <b><code>function.equalityDeep(a: any, b: any): boolean</code></b><br>
|
763 | <b><code>function.isOneOf(value: V, options: Array<OPTS>)</code></b><br>
|
764 | </dl>
|
765 | </details>
|
766 | <details><summary><b>[lib0/lib0]</b> Experimental method to import lib0.</summary>
|
767 | <pre>import * as lib0 from 'lib0/index'</pre>
|
768 |
|
769 | <p>Not recommended if the module bundler doesn't support dead code elimination.</p>
|
770 | <dl>
|
771 | </dl>
|
772 | </details>
|
773 | <details><summary><b>[lib0/indexeddb]</b> Helpers to work with IndexedDB.</summary>
|
774 | <pre>import * as indexeddb from 'lib0/indexeddb'</pre>
|
775 | <dl>
|
776 | <b><code>indexeddb.rtop(request: IDBRequest): Promise<any></code></b><br>
|
777 | <dd><p>IDB Request to Promise transformer</p></dd>
|
778 | <b><code>indexeddb.openDB(name: string, initDB: function(IDBDatabase):any): Promise<IDBDatabase></code></b><br>
|
779 | <b><code>indexeddb.deleteDB(name: string)</code></b><br>
|
780 | <b><code>indexeddb.createStores(db: IDBDatabase, definitions: Array<Array<string>|Array<string|IDBObjectStoreParameters|undefined>>)</code></b><br>
|
781 | <b><code>indexeddb.transact(db: IDBDatabase, stores: Array<string>, access: "readwrite"|"readonly"): Array<IDBObjectStore></code></b><br>
|
782 | <b><code>indexeddb.count(store: IDBObjectStore, range: IDBKeyRange): Promise<number></code></b><br>
|
783 | <b><code>indexeddb.get(store: IDBObjectStore, key: String | number | ArrayBuffer | Date | Array<any> ): Promise<String | number | ArrayBuffer | Date | Array<any>></code></b><br>
|
784 | <b><code>indexeddb.del(store: IDBObjectStore, key: String | number | ArrayBuffer | Date | IDBKeyRange | Array<any> )</code></b><br>
|
785 | <b><code>indexeddb.put(store: IDBObjectStore, item: String | number | ArrayBuffer | Date | boolean, key: String | number | ArrayBuffer | Date | Array<any>)</code></b><br>
|
786 | <b><code>indexeddb.add(store: IDBObjectStore, item: String|number|ArrayBuffer|Date|boolean, key: String|number|ArrayBuffer|Date|Array.<any>): Promise<any></code></b><br>
|
787 | <b><code>indexeddb.addAutoKey(store: IDBObjectStore, item: String|number|ArrayBuffer|Date): Promise<number></code></b><br>
|
788 | <b><code>indexeddb.getAll(store: IDBObjectStore, range: IDBKeyRange, limit: number): Promise<Array<any>></code></b><br>
|
789 | <b><code>indexeddb.getAllKeys(store: IDBObjectStore, range: IDBKeyRange, limit: number): Promise<Array<any>></code></b><br>
|
790 | <b><code>indexeddb.queryFirst(store: IDBObjectStore, query: IDBKeyRange|null, direction: 'next'|'prev'|'nextunique'|'prevunique'): Promise<any></code></b><br>
|
791 | <b><code>indexeddb.getLastKey(store: IDBObjectStore, range: IDBKeyRange?): Promise<any></code></b><br>
|
792 | <b><code>indexeddb.getFirstKey(store: IDBObjectStore, range: IDBKeyRange?): Promise<any></code></b><br>
|
793 | <b><code>indexeddb.getAllKeysValues(store: IDBObjectStore, range: IDBKeyRange, limit: number): Promise<Array<KeyValuePair>></code></b><br>
|
794 | <b><code>indexeddb.iterate(store: IDBObjectStore, keyrange: IDBKeyRange|null, f: function(any,any):void|boolean|Promise<void|boolean>, direction: 'next'|'prev'|'nextunique'|'prevunique')</code></b><br>
|
795 | <dd><p>Iterate on keys and values</p></dd>
|
796 | <b><code>indexeddb.iterateKeys(store: IDBObjectStore, keyrange: IDBKeyRange|null, f: function(any):void|boolean|Promise<void|boolean>, direction: 'next'|'prev'|'nextunique'|'prevunique')</code></b><br>
|
797 | <dd><p>Iterate on the keys (no values)</p></dd>
|
798 | <b><code>indexeddb.getStore(t: IDBTransaction, store: String)IDBObjectStore</code></b><br>
|
799 | <dd><p>Open store from transaction</p></dd>
|
800 | <b><code>indexeddb.createIDBKeyRangeBound(lower: any, upper: any, lowerOpen: boolean, upperOpen: boolean)</code></b><br>
|
801 | <b><code>indexeddb.createIDBKeyRangeUpperBound(upper: any, upperOpen: boolean)</code></b><br>
|
802 | <b><code>indexeddb.createIDBKeyRangeLowerBound(lower: any, lowerOpen: boolean)</code></b><br>
|
803 | </dl>
|
804 | </details>
|
805 | <details><summary><b>[lib0/isomorphic]</b> Isomorphic library exports from isomorphic.js.</summary>
|
806 | <pre>import * as isomorphic from 'lib0/isomorphic'</pre>
|
807 | <dl>
|
808 | </dl>
|
809 | </details>
|
810 | <details><summary><b>[lib0/iterator]</b> Utility module to create and manipulate Iterators.</summary>
|
811 | <pre>import * as iterator from 'lib0/iterator'</pre>
|
812 | <dl>
|
813 | <b><code>iterator.mapIterator(iterator: Iterator<T>, f: function(T):R): IterableIterator<R></code></b><br>
|
814 | <b><code>iterator.createIterator(next: function():IteratorResult<T>): IterableIterator<T></code></b><br>
|
815 | <b><code>iterator.iteratorFilter(iterator: Iterator<T>, filter: function(T):boolean)</code></b><br>
|
816 | <b><code>iterator.iteratorMap(iterator: Iterator<T>, fmap: function(T):M)</code></b><br>
|
817 | </dl>
|
818 | </details>
|
819 | <details><summary><b>[lib0/json]</b> JSON utility functions.</summary>
|
820 | <pre>import * as json from 'lib0/json'</pre>
|
821 | <dl>
|
822 | <b><code>json.stringify(object: any): string</code></b><br>
|
823 | <dd><p>Transform JavaScript object to JSON.</p></dd>
|
824 | <b><code>json.parse(json: string): any</code></b><br>
|
825 | <dd><p>Parse JSON object.</p></dd>
|
826 | </dl>
|
827 | </details>
|
828 | <details><summary><b>[lib0/list]</b> </summary>
|
829 | <pre>import * as list from 'lib0/list'</pre>
|
830 | <dl>
|
831 | <b><code>new e#ListNode()</code></b><br>
|
832 | <b><code>e#next: this|null</code></b><br>
|
833 | <b><code>e#prev: this|null</code></b><br>
|
834 | <b><code>new st()</code></b><br>
|
835 | <b><code>art: N | null</code></b><br>
|
836 | <b><code>d: N | null</code></b><br>
|
837 | <b><code>(): module:list.List<N></code></b><br>
|
838 | <b><code>()</code></b><br>
|
839 | <b><code>(queue: module:list.List<N>)</code></b><br>
|
840 | <b><code>()</code></b><br>
|
841 | <b><code>(queue: module:list.List<N>, node: N)</code></b><br>
|
842 | <dd><p>Remove a single node from the queue. Only works with Queues that operate on Doubly-linked lists of nodes.</p></dd>
|
843 | <b><code>()</code></b><br>
|
844 | <b><code>ode</code></b><br>
|
845 | <b><code>ode()</code></b><br>
|
846 | <b><code>etween(queue: module:list.List<N>, left: N| null, right: N| null, node: N)</code></b><br>
|
847 | <b><code>etween()</code></b><br>
|
848 | <b><code>(queue: module:list.List<N>, node: N, newNode: N)</code></b><br>
|
849 | <dd><p>Remove a single node from the queue. Only works with Queues that operate on Doubly-linked lists of nodes.</p></dd>
|
850 | <b><code>()</code></b><br>
|
851 | <b><code>(queue: module:list.List<N>, n: N)</code></b><br>
|
852 | <b><code>()</code></b><br>
|
853 | <b><code>nt(queue: module:list.List<N>, n: N)</code></b><br>
|
854 | <b><code>nt()</code></b><br>
|
855 | <b><code>t(list: module:list.List<N>): N| null</code></b><br>
|
856 | <b><code>t()</code></b><br>
|
857 | <b><code>(list: module:list.List<N>): N| null</code></b><br>
|
858 | <b><code>()</code></b><br>
|
859 | <b><code>(list: module:list.List<N>, f: function(N):M): Array<M></code></b><br>
|
860 | <b><code>()</code></b><br>
|
861 | <b><code>(list: module:list.List<N>)</code></b><br>
|
862 | <b><code>()</code></b><br>
|
863 | <b><code>(list: module:list.List<N>, f: function(N):M)</code></b><br>
|
864 | <b><code>()</code></b><br>
|
865 | </dl>
|
866 | </details>
|
867 | <details><summary><b>[lib0/logging]</b> Isomorphic logging module with support for colors!</summary>
|
868 | <pre>import * as logging from 'lib0/logging'</pre>
|
869 | <dl>
|
870 | <b><code>logging.BOLD</code></b><br>
|
871 | <b><code>logging.UNBOLD</code></b><br>
|
872 | <b><code>logging.BLUE</code></b><br>
|
873 | <b><code>logging.GREY</code></b><br>
|
874 | <b><code>logging.GREEN</code></b><br>
|
875 | <b><code>logging.RED</code></b><br>
|
876 | <b><code>logging.PURPLE</code></b><br>
|
877 | <b><code>logging.ORANGE</code></b><br>
|
878 | <b><code>logging.UNCOLOR</code></b><br>
|
879 | <b><code>logging.print(args: Array<string|Symbol|Object|number>)</code></b><br>
|
880 | <b><code>logging.warn(args: Array<string|Symbol|Object|number>)</code></b><br>
|
881 | <b><code>logging.printError(err: Error)</code></b><br>
|
882 | <b><code>logging.printImg(url: string, height: number)</code></b><br>
|
883 | <b><code>logging.printImgBase64(base64: string, height: number)</code></b><br>
|
884 | <b><code>logging.group(args: Array<string|Symbol|Object|number>)</code></b><br>
|
885 | <b><code>logging.groupCollapsed(args: Array<string|Symbol|Object|number>)</code></b><br>
|
886 | <b><code>logging.groupEnd</code></b><br>
|
887 | <b><code>logging.printDom(createNode: function():Node)</code></b><br>
|
888 | <b><code>logging.printCanvas(canvas: HTMLCanvasElement, height: number)</code></b><br>
|
889 | <b><code>logging.vconsoles</code></b><br>
|
890 | <b><code>new logging.VConsole(dom: Element)</code></b><br>
|
891 | <b><code>logging.VConsole#ccontainer: Element</code></b><br>
|
892 | <b><code>logging.VConsole#group(args: Array<string|Symbol|Object|number>, collapsed: boolean)</code></b><br>
|
893 | <b><code>logging.VConsole#groupCollapsed(args: Array<string|Symbol|Object|number>)</code></b><br>
|
894 | <b><code>logging.VConsole#groupEnd()</code></b><br>
|
895 | <b><code>logging.VConsole#print(args: Array<string|Symbol|Object|number>)</code></b><br>
|
896 | <b><code>logging.VConsole#printError(err: Error)</code></b><br>
|
897 | <b><code>logging.VConsole#printImg(url: string, height: number)</code></b><br>
|
898 | <b><code>logging.VConsole#printDom(node: Node)</code></b><br>
|
899 | <b><code>logging.VConsole#destroy()</code></b><br>
|
900 | <b><code>logging.createVConsole(dom: Element)</code></b><br>
|
901 | <b><code>logging.createModuleLogger(moduleName: string): function(...any):void</code></b><br>
|
902 | </dl>
|
903 | </details>
|
904 | <details><summary><b>[lib0/map]</b> Utility module to work with key-value stores.</summary>
|
905 | <pre>import * as map from 'lib0/map'</pre>
|
906 | <dl>
|
907 | <b><code>map.create(): Map<any, any></code></b><br>
|
908 | <dd><p>Creates a new Map instance.</p></dd>
|
909 | <b><code>map.copy(m: Map<X,Y>): Map<X,Y></code></b><br>
|
910 | <dd><p>Copy a Map object into a fresh Map object.</p></dd>
|
911 | <b><code>map.setIfUndefined(map: Map<K, T>, key: K, createT: function():T): T</code></b><br>
|
912 | <dd><p>Get map property. Create T if property is undefined and set T on map.</p>
|
913 | <pre class="prettyprint source lang-js"><code>const listeners = map.setIfUndefined(events, 'eventName', set.create)
|
914 | listeners.add(listener)
|
915 | </code></pre></dd>
|
916 | <b><code>map.map(m: Map<K,V>, f: function(V,K):R): Array<R></code></b><br>
|
917 | <dd><p>Creates an Array and populates it with the content of all key-value pairs using the <code>f(value, key)</code> function.</p></dd>
|
918 | <b><code>map.any(m: Map<K,V>, f: function(V,K):boolean): boolean</code></b><br>
|
919 | <dd><p>Tests whether any key-value pairs pass the test implemented by <code>f(value, key)</code>.</p></dd>
|
920 | <b><code>map.all(m: Map<K,V>, f: function(V,K):boolean): boolean</code></b><br>
|
921 | <dd><p>Tests whether all key-value pairs pass the test implemented by <code>f(value, key)</code>.</p></dd>
|
922 | </dl>
|
923 | </details>
|
924 | <details><summary><b>[lib0/math]</b> Common Math expressions.</summary>
|
925 | <pre>import * as math from 'lib0/math'</pre>
|
926 | <dl>
|
927 | <b><code>math.floor</code></b><br>
|
928 | <b><code>math.ceil</code></b><br>
|
929 | <b><code>math.abs</code></b><br>
|
930 | <b><code>math.imul</code></b><br>
|
931 | <b><code>math.round</code></b><br>
|
932 | <b><code>math.log10</code></b><br>
|
933 | <b><code>math.log2</code></b><br>
|
934 | <b><code>math.log</code></b><br>
|
935 | <b><code>math.sqrt</code></b><br>
|
936 | <b><code>math.add(a: number, b: number): number</code></b><br>
|
937 | <b><code>math.min(a: number, b: number): number</code></b><br>
|
938 | <b><code>math.max(a: number, b: number): number</code></b><br>
|
939 | <b><code>math.isNaN</code></b><br>
|
940 | <b><code>math.pow</code></b><br>
|
941 | <b><code>math.exp10(exp: number): number</code></b><br>
|
942 | <dd><p>Base 10 exponential function. Returns the value of 10 raised to the power of pow.</p></dd>
|
943 | <b><code>math.sign</code></b><br>
|
944 | <b><code>math.isNegativeZero(n: number): boolean</code></b><br>
|
945 | </dl>
|
946 | </details>
|
947 | <details><summary><b>[lib0/metric]</b> Utility module to convert metric values.</summary>
|
948 | <pre>import * as metric from 'lib0/metric'</pre>
|
949 | <dl>
|
950 | <b><code>metric.yotta</code></b><br>
|
951 | <b><code>metric.zetta</code></b><br>
|
952 | <b><code>metric.exa</code></b><br>
|
953 | <b><code>metric.peta</code></b><br>
|
954 | <b><code>metric.tera</code></b><br>
|
955 | <b><code>metric.giga</code></b><br>
|
956 | <b><code>metric.mega</code></b><br>
|
957 | <b><code>metric.kilo</code></b><br>
|
958 | <b><code>metric.hecto</code></b><br>
|
959 | <b><code>metric.deca</code></b><br>
|
960 | <b><code>metric.deci</code></b><br>
|
961 | <b><code>metric.centi</code></b><br>
|
962 | <b><code>metric.milli</code></b><br>
|
963 | <b><code>metric.micro</code></b><br>
|
964 | <b><code>metric.nano</code></b><br>
|
965 | <b><code>metric.pico</code></b><br>
|
966 | <b><code>metric.femto</code></b><br>
|
967 | <b><code>metric.atto</code></b><br>
|
968 | <b><code>metric.zepto</code></b><br>
|
969 | <b><code>metric.yocto</code></b><br>
|
970 | <b><code>metric.prefix(n: number, baseMultiplier: number): {n:number,prefix:string}</code></b><br>
|
971 | <dd><p>Calculate the metric prefix for a number. Assumes E.g. <code>prefix(1000) = { n: 1, prefix: 'k' }</code></p></dd>
|
972 | </dl>
|
973 | </details>
|
974 | <details><summary><b>[lib0/mutex]</b> Mutual exclude for JavaScript.</summary>
|
975 | <pre>import * as mutex from 'lib0/mutex'</pre>
|
976 | <dl>
|
977 | <b><code>mutex.createMutex(): mutex</code></b><br>
|
978 | <dd><p>Creates a mutual exclude function with the following property:</p>
|
979 | <pre class="prettyprint source lang-js"><code>const mutex = createMutex()
|
980 | mutex(() => {
|
981 | // This function is immediately executed
|
982 | mutex(() => {
|
983 | // This function is not executed, as the mutex is already active.
|
984 | })
|
985 | })
|
986 | </code></pre></dd>
|
987 | </dl>
|
988 | </details>
|
989 | <details><summary><b>[lib0/number]</b> </summary>
|
990 | <pre>import * as number from 'lib0/number'</pre>
|
991 | <dl>
|
992 | <b><code>number.MAX_SAFE_INTEGER</code></b><br>
|
993 | <b><code>number.MIN_SAFE_INTEGER</code></b><br>
|
994 | <b><code>number.LOWEST_INT32</code></b><br>
|
995 | <b><code>number.HIGHEST_INT32: number</code></b><br>
|
996 | <b><code>number.isInteger</code></b><br>
|
997 | <b><code>number.isNaN</code></b><br>
|
998 | <b><code>number.parseInt</code></b><br>
|
999 | </dl>
|
1000 | </details>
|
1001 | <details><summary><b>[lib0/object]</b> Utility functions for working with EcmaScript objects.</summary>
|
1002 | <pre>import * as object from 'lib0/object'</pre>
|
1003 | <dl>
|
1004 | <b><code>object.create(): Object<string,any></code></b><br>
|
1005 | <b><code>object.assign</code></b><br>
|
1006 | <dd><p>Object.assign</p></dd>
|
1007 | <b><code>object.keys(obj: Object<string,any>)</code></b><br>
|
1008 | <b><code>object.forEach(obj: Object<string,any>, f: function(any,string):any)</code></b><br>
|
1009 | <b><code>object.map(obj: Object<string,any>, f: function(any,string):R): Array<R></code></b><br>
|
1010 | <b><code>object.length(obj: Object<string,any>): number</code></b><br>
|
1011 | <b><code>object.some(obj: Object<string,any>, f: function(any,string):boolean): boolean</code></b><br>
|
1012 | <b><code>object.isEmpty(obj: Object|undefined)</code></b><br>
|
1013 | <b><code>object.every(obj: Object<string,any>, f: function(any,string):boolean): boolean</code></b><br>
|
1014 | <b><code>object.hasProperty(obj: any, key: string|symbol): boolean</code></b><br>
|
1015 | <dd><p>Calls <code>Object.prototype.hasOwnProperty</code>.</p></dd>
|
1016 | <b><code>object.equalFlat(a: Object<string,any>, b: Object<string,any>): boolean</code></b><br>
|
1017 | </dl>
|
1018 | </details>
|
1019 | <details><summary><b>[lib0/observable]</b> Observable class prototype.</summary>
|
1020 | <pre>import * as observable from 'lib0/observable'</pre>
|
1021 | <dl>
|
1022 | <b><code>new observable.Observable()</code></b><br>
|
1023 | <dd><p>Handles named events.</p></dd>
|
1024 | <b><code>observable.Observable#on(name: N, f: function)</code></b><br>
|
1025 | <b><code>observable.Observable#once(name: N, f: function)</code></b><br>
|
1026 | <b><code>observable.Observable#off(name: N, f: function)</code></b><br>
|
1027 | <b><code>observable.Observable#emit(name: N, args: Array<any>)</code></b><br>
|
1028 | <dd><p>Emit a named event. All registered event listeners that listen to the
|
1029 | specified name will receive the event.</p></dd>
|
1030 | <b><code>observable.Observable#destroy()</code></b><br>
|
1031 | <b><code>websocket.WebsocketClient#on(name: N, f: function)</code></b><br>
|
1032 | <b><code>websocket.WebsocketClient#once(name: N, f: function)</code></b><br>
|
1033 | <b><code>websocket.WebsocketClient#off(name: N, f: function)</code></b><br>
|
1034 | <b><code>websocket.WebsocketClient#emit(name: N, args: Array<any>)</code></b><br>
|
1035 | <dd><p>Emit a named event. All registered event listeners that listen to the
|
1036 | specified name will receive the event.</p></dd>
|
1037 | </dl>
|
1038 | </details>
|
1039 | <details><summary><b>[lib0/pair]</b> Working with value pairs.</summary>
|
1040 | <pre>import * as pair from 'lib0/pair'</pre>
|
1041 | <dl>
|
1042 | <b><code>new pair.Pair(left: L, right: R)</code></b><br>
|
1043 | <b><code>pair.create(left: L, right: R): module:pair.Pair<L,R></code></b><br>
|
1044 | <b><code>pair.createReversed(right: R, left: L): module:pair.Pair<L,R></code></b><br>
|
1045 | <b><code>pair.forEach(arr: Array<module:pair.Pair<L,R>>, f: function(L, R):any)</code></b><br>
|
1046 | <b><code>pair.map(arr: Array<module:pair.Pair<L,R>>, f: function(L, R):X): Array<X></code></b><br>
|
1047 | </dl>
|
1048 | </details>
|
1049 | <details><summary><b>[lib0/prng]</b> Fast Pseudo Random Number Generators.</summary>
|
1050 | <pre>import * as prng from 'lib0/prng'</pre>
|
1051 |
|
1052 | <p>Given a seed a PRNG generates a sequence of numbers that cannot be reasonably predicted.
|
1053 | Two PRNGs must generate the same random sequence of numbers if given the same seed.</p>
|
1054 | <dl>
|
1055 | <b><code>prng.DefaultPRNG</code></b><br>
|
1056 | <b><code>prng.create(seed: number): module:prng~PRNG</code></b><br>
|
1057 | <dd><p>Create a Xoroshiro128plus Pseudo-Random-Number-Generator.
|
1058 | This is the fastest full-period generator passing BigCrush without systematic failures.
|
1059 | But there are more PRNGs available in ./PRNG/.</p></dd>
|
1060 | <b><code>prng.bool(gen: module:prng~PRNG): Boolean</code></b><br>
|
1061 | <dd><p>Generates a single random bool.</p></dd>
|
1062 | <b><code>prng.int53(gen: module:prng~PRNG, min: Number, max: Number): Number</code></b><br>
|
1063 | <dd><p>Generates a random integer with 53 bit resolution.</p></dd>
|
1064 | <b><code>prng.uint53(gen: module:prng~PRNG, min: Number, max: Number): Number</code></b><br>
|
1065 | <dd><p>Generates a random integer with 53 bit resolution.</p></dd>
|
1066 | <b><code>prng.int32(gen: module:prng~PRNG, min: Number, max: Number): Number</code></b |