Skip to content

Commit

Permalink
refactor: improve types of signals
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca committed Nov 2, 2023
1 parent 25aadc0 commit 8943232
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
54 changes: 27 additions & 27 deletions src/utils/brisa-element/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ describe("utils", () => {
});

it("should work with empty nodes", () => {
function EmptyNodes({ }, { h }: any) {
function EmptyNodes({}, { h }: any) {
return h("div", {}, ["span", {}, ""]);
}

Expand Down Expand Up @@ -368,7 +368,7 @@ describe("utils", () => {
});

it("should render a timer component", () => {
function Timer({ }, { state, h }: any) {
function Timer({}, { state, h }: any) {
const time = state(0);
const interval = setInterval(() => {
time.value++;
Expand Down Expand Up @@ -419,7 +419,7 @@ describe("utils", () => {
"test-button",
brisaElement(Button as any, ["onAfterClick"]),
);
const onAfterClickMock = mock(() => { });
const onAfterClickMock = mock(() => {});

window.onAfterClick = onAfterClickMock;
document.body.innerHTML = `
Expand All @@ -437,9 +437,9 @@ describe("utils", () => {
});

it("should trigger events in different web-components", () => {
const onClickMock = mock(() => { });
const onClickMock = mock(() => {});

function Parent({ }, { h }: any) {
function Parent({}, { h }: any) {
return h("first-component", { onClickMe: onClickMock }, "click me");
}

Expand Down Expand Up @@ -574,7 +574,7 @@ describe("utils", () => {
});

it("should work an interactive TodoList with state", () => {
function TodoList({ }, { state, h }: any) {
function TodoList({}, { state, h }: any) {
const todos = state(["todo 1", "todo 2", "todo 3"]);
const newTodo = state("");
const addTodo = () => {
Expand Down Expand Up @@ -629,7 +629,7 @@ describe("utils", () => {
});

it("should be possible to change an static src attribute using the onerror event from img", () => {
function Image({ }, { h }: any) {
function Image({}, { h }: any) {
return h(
"img",
{
Expand Down Expand Up @@ -660,7 +660,7 @@ describe("utils", () => {
});

it("should be possible to change a dynamic src attribute using the onerror event from img", () => {
function Image({ }, { state, h }: any) {
function Image({}, { state, h }: any) {
const src = state("https://test.com/image.png");

return h(
Expand Down Expand Up @@ -693,10 +693,10 @@ describe("utils", () => {
});

it("should unregister effects when the component is disconnected", () => {
const mockEffect = mock((n: number) => { });
const mockEffect = mock((n: number) => {});
let interval: any;

function Test({ }, { state, effect, h }: any) {
function Test({}, { state, effect, h }: any) {
const count = state(0);

interval = setInterval(() => {
Expand Down Expand Up @@ -779,7 +779,7 @@ describe("utils", () => {
});

it("should work an async web-component", async () => {
async function AsyncComponent({ }, { state, h }: any) {
async function AsyncComponent({}, { state, h }: any) {
const count = state(await Promise.resolve(42));

return h("div", {}, () => count.value);
Expand All @@ -798,7 +798,7 @@ describe("utils", () => {
});

it("should work an async effect inside a web-component", async () => {
async function AsyncComponent({ }, { state, effect, h }: any) {
async function AsyncComponent({}, { state, effect, h }: any) {
const count = state(0);

effect(async () => {
Expand All @@ -822,10 +822,10 @@ describe("utils", () => {
});

it("should cleanup everytime an effect is re-called", () => {
const mockEffect = mock((num: number) => { });
const mockCleanup = mock(() => { });
const mockEffect = mock((num: number) => {});
const mockCleanup = mock(() => {});

function Test({ }, { state, effect, cleanup, h }: any) {
function Test({}, { state, effect, cleanup, h }: any) {
const count = state(0);

effect(() => {
Expand Down Expand Up @@ -863,10 +863,10 @@ describe("utils", () => {
});

it("should cleanup everytime the web-component is unmount", () => {
const mockEffect = mock(() => { });
const mockCleanup = mock(() => { });
const mockEffect = mock(() => {});
const mockCleanup = mock(() => {});

function Test({ }, { effect, cleanup, h }: any) {
function Test({}, { effect, cleanup, h }: any) {
effect(() => {
mockEffect();
cleanup(() => mockCleanup());
Expand All @@ -892,10 +892,10 @@ describe("utils", () => {
});

it("should cleanup async cleanups when the web-component is unmount", async () => {
const mockEffect = mock(() => { });
const mockCleanup = mock(() => { });
const mockEffect = mock(() => {});
const mockCleanup = mock(() => {});

function Test({ }, { effect, cleanup, h }: any) {
function Test({}, { effect, cleanup, h }: any) {
effect(async () => {
mockEffect();
cleanup(async () => mockCleanup());
Expand All @@ -921,10 +921,10 @@ describe("utils", () => {
});

it("should cleanup multi cleanups inside an effect when the web-component is unmount", async () => {
const mockEffect = mock(() => { });
const mockCleanup = mock(() => { });
const mockEffect = mock(() => {});
const mockCleanup = mock(() => {});

function Test({ }, { effect, cleanup, h }: any) {
function Test({}, { effect, cleanup, h }: any) {
effect(async () => {
mockEffect();
cleanup(async () => mockCleanup());
Expand All @@ -950,7 +950,7 @@ describe("utils", () => {
expect(mockCleanup).toHaveBeenCalledTimes(2);
});

it("should work with reactivity props in a SVG component", () => {
it.todo("should work with reactivity props in a SVG component", () => {
function ColorSVG(
{ firstColor, secondColor, thirdColor }: any,
{ h }: any,
Expand Down Expand Up @@ -1034,12 +1034,12 @@ describe("utils", () => {

it.todo(
"should SVG work with foreingObject setting correctly the namespace outside the foreingObject node",
() => { },
() => {},
);

it.todo(
"should reactively update the DOM after adding a new property to the web-component",
() => { },
() => {},
);
});
});
13 changes: 7 additions & 6 deletions src/utils/signals/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
type Fn = () => void | Promise<() => void>;
type Effect = () => void | Promise<() => void>;
type Cleanup = Effect;

export default function signals() {
const cleanups = new Map<Fn, Fn[]>();
let current: (() => void) | 0 = 0;
let cleanups = new Map<Effect, Cleanup[]>();
let current: Effect | 0 = 0;

return {
cleanAll() {
Expand All @@ -12,7 +13,7 @@ export default function signals() {
}
},
state<T>(initialValue: T): { value: T } {
const effects = new Set<() => void>();
const effects = new Set<Effect>();
return {
get value() {
if (current) effects.add(current);
Expand All @@ -29,12 +30,12 @@ export default function signals() {
},
};
},
effect(fn: () => void) {
effect(fn: Effect) {
current = fn;
fn();
current = 0;
},
cleanup(fn: () => void) {
cleanup(fn: Cleanup) {
const cleans = current ? cleanups.get(current) ?? [] : [];
cleans.push(fn);
if (current) cleanups.set(current, cleans);
Expand Down

0 comments on commit 8943232

Please sign in to comment.