Skip to content

Commit

Permalink
Use special equality of component functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
gdotdesign committed Jan 19, 2024
1 parent aa7dfec commit d13b3ce
Show file tree
Hide file tree
Showing 57 changed files with 400 additions and 281 deletions.
10 changes: 1 addition & 9 deletions runtime/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
export { useComputed, computed, signal, effect, batch } from "@preact/signals";
export { createElement, Fragment as fragment } from "preact";
export { useEffect, useMemo, useRef } from "preact/hooks";
export { createPortal } from "preact/compat";

export {
useComputed,
useSignal,
computed,
signal,
effect,
batch,
} from "@preact/signals";

export * from "./src/pattern_matching";
export * from "./src/normalize_event";
export * from "./src/utilities";
Expand Down
31 changes: 31 additions & 0 deletions runtime/src/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Component } from "preact";
import { bindFunctions, setConstants } from "./Utils";

export default const component = (...args) {
let render;

switch (args.length) {
case: 1
render = args[0]
break;
}


return class extends Component {
componentDidUpdate() {

}

componentDidMount() {

}

componentWillUnmount() {

}

render() {
return render();
}
}
}
7 changes: 6 additions & 1 deletion runtime/src/equality.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// We use a `Symbol` to have a custom equality functions and then use these
// functions when comparing two values.
export const Equals = Symbol("Equals");
export const Id = Symbol("Id");

Boolean.prototype[Equals] =
Symbol.prototype[Equals] =
Expand All @@ -16,7 +17,11 @@ Date.prototype[Equals] = function (other) {
};

Function.prototype[Equals] = Node.prototype[Equals] = function (other) {
return this === other;
if (this[Id]) {
return this[Id] === other[Id];
} else {
return this === other;
}
};

