From 20b1713dc30939901e8bbe18ebc220564d8de42b Mon Sep 17 00:00:00 2001 From: Martin PAUCOT Date: Wed, 25 Sep 2024 19:20:22 +0200 Subject: [PATCH] some refactoring --- index.ts | 2 +- providers/edgewire_provider.ts | 2 +- .../context.ts} | 2 +- src/{component.ts => component/main.ts} | 6 ++--- .../manager.ts} | 26 +++++++++--------- .../registry.ts} | 0 .../main.ts} | 6 ++--- .../registry.ts} | 2 +- src/edgewire.ts | 27 ++++++++++--------- .../manager.ts} | 12 ++++----- 10 files changed, 42 insertions(+), 43 deletions(-) rename src/{component_context.ts => component/context.ts} (88%) rename src/{component.ts => component/main.ts} (78%) rename src/{handle_components.ts => component/manager.ts} (87%) rename src/{component_registry.ts => component/registry.ts} (100%) rename src/{component_hook.ts => component_hook/main.ts} (79%) rename src/{component_hook_registry.ts => component_hook/registry.ts} (78%) rename src/{handle_requests.ts => request/manager.ts} (67%) diff --git a/index.ts b/index.ts index fe23552..3dda1a0 100644 --- a/index.ts +++ b/index.ts @@ -3,5 +3,5 @@ import 'reflect-metadata' export { configure } from './configure.js' export { defineConfig } from './src/define_config.js' -export { Component } from './src/component.js' +export { Component } from './src/component/main.js' export { view } from './src/view.js' diff --git a/providers/edgewire_provider.ts b/providers/edgewire_provider.ts index c06c8d8..3d082c6 100644 --- a/providers/edgewire_provider.ts +++ b/providers/edgewire_provider.ts @@ -1,7 +1,7 @@ import edge from 'edge.js' import { edgewireTag } from '../src/edge/tags/edgewire.js' import { ApplicationService } from '@adonisjs/core/types' -import { ComponentRegistry } from '../src/component_registry.js' +import { ComponentRegistry } from '../src/component/registry.js' import { edgewireScriptsTag } from '../src/edge/tags/edgewire_scripts.js' import { Edgewire } from '../src/edgewire.js' import { LifecycleComponentHook } from '../src/features/lifecycle/component_hook.js' diff --git a/src/component_context.ts b/src/component/context.ts similarity index 88% rename from src/component_context.ts rename to src/component/context.ts index acc0904..fa8a27e 100644 --- a/src/component_context.ts +++ b/src/component/context.ts @@ -1,4 +1,4 @@ -import { Component } from './component.js' +import { Component } from './main.js' export class ComponentContext { readonly isMounting: boolean diff --git a/src/component.ts b/src/component/main.ts similarity index 78% rename from src/component.ts rename to src/component/main.ts index 2710b24..a982849 100644 --- a/src/component.ts +++ b/src/component/main.ts @@ -1,8 +1,8 @@ import { HttpContext } from '@adonisjs/core/http' -import { View } from './view.js' import { compose } from '@adonisjs/core/helpers' -import { WithAttributes } from './mixins/with_attributes.js' -import { LifecycleHooks } from './features/lifecycle/mixins/lifecycle_hooks.js' +import { LifecycleHooks } from '../features/lifecycle/mixins/lifecycle_hooks.js' +import { WithAttributes } from '../mixins/with_attributes.js' +import { View } from '../view.js' class BaseComponent {} diff --git a/src/handle_components.ts b/src/component/manager.ts similarity index 87% rename from src/handle_components.ts rename to src/component/manager.ts index 9771d12..fe4a783 100644 --- a/src/handle_components.ts +++ b/src/component/manager.ts @@ -1,22 +1,21 @@ import { inject } from '@adonisjs/core' -import { Component } from './component.js' -import { ComponentRegistry } from './component_registry.js' -import { insertAttributesIntoHtmlRoot } from './utils.js' -import { ComponentSnapshot, ComponentCall, ComponentUpdates } from './types.js' -import { ComponentContext } from './component_context.js' -import { E_INVALID_CHECKSUM } from './errors.js' -import { generateChecksum, verifyChecksum } from './utils/checksum.js' -import { View } from './view.js' import string from '@adonisjs/core/helpers/string' -import { getPublicProperties } from './utils/object.js' import { HttpContext } from '@adonisjs/core/http' import app from '@adonisjs/core/services/app' -import emitter from '@adonisjs/core/services/emitter' -import { ComponentHookRegistry } from './component_hook_registry.js' -import { ViewContext } from './view_context.js' +import { ComponentRegistry } from './registry.js' +import { ComponentHookRegistry } from '../component_hook/registry.js' +import { insertAttributesIntoHtmlRoot } from '../utils.js' +import { ComponentCall, ComponentSnapshot, ComponentUpdates } from '../types.js' +import { Component } from './main.js' +import { getPublicProperties } from '../utils/object.js' +import { View } from '../view.js' +import { ViewContext } from '../view_context.js' +import { ComponentContext } from './context.js' +import { generateChecksum, verifyChecksum } from '../utils/checksum.js' +import { E_INVALID_CHECKSUM } from '../errors.js' @inject() -export class HandleComponents { +export class ComponentManager { #componentsRegistry: ComponentRegistry #componentHookRegistry: ComponentHookRegistry @@ -60,7 +59,6 @@ export class HandleComponents { let html = await this.#render(component) html = insertAttributesIntoHtmlRoot(html, { - 'wire:effects': [], 'wire:snapshot': newSnapshot, }) diff --git a/src/component_registry.ts b/src/component/registry.ts similarity index 100% rename from src/component_registry.ts rename to src/component/registry.ts diff --git a/src/component_hook.ts b/src/component_hook/main.ts similarity index 79% rename from src/component_hook.ts rename to src/component_hook/main.ts index 5765565..f84a5a7 100644 --- a/src/component_hook.ts +++ b/src/component_hook/main.ts @@ -1,7 +1,7 @@ import Hooks from '@poppinss/hooks' -import { Component } from './component.js' -import { View } from './view.js' -import { ViewContext } from './view_context.js' +import { Component } from '../component/main.js' +import { View } from '../view.js' +import { ViewContext } from '../view_context.js' export type ComponentHookEvents = { boot: [[Component], []] diff --git a/src/component_hook_registry.ts b/src/component_hook/registry.ts similarity index 78% rename from src/component_hook_registry.ts rename to src/component_hook/registry.ts index be113ad..2e1ee07 100644 --- a/src/component_hook_registry.ts +++ b/src/component_hook/registry.ts @@ -1,5 +1,5 @@ -import { ComponentHook, ComponentHookEvents } from './component_hook.js' import Hooks from '@poppinss/hooks' +import { ComponentHook, ComponentHookEvents } from './main.js' export class ComponentHookRegistry { components: ComponentHook[] = [] diff --git a/src/edgewire.ts b/src/edgewire.ts index da27074..c30e5b9 100644 --- a/src/edgewire.ts +++ b/src/edgewire.ts @@ -1,29 +1,30 @@ import { inject } from '@adonisjs/core' -import { ComponentRegistry } from './component_registry.js' -import { HandleComponents } from './handle_components.js' -import { HandleRequests } from './handle_requests.js' import { HttpContext } from '@adonisjs/core/http' import { View } from './view.js' -import { ComponentHook } from './component_hook.js' -import { ComponentHookRegistry } from './component_hook_registry.js' +import { ComponentRegistry } from './component/registry.js' +import { ComponentHookRegistry } from './component_hook/registry.js' +import { ComponentHook } from './component_hook/main.js' +import { ComponentManager } from './component/manager.js' +import { RequestManager } from './request/manager.js' @inject() export class Edgewire { #componentRegistry: ComponentRegistry #componentHookRegistry: ComponentHookRegistry - #handleComponents: HandleComponents - #handleRequests: HandleRequests + + #componentsManager: ComponentManager + #requestManager: RequestManager constructor( componentRegistry: ComponentRegistry, componentHookRegistry: ComponentHookRegistry, - handleComponents: HandleComponents, - handleRequests: HandleRequests + componentManager: ComponentManager, + requestManager: RequestManager ) { this.#componentRegistry = componentRegistry this.#componentHookRegistry = componentHookRegistry - this.#handleComponents = handleComponents - this.#handleRequests = handleRequests + this.#componentsManager = componentManager + this.#requestManager = requestManager } component(name: string, component: any) { @@ -31,11 +32,11 @@ export class Edgewire { } mount(name: string, ctx: HttpContext) { - return this.#handleComponents.mount(name, ctx) + return this.#componentsManager.mount(name, ctx) } handleUpdate(ctx: HttpContext) { - return this.#handleRequests.handleUpdate(ctx) + return this.#requestManager.handleUpdate(ctx) } view(templatePath: string, state: Record = {}) { diff --git a/src/handle_requests.ts b/src/request/manager.ts similarity index 67% rename from src/handle_requests.ts rename to src/request/manager.ts index 28d1cba..8721ecc 100644 --- a/src/handle_requests.ts +++ b/src/request/manager.ts @@ -1,13 +1,13 @@ import { HttpContext } from '@adonisjs/core/http' -import { HandleComponents } from './handle_components.js' import { inject } from '@adonisjs/core' +import { ComponentManager } from '../component/manager.js' @inject() -export class HandleRequests { - #handleComponents: HandleComponents +export class RequestManager { + #componentManager: ComponentManager - constructor(handleComponents: HandleComponents) { - this.#handleComponents = handleComponents + constructor(componentManager: ComponentManager) { + this.#componentManager = componentManager } public async handleUpdate(ctx: HttpContext) { @@ -15,7 +15,7 @@ export class HandleRequests { const componentResponses = [] for (const payload of payloads) { - const { snapshot, effects } = await this.#handleComponents.update( + const { snapshot, effects } = await this.#componentManager.update( JSON.parse(payload.snapshot), payload.updates, payload.calls,