Skip to content

Commit 3c07549

Browse files
tmulkernjpwsutton
authored andcommitted
Refactored Properties using Object.defineProperties (#120)
* Refactored Properties using Object.defineProperties Refactored Properties using Object.defineProperties as seen here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties This reduces the javascript code by removing repeated code. This will have a impact on overall file size. Signed-off-by: Tadhg Mulkern<[email protected]> * Added additional cleanup of code Added a piece of code to removed the need for all the Typed Array checks, base on this method https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/isView and from the Stackoverflow answer https://stackoverflow.com/questions/15251879/how-to-check-if-a-variable-is-a-typed-array-in-javascript/29651223#29651223
1 parent d9ab728 commit 3c07549

File tree

1 file changed

+147
-172
lines changed

1 file changed

+147
-172
lines changed

src/paho-mqtt.js

+147-172
Original file line numberDiff line numberDiff line change
@@ -1776,71 +1776,88 @@ var PahoMQTT = (function (global) {
17761776
throw new Error(format(ERROR.INVALID_ARGUMENT, [clientId, "clientId"]));
17771777

17781778
var client = new ClientImpl(uri, host, port, path, clientId);
1779-
this._getHost = function() { return host; };
1780-
this._setHost = function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); };
17811779

1782-
this._getPort = function() { return port; };
1783-
this._setPort = function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); };
1784-
1785-
this._getPath = function() { return path; };
1786-
this._setPath = function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); };
1787-
1788-
this._getURI = function() { return uri; };
1789-
this._setURI = function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); };
1790-
1791-
this._getClientId = function() { return client.clientId; };
1792-
this._setClientId = function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); };
1793-
1794-
this._getOnConnected = function() { return client.onConnected; };
1795-
this._setOnConnected = function(newOnConnected) {
1796-
if (typeof newOnConnected === "function")
1797-
client.onConnected = newOnConnected;
1798-
else
1799-
throw new Error(format(ERROR.INVALID_TYPE, [typeof newOnConnected, "onConnected"]));
1800-
};
1801-
1802-
this._getDisconnectedPublishing = function() { return client.disconnectedPublishing; };
1803-
this._setDisconnectedPublishing = function(newDisconnectedPublishing) {
1804-
client.disconnectedPublishing = newDisconnectedPublishing;
1805-
};
1806-
1807-
this._getDisconnectedBufferSize = function() { return client.disconnectedBufferSize; };
1808-
this._setDisconnectedBufferSize = function(newDisconnectedBufferSize) {
1809-
client.disconnectedBufferSize = newDisconnectedBufferSize;
1810-
};
1811-
1812-
this._getOnConnectionLost = function() { return client.onConnectionLost; };
1813-
this._setOnConnectionLost = function(newOnConnectionLost) {
1814-
if (typeof newOnConnectionLost === "function")
1815-
client.onConnectionLost = newOnConnectionLost;
1816-
else
1817-
throw new Error(format(ERROR.INVALID_TYPE, [typeof newOnConnectionLost, "onConnectionLost"]));
1818-
};
1819-
1820-
this._getOnMessageDelivered = function() { return client.onMessageDelivered; };
1821-
this._setOnMessageDelivered = function(newOnMessageDelivered) {
1822-
if (typeof newOnMessageDelivered === "function")
1823-
client.onMessageDelivered = newOnMessageDelivered;
1824-
else
1825-
throw new Error(format(ERROR.INVALID_TYPE, [typeof newOnMessageDelivered, "onMessageDelivered"]));
1826-
};
1827-
1828-
this._getOnMessageArrived = function() { return client.onMessageArrived; };
1829-
this._setOnMessageArrived = function(newOnMessageArrived) {
1830-
if (typeof newOnMessageArrived === "function")
1831-
client.onMessageArrived = newOnMessageArrived;
1832-
else
1833-
throw new Error(format(ERROR.INVALID_TYPE, [typeof newOnMessageArrived, "onMessageArrived"]));
1834-
};
1835-
1836-
this._getTrace = function() { return client.traceFunction; };
1837-
this._setTrace = function(trace) {
1838-
if(typeof trace === "function"){
1839-
client.traceFunction = trace;
1840-
}else{
1841-
throw new Error(format(ERROR.INVALID_TYPE, [typeof trace, "onTrace"]));
1842-
}
1843-
};
1780+
//Public Properties
1781+
Object.defineProperties(this,{
1782+
"host":{
1783+
get: function() { return host; },
1784+
set: function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); }
1785+
},
1786+
"port":{
1787+
get: function() { return port; },
1788+
set: function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); }
1789+
},
1790+
"path":{
1791+
get: function() { return path; },
1792+
set: function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); }
1793+
},
1794+
"uri":{
1795+
get: function() { return uri; },
1796+
set: function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); }
1797+
},
1798+
"clientId":{
1799+
get: function() { return client.clientId; },
1800+
set: function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); }
1801+
},
1802+
"onConnected":{
1803+
get: function() { return client.onConnected; },
1804+
set: function(newOnConnected) {
1805+
if (typeof newOnConnected === "function")
1806+
client.onConnected = newOnConnected;
1807+
else
1808+
throw new Error(format(ERROR.INVALID_TYPE, [typeof newOnConnected, "onConnected"]));
1809+
}
1810+
},
1811+
"disconnectedPublishing":{
1812+
get: function() { return client.disconnectedPublishing; },
1813+
set: function(newDisconnectedPublishing) {
1814+
client.disconnectedPublishing = newDisconnectedPublishing;
1815+
}
1816+
},
1817+
"disconnectedBufferSize":{
1818+
get: function() { return client.disconnectedBufferSize; },
1819+
set: function(newDisconnectedBufferSize) {
1820+
client.disconnectedBufferSize = newDisconnectedBufferSize;
1821+
}
1822+
},
1823+
"onConnectionLost":{
1824+
get: function() { return client.onConnectionLost; },
1825+
set: function(newOnConnectionLost) {
1826+
if (typeof newOnConnectionLost === "function")
1827+
client.onConnectionLost = newOnConnectionLost;
1828+
else
1829+
throw new Error(format(ERROR.INVALID_TYPE, [typeof newOnConnectionLost, "onConnectionLost"]));
1830+
}
1831+
},
1832+
"onMessageDelivered":{
1833+
get: function() { return client.onMessageDelivered; },
1834+
set: function(newOnMessageDelivered) {
1835+
if (typeof newOnMessageDelivered === "function")
1836+
client.onMessageDelivered = newOnMessageDelivered;
1837+
else
1838+
throw new Error(format(ERROR.INVALID_TYPE, [typeof newOnMessageDelivered, "onMessageDelivered"]));
1839+
}
1840+
},
1841+
"onMessageArrived":{
1842+
get: function() { return client.onMessageArrived; },
1843+
set: function(newOnMessageArrived) {
1844+
if (typeof newOnMessageArrived === "function")
1845+
client.onMessageArrived = newOnMessageArrived;
1846+
else
1847+
throw new Error(format(ERROR.INVALID_TYPE, [typeof newOnMessageArrived, "onMessageArrived"]));
1848+
}
1849+
},
1850+
"trace":{
1851+
get: function() { return client.traceFunction; },
1852+
set: function(trace) {
1853+
if(typeof trace === "function"){
1854+
client.traceFunction = trace;
1855+
}else{
1856+
throw new Error(format(ERROR.INVALID_TYPE, [typeof trace, "onTrace"]));
1857+
}
1858+
}
1859+
},
1860+
});
18441861

