避免动态require
This commit is contained in:
parent
678dc9cf56
commit
e5922fae82
31
Action.js
31
Action.js
@ -1,5 +1,6 @@
|
|||||||
const Ling = require('so.ling')
|
const Ling = require('so.ling')
|
||||||
const ticCrypto = require('tic.crypto')
|
const ticCrypto = require('tic.crypto')
|
||||||
|
const my = {} // 私有数据
|
||||||
|
|
||||||
/** ****************** Public of instance ********************/
|
/** ****************** Public of instance ********************/
|
||||||
|
|
||||||
@ -34,12 +35,12 @@ MOM._model = {
|
|||||||
json: { default: undefined, sqlite: 'TEXT' } // 给不同类型的 ActionXxx 子类来自定义其所需的数据结构
|
json: { default: undefined, sqlite: 'TEXT' } // 给不同类型的 ActionXxx 子类来自定义其所需的数据结构
|
||||||
}
|
}
|
||||||
|
|
||||||
MOM.packMe = async function (keypair) { // 由前端调用,后台不创建
|
MOM.packMe = async function ({seckey, pubkey}={}) { // 由前端调用,后台不创建
|
||||||
this.actorPubkey = keypair.pubkey
|
this.actorPubkey = pubkey
|
||||||
this.actorAddress = ticCrypto.pubkey2address(keypair.pubkey)
|
this.actorAddress = ticCrypto.pubkey2address(pubkey)
|
||||||
this.timestamp = new Date()
|
this.timestamp = new Date()
|
||||||
|
|
||||||
await this.signMe(keypair.seckey)
|
await this.signMe(seckey)
|
||||||
this.hashMe()
|
this.hashMe()
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
@ -119,17 +120,20 @@ DAD.execute = async function (action) { // For chain server.
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
DAD.getActionType = (type)=>{
|
DAD.getActionType = (type)=>{
|
||||||
return require(`./${type}`)
|
if (!my.ActionTypeDict) {
|
||||||
|
my.ActionTypeDict = require('./indexActionTypes.js') // 避免动态require
|
||||||
|
}
|
||||||
|
return my.ActionTypeDict[type]
|
||||||
}
|
}
|
||||||
DAD.createTypedAction = function(action){
|
DAD.createTypedAction = function(action){
|
||||||
return new (DAD.getActionType(action.type))(action)
|
return new (DAD.getActionType(action.type))(action)
|
||||||
}
|
}
|
||||||
DAD.buildUserAction = async function (action, keypair) { // Applicable on client. 客户端调用 Action.build,即可新建、并打包成一个完整的子事务,不需要亲自调用 constructor, packMe 等方法。
|
DAD.buildUserAction = async function (action, {seckey, pubkey}={}) { // Applicable on client. 客户端调用 Action.build,即可新建、并打包成一个完整的子事务,不需要亲自调用 constructor, packMe 等方法。
|
||||||
if (action && action.type && keypair && keypair.seckey && keypair.pubkey && ticCrypto.seckey2pubkey(keypair.seckey)===keypair.pubkey) {
|
if (action && action.type && seckey && pubkey && ticCrypto.seckey2pubkey(seckey)===pubkey) {
|
||||||
let typedAction = new (DAD.getActionType(action.type))(action)
|
let typedAction = new (DAD.getActionType(action.type))(action)
|
||||||
typedAction.actorPubkey = keypair.pubkey
|
typedAction.actorPubkey = pubkey
|
||||||
if (typedAction.validateMe()) {
|
if (typedAction.validateMe()) {
|
||||||
await typedAction.packMe(keypair) // 在 packMe 里,会把 actorPubkey 转存为 actorAddress。
|
await typedAction.packMe({seckey, pubkey}) // 在 packMe 里,会把 actorPubkey 转存为 actorAddress。
|
||||||
return typedAction
|
return typedAction
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -160,15 +164,14 @@ DAD.api = {}
|
|||||||
DAD.api.getAction = async function (option) {
|
DAD.api.getAction = async function (option) {
|
||||||
return await DAD.getOne(option)
|
return await DAD.getOne(option)
|
||||||
}
|
}
|
||||||
|
|
||||||
DAD.api.getActionList = async function (option) {
|
DAD.api.getActionList = async function (option) {
|
||||||
return await DAD.getAll(option)
|
return await DAD.getAll(option)
|
||||||
}
|
}
|
||||||
|
DAD.api.getMyActionList = async function ({Action={}, config}={}) {
|
||||||
DAD.api.getMyActionList = async function ({myAddress, config}={}) {
|
|
||||||
let list = {}
|
let list = {}
|
||||||
list.fromMe = await DAD.getAll({Action:{actorAddress:myAddress}, config:config})
|
let {myAddress, ...action} = Action
|
||||||
list.toMe = await DAD.getAll({Action:{toAddress: myAddress}, config:config})
|
list.fromMe = await DAD.getAll({Action:{actorAddress:myAddress, ...action}, config})
|
||||||
|
list.toMe = await DAD.getAll({Action:{toAddress:myAddress, ...action}, config})
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
|
4
indexActionTypes.js
Normal file
4
indexActionTypes.js
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
module.exports = {
|
||||||
|
ActionTransfer: require('./ActionTransfer.js'),
|
||||||
|
ActionStore: require('./ActionStore.js')
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user