From 8b6368003aa55f9ecde172a7fee3c9eb99f78a75 Mon Sep 17 00:00:00 2001 From: Matthew Wright Date: Sat, 18 Nov 2023 20:22:40 -0700 Subject: [PATCH] update pico-engine-core --- packages/pico-engine-core/package.json | 9 ++-- .../pico-engine-core/src/PicoEngineCore.ts | 2 +- .../src/PicoEngineCoreConfiguration.ts | 4 +- .../pico-engine-core/src/modules/schedule.ts | 46 +++++++++---------- packages/pico-engine-core/test/flush.ts | 10 ++-- .../test/helpers/makeCoreAndKrlCtx.ts | 4 +- .../pico-engine-core/test/helpers/mkdb.ts | 11 +++++ packages/pico-engine-core/test/use-module.ts | 11 ++--- 8 files changed, 51 insertions(+), 46 deletions(-) create mode 100644 packages/pico-engine-core/test/helpers/mkdb.ts diff --git a/packages/pico-engine-core/package.json b/packages/pico-engine-core/package.json index 22450189f..33b8e56c6 100644 --- a/packages/pico-engine-core/package.json +++ b/packages/pico-engine-core/package.json @@ -33,6 +33,7 @@ "test": "ava reset-cache && ava" }, "dependencies": { + "abstract-level": "^1.0.3", "bs58": "^4.0.1", "cross-fetch": "^4.0.0", "cuid": "^2.1.8", @@ -41,23 +42,23 @@ "krl-stdlib": "^1.3.0", "libsodium-wrappers": "^0.7.8", "lodash": "^4.17.11", - "memdown": "^5.1.0", "moment-timezone": "^0.5.31", "node-schedule": "^2.1.0", "normalize-url": "^5.0.0", "p-memoize": "^4.0.0", - "pico-framework": "^0.6.0", + "pico-framework": "^0.7.0", "qs": "^6.9.4", "random-words": "^1.1.1", "strftime": "^0.10.0", "tweetnacl": "^1.0.3" }, "devDependencies": { - "@types/abstract-leveldown": "^5.0.1", - "@types/levelup": "^4.3.3", "@types/lodash": "^4.14.123", "@types/node": "^20.8.10", "ava": "^5.3.1", + "charwise": "^3.0.1", + "level-json-coerce-null": "^1.0.1", + "memory-level": "^1.0.0", "ts-node": "^10.4.0", "typescript": "^5.2.2" }, diff --git a/packages/pico-engine-core/src/PicoEngineCore.ts b/packages/pico-engine-core/src/PicoEngineCore.ts index ee6f58cc7..fe5dd08af 100644 --- a/packages/pico-engine-core/src/PicoEngineCore.ts +++ b/packages/pico-engine-core/src/PicoEngineCore.ts @@ -45,7 +45,7 @@ export class PicoEngineCore { ); this.picoFramework = new PicoFramework({ - leveldown: conf.leveldown, + db: conf.db, genID: conf.genID, useEventInputTime: conf.useEventInputTime, diff --git a/packages/pico-engine-core/src/PicoEngineCoreConfiguration.ts b/packages/pico-engine-core/src/PicoEngineCoreConfiguration.ts index b67ceb04a..09aeb4be2 100644 --- a/packages/pico-engine-core/src/PicoEngineCoreConfiguration.ts +++ b/packages/pico-engine-core/src/PicoEngineCoreConfiguration.ts @@ -1,6 +1,6 @@ -import { AbstractLevelDOWN } from "abstract-leveldown"; import { krl, KrlLogger, PicoLogEntry } from "krl-stdlib"; import { RulesetRegistryLoader } from "./RulesetRegistry"; +import { PicoDb } from "pico-framework"; /** * Configuration options that may be set by the user @@ -9,7 +9,7 @@ export interface PicoEngineCoreConfiguration { /** * provide the persistence layer */ - leveldown: AbstractLevelDOWN; + db: PicoDb; /** * How should rulesets be loaded in? diff --git a/packages/pico-engine-core/src/modules/schedule.ts b/packages/pico-engine-core/src/modules/schedule.ts index 7652c7c96..8be71132a 100644 --- a/packages/pico-engine-core/src/modules/schedule.ts +++ b/packages/pico-engine-core/src/modules/schedule.ts @@ -25,7 +25,7 @@ export type ScheduledEvent = ScheduledEvent_at | ScheduledEvent_repeat; type ScheduleJob = ( timespec: string, - handler: () => void + handler: () => void, ) => { handler: () => void; cancel: () => void }; type SetTimeout = (handler: () => void, time: number) => void; @@ -56,7 +56,7 @@ export class Scheduler { scheduleJob?: ScheduleJob, setTimeout?: SetTimeout, clearTimeout?: ClearTimeout, - now?: () => number + now?: () => number, ) { this.scheduleJob = scheduleJob || nodeSchedule.scheduleJob; this.setTimeout = setTimeout || longTimeout.setTimeout; @@ -105,7 +105,7 @@ export class Scheduler { // If it's in the past it will happen on the next tick this.currTimeout = this.setTimeout( () => this.onTime(), - next.time - this.now() + next.time - this.now(), ); } @@ -166,7 +166,7 @@ export function initScheduleModule(pf: PicoFramework) { async function addToSchedule( ctx: KrlCtx, - sEvent: ScheduledEvent + sEvent: ScheduledEvent, ): Promise { const schedule = (await ctx.rsCtx.getEnt("_schedule")) || {}; schedule[sEvent.id] = sEvent; @@ -199,7 +199,7 @@ export function initScheduleModule(pf: PicoFramework) { if (posixTime === null || !krl.isNumber(posixTime)) { throw new TypeError( - "schedule .. at expected a timestamp but got " + krl.typeOf(time) + "schedule .. at expected a timestamp but got " + krl.typeOf(time), ); } @@ -215,7 +215,7 @@ export function initScheduleModule(pf: PicoFramework) { time: 0, }, }); - } + }, ), repeat: krl.Postlude( @@ -233,7 +233,7 @@ export function initScheduleModule(pf: PicoFramework) { time: 0, }, }); - } + }, ), list: krl.Function([], async function () { @@ -249,7 +249,7 @@ export function initScheduleModule(pf: PicoFramework) { remove: krl.Action(["id"], async function (id) { await this.rsCtx.putEnt( "_schedule", - _.omit(this.rsCtx.getEnt("_schedule") || {}, id) + _.omit(this.rsCtx.getEnt("_schedule") || {}, id), ); scheduler.remove(id); }), @@ -266,24 +266,20 @@ export function initScheduleModule(pf: PicoFramework) { return { module, - start() { - return new Promise((resolve, reject) => { - const s = pf.db.createReadStream({ - gte: ["entvar"], - lte: ["entvar", undefined], // charwise sorts with null at the bottom and undefined at the top - }); - s.on("error", reject); - s.on("end", () => resolve(undefined)); - s.on("data", (data) => { - if (data.key[3] === "_schedule") { - const rid = data.key[2]; - const value: { [id: string]: ScheduledEvent } = data.value; - for (const sEvent of Object.values(value)) { - addScheduledEvent(rid, sEvent); - } - } - }); + async start() { + const iter = pf.db.iterator({ + gte: ["entvar"], + lte: ["entvar", undefined], // charwise sorts with null at the bottom and undefined at the top }); + for await (const [key, val] of iter) { + if (key[3] === "_schedule") { + const rid = key[2] + ""; + const value: { [id: string]: ScheduledEvent } = val; + for (const sEvent of Object.values(value)) { + addScheduledEvent(rid, sEvent); + } + } + } }, }; } diff --git a/packages/pico-engine-core/test/flush.ts b/packages/pico-engine-core/test/flush.ts index 0bccee70d..d0e5ee965 100644 --- a/packages/pico-engine-core/test/flush.ts +++ b/packages/pico-engine-core/test/flush.ts @@ -5,8 +5,7 @@ import { RulesetRegistryLoaderMem } from "../src/RulesetRegistryLoaderMem"; import { allowAllChannelConf } from "./helpers/allowAllChannelConf"; import { mkSignalBase } from "./helpers/mkSignalBase"; import { sleep } from "./helpers/sleep"; - -const memdown = require("memdown"); +import { mkdb } from "./helpers/mkdb"; test("ruleset flush", async (t) => { function mkKrlVersion(v: string): string { @@ -29,7 +28,7 @@ test("ruleset flush", async (t) => { }; const core = new PicoEngineCore({ - leveldown: memdown(), + db: mkdb(), rsRegLoader: RulesetRegistryLoaderMem(async (url) => krlUrls[url]), log: makeKrlLogger((line: string) => null), async getPicoLogs(picoId) { @@ -38,9 +37,8 @@ test("ruleset flush", async (t) => { }); await core.start(); - const chann = await core.picoFramework.rootPico.newChannel( - allowAllChannelConf - ); + const chann = + await core.picoFramework.rootPico.newChannel(allowAllChannelConf); const eci = chann.id; const mkSignal = mkSignalBase(core.picoFramework); const signal = mkSignal(eci); diff --git a/packages/pico-engine-core/test/helpers/makeCoreAndKrlCtx.ts b/packages/pico-engine-core/test/helpers/makeCoreAndKrlCtx.ts index 914059178..246261e00 100644 --- a/packages/pico-engine-core/test/helpers/makeCoreAndKrlCtx.ts +++ b/packages/pico-engine-core/test/helpers/makeCoreAndKrlCtx.ts @@ -1,7 +1,7 @@ import { KrlCtx, makeKrlLogger } from "krl-stdlib"; import { PicoEngineCore } from "../../src/PicoEngineCore"; import { RulesetRegistryLoaderTesting } from "./RulesetRegistryLoaderTesting"; -const memdown = require("memdown"); +import { mkdb } from "./mkdb"; export default async function makeCoreAndKrlCtx(): Promise<{ core: PicoEngineCore; @@ -9,7 +9,7 @@ export default async function makeCoreAndKrlCtx(): Promise<{ }> { let krlCtx: any; let core = new PicoEngineCore({ - leveldown: memdown(), + db: mkdb(), rsRegLoader: RulesetRegistryLoaderTesting({ getKrlCtx: { rid: "getKrlCtx", diff --git a/packages/pico-engine-core/test/helpers/mkdb.ts b/packages/pico-engine-core/test/helpers/mkdb.ts new file mode 100644 index 000000000..644e19046 --- /dev/null +++ b/packages/pico-engine-core/test/helpers/mkdb.ts @@ -0,0 +1,11 @@ +import { MemoryLevel } from "memory-level"; +import { PicoDb, PicoDbKey } from "pico-framework"; +const charwise = require("charwise"); +const safeJsonCodec = require("level-json-coerce-null"); + +export function mkdb(): PicoDb { + return new MemoryLevel({ + keyEncoding: charwise, + valueEncoding: safeJsonCodec, + }); +} diff --git a/packages/pico-engine-core/test/use-module.ts b/packages/pico-engine-core/test/use-module.ts index 012105fe2..26bf7a667 100644 --- a/packages/pico-engine-core/test/use-module.ts +++ b/packages/pico-engine-core/test/use-module.ts @@ -7,8 +7,7 @@ import { } from "../src"; import { allowAllChannelConf } from "./helpers/allowAllChannelConf"; import { mkSignalBase } from "./helpers/mkSignalBase"; - -const memdown = require("memdown"); +import { mkdb } from "./helpers/mkdb"; test("use-module install order", async (t) => { const krlUrls: { [url: string]: string } = { @@ -26,7 +25,7 @@ test("use-module install order", async (t) => { "mem://ccc": `ruleset ccc{meta{use module bbb}}`, }; const conf: PicoEngineCoreConfiguration = { - leveldown: memdown(), + db: mkdb(), rsRegLoader: RulesetRegistryLoaderMem(async (url) => krlUrls[url]), log: makeKrlLogger((line: string) => null), async getPicoLogs(picoId) { @@ -47,7 +46,7 @@ test("use-module install order", async (t) => { let signal = mkSignalBase(pe.picoFramework)(eci); let err = await t.throwsAsync( - signal("main", "install", { url: "mem://aaa" }) + signal("main", "install", { url: "mem://aaa" }), ); t.is("" + err, "Error: Module not found: bbb"); err = await t.throwsAsync(signal("main", "install", { url: "mem://ccc" })); @@ -83,7 +82,7 @@ test("use-module startup dependency", async (t) => { }`, }; const conf: PicoEngineCoreConfiguration = { - leveldown: memdown(), + db: mkdb(), rsRegLoader: RulesetRegistryLoaderMem(async (url) => krlUrls[url]), log: makeKrlLogger((line: string) => null), async getPicoLogs(picoId) { @@ -150,7 +149,7 @@ test("use-module get dependency updates", async (t) => { }; const conf: PicoEngineCoreConfiguration = { - leveldown: memdown(), + db: mkdb(), rsRegLoader: RulesetRegistryLoaderMem(async (url) => krlUrls[url]), log: makeKrlLogger((line: string) => null), async getPicoLogs(picoId) {