Work
This commit is contained in:
20
src/effect/schema/class/Class.ts
Normal file
20
src/effect/schema/class/Class.ts
Normal 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>,
|
||||
{},
|
||||
{}
|
||||
>
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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>,
|
||||
|
||||
@@ -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,19 +26,17 @@ 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<
|
||||
Extended,
|
||||
Fields & newFields,
|
||||
I & Struct.Encoded<newFields>,
|
||||
R | Struct.Context<newFields>,
|
||||
C & Struct.Constructor<newFields>,
|
||||
Self,
|
||||
Proto
|
||||
>
|
||||
) => TMutableClass<
|
||||
Extended,
|
||||
Fields & newFields,
|
||||
I & Struct.Encoded<newFields>,
|
||||
R | Struct.Context<newFields>,
|
||||
C & Struct.Constructor<newFields>,
|
||||
Self,
|
||||
Proto
|
||||
>
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export { Class } from "./Class"
|
||||
export { MobXObservable } from "./MobXObservable"
|
||||
export { Mutable } from "./Mutable"
|
||||
export { MutableClass } from "./MutableClass"
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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> }
|
||||
|
||||
Reference in New Issue
Block a user