From cf2e5f115c212b08339ce65d9b234ea6387e539e Mon Sep 17 00:00:00 2001 From: 1aerostorm Date: Sat, 30 Mar 2024 01:41:58 +0300 Subject: [PATCH] JS - 0.9.68, golos.libs --- golos-lib-js/package.json | 2 +- golos-lib-js/src/browser.js | 3 +++ golos-lib-js/src/index.js | 9 +++++++-- golos-lib-js/src/libs.js | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 golos-lib-js/src/libs.js diff --git a/golos-lib-js/package.json b/golos-lib-js/package.json index 0521c5c..e3d4a48 100644 --- a/golos-lib-js/package.json +++ b/golos-lib-js/package.json @@ -1,6 +1,6 @@ { "name": "golos-lib-js", - "version": "0.9.67", + "version": "0.9.68", "description": "Golos-js the JavaScript library with API for GOLOS blockchain", "main": "lib/index.js", "scripts": { diff --git a/golos-lib-js/src/browser.js b/golos-lib-js/src/browser.js index eea90b6..4e499ac 100644 --- a/golos-lib-js/src/browser.js +++ b/golos-lib-js/src/browser.js @@ -8,6 +8,7 @@ const formatter = require('./formatter')(api); const memo = require('./auth/memo'); const messages = require('./auth/messages'); const utils = require('./utils'); +const Libs = require('./libs') const { importNativeLibCtx, importNativeLib, isNativeLibLoaded, assertNativeLib, } = require('./core'); @@ -29,6 +30,8 @@ const golos = { utils, }; +new Libs(golos) + if (typeof window !== 'undefined') { window.golos = golos; } diff --git a/golos-lib-js/src/index.js b/golos-lib-js/src/index.js index 0b49497..0d8727a 100644 --- a/golos-lib-js/src/index.js +++ b/golos-lib-js/src/index.js @@ -8,11 +8,12 @@ const memo = require('./auth/memo'); const messages = require('./auth/messages'); const config = require('./config'); const utils = require('./utils'); +const Libs = require('./libs') const ecc = require('./auth/ecc/'); const { importNativeLibCtx, importNativeLib, isNativeLibLoaded, assertNativeLib, } = require('./core'); -module.exports = { +const rootObj = { importNativeLibCtx, importNativeLib, isNativeLibLoaded, @@ -29,4 +30,8 @@ module.exports = { use: middlewares.use, utils, ecc, -}; +} + +new Libs(rootObj) + +module.exports = rootObj diff --git a/golos-lib-js/src/libs.js b/golos-lib-js/src/libs.js new file mode 100644 index 0000000..9dc84dc --- /dev/null +++ b/golos-lib-js/src/libs.js @@ -0,0 +1,37 @@ + +class Libs { + constructor(rootObj) { + this.rootObj = rootObj + rootObj.libs = this + } + + add = (lib) => { + const { rootObj } = this + const { libName } = lib + if (!libName) throw new Error('Lib should have field libName') + if (!libName.split) throw new Error('libName should be string') + if (rootObj.libs[libName]) { + rootObj.libs[libName] = lib + return false + } else { + rootObj.libs[libName] = lib + return true + } + } + + remove = (libOrName) => { + const { rootObj } = this + const libName = libOrName + if (!libName.split) { + libName = libName.libName + if (!libName) throw new Error('Lib should have field libName') + } + if (rootObj.libs[libName]) { + delete rootObj.libs[libName] + return true + } + return false + } +} + +module.exports = Libs