Browse Source

add caching feature for bitcore.X requires

Normally when one loads the bitcore object, all classes are loaded into memory,
even if they are not used. This comment updates this so that the classes are
only required when they are actually used, preserving memory and making bitcore
as small and light for each project. This code is only relevant for node, since
the browser will have the ability to compile only the selected components.
Also, the Key class does not yet work with this, but everything else does.
patch-2
Ryan X. Charles 11 years ago
parent
commit
8a41414114
  1. 73
      bitcore.js

73
bitcore.js

@ -1,49 +1,50 @@
/*
* Bitcore bindings for the browser
*/
module.exports.bignum = require('bignum');
module.exports.base58 = require('base58-native');
module.exports.buffertools = require('buffertools');
/*
One way to require files is this simple way:
module.exports.Address = require('./Address');
module.exports.config = require('./config');
module.exports.const = require('./const');
module.exports.Deserialize = require('./Deserialize');
module.exports.log = require('./util/log');
module.exports.networks = require('./networks');
module.exports.util = require('./util/util');
However, that will load all classes in memory even if they are not used.
Instead, we can set the 'get' property of each class to only require them when
they are accessed, saving memory if they are not used in a given project.
*/
var requireWhenAccessed = function(name, file) {
Object.defineProperty(module.exports, name, {get: function() {return require(file)}});
};
module.exports.EncodedData = require('./util/EncodedData');
module.exports.VersionedData = require('./util/VersionedData');
module.exports.Address = require('./Address');
module.exports.Opcode = require('./Opcode');
module.exports.Script = require('./Script');
module.exports.Transaction = require('./Transaction');
module.exports.Connection = require('./Connection');
module.exports.Peer = require('./Peer');
module.exports.Block = require('./Block');
module.exports.ScriptInterpreter = require('./ScriptInterpreter');
module.exports.Bloom = require('./Bloom');
requireWhenAccessed('bignum', 'bignum');
requireWhenAccessed('base58', 'base58-native');
requireWhenAccessed('buffertools', 'buffertools');
requireWhenAccessed('config', './config');
requireWhenAccessed('const', './const');
requireWhenAccessed('Deserialize', './Deserialize');
requireWhenAccessed('log', './util/log');
requireWhenAccessed('networks', './networks');
requireWhenAccessed('util', './util/util');
requireWhenAccessed('EncodedData', './util/EncodedData');
requireWhenAccessed('VersionedData', './util/VersionedData');
requireWhenAccessed('Address', './Address');
requireWhenAccessed('Opcode', './Opcode');
requireWhenAccessed('Script', './Script');
requireWhenAccessed('Transaction', './Transaction');
requireWhenAccessed('Connection', './Connection');
requireWhenAccessed('Peer', './Peer');
requireWhenAccessed('Block', './Block');
requireWhenAccessed('ScriptInterpreter', './ScriptInterpreter');
requireWhenAccessed('Bloom', './Bloom');
module.exports.KeyModule = require('./Key');
module.exports.SINKey = require('./SINKey');
module.exports.SIN = require('./SIN');
module.exports.PrivateKey = require('./PrivateKey');
module.exports.RpcClient = require('./RpcClient');
module.exports.Wallet = require('./Wallet');
module.exports.WalletKey = require('./WalletKey');
requireWhenAccessed('SINKey', './SINKey');
requireWhenAccessed('SIN', './SIN');
requireWhenAccessed('PrivateKey', './PrivateKey');
requireWhenAccessed('RpcClient', './RpcClient');
requireWhenAccessed('Wallet', './Wallet');
requireWhenAccessed('WalletKey', './WalletKey');
module.exports.Buffer = Buffer;
if (typeof process.versions === 'undefined') {
// Browser specific
module.exports.bignum.config({EXPONENTIAL_AT: 9999999, DECIMAL_PLACES: 0, ROUNDING_MODE: 1});
// module.exports.PeerManager = function () {
// throw new Error('PeerManager not availabe in browser Bitcore, under .bitcore. Use it with: require(\'PeerManager\');');
// };
}
else {
// Nodejs specific
module.exports.PeerManager = require('./PeerManager');
requireWhenAccessed('PeerManager', './PeerManager');
}

Loading…
Cancel
Save