Skip to content

Commit

Permalink
Format runtime source files.
Browse files Browse the repository at this point in the history
  • Loading branch information
gdotdesign committed Nov 17, 2024
1 parent 942c4b6 commit 21b5e52
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 59 deletions.
42 changes: 20 additions & 22 deletions runtime/src/debug.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import indentString from "indent-string";
import { isVnode } from './equality';
import { Variant } from './variant';
import { Name } from './symbols';
import { isVnode } from "./equality";
import { Variant } from "./variant";
import { Name } from "./symbols";

const render = (items, prefix, suffix, fn) => {
items =
items.map(fn);
items = items.map(fn);

const newLines =
items.size > 3 || items.filter((item) => item.indexOf("\n") > 0).length;

const joined =
items.join(newLines ? ",\n" : ", ");
const joined = items.join(newLines ? ",\n" : ", ");

if (newLines) {
return `${prefix.trim()}\n${indentString(joined, 2)}\n${suffix.trim()}`;
} else {
return `${prefix}${joined}${suffix}`;
}
}
};

const toString = (object) => {
if (object.type === "null") {
Expand All @@ -32,7 +30,7 @@ const toString = (object) => {
} else if (object.type === "boolean") {
return `${object.value}`;
} else if (object.type === "element") {
return `<${object.value.toLowerCase()}>`
return `<${object.value.toLowerCase()}>`;
} else if (object.type === "variant") {
if (object.items) {
return render(object.items, `${object.value}(`, `)`, toString);
Expand All @@ -54,7 +52,7 @@ const toString = (object) => {
} else if (object.value) {
return toString(object.value);
}
}
};

const objectify = (value) => {
if (value === null) {
Expand All @@ -76,19 +74,19 @@ const objectify = (value) => {
for (const key in value) {
if (key === "length" || key === "record" || key.startsWith("_")) {
continue;
};
}

items.push({
value: objectify(value[key]),
key: key
key: key,
});
}
} else {
for (let i = 0; i < value.length; i++) {
items.push({
value: objectify(value[`_${i}`])
value: objectify(value[`_${i}`]),
});
};
}
}

if (items.length) {
Expand All @@ -99,30 +97,30 @@ const objectify = (value) => {
} else if (Array.isArray(value)) {
return {
items: value.map((item) => ({ value: objectify(item) })),
type: "array"
type: "array",
};
} else if (isVnode(value)) {
return { type: "vnode" }
return { type: "vnode" };
} else if (typeof value == "object") {
const items = [];

for (const key in value) {
items.push({
value: objectify(value[key]),
key: key
key: key,
});
};
}

if (Name in value) {
return { type: "record", value: value[Name], items: items };
return { type: "record", value: value[Name], items: items };
} else {
return { type: "object", items: items };
}
} else {
return { type: "unknown", value: value.toString() };
}
}
};

