godprotocol
Version:
A distributed computing environment for Web 4.0 — integrating AI, decentralisation, and virtual computation.
117 lines (91 loc) • 2.37 kB
JavaScript
import Arr from "./arr.js";
import Storage from "./functions/storage.js";
class Conf extends Storage{
constructor(object, account){
super(object, account)
this.value = object
this.type = 'config'
}
persist = async()=>{
await this.manager.oracle.fs.write(`${this.value.path}/.config`, JSON.stringify(this.value))
}
set_next = async(addr)=>{
this.value.next = addr;
await this.persist()
}
history = async(query)=>{
let res = []
let arr, pure;
if (typeof query === 'number'){
if(query >= 0){}
else{
arr= []
let x=query
while (x < 0){
arr.push(x)
x++
}
}
}
let i = 0;
let age = this.value.age;
while(i < arr.length){
let timestep = arr[i], my_age;
if (timestep < 0){
my_age = age + timestep + 1
}else {
my_age = timestep;
}
if (my_age > age)continue;
let config = await this.manager.oracle.fs.get_config(this.value.path, null, {age: my_age})
res.push(pure?config: await config.read())
i++
}
return new Arr({value:res}, this.manager)
}
read = async()=>{
let current = this.value.current;
if (!current)return
return await this.manager.oracle.fs.read_file(current)
}
set_previous = async(addr)=>{
this.value.previous = addr;
await this.persist()
}
push = async(parent)=>{
this.value.lapse++
await this.persist()
}
link = async(addr)=>{
if (!addr )return;
console.log(this.value, addr)
if (this.value.current){
let conf = await this.manager.oracle.fs.get_config(this.value.current)
await conf.set_next(addr)
let addr_conf = await this.manager.oracle.fs.get_config(`${addr}`)
await addr_conf.set_previous(this.value.current)
}
this.value.age++
this.value.current = addr;
await this.persist()
}
}
export default Conf
// test = [1,2,3]
// ./test
// ./0
// ./num1 {content:1, previous: null}
// ./config {recent: './num1'}
// ./1
// ./num2 {content:2, previous: null}
// ./config {recent: './num2'}
// ./2
// ./num3 {content:3, previous:.null}
// ./config {recent: './num3'}
// ./.config
// {type:'array', recent: './2'}
// test[1] = 4
// ./1
// ./num4 {content:4, previous: './num2'}
// ./config {recent: './num4'}
//