feature/research-di #1
@ -1,13 +0,0 @@
|
||||
"use client"
|
||||
import Button from "@/app/components/button/button"
|
||||
import { TestButtonVM } from "@/app/test/client/vm/test-button-vm"
|
||||
import { useDI } from "@/bootstrap/di/di-context"
|
||||
import { useRef } from "react"
|
||||
|
||||
export default function ParentView() {
|
||||
const di = useDI()
|
||||
|
||||
const vmRef = useRef(di.resolve(TestButtonVM))
|
||||
|
||||
return <Button vm={vmRef.current} />
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
"use client"
|
||||
import BaseVM from "@/bootstrap/helpers/vm/base-vm";
|
||||
import ButtonVm from "@/app/components/button/button-vm";
|
||||
import { useEffect, useState } from "react";
|
||||
import getButtonTitle from "@/feature/domain/test/service/test-get-button-title-service";
|
||||
|
||||
export class TestButtonVM extends BaseVM<ButtonVm> {
|
||||
private getButtonTitle: () => Promise<string>
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
this.getButtonTitle = this.di.resolve(getButtonTitle.name)
|
||||
}
|
||||
|
||||
useVM(): ButtonVm {
|
||||
const [ buttonTitle, setTitle ] = useState("Default title")
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
const title = await this.getButtonTitle()
|
||||
setTitle(title)
|
||||
})()
|
||||
}, [])
|
||||
return {
|
||||
props: {
|
||||
title: buttonTitle
|
||||
},
|
||||
onClick: () => {
|
||||
console.log("clicked on the button");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const testKey = "testKey"
|
@ -1,10 +0,0 @@
|
||||
"use client"
|
||||
import testAppModule from "@/app/test/modules/test-app-module";
|
||||
import { DiContext } from "@/bootstrap/di/di-context";
|
||||
import { PropsWithChildren, useRef } from "react";
|
||||
|
||||
export default function WithDILayout(props: PropsWithChildren) {
|
||||
const testDi = useRef(testAppModule())
|
||||
return <DiContext.Provider value={testDi.current}>{props.children}</DiContext.Provider>
|
||||
}
|
||||
|
@ -1,13 +0,0 @@
|
||||
import { TestButtonVM, testKey } from "@/app/test/client/vm/test-button-vm";
|
||||
import di from "@/bootstrap/di/init-di"
|
||||
import getButtonTitle from "@/feature/domain/test/service/test-get-button-title-service";
|
||||
|
||||
export default function testAppModule() {
|
||||
const testDi = di.createChildContainer()
|
||||
|
||||
testDi.registerInstance(testKey, TestButtonVM);
|
||||
testDi.register(getButtonTitle.name, {
|
||||
useValue: getButtonTitle
|
||||
})
|
||||
return testDi
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
import ParentView from "@/app/test/client/view/parent-view";
|
||||
|
||||
export default function Page() {
|
||||
return <ParentView />
|
||||
}
|
@ -2,8 +2,6 @@ import getCustomerInvoiceDi from "@/feature/core/customer-invoice/data/module/cu
|
||||
import { customerInvoiceModuleKey } from "@/feature/core/customer-invoice/invoice-module-key";
|
||||
import { customerKey } from "@/feature/core/customer/customer-key";
|
||||
import getCustomerDi from "@/feature/core/customer/data/module/customer-di";
|
||||
import { testModuleKey } from "@/feature/domain/test/test-module-key";
|
||||
import getTestModule from "@/feature/infra/test/module/test-module";
|
||||
import getInvoiceDi from "@/feature/core/invoice/data/module/invoice-di";
|
||||
import { invoiceModuleKey } from "@/feature/core/invoice/invoice-module-key";
|
||||
import { DependencyContainer } from "tsyringe";
|
||||
@ -17,7 +15,6 @@ const memoizedDis: Record<string, DependencyContainer> = {}
|
||||
export default function serverDi(module: string): DependencyContainer {
|
||||
if (memoizedDis[module]) return memoizedDis[module]
|
||||
const getDi = {
|
||||
[testModuleKey]: getTestModule,
|
||||
[customerKey]: getCustomerDi,
|
||||
[customerInvoiceModuleKey]: getCustomerInvoiceDi,
|
||||
[invoiceModuleKey]: getInvoiceDi,
|
||||
|
@ -1,9 +0,0 @@
|
||||
"use server"
|
||||
import serverDi from "@/feature/common/server-di";
|
||||
import TestRepo, { testRepoKey } from "@/feature/domain/test/service/test-service-repo"
|
||||
import { testModuleKey } from "@/feature/domain/test/test-module-key";
|
||||
|
||||
export default async function getButtonTitle() {
|
||||
const repo = serverDi(testModuleKey).resolve<TestRepo>(testRepoKey)
|
||||
return repo.getButtonTitle()
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
export default interface TestRepo {
|
||||
getButtonTitle(): Promise<string>
|
||||
}
|
||||
|
||||
export const testRepoKey = "restRepoKey"
|
@ -1 +0,0 @@
|
||||
export const testModuleKey = "testModuleKey"
|
@ -1,12 +0,0 @@
|
||||
import di from "@/bootstrap/di/init-di"
|
||||
import { testRepoKey } from "@/feature/domain/test/service/test-service-repo"
|
||||
import TestRepoImpl from "@/feature/infra/test/repo/test-repo-iml"
|
||||
|
||||
export default function getTestModule() {
|
||||
const testDi = di.createChildContainer()
|
||||
|
||||
di.register(testRepoKey, {
|
||||
useClass: TestRepoImpl
|
||||
})
|
||||
return testDi
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
import TestRepo from "@/feature/domain/test/service/test-service-repo";
|
||||
|
||||
export default class TestRepoImpl implements TestRepo {
|
||||
async getButtonTitle(): Promise<string> {
|
||||
await new Promise((res) => {
|
||||
setTimeout(() => {
|
||||
res(true)
|
||||
}, 3000)
|
||||
})
|
||||
console.log('hereee');
|
||||
return Promise.resolve("Button title")
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user