MobXObservable

This commit is contained in:
Julien Valverdé
2024-06-10 22:36:37 +02:00
parent cc33865e1a
commit 606ea43aab
4 changed files with 27 additions and 1 deletions

BIN
bun.lockb

Binary file not shown.

View File

@@ -52,6 +52,7 @@
},
"optionalDependencies": {
"@effect/schema": "^0.67.22",
"effect": "^3.3.1"
"effect": "^3.3.1",
"mobx": "^6.12.3"
}
}

View File

@@ -0,0 +1,24 @@
import type { Class, Struct } from "@effect/schema/Schema"
import { makeObservable, observable, type CreateObservableOptions } from "mobx"
import { mapValues } from "remeda"
export function MobXObservable<
C extends Class<any, Struct.Fields, any, any, any, any, any>
>(
class_: C,
options?: Omit<CreateObservableOptions, "proxy">,
) {
return class MobXObservable extends (class_ as Class<any, Struct.Fields, any, any, any, any, any>) {
declare ["constructor"]: typeof MobXObservable
constructor(...args: any[]) {
super(...args)
makeObservable(this,
mapValues(this.constructor.fields, () => observable),
options,
)
}
} as C
}

View File

@@ -1,2 +1,3 @@
export { MobXObservable } from "./MobXObservable"
export { MutableClass } from "./MutableClass"
export type { TMutableClass } from "./MutableClass"