From fa07601a2a5f040434295677501c7f30309d3d0f Mon Sep 17 00:00:00 2001 From: Stefan Thomas Date: Sat, 25 Jan 2014 11:30:58 -0800 Subject: [PATCH] UInt: Add UInt#_update for creating subclasses that have dependent state. --- src/js/ripple/uint.js | 30 ++++++++++++++++++++++++++++++ src/js/ripple/uint160.js | 6 ++++++ 2 files changed, 36 insertions(+) diff --git a/src/js/ripple/uint.js b/src/js/ripple/uint.js index 545d517b..4f2338be 100644 --- a/src/js/ripple/uint.js +++ b/src/js/ripple/uint.js @@ -15,6 +15,8 @@ var Base = require('./base').Base; var UInt = function () { // Internal form: NaN or BigInteger this._value = NaN; + + this._update(); }; UInt.json_rewrite = function (j, opts) { @@ -96,6 +98,8 @@ UInt.prototype.clone = function () { UInt.prototype.copyTo = function (d) { d._value = this._value; + if ("function" === typeof d._update) d._update(); + return d; }; @@ -111,6 +115,20 @@ UInt.prototype.is_zero = function () { return this._value.equals(BigInteger.ZERO); }; +/** + * Update any derivative values. + * + * This allows subclasses to maintain caches of any data that they derive from + * the main _value. For example, the Currency class keeps the currency type, the + * currency code and other information about the currency cached. + * + * The reason for keeping this mechanism in this class is so every subclass can + * call it whenever it modifies the internal state. + */ +UInt.prototype._update = function () { + // Nothing to do by default. Subclasses will override this. +}; + // value = NaN on error. UInt.prototype.parse_generic = function (j) { // Canonicalize and validate @@ -150,6 +168,8 @@ UInt.prototype.parse_generic = function (j) { } } + this._update(); + return this; }; @@ -161,6 +181,8 @@ UInt.prototype.parse_hex = function (j) { this._value = NaN; } + this._update(); + return this; }; @@ -172,6 +194,8 @@ UInt.prototype.parse_bits = function (j) { this.parse_bytes(bytes); } + this._update(); + return this; }; @@ -183,6 +207,8 @@ UInt.prototype.parse_bytes = function (j) { this._value = new BigInteger([0].concat(j), 256); } + this._update(); + return this; }; @@ -198,6 +224,8 @@ UInt.prototype.parse_bn = function (j) { this._value = NaN; } + this._update(); + return this; }; @@ -211,6 +239,8 @@ UInt.prototype.parse_number = function (j) { this._value = new BigInteger(""+j); } + this._update(); + return this; }; diff --git a/src/js/ripple/uint160.js b/src/js/ripple/uint160.js index 8b405959..55fc4d0d 100644 --- a/src/js/ripple/uint160.js +++ b/src/js/ripple/uint160.js @@ -16,6 +16,8 @@ var UInt160 = extend(function () { // Internal form: NaN or BigInteger this._value = NaN; this._version_byte = void(0); + + this._update(); }, UInt); UInt160.width = 20; @@ -61,6 +63,8 @@ UInt160.prototype.parse_json = function (j) { this.parse_hex(j); } + this._update(); + return this; }; @@ -73,6 +77,8 @@ UInt160.prototype.parse_generic = function (j) { } } + this._update(); + return this; };