Skip to content

Commit

Permalink
Rename explicit to selected
Browse files Browse the repository at this point in the history
  • Loading branch information
paldepind committed Aug 14, 2019
1 parent 99f9384 commit d152527
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 101 deletions.
78 changes: 39 additions & 39 deletions src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface DomApi {
}

export type Out<O, A> = {
explicit: O;
selected: O;
output: A;
};

Expand Down Expand Up @@ -86,7 +86,7 @@ export abstract class Component<O, A> implements Monad<A> {
abstract run(
parent: DomApi,
destroyed: Future<boolean>
): { explicit: O; output: A };
): { selected: O; output: A };
// Definitions below are inserted by Jabz
flatten: <B>() => Component<O, B>;
map: <B>(f: (a: A) => B) => Component<O, B>;
Expand All @@ -99,17 +99,17 @@ class OfComponent<A> extends Component<{}, A> {
constructor(private value: A) {
super();
}
run(_1: Node, _2: Future<boolean>): { explicit: {}; output: A } {
return { explicit: {}, output: this.value };
run(_1: Node, _2: Future<boolean>): { selected: {}; output: A } {
return { selected: {}, output: this.value };
}
}

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() };
run(_1: Node, _2: Future<boolean>): { selected: {}; output: A } {
return { selected: {}, output: this.cb() };
}
}

Expand All @@ -127,15 +127,15 @@ export function liftNow<A>(now: Now<A>): Component<{}, A> {

class HandleOutput<O, A, P, B> extends Component<P, B> {
constructor(
private readonly handler: (explicit: O, output: A) => [P, B],
private readonly handler: (selected: O, output: A) => [P, B],
private readonly c: Component<O, A>
) {
super();
}
run(parent: DomApi, destroyed: Future<boolean>): Out<P, B> {
const { explicit, output } = this.c.run(parent, destroyed);
const [newExplicit, newOutput] = this.handler(explicit, output);
return { explicit: newExplicit, output: newOutput };
const { selected, output } = this.c.run(parent, destroyed);
const [newSelected, newOutput] = this.handler(selected, output);
return { selected: newSelected, output: newOutput };
}
}

Expand Down Expand Up @@ -180,12 +180,12 @@ class FlatMapComponent<O, A, B> extends Component<O, B> {
super();
}
run(parent: DomApi, destroyed: Future<boolean>): Out<O, B> {
const { explicit, output: outputFirst } = this.component.run(
const { selected, output: outputFirst } = this.component.run(
parent,
destroyed
);
const { output } = this.f(outputFirst).run(parent, destroyed);
return { explicit, output };
return { selected, output };
}
}

Expand All @@ -211,14 +211,14 @@ export function testComponent<O, A>(
): {
out: A;
dom: HTMLDivElement;
explicit: O;
selected: O;
destroy: (toplevel: boolean) => void;
} {
const dom = document.createElement("div");
const destroyed = sinkFuture<boolean>();
const { output: out, explicit } = c.run(dom, destroyed);
const { output: out, selected } = c.run(dom, destroyed);
const destroy = destroyed.resolve.bind(destroyed);
return { out, dom, destroy, explicit };
return { out, dom, destroy, selected };
}

export function isComponent(c: any): c is Component<any, any> {
Expand Down Expand Up @@ -256,15 +256,15 @@ class LoopComponent<O> extends Component<{}, O> {
}
}
}
const { explicit, output } = toComponent(this.f(placeholderObject)).run(
const { selected, output } = toComponent(this.f(placeholderObject)).run(
parent,
destroyed
);
const returned: (keyof O)[] = <any>Object.keys(output);
for (const name of returned) {
placeholderObject[name].replaceWith(output[name]);
}
return { explicit: {}, output };
return { selected: {}, output };
}
}

