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 { makeObservable, observable, type CreateObservableOptions } from "mobx"
|
||||||
import { mapValues } from "remeda"
|
import { mapValues } from "remeda"
|
||||||
|
import type { Constructor } from "type-fest"
|
||||||
|
|
||||||
|
|
||||||
export function MobXObservable<
|
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">,
|
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
|
declare ["constructor"]: typeof MobXObservable
|
||||||
|
|
||||||
constructor(...args: any[]) {
|
constructor(...args: any[]) {
|
||||||
@@ -20,5 +23,5 @@ export function MobXObservable<
|
|||||||
options,
|
options,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} as C
|
} as This
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import type { TMutableClass } from "./TMutableClass"
|
|||||||
import type { HasFields } from "./util"
|
import type { HasFields } from "./util"
|
||||||
|
|
||||||
|
|
||||||
export function MutableClass<Self = never>(identifier: string) {
|
export function MutableClass<Self>(identifier: string) {
|
||||||
return <Fields extends Struct.Fields>(
|
return <Fields extends Struct.Fields>(
|
||||||
fieldsOr: Fields | HasFields<Fields>,
|
fieldsOr: Fields | HasFields<Fields>,
|
||||||
annotations?: Annotations.Schema<Self>,
|
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 { Mutable } from "effect/Types"
|
||||||
import type { Constructor } from "type-fest"
|
import type { Constructor } from "type-fest"
|
||||||
import type { StaticType } from "../../.."
|
import type { StaticType } from "../../.."
|
||||||
import type { HasFields, MissingSelfGeneric } from "./util"
|
import type { HasFields } from "./util"
|
||||||
|
|
||||||
|
|
||||||
export type TMutableClass<
|
export type TMutableClass<
|
||||||
@@ -26,12 +26,10 @@ export type TMutableClass<
|
|||||||
StaticType<Class<Self, Fields, I, R, C, Inherited, Proto>>,
|
StaticType<Class<Self, Fields, I, R, C, Inherited, Proto>>,
|
||||||
"extend"
|
"extend"
|
||||||
> & {
|
> & {
|
||||||
extend<Extended = never>(identifier: string): <newFields extends Struct.Fields>(
|
extend<Extended>(identifier: string): <newFields extends Struct.Fields>(
|
||||||
fields: newFields | HasFields<newFields>,
|
fields: newFields | HasFields<newFields>,
|
||||||
annotations?: Annotations.Schema<Extended>,
|
annotations?: Annotations.Schema<Extended>,
|
||||||
) => [Extended] extends [never]
|
) => TMutableClass<
|
||||||
? MissingSelfGeneric<"Base.extend">
|
|
||||||
: TMutableClass<
|
|
||||||
Extended,
|
Extended,
|
||||||
Fields & newFields,
|
Fields & newFields,
|
||||||
I & Struct.Encoded<newFields>,
|
I & Struct.Encoded<newFields>,
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
export { Class } from "./Class"
|
||||||
export { MobXObservable } from "./MobXObservable"
|
export { MobXObservable } from "./MobXObservable"
|
||||||
export { Mutable } from "./Mutable"
|
export { Mutable } from "./Mutable"
|
||||||
export { MutableClass } from "./MutableClass"
|
export { MutableClass } from "./MutableClass"
|
||||||
|
|||||||
@@ -3,22 +3,21 @@ import { MutableClass } from "./MutableClass"
|
|||||||
import { pipe } from "remeda"
|
import { pipe } from "remeda"
|
||||||
import { Mutable } from "./Mutable"
|
import { Mutable } from "./Mutable"
|
||||||
import { MobXObservable } from "./MobXObservable"
|
import { MobXObservable } from "./MobXObservable"
|
||||||
|
import { Class } from "./Class"
|
||||||
|
|
||||||
|
|
||||||
const UserSuper = <Self>() => pipe(
|
const UserSuper = <Self>() => pipe(
|
||||||
MutableClass<Self>("User")({
|
Class<Self>("User")({
|
||||||
id: S.BigIntFromSelf,
|
id: S.BigIntFromSelf,
|
||||||
role: S.Union(S.Literal("BasicUser"), S.Literal("Admin")),
|
role: S.Union(S.Literal("BasicUser"), S.Literal("Admin")),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
v => MobXObservable(v),
|
Mutable,
|
||||||
|
MobXObservable,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class User extends MutableClass<User>("User")({
|
class User extends UserSuper<User>() {}
|
||||||
id: S.BigIntFromSelf,
|
|
||||||
role: S.Union(S.Literal("BasicUser"), S.Literal("Admin")),
|
|
||||||
}) {}
|
|
||||||
|
|
||||||
const user1 = new User({ id: 1n, role: "BasicUser" })
|
const user1 = new User({ id: 1n, role: "BasicUser" })
|
||||||
user1.id = 1n
|
user1.id = 1n
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
import type { Struct } from "@effect/schema/Schema"
|
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> = (
|
export type HasFields<Fields extends Struct.Fields> = (
|
||||||
| { readonly fields: Fields }
|
| { readonly fields: Fields }
|
||||||
| { readonly from: HasFields<Fields> }
|
| { readonly from: HasFields<Fields> }
|
||||||
|
|||||||
Reference in New Issue
Block a user