0.1.4 #5
@@ -12,15 +12,28 @@ import type * as TF from "type-fest"
|
|||||||
// return jsonifiable.pipe(S.compose(schema))
|
// return jsonifiable.pipe(S.compose(schema))
|
||||||
// }
|
// }
|
||||||
|
|
||||||
export function Jsonifiable<
|
export function Jsonifiable<A, I, R>(
|
||||||
|
schema: S.Schema<A, I, R>
|
||||||
|
) {
|
||||||
|
return <
|
||||||
JsonifiableA extends I,
|
JsonifiableA extends I,
|
||||||
JsonifiableI extends TF.Jsonifiable,
|
JsonifiableI extends TF.Jsonifiable,
|
||||||
JsonifiableR,
|
JsonifiableR,
|
||||||
|
>(
|
||||||
|
jsonifiable: S.Schema<JsonifiableA, JsonifiableI, JsonifiableR>
|
||||||
|
) =>
|
||||||
|
jsonifiable.pipe(S.compose(schema))
|
||||||
|
}
|
||||||
|
|
||||||
|
export function Jsonifiable2<
|
||||||
|
JsonifiableA,
|
||||||
|
JsonifiableI extends TF.Jsonifiable,
|
||||||
|
JsonifiableR,
|
||||||
>(
|
>(
|
||||||
jsonifiable: S.Schema<JsonifiableA, JsonifiableI, JsonifiableR>
|
jsonifiable: S.Schema<JsonifiableA, JsonifiableI, JsonifiableR>
|
||||||
) {
|
) {
|
||||||
return <A, I, R>(
|
return <A, R>(
|
||||||
schema: S.Schema<A, I, R>
|
schema: S.Schema<A, JsonifiableA, R>
|
||||||
) =>
|
) =>
|
||||||
jsonifiable.pipe(S.compose(schema))
|
jsonifiable.pipe(S.compose(schema))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,18 +3,16 @@ import { makeObservable, observable, type CreateObservableOptions } from "mobx"
|
|||||||
import { mapValues } from "remeda"
|
import { mapValues } from "remeda"
|
||||||
|
|
||||||
|
|
||||||
interface MobXObservableInput {
|
interface MobXObservableA {
|
||||||
new(...args: any[]): S.Struct.Type<S.Struct.Fields>
|
new(...args: any[]): S.Struct.Type<S.Struct.Fields>
|
||||||
readonly fields: { readonly [K in keyof S.Struct.Fields]: S.Struct.Fields[K] }
|
readonly fields: { readonly [K in keyof S.Struct.Fields]: S.Struct.Fields[K] }
|
||||||
}
|
}
|
||||||
|
|
||||||
export function MobXObservable<
|
export function MobXObservable<A extends MobXObservableA>(
|
||||||
This extends MobXObservableInput
|
self: A,
|
||||||
>(
|
|
||||||
class_: This,
|
|
||||||
options?: Omit<CreateObservableOptions, "proxy">,
|
options?: Omit<CreateObservableOptions, "proxy">,
|
||||||
) {
|
) {
|
||||||
return class MobXObservable extends class_ {
|
return class MobXObservable extends self {
|
||||||
declare ["constructor"]: typeof MobXObservable
|
declare ["constructor"]: typeof MobXObservable
|
||||||
|
|
||||||
constructor(...args: any[]) {
|
constructor(...args: any[]) {
|
||||||
@@ -25,5 +23,5 @@ export function MobXObservable<
|
|||||||
options,
|
options,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} as This
|
} as A
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Schema as S } from "@effect/schema"
|
import { Schema as S } from "@effect/schema"
|
||||||
import { reaction, runInAction } from "mobx"
|
import { reaction, runInAction } from "mobx"
|
||||||
import { Jsonifiable } from "./Jsonifiable"
|
import { Jsonifiable2 } from "./Jsonifiable"
|
||||||
import { MobXObservable, MutableTaggedClass } from "./class"
|
import { MobXObservable, MutableTaggedClass } from "./class"
|
||||||
|
|
||||||
|
|
||||||
@@ -12,17 +12,12 @@ class User extends MutableTaggedClass<User>()("User", {
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
const JsonifiableUser = User.pipe(
|
const JsonifiableUser = User.pipe(
|
||||||
Jsonifiable(S.Struct({
|
Jsonifiable2(S.Struct({
|
||||||
...User.fields,
|
...User.fields,
|
||||||
id: S.BigInt,
|
id: S.BigInt,
|
||||||
}))
|
}))
|
||||||
)
|
)
|
||||||
|
|
||||||
const JsonifiableUser2 = Jsonifiable(User)(S.Struct({
|
|
||||||
...User.fields,
|
|
||||||
id: S.BigInt,
|
|
||||||
}))
|
|
||||||
|
|
||||||
|
|
||||||
const user1 = new User({ id: -1n, role: "BasicUser" })
|
const user1 = new User({ id: -1n, role: "BasicUser" })
|
||||||
reaction(() => user1.id, id => console.log(`user1 id changed: ${ id }`))
|
reaction(() => user1.id, id => console.log(`user1 id changed: ${ id }`))
|
||||||
|
|||||||
Reference in New Issue
Block a user