Skip to content

Commit

Permalink
refactor(core): Move state loaders parts upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
oliversalzburg committed Nov 21, 2023
1 parent e30ddcc commit 8885453
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 3,285 deletions.
3,177 changes: 0 additions & 3,177 deletions load-profile.result.json

This file was deleted.

2 changes: 1 addition & 1 deletion packages/kitten-scientists/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"types": "./build/index.ts",
"dependencies": {
"@oliversalzburg/js-utils": "0.0.21",
"@oliversalzburg/js-utils": "0.0.22",
"date-fns": "2.30.0",
"semver": "7.5.4",
"tslib": "2.6.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { difference } from "@oliversalzburg/js-utils/array.js";
import { difference } from "@oliversalzburg/js-utils/data/array.js";
import { Maybe, isNil } from "@oliversalzburg/js-utils/nil.js";
import { consumeEntriesPedantic } from "../tools/Entries.js";
import { cwarn } from "../tools/Log.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { difference } from "@oliversalzburg/js-utils/array.js";
import { difference } from "@oliversalzburg/js-utils/data/array.js";
import { Maybe, isNil } from "@oliversalzburg/js-utils/nil.js";
import { consumeEntriesPedantic } from "../tools/Entries.js";
import { cwarn } from "../tools/Log.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/kitten-scientists/source/settings/TechSettings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { difference } from "@oliversalzburg/js-utils/array.js";
import { difference } from "@oliversalzburg/js-utils/data/array.js";
import { Maybe, isNil } from "@oliversalzburg/js-utils/nil.js";
import { consumeEntriesPedantic } from "../tools/Entries.js";
import { cwarn } from "../tools/Log.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { difference } from "@oliversalzburg/js-utils/array.js";
import { difference } from "@oliversalzburg/js-utils/data/array.js";
import { Maybe, isNil } from "@oliversalzburg/js-utils/nil.js";
import { consumeEntriesPedantic } from "../tools/Entries.js";
import { cwarn } from "../tools/Log.js";
Expand Down
7 changes: 3 additions & 4 deletions packages/kitten-scientists/source/state/State.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { TreeNode } from "@oliversalzburg/js-utils";
import { UserScript } from "../UserScript.js";
import { StateLoader } from "./StateLoader.js";
import { StateMerger } from "./StateMerger.js";

export class State {
export class State extends TreeNode<State> {
readonly originUrl: string;
readonly parent: State | undefined;
readonly children = new Array<State>();

readonly loader: StateLoader;

Expand All @@ -14,8 +13,8 @@ export class State {
}

constructor(originUrl: string, parent?: State) {
super(parent);
this.originUrl = originUrl;
this.parent = parent;
this.loader = new StateLoader(this);
}

Expand Down
113 changes: 21 additions & 92 deletions packages/kitten-scientists/source/state/StateLoader.ts
Original file line number Diff line number Diff line change
@@ -1,93 +1,23 @@
import { retry } from "@oliversalzburg/js-utils";
import { TreeNode, retry } from "@oliversalzburg/js-utils";
import { errorToRecord, unknownToError } from "@oliversalzburg/js-utils/error-serializer.js";
import { Report } from "@oliversalzburg/js-utils/log/report.js";
import { mustExist } from "@oliversalzburg/js-utils/nil.js";
import { State } from "./State.js";

export interface ResolverStateView {
extends?: Array<string>;
}
export interface ReportEntry {
message: string;
context: Record<string, unknown> | undefined;
}

const indent = (text: string, depth = 0) => text.replaceAll(/^/gm, " ".repeat(depth));

export class LoaderReport {
readonly origin: string;
#store = new Map<string, Array<ReportEntry>>();
#parent: LoaderReport | undefined;
readonly children = new Array<LoaderReport>();

constructor(origin: string) {
this.origin = origin;
}

#getStoreEntry(entry: string) {
if (!this.#store.has(entry)) {
this.#store.set(entry, new Array<ReportEntry>());
}
return this.#store.get(entry);
}

info(message: string, context?: Record<string, unknown>) {
this.#getStoreEntry(this.origin)?.push({ message, context });
}
request(message: string, context?: Record<string, unknown>) {
this.#getStoreEntry(this.origin)?.push({ message, context });
}
failure(message: string, context?: Record<string, unknown>) {
this.#getStoreEntry(this.origin)?.push({ message, context });
}
success(message: string, context?: Record<string, unknown>) {
this.#getStoreEntry(this.origin)?.push({ message, context });
}

aggregateLog(depth = 0) {
for (const [profileId, records] of this.#store.entries()) {
console.log(indent(`PROFILE: ${profileId}`, depth));
for (const entry of records) {
console.log(indent(` - ${entry.message}`, depth));
if (entry.context) {
console.log(indent(JSON.stringify(entry.context, undefined, depth + 4), depth));
}
}
}

if (this.children.length === 0) {
return;
}

console.log(indent(` > Child loader reports:`, depth));
for (const child of this.children) {
child.aggregateLog(depth + 1);
}
}

child(childOrigin: string) {
const childReport = new LoaderReport(childOrigin);
childReport.#parent = this;
this.children.push(childReport);
return childReport;
}
}

export class StateLoader {
export class StateLoader extends TreeNode<StateLoader> {
readonly cache = new Map<string, StateLoader>();
readonly report: LoaderReport;
readonly report: Report;
readonly state: State;

#children = new Array<StateLoader>();
#parent: State | undefined;

#request: Promise<Response> | undefined;
#response: Response | undefined;
#dataParsed: Promise<Record<string, unknown>> | undefined;
#data: Record<string, unknown> | undefined;

get children() {
return this.#children;
}
get data() {
return this.#data;
}
Expand All @@ -99,36 +29,36 @@ export class StateLoader {
}

constructor(state: State) {
super(state.parent?.loader);

this.state = state;
this.#parent = state.parent?.loader?.state;
this.report = state.parent
? mustExist(state.parent.loader).report.child(state.originUrl)
: new LoaderReport(state.originUrl);
? mustExist(state.parent.loader).report.child(new Report(state.originUrl))
: new Report(state.originUrl);
}

async load(cache = this.cache): Promise<LoaderReport> {
async load(cache = this.cache): Promise<Report> {
const entry = performance.now();

let data;
let timeHttp = 0;
try {
const cached = cache.get(this.state.originUrl);
if (cached) {
this.report.success(`✅ Using existing request from cache.`);
this.report.log(`✅ Using existing request from cache.`);
} else {
this.report.request("🔍 Resolving document from URL...");
this.report.log("🔍 Resolving document from URL...");
}

const start = performance.now();
const requestExecutor = async () => {
const request = cached?.request ?? fetch(this.state.originUrl);
this.#request = request;
this.#request = cached?.request ?? fetch(this.state.originUrl);

if (!cached) {
cache.set(this.state.originUrl, this);
}

this.#response = cached?.response ?? (await request);
this.#response = cached?.response ?? (await this.#request);

this.#dataParsed =
cached && cached.#dataParsed ? cached.#dataParsed : this.#response.json();
Expand All @@ -140,7 +70,7 @@ export class StateLoader {

timeHttp = end - start;
} catch (error) {
this.report.failure(
this.report.log(
`😑 Error while resolving ${this.state.originUrl}!`,
errorToRecord(unknownToError(error)),
);
Expand All @@ -151,14 +81,14 @@ export class StateLoader {
this.#data = data;
const bases = await this.#resolveBases(this.#data, cache);
if (bases.length === 0) {
this.report.info("🍁 Profile is a leaf node and should be a baseline!");
this.report.log("🍁 Profile is a leaf node and should be a baseline!");
} else {
this.report.info("🌳 Profile is a tree node.");
this.report.log("🌳 Profile is a tree node.");
}

const exit = performance.now();
const timeSelf = exit - entry - timeHttp;
this.report.success(
this.report.log(
`😊 Profile resolved successfully. (req:${Math.round(timeHttp)}ms, self:${Math.round(
timeSelf,
)}ms)`,
Expand All @@ -171,14 +101,13 @@ export class StateLoader {
return Promise.resolve([]);
}

this.report.request("🧶 Resolving bases...");
this.report.log("🧶 Resolving bases...");

for (const base of state.extends) {
const baseState = new State(base, this.state);
this.state.children.push(baseState);
this.#children.push(baseState.loader);
const baseState = this.state.child(new State(base, this.state));
this.child(baseState.loader);
}

return Promise.all(this.#children.map(loader => loader.load(cache)));
return Promise.all([...this.children.values()].map(loader => loader.load(cache)));
}
}
2 changes: 1 addition & 1 deletion packages/kitten-scientists/source/state/StateMerger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class StateMerger {

for (const child of this.state.children) {
let childState = mustExist(child.state);
if (child.children.length !== 0) {
if (child.children.size !== 0) {
childState = new StateMerger(child).merge(stateSubject);
}

Expand Down
2 changes: 1 addition & 1 deletion schemas/.scripts/load-profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const state = new State(
);

const profile = await state.resolve();
profile.report.aggregateLog();
profile.report.aggregate(console);

const engineState = state.merge();
writeFileSync("load-profile.result.json", JSON.stringify(engineState, undefined, 2));
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ __metadata:
resolution: "@kitten-science/kitten-scientists@workspace:packages/kitten-scientists"
dependencies:
"@kitten-science/action-release-info": "workspace:*"
"@oliversalzburg/js-utils": "npm:0.0.21"
"@oliversalzburg/js-utils": "npm:0.0.22"
"@types/dojo": "npm:1.9.48"
"@types/jquery": "npm:3.5.28"
"@types/semver": "npm:7.5.6"
Expand Down Expand Up @@ -1079,10 +1079,10 @@ __metadata:
languageName: node
linkType: hard

"@oliversalzburg/js-utils@npm:0.0.21":
version: 0.0.21
resolution: "@oliversalzburg/js-utils@npm:0.0.21"
checksum: 41ef3b2a19c07cbd29b5d7d6dbe1de78c47b5f915a0e6907c16e8cd5234c4d1a6ed9fe3cad3cfc4d804872bcf26e097274750575fface9caf909bda0490852f0
"@oliversalzburg/js-utils@npm:0.0.22":
version: 0.0.22
resolution: "@oliversalzburg/js-utils@npm:0.0.22"
checksum: ffad84e9532de3bea6bf8308270f5da8a623e8d8e4e67745d18a3f71b9ff75df14009bba4b895ed99d0a3be1f401a191cfc1539440a0c4432485710df8baca46
languageName: node
linkType: hard

Expand Down

0 comments on commit 8885453

Please sign in to comment.