// Search parameters need to be the same string to be equal.
Expand Down
4 changes: 3 additions & 1 deletion runtime/src/normalize_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ if (!("DataTransfer" in window)) {
export const normalizeEvent = (event) => {
return new Proxy(event, {
get: function (obj, prop) {
if (prop in obj) {
if (prop === "event") {
return event
} else if (prop in obj) {
const value = obj[prop];

if (value instanceof Function) {
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class Program {
elem =
this.root.querySelector(hash) || // ID
this.root.querySelector(`a[name="${hash.slice(1)}"]`); // Anchor
} finally {
} catch {
}

if (elem) {
Expand Down
10 changes: 6 additions & 4 deletions runtime/src/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ export const createProvider = (subscriptions, update) => {

// This runs on every update so we don't return a cleanup function.
useEffect(() => {
const data = object();

// If the object is null that means we need to unsubscribe.
if (object === null) {
if (data === null) {
unsubscribe();
} else {
const current = subscriptions.get(owner);

if (!compare(current, object)) {
subscriptions.set(owner, object);
if (!compare(current, data)) {
subscriptions.set(owner, data);
untracked(update);
}
}
Expand All @@ -42,4 +44,4 @@ export const subscriptions = (items) => Array.from(items.values());

// Returns a unique ID for a component which doesn't change.
export const useId = () => useMemo(uuid, []);

export {uuid};
25 changes: 24 additions & 1 deletion runtime/src/utilities.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { useEffect, useRef, useCallback } from "preact/hooks";
import { useEffect, useRef, useCallback, useMemo } from "preact/hooks";
import { createRef as createRefOriginal } from "preact";
import { signal } from "@preact/signals";

import { Id } from "./equality";

// 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 All @@ -23,6 +27,20 @@ export const setRef = (value, just) => (element) => {
}
}

// A version of useSignal which subscribes to the signal by default (like a
// state) since we want to re-render every time the signal changes.
export const useSignal = (value) => {
const sig = useMemo(() => signal(value), [])
sig.value;
return sig
}

export const createRef = (value) => {
const ref = createRefOriginal()
ref.current = value
return ref
}

// A hook to replace the `componentDidUpdate` function.
export const useDidUpdate = (callback) => {
const hasMount = useRef(false);
Expand Down Expand Up @@ -60,3 +78,8 @@ export const access = (field) => (value) => value[field];

// Identity function used in encoders.
export const identity = (a) => a;

export const define = (id, method) => {
method[Id] = id
return method
}
35 changes: 35 additions & 0 deletions spec/compilers2/access_deep
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
type Locale {
level1: Level1
}

type Level1 {
level2: Level2
}

type Level2 {
name : String
}

store Settings {
state locale : Locale = { level1: { level2: { name: "Test" }} }
}

component Main {
fun render : String {
Settings.locale.level1.level2.name
}
}
--------------------------------------------------------------------------------
import { signal as A } from "runtime";

const
a = A({
level1: {
level2: {
name: `Test`
}
}
}),
B = () => {
return a.value.level1.level2.name
};
4 changes: 2 additions & 2 deletions spec/compilers2/argument
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ component Main {
}
}
--------------------------------------------------------------------------------
import { useFunction as B } from "runtime";
import { define as B } from "runtime";

const A = () => {
const a = B((b, c) => {
const a = B('a', (b, c) => {
return c
});
a(``, 0);
Expand Down
4 changes: 2 additions & 2 deletions spec/compilers2/argument_with_default
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ component Main {
}
}
--------------------------------------------------------------------------------
import { useFunction as B } from "runtime";
import { define as B } from "runtime";

const A = () => {
const a = B((b, c = 0) => {
const a = B('a', (b, c = 0) => {
return c
});
a(``);
Expand Down
6 changes: 3 additions & 3 deletions spec/compilers2/block_with_await
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ component Main {
}
}
--------------------------------------------------------------------------------
import { useFunction as B } from "runtime";
import { define as B } from "runtime";

const A = () => {
const
a = B(() => {
a = B('a', () => {
return undefined
}),
b = B(async () => {
b = B('b', async () => {
return await a()
});
b();
Expand Down
4 changes: 2 additions & 2 deletions spec/compilers2/call_labelled
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ component Main {
}
}
--------------------------------------------------------------------------------
import { useFunction as B } from "runtime";
import { define as B } from "runtime";

const A = () => {
const a = B((b, c) => {
const a = B('a', (b, c) => {
return ``
});
return a(``, 0)
Expand Down
4 changes: 2 additions & 2 deletions spec/compilers2/component_instance_access
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ component Main {
import {
patternVariable as M,
createElement as G,
useFunction as J,
useComputed as E,
pattern as L,
useMemo as F,
variant as B,
setRef as N,
define as J,
useRef as I,
match as K
} from "runtime";
Expand All @@ -61,7 +61,7 @@ const
H = () => {
const
c = I(new C()),
d = J(() => {
d = J('d', () => {
return K(c.current, [
[
L(A, [M]),
Expand Down
18 changes: 10 additions & 8 deletions spec/compilers2/component_with_provider
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ const
}),
C = () => {
const b = D();
A(b, (false ? {
moves: (c) => {
return null
},
ups: (d) => {
return null
}
} : null));
A(b, () => {
return (false ? {
moves: (c) => {
return null
},
ups: (d) => {
return null
}
} : null)
});
return E(`div`, {})
};

18 changes: 10 additions & 8 deletions spec/compilers2/component_with_provider_and_lifecycle_functions
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ const
F(() => {
return null
});
A(b, (false ? {
moves: (c) => {
return null
},
ups: (d) => {
return null
}
} : null));
A(b, () => {
return (false ? {
moves: (c) => {
return null
},
ups: (d) => {
return null
}
} : null)
});
return G(`div`, {})
};
18 changes: 10 additions & 8 deletions spec/compilers2/component_with_provider_and_store
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ const
}),
D = () => {
const d = E();
B(d, (false ? {
moves: (e) => {
return null
},
ups: (f) => {
return null
}
} : null));
B(d, () => {
return (false ? {
moves: (e) => {
return null
},
ups: (f) => {
return null
}
} : null)
});
return F(`div`, {})
};
13 changes: 6 additions & 7 deletions spec/compilers2/css_definition
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,25 @@ component Main {
}
--------------------------------------------------------------------------------
import {
createElement as D,
useFunction as C,
createElement as C,
useComputed as B,
style as E
style as D
} from "runtime";

const A = () => {
const
a = B(() => {
return 10
}),
b = C(() => {
b = () => {
const _ = {
[`--a-a`]: a.value + `px 0px`
};
return _
});
return D(`div`, {
};
return C(`div`, {
className: `Main_test`,
style: E([b()])
style: D([b()])
})
};

Expand Down
Loading

0 comments on commit d13b3ce

Please sign in to comment.