Rewrote some navigation stuff based on new parser

This commit is contained in:
Zef Hemel
2022-04-03 18:42:12 +02:00
parent 16bf0d866d
commit 07453d638b
21 changed files with 206 additions and 235 deletions
+3 -3
View File
@@ -1,9 +1,9 @@
import { SysCallMapping } from "../system";
import { EventHook } from "../hooks/event";
import {SysCallMapping} from "../system";
import {EventHook} from "../hooks/event";
export function eventSyscalls(eventHook: EventHook): SysCallMapping {
return {
async dispatch(ctx, eventName: string, data: any) {
"event.dispatch": async(ctx, eventName: string, data: any) => {
return eventHook.dispatchEvent(eventName, data);
},
};
+4 -4
View File
@@ -1,13 +1,13 @@
import fetch, { RequestInfo, RequestInit } from "node-fetch";
import { SysCallMapping } from "../system";
import fetch, {RequestInfo, RequestInit} from "node-fetch";
import {SysCallMapping} from "../system";
export function fetchSyscalls(): SysCallMapping {
return {
async json(ctx, url: RequestInfo, init: RequestInit) {
"fetch.json": async (ctx, url: RequestInfo, init: RequestInit) => {
let resp = await fetch(url, init);
return resp.json();
},
async text(ctx, url: RequestInfo, init: RequestInit) {
"fetch.text": async(ctx, url: RequestInfo, init: RequestInit) => {
let resp = await fetch(url, init);
return resp.text();
},
+4 -4
View File
@@ -1,12 +1,12 @@
import { promisify } from "util";
import { execFile } from "child_process";
import type { SysCallMapping } from "../system";
import {promisify} from "util";
import {execFile} from "child_process";
import type {SysCallMapping} from "../system";
const execFilePromise = promisify(execFile);
export default function (cwd: string): SysCallMapping {
return {
run: async (
"shell.run": async (
ctx,
cmd: string,
args: string[]
+5 -5
View File
@@ -1,14 +1,14 @@
import { createSandbox } from "../environments/node_sandbox";
import { expect, test } from "@jest/globals";
import { System } from "../system";
import { storeSyscalls } from "./store.dexie_browser";
import {createSandbox} from "../environments/node_sandbox";
import {expect, test} from "@jest/globals";
import {System} from "../system";
import {storeSyscalls} from "./store.dexie_browser";
// For testing in node.js
require("fake-indexeddb/auto");
test("Test store", async () => {
let system = new System("server");
system.registerSyscalls("store", [], storeSyscalls("test", "test"));
system.registerSyscalls([], storeSyscalls("test", "test"));
let plug = await system.load(
"test",
{
+9 -9
View File
@@ -1,5 +1,5 @@
import Dexie from "dexie";
import { SysCallMapping } from "../system";
import {SysCallMapping} from "../system";
export type KV = {
key: string;
@@ -17,26 +17,26 @@ export function storeSyscalls(
const items = db.table(tableName);
return {
async delete(ctx, key: string) {
"store.delete": async (ctx, key: string) => {
await items.delete(key);
},
async deletePrefix(ctx, prefix: string) {
"store.deletePrefix": async (ctx, prefix: string) => {
await items.where("key").startsWith(prefix).delete();
},
async deleteAll() {
"store.deleteAll": async () => {
await items.clear();
},
async set(ctx, key: string, value: any) {
"store.set": async (ctx, key: string, value: any) => {
await items.put({
key,
value,
});
},
async batchSet(ctx, kvs: KV[]) {
"store.batchSet": async (ctx, kvs: KV[]) => {
await items.bulkPut(
kvs.map(({ key, value }) => ({
key,
@@ -45,17 +45,17 @@ export function storeSyscalls(
);
},
async get(ctx, key: string): Promise<any | null> {
"store.get": async (ctx, key: string): Promise<any | null> => {
let result = await items.get({
key,
});
return result ? result.value : null;
},
async queryPrefix(
"store.queryPrefix": async (
ctx,
keyPrefix: string
): Promise<{ key: string; value: any }[]> {
): Promise<{ key: string; value: any }[]> => {
let results = await items.where("key").startsWith(keyPrefix).toArray();
return results.map((result) => ({
key: result.key,
+5 -14
View File
@@ -1,11 +1,7 @@
import { createSandbox } from "../environments/node_sandbox";
import { expect, test } from "@jest/globals";
import { System } from "../system";
import {
ensureTable,
storeReadSyscalls,
storeWriteSyscalls,
} from "./store.knex_node";
import {createSandbox} from "../environments/node_sandbox";
import {expect, test} from "@jest/globals";
import {System} from "../system";
import {ensureTable, storeSyscalls} from "./store.knex_node";
import knex from "knex";
import fs from "fs/promises";
@@ -19,12 +15,7 @@ test("Test store", async () => {
});
await ensureTable(db, "test_table");
let system = new System("server");
system.registerSyscalls(
"store",
[],
storeWriteSyscalls(db, "test_table"),
storeReadSyscalls(db, "test_table")
);
system.registerSyscalls([], storeSyscalls(db, "test_table"));
let plug = await system.load(
"test",
{
+14 -22
View File
@@ -1,5 +1,5 @@
import { Knex } from "knex";
import { SysCallMapping } from "../system";
import {Knex} from "knex";
import {SysCallMapping} from "../system";
type Item = {
page: string;
@@ -23,21 +23,21 @@ export async function ensureTable(db: Knex<any, unknown>, tableName: string) {
}
}
export function storeWriteSyscalls(
export function storeSyscalls(
db: Knex<any, unknown>,
tableName: string
): SysCallMapping {
const apiObj: SysCallMapping = {
delete: async (ctx, key: string) => {
"store.delete": async (ctx, key: string) => {
await db<Item>(tableName).where({ key }).del();
},
deletePrefix: async (ctx, prefix: string) => {
"store.deletePrefix": async (ctx, prefix: string) => {
return db<Item>(tableName).andWhereLike("key", `${prefix}%`).del();
},
deleteAll: async (ctx) => {
"store.deleteAll": async (ctx) => {
await db<Item>(tableName).del();
},
set: async (ctx, key: string, value: any) => {
"store.set": async (ctx, key: string, value: any) => {
let changed = await db<Item>(tableName)
.where({ key })
.update("value", JSON.stringify(value));
@@ -49,26 +49,17 @@ export function storeWriteSyscalls(
}
},
// TODO: Optimize
batchSet: async (ctx, kvs: KV[]) => {
"store.batchSet": async (ctx, kvs: KV[]) => {
for (let { key, value } of kvs) {
await apiObj.set(ctx, key, value);
await apiObj["store.set"](ctx, key, value);
}
},
batchDelete: async (ctx, keys: string[]) => {
"store.batchDelete": async (ctx, keys: string[]) => {
for (let key of keys) {
await apiObj.delete(ctx, key);
await apiObj["store.delete"](ctx, key);
}
},
};
return apiObj;
}
export function storeReadSyscalls(
db: Knex<any, unknown>,
tableName: string
): SysCallMapping {
return {
get: async (ctx, key: string): Promise<any | null> => {
"store.get": async (ctx, key: string): Promise<any | null> => {
let result = await db<Item>(tableName).where({ key }).select("value");
if (result.length) {
return JSON.parse(result[0].value);
@@ -76,7 +67,7 @@ export function storeReadSyscalls(
return null;
}
},
queryPrefix: async (ctx, prefix: string) => {
"store.queryPrefix": async (ctx, prefix: string) => {
return (
await db<Item>(tableName)
.andWhereLike("key", `${prefix}%`)
@@ -87,4 +78,5 @@ export function storeReadSyscalls(
}));
},
};
return apiObj;
}