Skip to content

Commit

Permalink
update pico-engine-core
Browse files Browse the repository at this point in the history
  • Loading branch information
farskipper committed Nov 19, 2023
1 parent 1c04224 commit 8b63680
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 46 deletions.
9 changes: 5 additions & 4 deletions packages/pico-engine-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/pico-engine-core/src/PicoEngineCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class PicoEngineCore {
);

this.picoFramework = new PicoFramework({
leveldown: conf.leveldown,
db: conf.db,
genID: conf.genID,
useEventInputTime: conf.useEventInputTime,

Expand Down
4 changes: 2 additions & 2 deletions packages/pico-engine-core/src/PicoEngineCoreConfiguration.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,7 +9,7 @@ export interface PicoEngineCoreConfiguration {
/**
* provide the persistence layer
*/
leveldown: AbstractLevelDOWN;
db: PicoDb;

/**
* How should rulesets be loaded in?
Expand Down
46 changes: 21 additions & 25 deletions packages/pico-engine-core/src/modules/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
);
}

Expand Down Expand Up @@ -166,7 +166,7 @@ export function initScheduleModule(pf: PicoFramework) {

async function addToSchedule(
ctx: KrlCtx,
sEvent: ScheduledEvent
sEvent: ScheduledEvent,
): Promise<ScheduledEvent> {
const schedule = (await ctx.rsCtx.getEnt("_schedule")) || {};
schedule[sEvent.id] = sEvent;
Expand Down Expand Up @@ -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),
);
}

Expand All @@ -215,7 +215,7 @@ export function initScheduleModule(pf: PicoFramework) {
time: 0,
},
});
}
},
),

repeat: krl.Postlude(
Expand All @@ -233,7 +233,7 @@ export function initScheduleModule(pf: PicoFramework) {
time: 0,
},
});
}
},
),

list: krl.Function([], async function () {
Expand All @@ -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);
}),
Expand All @@ -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);
}
}
}
},
};
}
10 changes: 4 additions & 6 deletions packages/pico-engine-core/test/flush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions packages/pico-engine-core/test/helpers/makeCoreAndKrlCtx.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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;
krlCtx: KrlCtx;
}> {
let krlCtx: any;
let core = new PicoEngineCore({
leveldown: memdown(),
db: mkdb(),
rsRegLoader: RulesetRegistryLoaderTesting({
getKrlCtx: {
rid: "getKrlCtx",
Expand Down
11 changes: 11 additions & 0 deletions packages/pico-engine-core/test/helpers/mkdb.ts
Original file line number Diff line number Diff line change
@@ -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<PicoDbKey, any>({
keyEncoding: charwise,
valueEncoding: safeJsonCodec,
});
}
11 changes: 5 additions & 6 deletions packages/pico-engine-core/test/use-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } = {
Expand All @@ -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) {
Expand All @@ -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" }));
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 8b63680

Please sign in to comment.