UNPKG

godprotocol

Version:
209 lines (181 loc) 7.21 kB
import Storage from "godprotocol/Datatypes/functions/storage"; class Folder extends Storage{ constructor(address, parent) { super(parent && parent.account.manager) this.account = parent.account; this.physical_address = address this.path = ${this.account.manager.path}/${this.physical_address} this.parent = parent; } sync = async()=>{} birth = async physical_address =>{ let folder = new Folder(physical_address, this); await folder.sync() return folder } chain = async(address)=>{ if (!address.includes('/'))address = ${this.physical_address}/${address} let chain = await this.account.manager.web.set(address, this) return chain; } write = async (content, options)=>{ let res; if (content && content.type === 'address' && options.hash) { let oracle = this.account.manager.oracle; let conf = await oracle.fs.read(${this.path}/.config) || {} let pth = ${this.path}/${options.hash} let p_conf = await oracle.fs.read(${pth}/.config) || {} let obj = {content, type: 'reference', previous: p_conf.current, sibling: conf.sibling} let hsh = await oracle.hash(obj) res = ${pth}/${hsh}; await oracle.fs.write(res, JSON.stringify(obj)) p_conf.current = res await oracle.fs.write(${pth}/.config, JSON.stringify(p_conf)) conf.sibling = res; await oracle.fs.write(${this.path}/.config, JSON.stringify(conf)) }else { res = await this.persist(content, {...options }) } return res } read =async (options={})=>{ let res = await this.retrieve(options) return res; } write_config = async (config, options={})=>{ return await this.persist(config, {...options, config: true, }) } add_config = async (config, options={})=>{ let result; switch(config.type){ case 'module': for (let b=0; b < config.body.length; b++){ let line = config.body[b] config.body[b] = await this.add_config(line) } result = await this.write_config(config) break; case 'adm': let adm_chain = await this.chain(config.location) config.props = await this.add_config(config.props) for (let c=0; c< config.children.length; c++){ config.children[c] = await this.add_config(config.children[c]) } result = await adm_chain.write_config(config) break; case 'class': let class_chain = await this.chain(config.name) for (let name in config.methods){ config.methods[name] = await class_chain.add_config(config.methods[name]) } result = await class_chain.write_config(config) break; case 'function': let fn_chain = await this.chain(config.name) for (let p=0; p< config.parameters.length; p++){ config.parameters[p] = await fn_chain.add_config(config.parameters[p]) } for (let b=0; b< config.body.length; b++){ config.body[b] = await fn_chain.add_config(config.body[b]) } result = await fn_chain.write_config(config) break; case 'return': config.output = await this.add_config(config.output) result = config break; case 'raise': config.object = await this.add_config(config.object) result = config break; case 'trycatch': let try_chain = await this.chain(config.location); for (let t=0;t< config.try_body.length; t++){ config.try_body[t] = await this.add_config(config.try_body[t]) } for (let t=0;t< config.catch_body.length; t++){ config.catch_body[t] = await this.add_config(config.catch_body[t]) } if (config.alias){ config.alias = await this.add_config({type:'assignment', identifier: config.alias.value, value: {type:'void', value:'void'}}) } result = await try_chain.write_config(config) break; case 'condition': let cond_chain = await this.chain(config.location) for(let b= 0; b< config.blocks.length; b++){ let block= config.blocks[b] block.predicate = await cond_chain.add_config(block.predicate) for (let i=0; i< block.body.length; i++){ block.body[i] = await cond_chain.add_config(block.body[i]) } } result = await cond_chain.write_config(config) break; case 'assignment': let ass_chain = await this.chain(config.identifier) config.value = await ass_chain.add_config(config.value); result = await ass_chain.write_config(config) break; case 'loop': let loop_chain = await this.chain(config.location) config.condition = await this.add_config(config.condition) if(config.loop_type === 'for'){ config.iterator = await this.add_config(config.iterator) config.update = await this.add_config(config.update) } for (let b=0; b< config.body.length; b++){ config.body[b] = await this.add_config(config.body[b]) } result = await loop_chain.write_config(config) break; case 'call': if(config.identifier.type === 'nest'){ let addr = await this.add_config(config.identifier) config.identifier = {address: addr, type: 'nest'} } let call_chain = await this.chain(config.location) for(let a=0; a< config.arguments.length; a++){ config.arguments[a] = await call_chain.add_config(config.arguments[a]) } result = await call_chain.write_config(config) break; case 'nest': let nest_chain = await this.chain(config.location) for (let n=0; n< config.nests.length; n++){ config.nests[n] = await nest_chain.add_config(config.nests[n]) } if(config.assignment) config.assignment = await nest_chain.add_config(config.assignment) result = await nest_chain.write_config(config) break; default: if (['number', 'string', 'boolean'].includes(config.type)){ result = await (await this.chain(config.location)).write_config(config) }else if(['variable', 'address', 'reference'].includes(config.type)){ result = config; }else if (config.type === 'void'){ result = config }else if(config.type === 'twain'){ let c_chain = await this.chain(config.location) for (let p in config.value){ let pair = config.value[p] pair[0] = await this.add_config(pair[0]) pair[1] = await this.add_config(pair[1]) } result = await c_chain.write_config(config) } else if(config.type === 'array'){ let c_chain = await this.chain(config.location) for (let i=0; i< config.value.length; i++){ let item = config.value[i] config.value[i] = await this.add_config(item) } result = await c_chain.write_config(config) }else if (['break', 'continue'].includes(config.type)){ result = config; } } return result; } } export default Folder;