Skip to content

Commit

Permalink
Apr 4, 2023, 8:34 AM
Browse files Browse the repository at this point in the history
  • Loading branch information
nuxodin committed Apr 4, 2023
1 parent d8f07f5 commit f37b041
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 55 deletions.
4 changes: 2 additions & 2 deletions drivers/denoFs.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class FsItem extends AsyncItem {
return Deno.readTextFile(this.fsPath);
}
if (info.isDirectory) {
// this.loadAll();
// this.loadItems();
// return this;
const list = Object.create(null);
for await (const dirEntry of Deno.readDir(this.fsPath)) {
Expand All @@ -54,7 +54,7 @@ class FsItem extends AsyncItem {
return list;
}
}
// async loadAll() { // if directory, load all children, todo
// async loadItems() { // if directory, load all children, todo
// for await (const dirEntry of Deno.readDir(this.fsPath)) {
// this.item(dirEntry.name);
// // this.item(dirEntry.name).type = dirEntry.isDirectory ? 'directory' : 'file';
Expand Down
50 changes: 21 additions & 29 deletions drivers/rest.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,46 @@
// alfa version, todo!
// alpha!

//import {Item} from '../item.js';
import {AsyncItem} from '../tools/AsyncItem.js';

// restApi
export function restApi(url, options){
export function restApi(url, options={}){

// todo, cancel request if item turns into an object, great for proxified items
class RestApiItem extends AsyncItem {
static isPrimitive(){ return false; }
createGetter() {
return itemRequest(this, 'GET').promise;
return itemRequest(this, 'GET');
}
createSetter(value) {
return itemRequest(this, 'PUT', JSON.stringify(value)).promise;
createSetter(value, signal) {
return itemRequest(this, 'PUT', JSON.stringify(value), signal);
}
ChildClass = RestApiItem;
}

const root = new RestApiItem();

function itemRequest(item, method, body){
async function itemRequest(item, method, body, signal){
const headers = new Headers();
if (options?.auth) {
const {username, password} = options.auth;
headers.append('Authorization', 'Basic' + base64.encode(username + ":" + password));
}
const controller = new AbortController();

setTimeout(() => controller.abort(), options?.timeout ?? 10000);

const promise = fetchDelay(url + '/' + item.pathString, {headers, method, body, signal:controller.signal}).then(response => {
const value = response.json();
//item.value = value; // no more a promise, trigger setter to update
return value;
}).catch(error => {
if (error.name === 'AbortError') {
console.log('fetch aborted');
} else {
console.error(error);
}
});
return {controller,promise};
}

function fetchDelay(url, options){ // not direct fetch, can be aborted quickly
return new Promise(resolve => {
setTimeout(() => {
resolve(fetch(url, options));
}, 1);
});
//setTimeout(() => signal.abort(), options?.timeout ?? 10000);

const endPoint = url + '/' + item.pathKeys.join('/');

const response = await fetch(endPoint, {headers, method, body, signal});
let data = await response.json();

if (options.map) data = options.map(data);

for (const[key, value] of Object.entries(data)) {
item.item(key);
}

return data;
}

return root;
Expand Down
8 changes: 8 additions & 0 deletions drivers/sql/Items/Db.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ export class Db extends Item {
return "'"+(value+'').replace(/'/g, "\'")+"'";
}

async loadItems(){
const tables = await this.query("SHOW TABLES");
for (const table of tables) {
this.item(table.key);
}
}


// schema
async setSchema(schema){
this.schema = schema;
Expand Down
31 changes: 8 additions & 23 deletions tests/rest.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,21 @@ <h1>Test</h1>
import {restApi} from "../drivers/rest.js";

// rest api
/*
/* */
const api = restApi('https://restcountries.com/v3.1');
api.item('currency').item('chf').value.then(chf => {
console.log(chf);
});
*/
const chf = await api.item('currency').item('chf').value;
console.log(chf)
/* *
const api = restApi('https://reqres.in/api', {map: result => {
return Object.fromEntries(result.data.map(item => [item.id, item]));
}});
api.item('user').value.then(data => {
console.log(api.item('user'), data)
});

const user2 = api.item('user').item('2');

user2.value.then(data => {
console.log(data)
});

user2.value = {
name: 'morpheus',
job: 'zion resident'
};

api.item('user').item('23').value.then(data => {
console.log(data)
});
const i3 = api.item(2);
console.log(api)
console.log('3', await i3.value);
/* */


</script>
2 changes: 1 addition & 1 deletion tests/syncWith.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h1>Test</h1>

import {item, effect} from "../item.js";
import {syncWith} from "../tools/syncWith.js";
import {dump} from 'https://cdn.jsdelivr.net/gh/nuxodin/dump.js@1.2.1/mod.min.js';
import {dump} from 'https://cdn.jsdelivr.net/gh/nuxodin/dump.js@1.2.2/mod.min.js';


const original = {
Expand Down
5 changes: 5 additions & 0 deletions tools/AsyncItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ export class AsyncItem extends Item {
createGetter() { throw new Error('createGetter not implemented'); }
createSetter(value) { throw new Error('createSetter not implemented (value: ' + value + ')'); }

//async loadKeys() { throw new Error('loadKeys not implemented'); }
//async loadItems() { throw new Error('loadItems not implemented'); }

$get() {
return this.master.get();
}
$set(value) {
return this.master.set(value);
}


ChildClass = AsyncChild;
}

Expand Down

0 comments on commit f37b041

Please sign in to comment.