Open xaviergonz opened 5 years ago
Well, actually there might be one more to add a better way to do async actions like in here: https://github.com/mobxjs/mobx/pull/2118 but it depends on this mobx PR being accepted: https://github.com/mobxjs/mobx/pull/2117
I have been thinking whether it makes sense to add a utility function findParentPath
in addition to findParent
which also returns the path of the found parent like in RootPath
.
Wouldn't that be the same as running getRootPath over the result?
No, I meant that findParentPath
may return the parent object and the path from the parent object to the source object from which findParentPath
begins its search, similar to getParentPath
but with depth ≥ 1.
Ah, sounds good :) https://github.com/xaviergonz/mobx-keystone/pull/43
About backrefs: Shouldn't the return type of getRefsResolvingTo
be a readonly ObservableSet<Ref<T>>
? (Not sure if that's possible by the way because MobX doesn't seem to have a ReadonlyObservableSet
type ...)
It should, but there are no readonly versions of observable stuff in mobx types :(
Didn't find any mention of custom types in docs, @xaviergonz.
Simple case: My server responds with a timestamp as a date, I'm converting it into regular Date object in js, and converting back into the timestamp for snapshots. In MST I've used to use types.custom
to address it. Is there something similar in mobx-keystone?
There's none at the moment, but in theory you could have something like
class ... extends Model({timestamp: prop<number>()}) {
@computed
get date() { return new Date(this.timestamp) }
@modelAction
setDate(date: Date) { this.timestamp = +date }
}
I think there could be a way to extract this into a composable pattern (not working code):
const asDate = (getter: () => number, setter: (ts: number) => void) => {
return {
get: computed(() => new Date(getter())),
set: modelAction((d: Date) => { setter(+d) })
}
}
class ... {
date = asDate(() => this.timestamp, (ts) => { this.timestamp = ts })
}
which maybe in turn could be simplified into something like
const asDate = dataTransform((ts) => new Date(ts), (date) => +date)
class ... extends Model({ timestamp: prop<number>() }) {
@asDate("timestamp")
date!: Date
}
const date = model.date
model.date = new Date()
// or
class ... extends Model({ timestamp: prop<number>() }) {
date = asDate(this, "timestamp")
}
const date = model.date.get()
model.date.set(new Date())
would something like that be good enough for custom types? (which syntax btw, the decorator or the func one?)
Just created a PR with the idea: https://github.com/xaviergonz/mobx-keystone/pull/48
I'm ok with decorator I think. This is a pretty general approach allows creating complex custom types.
Published prop transforms as 0.23.0
Does it mean asyncAction from mobx-utils can be used just instead modelFlow?? =) or modelFlow still has something specific for the keystone?
Time for v1.0.0? =)))
May be Jan 01 2021 ?
After the latest PR to remove "$" from paths I don't foresee big changes to the API for the time being. Also most issues seem to have been resolved now. Do you think it is time to brand it as v1.0.0?