godprotocol
Version:
A distributed computing environment
155 lines (125 loc) • 4.68 kB
JavaScript
import Datatypes from "godprotocol/Objects/Datatypes";
class Instance extends Datatypes{
constructor(payload, account){
super()
this.account = account
this.value = payload;
this.type = 'instance'
}
operate = async(op, args, options={})=>{
let {classifier} = options;
let value;
switch(classifier){
case 'twain':
value = await this.twain(op, args, options)
break;
case 'number':
value = await this.number(op, args)
break;
case 'array':
value = await this.array(op, args, options)
break;
case 'string':
value = await this.string(op, args)
break;
}
return value;
}
set = async(last, value)=>{
let prop = await last.literal()
let oracle = this.account.manager.oracle
let hsh = await oracle.hash(prop)
let {content, path} = this.value
let prop_path = `${path}/${hsh}`
await oracle.fs.mkdir(prop_path)
let prop_conf = await oracle.fs.read(`${prop_path}/.config`)
if (!prop_conf) prop_conf = {index: -1}
if (value.type !== 'address'){
value = {type: 'address', value: value.value.path}
}
await oracle.fs.write(`${prop_path}/${prop_conf.index + 1}`, JSON.stringify([last.value.path, value.value]))
prop_conf.index += 1
await oracle.fs.write(`${prop_path}/.config`, JSON.stringify(prop_conf))
content[hsh] = prop_conf.index
let path_conf = await oracle.fs.read(`${path}/.config`)
let n_hash = await oracle.hash(content)
await oracle.fs.write(`${path}/${n_hash}`, JSON.stringify({content, type: 'instance', previous: `${path}/${path_conf.current}`}))
path_conf.current = n_hash;
await oracle.fs.write(`${path}/.config`, JSON.stringify(path_conf))
}
get = async (prop, options={})=>{
let oracle = this.account.manager.oracle
let hsh = await oracle.hash(prop)
let {config, chain} = options;
let {content, path} = this.value, result;
let classifier = await oracle.hash('__classifier__')
let pair = await oracle.fs.read(`${path}/${classifier}/${content[classifier]}`);
let val = await oracle.fs.read(pair[1])
let is_datatype = this.account.vm.datatypes.includes(val)
if(Object.keys(content).includes(hsh)){
result = await oracle.fs.read(`${path}/${hsh}/${content[hsh]}`)
if (result){
let res = await oracle.fs.read(result[1])
if(!res) result = {type: 'address', value: result[1]}
else result = res
}
}else {
if (is_datatype){
let res = {
type:'address',
value: prop,
object: path
}
if(val === 'array' && typeof prop === 'number' && options.obj){
result = await this.operate('index', [options.obj], {classifier: val})
}else if (val === 'twain'){
if (!this.is_literal && options.obj){
result = await this.operate('get', [options.obj], {classifier: val})
result= await this.account.vm.operate_result(result, config, chain)
}else result = res
} else result = res
}
}
return result;
}
literal = async ()=>{
let oracle = this.account.manager.oracle;
if (!this.lit){
this.lit = {}
let {content, path} = this.value;
for (let hsh in content){
let value = content[hsh];
let addr = `${path}/${hsh}`;
let pair = await oracle.fs.read(`${addr}/${value}`);
let prop = await oracle.fs.read(pair[0])
let val = await oracle.fs.read(pair[1])
this.lit[prop] = val
}
}
if (this.lit.__classifier__ === 'twain'){
let vm = this.account.vm;
if(!this.lit.content_addr) this.lit.content_addr = {...this.lit.content}
for (let p in this.lit.content_addr){
let [prop, value] = this.lit.content_addr[p]
prop = await vm.cloth_content(await vm.resolve_address(prop))
value = await vm.cloth_content(await vm.resolve_address(value))
delete this.lit.content[p]
this.lit.content[await prop.literal()] = await value.literal()
}
}else if (this.lit.__classifier__ === 'array'){
let vm = this.account.vm;
if(!this.lit.content_addr)
this.lit.content_addr = [...this.lit.content]
for (let i=0; i< this.lit.content_addr.length; i++){
let item = this.lit.content_addr[i]
item = await vm.cloth_content(await vm.resolve_address(item))
this.lit.content[i] = await item.literal()
}
}
return this.lit.content;
}
sync = async()=>{
return this;
}
}
export default Instance