Skip to content
This repository has been archived by the owner on Mar 15, 2023. It is now read-only.

Commit

Permalink
✨ Adding chainable createApp
Browse files Browse the repository at this point in the history
  • Loading branch information
amoutonbrady committed Dec 24, 2020
1 parent 60d219f commit 5114fff
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/createApp.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>} - The optional options
*/
use(provider: Component, options?: Record<string, any>): 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<typeof render>;
}
interface Provider {
provider: Component;
opts?: Record<string, any>;
Expand Down Expand Up @@ -50,27 +68,17 @@ function mergeProviders(app: Element, providers: Provider[]) {
export function createApp<T extends unknown>(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<string, any>} - The optional options
*/
use(provider: Component, opts?: Record<string, any>) {
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;
}

0 comments on commit 5114fff

Please sign in to comment.