62 lines
1.4 KiB
TypeScript
62 lines
1.4 KiB
TypeScript
import { Schema as S } from "@effect/schema"
|
|
import { computed, makeObservable, reaction, runInAction } from "mobx"
|
|
import type { Simplify } from "type-fest"
|
|
import { MutableTaggedClass, toJsonifiable } from "./Schema"
|
|
import { ObservableClass } from "./Schema/MobX"
|
|
import type { ExtendAll } from "./Types"
|
|
|
|
|
|
type TestA = {
|
|
heugneu: string
|
|
type: "Type1" | "Type2"
|
|
}
|
|
|
|
type TestB = {
|
|
type: "Type1"
|
|
}
|
|
|
|
type Merged = Simplify<ExtendAll<[TestA, TestB]>>
|
|
|
|
|
|
|
|
const UserSchema = MutableTaggedClass<User>()("User", {
|
|
id: S.BigIntFromSelf,
|
|
role: S.Union(S.Literal("BasicUser"), S.Literal("Admin")),
|
|
}).pipe(
|
|
ObservableClass()
|
|
)
|
|
|
|
class User extends UserSchema {
|
|
constructor(...args: ConstructorParameters<typeof UserSchema>) {
|
|
super(...args)
|
|
makeObservable(this, { idAsString: computed })
|
|
}
|
|
|
|
get idAsString() {
|
|
return this.id.toString()
|
|
}
|
|
}
|
|
|
|
const JsonifiableUser = User.pipe(
|
|
toJsonifiable(S.Struct({
|
|
...User.fields,
|
|
id: S.BigInt,
|
|
}))
|
|
)
|
|
|
|
|
|
const user1 = new User({ id: -1n, role: "BasicUser" })
|
|
reaction(() => user1.id, id => console.log(`user1 id changed: ${ id }`))
|
|
|
|
|
|
class Admin extends User.extend<Admin>("Admin")({
|
|
// role: S.Literal("Admin")
|
|
}) {}
|
|
|
|
const user2 = new Admin({ id: -1n, role: "Admin" })
|
|
reaction(() => user2.id, id => console.log(`user2 id changed: ${ id }`))
|
|
|
|
|
|
runInAction(() => { user1.id = 1n })
|
|
runInAction(() => { user2.id = 2n })
|