Expand All @@ -286,15 +286,15 @@ class MergeComponent<
super();
}
run(parent: DomApi, destroyed: Future<boolean>): Out<O & P, O & P> {
const { explicit: explicit1 } = this.c1.run(parent, destroyed);
const { explicit: explicit2 } = this.c2.run(parent, destroyed);
const merged = Object.assign({}, explicit1, explicit2);
return { explicit: merged, output: merged };
const { selected: selected1 } = this.c1.run(parent, destroyed);
const { selected: selected2 } = this.c2.run(parent, destroyed);
const merged = Object.assign({}, selected1, selected2);
return { selected: merged, output: merged };
}
}

/**
* Merges two components. Their explicit output is combined.
* Merges two components. Their selected output is combined.
*/
export function merge<O extends object, A, P extends object, B>(
c1: Component<O, A>,
Expand Down Expand Up @@ -350,7 +350,7 @@ class ModelViewComponent<M extends ReactivesObject, V> extends Component<
}
}
}
const { explicit: viewOutput } = toComponent(
const { selected: viewOutput } = toComponent(
viewF(placeholders, ...args)
).run(parent, destroyed);
const helpfulViewOutput = addErrorHandler(
Expand All @@ -363,7 +363,7 @@ class ModelViewComponent<M extends ReactivesObject, V> extends Component<
for (const name of Object.keys(behaviors)) {
placeholders[name].replaceWith(behaviors[name]);
}
return { explicit: {}, output: behaviors };
return { selected: {}, output: behaviors };
}
}

Expand Down Expand Up @@ -394,7 +394,7 @@ export function modelView<M extends ReactivesObject, V>(
}

export function view<O>(c: Component<O, any>): Component<{}, O> {
return new HandleOutput((explicit, _) => [{}, explicit], c);
return new HandleOutput((selected, _) => [{}, selected], c);
}

// Child element
Expand Down Expand Up @@ -430,9 +430,9 @@ export type Child<O = any> =
export interface ChildList extends Array<CE> {}

/**
* Takes a component type and returns the explicit output of the component.
* Takes a component type and returns the selected output of the component.
*/
export type ComponentExplicitOutput<C> = C extends Component<infer O, any>
export type ComponentSelectedOutput<C> = C extends Component<infer O, any>
? O
: never;

Expand All @@ -441,14 +441,14 @@ export type ComponentExplicitOutput<C> = C extends Component<infer O, any>
*/
export type ComponentOutput<C> = C extends Component<any, infer A> ? A : never;

export type ChildExplicitOutput<Ch extends Child> = ComponentExplicitOutput<
export type ChildSelectedOutput<Ch extends Child> = ComponentSelectedOutput<
ToComponent<Ch>
>;

// Merge component
export type MC<C1 extends CE, C2 extends CE> = Component<
ComponentExplicitOutput<TC<C1>> & ComponentExplicitOutput<TC<C2>>,
Merge<ComponentExplicitOutput<TC<C1>> & ComponentExplicitOutput<TC<C2>>>
ComponentSelectedOutput<TC<C1>> & ComponentSelectedOutput<TC<C2>>,
Merge<ComponentSelectedOutput<TC<C1>> & ComponentSelectedOutput<TC<C2>>>
>;

// prettier-ignore
Expand Down Expand Up @@ -500,7 +500,7 @@ class TextComponent extends Component<{}, {}> {
parent.removeChild(node);
}
});
return { explicit: {}, output: {} };
return { selected: {}, output: {} };
}
}

Expand All @@ -522,10 +522,10 @@ class ListComponent extends Component<any, any> {
const output: Record<string, any> = {};
for (let i = 0; i < this.components.length; ++i) {
const component = this.components[i];
const { explicit } = component.run(parent, destroyed);
Object.assign(output, explicit);
const { selected } = component.run(parent, destroyed);
Object.assign(output, selected);
}
return { explicit: output, output };
return { selected: output, output };
}
}

Expand Down Expand Up @@ -577,16 +577,16 @@ class DynamicComponent<O> extends Component<{}, Behavior<O>> {
destroyPrevious.resolve(true);
}
destroyPrevious = sinkFuture<boolean>();
const { explicit } = toComponent(child).run(
const { selected } = toComponent(child).run(
parentWrap,
destroyPrevious.combine(dynamicDestroyed)
);
return explicit;
return selected;
});
// To activate behavior
render(id, output);

