Skip to content

Commit

Permalink
Merge f3bc928 into 35cf97a
Browse files Browse the repository at this point in the history
  • Loading branch information
smalluban committed Dec 12, 2019
2 parents 35cf97a + f3bc928 commit b7e53b5
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -5,6 +5,7 @@
"main": "lib/index.js",
"module": "esm/index.js",
"jsnext:main": "esm/index.js",
"types": "types/index.d.ts",
"author": "Dominik Lubański <dominik.lubanski@gmail.com>",
"repository": "https://github.com/hybridsjs/hybrids",
"scripts": {
Expand Down
14 changes: 7 additions & 7 deletions src/render.js
@@ -1,6 +1,6 @@
export default function render(get, customOptions = {}) {
if (typeof get !== 'function') {
throw TypeError(`The first argument must be a function: ${typeof get}`);
export default function render(fn, customOptions = {}) {
if (typeof fn !== 'function') {
throw TypeError(`The first argument must be a function: ${typeof fn}`);
}

const options = { shadowRoot: true, ...customOptions };
Expand All @@ -12,18 +12,18 @@ export default function render(get, customOptions = {}) {

return {
get(host) {
const fn = get(host);
const update = fn(host);
return function flush() {
fn(host, options.shadowRoot ? host.shadowRoot : host);
update(host, options.shadowRoot ? host.shadowRoot : host);
};
},
connect(host) {
if (options.shadowRoot && !host.shadowRoot) {
host.attachShadow(shadowRootInit);
}
},
observe(host, fn) {
fn();
observe(host, flush) {
flush();
},
};
}
94 changes: 94 additions & 0 deletions types/index.d.ts
@@ -0,0 +1,94 @@
// tslint:disable-next-line:export-just-namespace
export = hybrids;
export as namespace hybrids;

declare namespace hybrids {
interface Descriptor<E extends HTMLElement> {
get?(host: E, lastValue: any): any;
set?(host: E, value: any, lastValue: any): any;
connect?(host: E, key: string, invalidate: Function): Function;
observe?(host: E, value: any, lastValue: any): void;
}

type Property<E extends HTMLElement> =
| string
| number
| boolean
| null
| undefined
| any[]
| Descriptor<E>;

interface UpdateFunction<E extends HTMLElement> {
(host: E, target?: HTMLElement): void;
}

interface RenderFunction<E extends HTMLElement> {
(host: E) : UpdateFunction<E>;
}

type Hybrids<E extends HTMLElement> =
{ render?: RenderFunction<E> | Property<E> }
& { [property in keyof E]?: Property<E> | Descriptor<E>['get'] }

interface MapOfHybridsOrConstructors {
[tagName: string]: Hybrids<any> | typeof HTMLElement,
}

type MapOfConstructors<T> = {
[tagName in keyof T]: typeof HTMLElement;
}

interface HybridElement<E extends HTMLElement> {
new(): E;
prototype: E;
}

/* Define */

function define<E extends HTMLElement>(tagName: string, hybridsOrConstructor: Hybrids<E> | typeof HTMLElement): HybridElement<E>;
function define(mapOfHybridsOrConstructors: MapOfHybridsOrConstructors): MapOfConstructors<typeof mapOfHybridsOrConstructors>;

/* Factories */

function property<E extends HTMLElement>(value: any, connect?: Descriptor<E>['connect']): Descriptor<E>;
function parent<E extends HTMLElement>(hybridsOrFn: Hybrids<E> | ((hybrids: Hybrids<E>) => boolean)): Descriptor<E>;
function children<E extends HTMLElement>(hybridsOrFn: (Hybrids<E> | ((hybrids: Hybrids<E>) => boolean)), options? : { deep?: boolean, nested?: boolean }): Descriptor<E>;
function render<E extends HTMLElement>(fn: RenderFunction<E>, customOptions?: { shadowRoot?: boolean | object }): Descriptor<E>;

/* Template Engine */

interface UpdateFunctionWithMethods<E extends HTMLElement> extends UpdateFunction<E> {
define: (elements: MapOfHybridsOrConstructors) => this;
key: (id: any) => this;
style: (...styles: string[]) => this;
}

interface EventHandler<E extends HTMLElement> {
(host: E, event?: Event) : any;
}

type TemplateValue<E extends HTMLElement> =
| string
| number
| boolean
| null
| undefined
| UpdateFunction<E>;

type TemplateExpression<E extends HTMLElement> = TemplateValue<E> | TemplateValue<E>[] | EventHandler<E>;

namespace html {
function set<E extends HTMLElement>(propertyName: keyof E, value?: any): EventHandler<E>;
function resolve<E extends HTMLElement>(promise: Promise<UpdateFunction<E>>, placeholder?: UpdateFunction<E>, delay?: number): UpdateFunction<E>;
}

function html<E extends HTMLElement>(parts: TemplateStringsArray, ...args: TemplateExpression<E>[]): UpdateFunctionWithMethods<E>;

namespace svg {
function set<E extends HTMLElement>(propertyName: keyof E, value?: any): EventHandler<E>;
function resolve<E extends HTMLElement>(promise: Promise<UpdateFunction<E>>, placeholder?: UpdateFunction<E>, delay?: number) : UpdateFunction<E>;
}

function svg<E extends HTMLElement>(parts: TemplateStringsArray, ...args: TemplateExpression<E>[]): UpdateFunctionWithMethods<E>;
}

0 comments on commit b7e53b5

Please sign in to comment.