Add API for converting StoreWritable and EventCallable to readonly
chshanovskiy opened this issue · 3 comments
chshanovskiy commented
For good and consistent API design it would be nice to have a simple helper, that converts StoreWritable/EventCallable to their readonly types.
Something like this:
function readonly(store: StoreWritable<T>): Store<T> {
return store.map(store => store)
}
function readonly(event: EventCallable<T>): Event<T> {
return event.map(event => event)
}
Example – dummy user factory:
export const createUser = createFactory(() => {
const check = createEvent()
const authorized = createEvent<User>()
const $user = createStore<User | null>(null)
sample({
clock: check,
target: checkFx,
})
sample({
clock: checkFx.doneData,
target: $user,
})
sample({
clock: $user,
filter: Boolean,
target: authorized,
})
return {
check,
authorized: authorized.map((user) => user), // or readonly(authorized)
$user: $user.map((user) => user), // or readonly($user)
}
})
Here authorized
is EventCallable, but for factory consumers it must be readonly event, only for subscribing. Also $user
is readonly store for consumers.
Naming is under discussion, maybe readonly
is too ambiguous.
chshanovskiy commented
Maybe readonly must support object form like this readonly({$someStore, someEvent})
chshanovskiy commented
Also, readonly
helper can support already readonly stores and events, in favor of factory developers and producing strong api for consumers
sergeysova commented
Done by #320