return { explicit: {}, output };
return { selected: {}, output };
}
}

Expand Down Expand Up @@ -654,7 +654,7 @@ class ComponentList<A, O> extends Component<{}, Behavior<O[]>> {
recorder,
destroy.combine(listDestroyed)
);
stuff = { elms: recorder.elms, out: out.explicit, destroy };
stuff = { elms: recorder.elms, out: out.selected, destroy };
} else {
for (const elm of stuff.elms) {
parentWrap.appendChild(elm);
Expand All @@ -673,7 +673,7 @@ class ComponentList<A, O> extends Component<{}, Behavior<O[]>> {
keyToElm = newKeyToElm;
resultB.push(newArray);
});
return { explicit: {}, output: resultB };
return { selected: {}, output: resultB };
}
}

Expand Down
28 changes: 14 additions & 14 deletions src/dom-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import {
Child,
Component,
ComponentExplicitOutput,
ComponentSelectedOutput,
isChild,
Out,
Showable,
Expand All @@ -32,7 +32,7 @@ export type StreamDescriptions = {
};

export type OutputStream<T extends StreamDescriptions> = {
[K in keyof T]: Stream<T[K][2]>
[K in keyof T]: Stream<T[K][2]>;
};

export type BehaviorDescription<A> = [
Expand All @@ -55,7 +55,7 @@ export type BehaviorDescriptions = {
};

export type BehaviorOutput<T extends BehaviorDescriptions> = {
[K in keyof T]: Behavior<T[K][3]>
[K in keyof T]: Behavior<T[K][3]>;
};

export type ActionDefinitions = {
Expand All @@ -73,7 +73,7 @@ export type Setters = {
export type Style = {
[N in keyof CSSStyleDeclaration]?:
| Behavior<CSSStyleDeclaration[N]>
| CSSStyleDeclaration[N]
| CSSStyleDeclaration[N];
};

export type ClassNames = Behavior<string> | string;
Expand Down Expand Up @@ -113,7 +113,7 @@ export type InitialProperties =
| (_InitialProperties & Attributes);

export type DefaultOutput = {
[E in EventName]: Stream<HTMLElementEventMap[E]>
[E in EventName]: Stream<HTMLElementEventMap[E]>;
};

export type InitialOutput<
Expand Down Expand Up @@ -346,26 +346,26 @@ class DomComponent<O, P, A> extends Component<O & P, A & P> {
: document.createElement(this.tagName);

const output: any = handleProps(this.props, elm);
let explicit: any = {};
let selected: any = {};

parent.appendChild(elm);

if (this.child !== undefined) {
const childResult = this.child.run(elm, destroyed.mapTo(false));
Object.assign(explicit, childResult.explicit);
Object.assign(output, childResult.explicit);
Object.assign(selected, childResult.selected);
Object.assign(output, childResult.selected);
}
destroyed.subscribe((toplevel) => {
if (toplevel) {
parent.removeChild(elm);
}
// TODO: cleanup listeners
});
return { explicit, output };
return { selected, output };
}
}

type ChildExplicitOutput<Ch extends Child> = ComponentExplicitOutput<
type ChildSelectedOutput<Ch extends Child> = ComponentSelectedOutput<
ToComponent<Ch>
>;

Expand All @@ -377,8 +377,8 @@ export type Wrapped<P, O> = (undefined extends P
(props?: P): Component<{}, O>;
// Only child
<Ch extends Child>(child: Ch): Component<
ChildExplicitOutput<Ch>,
ChildExplicitOutput<Ch> & O
ChildSelectedOutput<Ch>,
ChildSelectedOutput<Ch> & O
>;
}
: {
Expand All @@ -388,8 +388,8 @@ export type Wrapped<P, O> = (undefined extends P
}) & {
// Both props and child
<Ch extends Child>(props: P, child: Ch): Component<
ChildExplicitOutput<Ch>,
ChildExplicitOutput<Ch> & O
ChildSelectedOutput<Ch>,
ChildSelectedOutput<Ch> & O
>;
};

Expand Down
Loading

0 comments on commit d152527

Please sign in to comment.