export const inspect = (value) => {
return toString(objectify(value))
}
return toString(objectify(value));
};
2 changes: 1 addition & 1 deletion runtime/src/decoders.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ export const decodeMap = (decoder, ok, err) => (input) => {

// Decodes a record, using the mappings.
export const decoder = (name, mappings, ok, err) => (input) => {
const object = {[Name]: name};
const object = { [Name]: name };

for (let key in mappings) {
let decoder = mappings[key];
Expand Down
8 changes: 4 additions & 4 deletions runtime/src/equality.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// This file contains code to have value equality instead of reference equality.
// We use a `Symbol` to have a custom equality functions and then use these
// functions when comparing two values.
import { Equals } from './symbols';
import { Equals } from "./symbols";

/* v8 ignore next 3 */
if (typeof Node === "undefined") {
self.Node = class {}
self.Node = class {};
}

// We use regular functions instead of arrow functions because they have
Expand Down Expand Up @@ -132,7 +132,7 @@ export const isVnode = (object) =>
"type" in object &&
"ref" in object &&
"key" in object &&
"__" in object
"__" in object;

// This is the custom comparison function.
export const compare = (a, b) => {
Expand All @@ -143,7 +143,7 @@ export const compare = (a, b) => {
} else if (b != null && b != undefined && b[Equals]) {
return b[Equals](a);
} else if (isVnode(a) || isVnode(b)) {
return a === b
return a === b;
} else {
return compareObjects(a, b);
}
Expand Down
4 changes: 1 addition & 3 deletions runtime/src/pattern_matching.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ export const destructure = (value, pattern, values = []) => {
} else if (pattern instanceof Pattern) {
if (value instanceof pattern.variant) {
for (let index in pattern.pattern) {
if (
!destructure(value[`_${index}`], pattern.pattern[index], values)
) {
if (!destructure(value[`_${index}`], pattern.pattern[index], values)) {
return false;
}
}
Expand Down
6 changes: 4 additions & 2 deletions runtime/src/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ class Program {
!equals(routeInfo.vars, this.routeInfo.vars)
) {
const handler = this.runRouteHandler(routeInfo);
if (routeInfo.route.await) { await handler }
if (routeInfo.route.await) {
await handler;
}
}

this.resolvePagePosition(!!event?.triggerJump);
Expand Down Expand Up @@ -198,7 +200,7 @@ class Program {

let mainNode;
if (typeof main !== "undefined") {
mainNode = h(main, { key: "MINT_MAIN" })
mainNode = h(main, { key: "MINT_MAIN" });
}

render([...components, mainNode], this.root);
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/symbols.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const Equals = Symbol("Equals");
export const Name = Symbol('Name');
export const Name = Symbol("Name");
27 changes: 16 additions & 11 deletions runtime/src/utilities.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { createRef as createRefOriginal, Component, createElement } from "preact";
import { useEffect, useRef, useMemo } from "preact/hooks";
import { signal } from "@preact/signals";

import {
createRef as createRefOriginal,
createElement,
Component,
} from "preact";

import { compare } from "./equality";
import { Name } from "./symbols";

// This finds the first element matching the key in a map ([[key, value]]).
export const mapAccess = (map, key, just, nothing) => {
for (const item of map) {
if (compare(item[0], key)) {
return new just(item[1])
return new just(item[1]);
}
}

return new nothing();
}
};

// We need to have a different function for accessing array items because there
// is no concept of `null` in Mint so we return `Just(a)` or `Nothing`.
Expand Down Expand Up @@ -89,29 +94,29 @@ export const access = (field) => (value) => value[field];
export const identity = (a) => a;

// Creates an instrumented object so we know which record it belongs to.
export const record = (name) => (value) => ({ [Name]: name, ...value})
export const record = (name) => (value) => ({ [Name]: name, ...value });

// A component to lazy load another component.
export class lazyComponent extends Component {
async componentDidMount() {
let x = await this.props.x();
this.setState({ x: x })
this.setState({ x: x });
}

render() {
if (this.state.x) {
return createElement(this.state.x, this.props.p, this.props.c)
return createElement(this.state.x, this.props.p, this.props.c);
} else {
return null
return null;
}
}
}

// A higher order function to lazy load a module.
export const lazy = (path) => async () => load(path)
export const lazy = (path) => async () => load(path);

// Loads load a module.
export const load = async (path) => {
const x = await import(path)
return x.default
}
const x = await import(path);
return x.default;
};
2 changes: 1 addition & 1 deletion runtime/src/variant.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const variant = (input, name) => {
return class extends Variant {
constructor(...args) {
super();
this[Name] = name
this[Name] = name;
if (Array.isArray(input)) {
this.length = input.length;
this.record = true;
Expand Down
24 changes: 16 additions & 8 deletions runtime/tests/debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ test("inspecting boolean", () => {
});

test("inspecting boolean", () => {
expect(inspect({props: {}, type: {}, ref: {}, key: {},"__": {}})).toBe(`VNode`);
expect(inspect({ props: {}, type: {}, ref: {}, key: {}, __: {} })).toBe(
`VNode`,
);
});

test("inspecting object", () => {
Expand All @@ -38,23 +40,27 @@ test("inspecting element", () => {
});

test("inspecting variant", () => {
const Test = variant(0, `Test`)
const Test = variant(0, `Test`);
expect(inspect(newVariant(Test)())).toBe(`Test`);
});

test("inspecting variant (with parameters)", () => {
const Test = variant(1, `Test`)
const Test = variant(1, `Test`);
expect(inspect(newVariant(Test)("Hello"))).toBe(`Test("Hello")`);
});

test("inspecting variant (with named parameters)", () => {
const Test = variant(["a", "b"], `Test`)
expect(inspect(newVariant(Test)("Hello", "World!"))).toBe(`Test(a: "Hello", b: "World!")`);
const Test = variant(["a", "b"], `Test`);
expect(inspect(newVariant(Test)("Hello", "World!"))).toBe(
`Test(a: "Hello", b: "World!")`,
);
});

test("inspecting record", () => {
const Test = record(`Test`)
expect(inspect(Test({ a: "Hello", b: "World!"}))).toBe(`Test { a: "Hello", b: "World!" }`);
const Test = record(`Test`);
expect(inspect(Test({ a: "Hello", b: "World!" }))).toBe(
`Test { a: "Hello", b: "World!" }`,
);
});

test("inspecting array", () => {
Expand All @@ -66,7 +72,9 @@ test("inspecting unkown", () => {
});

test("inspecting nested", () => {
expect(inspect({ a: "Hello", b: "World!", nested: { x: "With new line!\nYes!"}})).toBe(`{
expect(
inspect({ a: "Hello", b: "World!", nested: { x: "With new line!\nYes!" } }),
).toBe(`{
a: "Hello",
b: "World!",
nested: {
Expand Down
12 changes: 7 additions & 5 deletions runtime/tests/equality.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ test("comparing same symbols", () => {
});

test("comparing vnodes", () => {
expect(compare(
{props: {}, type: {}, ref: {}, key: {},"__": {}},
{props: {}, type: {}, ref: {}, key: {},"__": {}}
)).toBe(false);
})
expect(
compare(
{ props: {}, type: {}, ref: {}, key: {}, __: {} },
{ props: {}, type: {}, ref: {}, key: {}, __: {} },
),
).toBe(false);
});

test("comparing same arrays", () => {
expect(["A"] == ["A"]).toBe(false);
Expand Down
2 changes: 1 addition & 1 deletion runtime/tests/utilities.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe("or", () => {
});

test("it returns the given item", () => {
expect(or(Nothing, Err, new Nothing, "b")).toEqual("b");
expect(or(Nothing, Err, new Nothing(), "b")).toEqual("b");
});
});

Expand Down

0 comments on commit 21b5e52

Please sign in to comment.