git ssb

0+

wanderer🌟 / js-primea-message



Commit 0117dfc955ea513c34b7d0904076ec5b8f80fd77

dynamic assign props

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 11/27/2017, 6:00:16 PM
Parent: 0741dd5a555109ad81a99902b63c8dab698445b5

Files changed

index.jschanged
tests/index.jschanged
index.jsView
@@ -13,50 +13,27 @@
1313 constructor (opts = {}) {
1414 super()
1515 const defaults = this.constructor.defaults
1616 this._opts = Object.assign(defaults, opts)
17 + Object.keys(this._opts).forEach(key => {
18 + Object.defineProperty(this, key, {
19 + get: function () {
20 + return this._opts[key]
21 + },
22 + set: function (y) {
23 + this._opts[key] = y
24 + }
25 + })
26 + })
1727
1828 // set by the kernel
19- this._hops = 0
2029 this._fromTicks = 0
2130 }
2231
2332 toJSON () {
2433 return this._opts
2534 }
2635
27- /**
28- * Returns the messages payload
29- * @returns {ArrayBuffer}
30- */
31- get data () {
32- return this._opts.data
33- }
34-
35- /**
36- * Returns an array of capabilities that a message is carrying
37- * @returns {[]}
38- */
39- get caps () {
40- return this._opts.caps
41- }
42-
43- /**
44- * Returns the messages payload
45- * @returns {ArrayBuffer}
46- */
47- get ticks () {
48- return this._opts.ticks
49- }
50-
51- /**
52- * returns the number of hops a packet has undergone
53- * @returns {integer}
54- */
55- get hops () {
56- return this._hops
57- }
58-
5936 static get defaults () {
6037 return {
6138 ticks: 0,
6239 data: new ArrayBuffer([]),
tests/index.jsView
@@ -15,11 +15,12 @@
1515
1616 t.equals(message.caps[0], capA, 'to getter should work')
1717 t.equals(message.data.toString(), 'test', 'to getter should work')
1818 t.equals(message.ticks, 77, 'resources getter should work')
19- t.equals(message.hops, 0, 'hops should return correctly')
2019 t.equals(JSON.stringify(message), json)
2120
2221 message = new Message()
2322 t.equals(message.ticks, 0, 'resources getter should work')
23 + message.ticks = 99
24 + t.equals(message.ticks, 99, 'setter should work')
2425 t.end()
2526 })

Built with git-ssb-web