From 5114fffc885be298305ed4f6d0d19f194936a2b3 Mon Sep 17 00:00:00 2001 From: Alexandre Mouton-Brady Date: Thu, 24 Dec 2020 13:23:00 +0100 Subject: [PATCH] :sparkles: Adding chainable createApp --- src/createApp.ts | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/createApp.ts b/src/createApp.ts index ed0e61b..f598d99 100644 --- a/src/createApp.ts +++ b/src/createApp.ts @@ -1,6 +1,24 @@ import type { Component } from 'solid-js'; import { createComponent, render } from 'solid-js/web'; +interface App { + /** + * Add a Provider to the app. The list of provider will be merged + * at mount time. + * + * @param provider {Component} - The provider to add to the list + * @param opts {Record} - The optional options + */ + use(provider: Component, options?: Record): App; + + /** + * It first merges all the Providers and then uses the `render` function + * to mount the application. + * + * @param dom {HTMLElement | string} - The element to mount your app on + */ + mount(domElement: HTMLElement | string): ReturnType; +} interface Provider { provider: Component; opts?: Record; @@ -50,27 +68,17 @@ function mergeProviders(app: Element, providers: Provider[]) { export function createApp(app: T) { const providers: Provider[] = []; - return { - /** - * Add a Provider to the app. The list of provider will be merged - * at mount time. - * - * @param provider {Component} - The provider to add to the list - * @param opts {Record} - The optional options - */ - use(provider: Component, opts?: Record) { + const _app: App = { + use(provider, opts) { providers.push({ provider, opts }); + return _app; }, - /** - * It first merges all the Providers and then uses the `render` function - * to mount the application. - * - * @param dom {HTMLElement | string} - The element to mount your app on - */ - mount(dom: HTMLElement | string) { + mount(dom) { const application = mergeProviders(app as Element, providers); const root = typeof dom === 'string' ? document.querySelector(dom) : dom; return render(application, root); }, }; + + return _app; }