git ssb

0+

wanderer🌟 / js-primea-wasm-container



Tree: b224e90ce1926c40c37deee54fcea21661d95f26

Files: b224e90ce1926c40c37deee54fcea21661d95f26 / referanceMap.js

961 bytesRaw
1module.exports = class ReferanceMap {
2 /**
3 * Handle mapping arbitary JS object to ints
4 */
5 constructor () {
6 this._map = new Map()
7 this._nonce = 0
8 }
9
10 /**
11 * Adds an object to the referance map returning an int to be used as a
12 * referance
13 * @param {*} obj
14 * @return {integer}
15 */
16 add (obj) {
17 const nonce = this._nonce
18 this._map.set(this._nonce, obj)
19 this._nonce++
20 return nonce
21 }
22
23 /**
24 * gets a POJO given a refernce as an int
25 * @param {integer} ref
26 * @return {*}
27 */
28 get (ref) {
29 const obj = this._map.get(ref)
30 if (!obj) {
31 throw new Error('invalid referance')
32 }
33 return obj
34 }
35
36 /**
37 * deletes an object given a referance as an int
38 * @param {integer}
39 * @return {boolean} whether or not the object was deleted
40 */
41 delete (ref) {
42 return this._map.delete(ref)
43 }
44
45 /**
46 * clears the referance map of a objects
47 */
48 clear () {
49 this._map.clear()
50 }
51}
52

Built with git-ssb-web