Skip to content

Commit

Permalink
some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kerwanp committed Sep 25, 2024
1 parent 5508e39 commit 20b1713
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 43 deletions.
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
2 changes: 1 addition & 1 deletion providers/edgewire_provider.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion src/component_context.ts → src/component/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component } from './component.js'
import { Component } from './main.js'

export class ComponentContext {
readonly isMounting: boolean
Expand Down
6 changes: 3 additions & 3 deletions src/component.ts → src/component/main.ts
Original file line number Diff line number Diff line change
@@ -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 {}

Expand Down
26 changes: 12 additions & 14 deletions src/handle_components.ts → src/component/manager.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -60,7 +59,6 @@ export class HandleComponents {

let html = await this.#render(component)
html = insertAttributesIntoHtmlRoot(html, {
'wire:effects': [],
'wire:snapshot': newSnapshot,
})

Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions src/component_hook.ts → src/component_hook/main.ts
Original file line number Diff line number Diff line change
@@ -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], []]
Expand Down
Original file line number Diff line number Diff line change
@@ -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[] = []
Expand Down
27 changes: 14 additions & 13 deletions src/edgewire.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
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) {
this.#componentRegistry.component(name, component)
}

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<string, any> = {}) {
Expand Down
12 changes: 6 additions & 6 deletions src/handle_requests.ts → src/request/manager.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
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) {
const payloads = ctx.request.body().components // TODO: Type this

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,
Expand Down

0 comments on commit 20b1713

Please sign in to comment.