This commit is contained in:
Julien Valverdé
2024-06-12 02:14:19 +02:00
parent fe5a863d9b
commit 0c8b9f3452
7 changed files with 46 additions and 29 deletions

View File

@@ -0,0 +1,20 @@
import { Schema as S } from "@effect/schema"
import type { Annotations, Struct } from "@effect/schema/Schema"
import type { HasFields } from "./util"
export function Class<Self>(identifier: string) {
return <Fields extends Struct.Fields>(
fieldsOr: Fields | HasFields<Fields>,
annotations?: Annotations.Schema<Self>,
) =>
S.Class<Self>(identifier)(fieldsOr, annotations) as S.Class<
Self,
Fields,
Struct.Encoded<Fields>,
Struct.Context<Fields>,
Struct.Constructor<Fields>,
{},
{}
>
}

View File

@@ -1,15 +1,18 @@
import type { Class, Struct } from "@effect/schema/Schema"
import type { Struct } from "@effect/schema/Schema"
import { makeObservable, observable, type CreateObservableOptions } from "mobx"
import { mapValues } from "remeda"
import type { Constructor } from "type-fest"
export function MobXObservable<
C extends Class<any, Struct.Fields, any, any, any, any, any>
This extends Constructor<Struct.Type<Struct.Fields>, any> & {
readonly fields: { readonly [K in keyof Struct.Fields]: Struct.Fields[K] }
}
>(
class_: C,
class_: This,
options?: Omit<CreateObservableOptions, "proxy">,
) {
return class MobXObservable extends (class_ as Class<any, Struct.Fields, any, any, any, any, any>) {
return class MobXObservable extends class_ {
declare ["constructor"]: typeof MobXObservable
constructor(...args: any[]) {
@@ -20,5 +23,5 @@ export function MobXObservable<
options,
)
}
} as C
} as This
}

View File

@@ -4,7 +4,7 @@ import type { TMutableClass } from "./TMutableClass"
import type { HasFields } from "./util"
export function MutableClass<Self = never>(identifier: string) {
export function MutableClass<Self>(identifier: string) {
return <Fields extends Struct.Fields>(
fieldsOr: Fields | HasFields<Fields>,
annotations?: Annotations.Schema<Self>,

View File

@@ -2,7 +2,7 @@ import type { Annotations, Class, Struct } from "@effect/schema/Schema"
import type { Mutable } from "effect/Types"
import type { Constructor } from "type-fest"
import type { StaticType } from "../../.."
import type { HasFields, MissingSelfGeneric } from "./util"
import type { HasFields } from "./util"
export type TMutableClass<
@@ -26,12 +26,10 @@ export type TMutableClass<
StaticType<Class<Self, Fields, I, R, C, Inherited, Proto>>,
"extend"
> & {
extend<Extended = never>(identifier: string): <newFields extends Struct.Fields>(
extend<Extended>(identifier: string): <newFields extends Struct.Fields>(
fields: newFields | HasFields<newFields>,
annotations?: Annotations.Schema<Extended>,
) => [Extended] extends [never]
? MissingSelfGeneric<"Base.extend">
: TMutableClass<
) => TMutableClass<
Extended,
Fields & newFields,
I & Struct.Encoded<newFields>,

View File

@@ -1,3 +1,4 @@
export { Class } from "./Class"
export { MobXObservable } from "./MobXObservable"
export { Mutable } from "./Mutable"
export { MutableClass } from "./MutableClass"

View File

@@ -3,22 +3,21 @@ import { MutableClass } from "./MutableClass"
import { pipe } from "remeda"
import { Mutable } from "./Mutable"
import { MobXObservable } from "./MobXObservable"
import { Class } from "./Class"
const UserSuper = <Self>() => pipe(
MutableClass<Self>("User")({
Class<Self>("User")({
id: S.BigIntFromSelf,
role: S.Union(S.Literal("BasicUser"), S.Literal("Admin")),
}),
v => MobXObservable(v),
Mutable,
MobXObservable,
)
class User extends MutableClass<User>("User")({
id: S.BigIntFromSelf,
role: S.Union(S.Literal("BasicUser"), S.Literal("Admin")),
}) {}
class User extends UserSuper<User>() {}
const user1 = new User({ id: 1n, role: "BasicUser" })
user1.id = 1n

View File

@@ -1,10 +1,6 @@
import type { Struct } from "@effect/schema/Schema"
export type MissingSelfGeneric<Usage extends string, Params extends string = ""> = (
`Missing \`Self\` generic - use \`class Self extends ${Usage}<Self>()(${Params}{ ... })\``
)
export type HasFields<Fields extends Struct.Fields> = (
| { readonly fields: Fields }
| { readonly from: HasFields<Fields> }