Skip to content

Commit

Permalink
Add liftNow and performComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
paldepind committed Feb 12, 2019
1 parent e1897b2 commit 197aaa0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
22 changes: 21 additions & 1 deletion src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export abstract class Component<O, A> implements Monad<A> {
}
// return new OutputComponent(remaps, this);
}
// explicitOutput: string[] | undefined;
static multi: boolean = false;
multi: boolean = false;
abstract run(
Expand All @@ -103,6 +102,27 @@ class OfComponent<A> extends Component<{}, A> {
}
}

class PerformComponent<A> extends Component<{}, A> {
constructor(private cb: () => A) {
super();
}
run(_1: Node, _2: Future<boolean>): { explicit: {}; output: A } {
return { explicit: {}, output: this.cb() };
}
}

/**
* Takes a callback, potentially with side-effects. The callback is invoked when
* the component is run and the return value becomes the components output.
*/
export function performComponent<A>(callback: () => A): Component<{}, A> {
return new PerformComponent(callback);
}

export function liftNow<A>(now: Now<A>): Component<{}, A> {
return performComponent(() => runNow(now));
}

class OutputComponent extends Component<any, any> {
constructor(
private remaps: Record<string, string>,
Expand Down
31 changes: 25 additions & 6 deletions test/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,35 @@ import {
list,
runComponent,
output,
merge
merge,
performComponent,
liftNow
} from "../src";
const { span, div, button, input } = elements;

const supportsProxy = "Proxy" in window;

describe("component specs", () => {
describe("performComponent", () => {
it("invokes callback", () => {
let result: number | undefined = undefined;
const c = performComponent(() => (result = 12));
assert.strictEqual(result, undefined);
const { out: actual } = testComponent(c);
assert.strictEqual(result, 12);
assert.strictEqual(actual, 12);
});
});
describe("liftNow", () => {
it("runs now", () => {
let result: number | undefined = undefined;
const c = liftNow(H.perform(() => (result = 12)));
assert.strictEqual(result, undefined);
const { out: actual } = testComponent(c);
assert.strictEqual(result, 12);
assert.strictEqual(actual, 12);
});
});
describe("toComponent", () => {
it("converts a behavior of a string to a component", () => {
const b = H.sinkBehavior("Hello");
Expand All @@ -36,7 +58,7 @@ describe("component specs", () => {
expect(dom).to.have.text("world");
});
it("converts a behavior of a boolean to a component", () => {
const b = H.sinkBehavior(true)
const b = H.sinkBehavior(true);
const component = toComponent(b);
const { dom } = testComponent(component);
expect(dom).to.have.text("true");
Expand Down Expand Up @@ -279,10 +301,7 @@ describe("modelView", () => {
fromView = args;
return H.Now.of({});
},
(): Child<FromView> => [
span("Hello"),
input().output({ value: "value" })
]
(): Child<FromView> => [span("Hello"), input().output({ value: "value" })]
)();
const { dom } = testComponent(c);
expect(dom.querySelector("span")).to.exist;
Expand Down

0 comments on commit 197aaa0

Please sign in to comment.