18451862
/**
18461863
* Connect this Messaging client to its server.
@@ -2244,42 +2261,6 @@ var PahoMQTT = (function (global) {
22442261
};
22452262
};
22462263

2247-
Client.prototype = {
2248-
get host() { return this._getHost(); },
2249-
set host(newHost) { this._setHost(newHost); },
2250-
2251-
get port() { return this._getPort(); },
2252-
set port(newPort) { this._setPort(newPort); },
2253-
2254-
get path() { return this._getPath(); },
2255-
set path(newPath) { this._setPath(newPath); },
2256-
2257-
get clientId() { return this._getClientId(); },
2258-
set clientId(newClientId) { this._setClientId(newClientId); },
2259-
2260-
get onConnected() { return this._getOnConnected(); },
2261-
set onConnected(newOnConnected) { this._setOnConnected(newOnConnected); },
2262-
2263-
get disconnectedPublishing() { return this._getDisconnectedPublishing(); },
2264-
set disconnectedPublishing(newDisconnectedPublishing) { this._setDisconnectedPublishing(newDisconnectedPublishing); },
2265-
2266-
get disconnectedBufferSize() { return this._getDisconnectedBufferSize(); },
2267-
set disconnectedBufferSize(newDisconnectedBufferSize) { this._setDisconnectedBufferSize(newDisconnectedBufferSize); },
2268-
2269-
get onConnectionLost() { return this._getOnConnectionLost(); },
2270-
set onConnectionLost(newOnConnectionLost) { this._setOnConnectionLost(newOnConnectionLost); },
2271-
2272-
get onMessageDelivered() { return this._getOnMessageDelivered(); },
2273-
set onMessageDelivered(newOnMessageDelivered) { this._setOnMessageDelivered(newOnMessageDelivered); },
2274-
2275-
get onMessageArrived() { return this._getOnMessageArrived(); },
2276-
set onMessageArrived(newOnMessageArrived) { this._setOnMessageArrived(newOnMessageArrived); },
2277-
2278-
get trace() { return this._getTrace(); },
2279-
set trace(newTraceFunction) { this._setTrace(newTraceFunction); }
2280-
2281-
};
2282-
22832264
/**
22842265
* An application message, sent or received.
22852266
* <p>
@@ -2318,89 +2299,83 @@ var PahoMQTT = (function (global) {
23182299
var payload;
23192300
if ( typeof newPayload === "string" ||
23202301
newPayload instanceof ArrayBuffer ||
2321-
newPayload instanceof Int8Array ||
2322-
newPayload instanceof Uint8Array ||
2323-
newPayload instanceof Int16Array ||
2324-
newPayload instanceof Uint16Array ||
2325-
newPayload instanceof Int32Array ||
2326-
newPayload instanceof Uint32Array ||
2327-
newPayload instanceof Float32Array ||
2328-
newPayload instanceof Float64Array
2302+
(ArrayBuffer.isView(newPayload) && !(newPayload instanceof DataView))
23292303
) {
23302304
payload = newPayload;
23312305
} else {
23322306
throw (format(ERROR.INVALID_ARGUMENT, [newPayload, "newPayload"]));
23332307
}
23342308

2335-
this._getPayloadString = function () {
2336-
if (typeof payload === "string")
2337-
return payload;
2338-
else
2339-
return parseUTF8(payload, 0, payload.length);
2340-
};
2341-
2342-
this._getPayloadBytes = function() {
2343-
if (typeof payload === "string") {
2344-
var buffer = new ArrayBuffer(UTF8Length(payload));
2345-
var byteStream = new Uint8Array(buffer);
2346-
stringToUTF8(payload, byteStream, 0);
2347-
2348-
return byteStream;
2349-
} else {
2350-
return payload;
2351-
}
2352-
};
2353-
23542309
var destinationName;
2355-
this._getDestinationName = function() { return destinationName; };
2356-
this._setDestinationName = function(newDestinationName) {
2357-
if (typeof newDestinationName === "string")
2358-
destinationName = newDestinationName;
2359-
else
2360-
throw new Error(format(ERROR.INVALID_ARGUMENT, [newDestinationName, "newDestinationName"]));
2361-
};
2362-
23632310
var qos = 0;
2364-
this._getQos = function() { return qos; };
2365-
this._setQos = function(newQos) {
2366-
if (newQos === 0 || newQos === 1 || newQos === 2 )
2367-
qos = newQos;
2368-
else
2369-
throw new Error("Invalid argument:"+newQos);
2370-
};
2371-
23722311
var retained = false;
2373-
this._getRetained = function() { return retained; };
2374-
this._setRetained = function(newRetained) {
2375-
if (typeof newRetained === "boolean")
2376-
retained = newRetained;
2377-
else
2378-
throw new Error(format(ERROR.INVALID_ARGUMENT, [newRetained, "newRetained"]));
2379-
};
2380-
23812312
var duplicate = false;
2382-
this._getDuplicate = function() { return duplicate; };
2383-
this._setDuplicate = function(newDuplicate) { duplicate = newDuplicate; };
2384-
};
2385-
2386-
Message.prototype = {
2387-
get payloadString() { return this._getPayloadString(); },
2388-
get payloadBytes() { return this._getPayloadBytes(); },
2389-
2390-
get destinationName() { return this._getDestinationName(); },
2391-
set destinationName(newDestinationName) { this._setDestinationName(newDestinationName); },
2392-
2393-
get topic() { return this._getDestinationName(); },
2394-
set topic(newTopic) { this._setDestinationName(newTopic); },
23952313

2396-
get qos() { return this._getQos(); },
2397-
set qos(newQos) { this._setQos(newQos); },
2398-
2399-
get retained() { return this._getRetained(); },
2400-
set retained(newRetained) { this._setRetained(newRetained); },
2401-
2402-
get duplicate() { return this._getDuplicate(); },
2403-
set duplicate(newDuplicate) { this._setDuplicate(newDuplicate); }
2314+
Object.defineProperties(this,{
2315+
"payloadString":{
2316+
enumerable : true,
2317+
get : function () {
2318+
if (typeof payload === "string")
2319+
return payload;
2320+
else
2321+
return parseUTF8(payload, 0, payload.length);
2322+
}
2323+
},
2324+
"payloadBytes":{
2325+
enumerable: true,
2326+
get: function() {
2327+
if (typeof payload === "string") {
2328+
var buffer = new ArrayBuffer(UTF8Length(payload));
2329+
var byteStream = new Uint8Array(buffer);
2330+
stringToUTF8(payload, byteStream, 0);
2331+
2332+
return byteStream;
2333+
} else {
2334+
return payload;
2335+
}
2336+
}
2337+
},
2338+
"destinationName":{
2339+
enumerable: true,
2340+
get: function() { return destinationName; },
2341+
set: function(newDestinationName) {
2342+
if (typeof newDestinationName === "string")
2343+
destinationName = newDestinationName;
2344+
else
2345+
throw new Error(format(ERROR.INVALID_ARGUMENT, [newDestinationName, "newDestinationName"]));
2346+
}
2347+
},
2348+
"qos":{
2349+
enumerable: true,
2350+
get: function() { return qos; },
2351+
set: function(newQos) {
2352+
if (newQos === 0 || newQos === 1 || newQos === 2 )
2353+
qos = newQos;
2354+
else
2355+
throw new Error("Invalid argument:"+newQos);
2356+
}
2357+
},
2358+
"retained":{
2359+
enumerable: true,
2360+
get: function() { return retained; },
2361+
set: function(newRetained) {
2362+
if (typeof newRetained === "boolean")
2363+
retained = newRetained;
2364+
else
2365+
throw new Error(format(ERROR.INVALID_ARGUMENT, [newRetained, "newRetained"]));
2366+
}
2367+
},
2368+
"topic":{
2369+
enumerable: true,
2370+
get: function() { return destinationName; },
2371+
set: function(newTopic) {destinationName=newTopic;}
2372+
},
2373+
"duplicate":{
2374+
enumerable: true,
2375+
get: function() { return duplicate; },
2376+
set: function(newDuplicate) {duplicate=newDuplicate;}
2377+
}
2378+
});
24042379
};
24052380

24062381
// Module contents.

0 commit comments

Comments
 (0)