30 lines
827 B
TypeScript
30 lines
827 B
TypeScript
import { Schema as S } from "@effect/schema"
|
|
import { makeObservable, observable, type CreateObservableOptions } from "mobx"
|
|
import { mapValues } from "remeda"
|
|
|
|
|
|
interface MobXObservableInput {
|
|
new(...args: any[]): S.Struct.Type<S.Struct.Fields>
|
|
readonly fields: { readonly [K in keyof S.Struct.Fields]: S.Struct.Fields[K] }
|
|
}
|
|
|
|
export function MobXObservable<
|
|
This extends MobXObservableInput
|
|
>(
|
|
class_: This,
|
|
options?: Omit<CreateObservableOptions, "proxy">,
|
|
) {
|
|
return class MobXObservable extends class_ {
|
|
declare ["constructor"]: typeof MobXObservable
|
|
|
|
constructor(...args: any[]) {
|
|
super(...args)
|
|
|
|
makeObservable(this,
|
|
mapValues(this.constructor.fields, () => observable),
|
|
options,
|
|
)
|
|
}
|
|
} as This
|
|
}
|