From fec92b337db17dc69ff0501dc76695ccb4cc7985 Mon Sep 17 00:00:00 2001 From: Kirill Date: Mon, 15 Oct 2018 16:32:17 +0300 Subject: [PATCH 1/2] made DApp from iz3 --- DApp.js | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 DApp.js diff --git a/DApp.js b/DApp.js new file mode 100644 index 0000000..426a6a3 --- /dev/null +++ b/DApp.js @@ -0,0 +1,113 @@ + +//unify browser and node +if (typeof _this ==='undefined') { + var _this = this; +} + +class DApp { + + constructor(candy) { + let that = this; + + this.candy = candy; + + /*** + * @var {starwaveProtocol} this.starwave + */ + this._starwave = candy.starwave; + + /** + * Network functions + * @type {{getCurrentPeers: ((function(*=): (*|Array))|getCurrentPeers), getSocketByBusAddress: getSocketByBusAddress, socketSend: *, rpc: {registerGetHandler: DApp.network.rpc.registerGetHandler, registerPostHandler: DApp.network.rpc.registerPostHandler}}} + */ + this.network = { + getCurrentPeers: this._starwave.getCurrentPeers, + getSocketByBusAddress: this._starwave.getSocketByBusAddress, + socketSend: this._starwave.write, + }; + + /** + * Messaging functions + * @type {{registerMessageHandler: (function(): boolean), broadcastMessage: (function(): void), sendMessage: (function(): void), starwave: {registerMessageHandler: (function(): any), sendMessage: (function(): any), createMessage: (function(): any)}}} + */ + this.messaging = { + registerMessageHandler: function () { + return that.candy.registerMessageHandler.apply(that.candy, arguments) + }, + broadcastMessage: function () { + return that.candy.broadcastMessage.apply(that.candy, arguments) + }, + sendMessage: function () { + return that.network.socketSend.apply(that, arguments) + }, + starwave: { + registerMessageHandler: function () { + return that._starwave.registerMessageHandler.apply(that._starwave, arguments) + }, + sendMessage: function () { + return that._starwave.sendMessage.apply(that._starwave, arguments) + }, + createMessage: function () { + return that._starwave.createMessage.apply(that._starwave, arguments) + }, + } + }; + + /** + * System functions + * @type {{getConfig: *}} + */ + this.system = { + getConfig: this.getConfig() + }; + } + + + /** + * Register message handler + * @param {string} message + * @param {function} handler + * @return {boolean} + */ + registerMessageHandler(message, handler) { + return this.candy.registerMessageHandler(message, handler); + } + + + /** + * Returns config object + * @return {*} + */ + getConfig() { + return this.candy; + } + + /** + * Returns array of peers addresses or sockets + * @param fullSockets + * @return {*|Array} + */ + getCurrentPeers(fullSockets) { + return this._starwave.getCurrentPeers(fullSockets); + } + + /** + * Initiate Application start + */ + init() { + this.candy.start(); + + } + + /** + * Terminating app + */ + terminate(cb) { + cb(); + } +} + +//unify browser and node +if (this.window === undefined) { + module.exports = DApp; +} \ No newline at end of file From 48ecb9d13d552a958f3aad332e93725299257c3f Mon Sep 17 00:00:00 2001 From: Kirill Date: Mon, 15 Oct 2018 17:16:46 +0300 Subject: [PATCH 2/2] refactor --- DApp.js | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/DApp.js b/DApp.js index 426a6a3..7d33a3c 100644 --- a/DApp.js +++ b/DApp.js @@ -31,25 +31,19 @@ class DApp { * @type {{registerMessageHandler: (function(): boolean), broadcastMessage: (function(): void), sendMessage: (function(): void), starwave: {registerMessageHandler: (function(): any), sendMessage: (function(): any), createMessage: (function(): any)}}} */ this.messaging = { - registerMessageHandler: function () { - return that.candy.registerMessageHandler.apply(that.candy, arguments) - }, - broadcastMessage: function () { - return that.candy.broadcastMessage.apply(that.candy, arguments) - }, - sendMessage: function () { - return that.network.socketSend.apply(that, arguments) - }, + registerMessageHandler: (args) => this.candy.registerMessageHandler(args), + + broadcastMessage: (args) => this.candy.broadcastMessage(args), + + sendMessage: (args) => this.network.socketSend(args), + starwave: { - registerMessageHandler: function () { - return that._starwave.registerMessageHandler.apply(that._starwave, arguments) - }, - sendMessage: function () { - return that._starwave.sendMessage.apply(that._starwave, arguments) - }, - createMessage: function () { - return that._starwave.createMessage.apply(that._starwave, arguments) - }, + registerMessageHandler: (args) => this._starwave.registerMessageHandler(args), + + sendMessage: (args) => this._starwave.sendMessage(args), + + createMessage: (args) => this._starwave.createMessage(args), + } }; @@ -94,16 +88,20 @@ class DApp { /** * Initiate Application start */ - init() { + init(cb) { this.candy.start(); - - } + if (typeof cb === 'function'){ + cb(); + } + }; /** * Terminating app */ terminate(cb) { - cb(); + if (typeof cb === 'function'){ + cb(); + } } }