Package Exports
- mobx-utils
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mobx-utils) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
MobX-utils
Work in progress
Installation
npm install mobx-utils --save
API
fromPromise
Parameters
promise
IThenable<T>initialValue
[T] (optional, defaultundefined
)modifier
[any] (optional, defaultIDENTITY
)
Returns IPromiseBasedObservable<T>
whenWithTimeout
Like normal when
, except that this when
will automatically dispose if the condition isn't met within a certain amount of time.
Parameters
expr
action
timeout
[number] maximum amount when spends waiting before giving up (optional, default10000
)onTimeout
[any] the ontimeout handler will be called if the condition wasn't met withing the given time (optional, default()
)
Examples
test("expect store to load, t => {
const store = {
items: [],
loaded: false
}
fetchDataForStore((data) => {
store.items = data;
store.loaded = true;
})
whenWithTimeout(
() => store.loaded
() => t.end()
2000,
() => t.fail("expected store to load")
)
})
Returns IDisposer disposer function that can be used to cancel the when prematurely. Neither action or onTimeout will be fired if disposed
keepAlive
MobX normally suspends any computed value that is not in use by any reaction,
and lazily re-evaluates the expression if needed outside a reaction while not in use.
keepAlive
marks a computed value as always in use, meaning that it will always fresh, but never disposed.
Parameters
computedValue
IComputedValue<any> created using thecomputed
function_1
_2
Examples
const number = observable(3)
const doubler = computed(() => number.get() * 2)
const stop = keepAlive(doubler)
// doubler will now stay in sync reactively even when there are no further observers
stop()
// normal behavior, doubler results will be recomputed if not observed but needed, but lazily
Returns IDisposer stops this keep alive so that the computed value goes back to normal behavior
keepAlive
MobX normally suspends any computed value that is not in use by any reaction,
and lazily re-evaluates the expression if needed outside a reaction while not in use.
keepAlive
marks a computed value as always in use, meaning that it will always fresh, but never disposed.
Parameters
target
Object an object that has a computed property, created by@computed
orextendObservable
property
string the name of the property to keep alive_1
_2
Examples
const obj = observable({
number: 3,
doubler: function() { return this.number * 2 }
})
const stop = keepAlive(obj, "doubler")
Returns IDisposer stops this keep alive so that the computed value goes back to normal behavior
fromResource
fromResource creates an observable which current state can be inspected using .get()
,
and which can be kept in sync with some external datasource that can be subscribed to.
the created observable will only subscribe to the datasource if it is in use somewhere,
(un)subscribing when needed. To enable fromResource
to do that two callbacks need to be provided,
one to subscribe, and one to unsubscribe. The subscribe callback itself will receive a sink
callback, which can be used
to update the current state of the observable, allowing observes to react.
Whatever is passed to sink
will be returned by get()
. It is the get()
call itself which is being tracked,
so make sure that you don't dereference to early.
The following example code creates an observable that connects to a dbUserRecord
,
which comes from an imaginary database and notifies when it has changed.
Parameters
subscriber
unsubscriber
[IDisposer] (optional, defaultNOOP
)initialValue
[T] the data that will be returned byget()
until thesink
has emitted its first data (optional, defaultundefined
)
Examples
function createObservableUser(dbUserRecord) {
let currentSubscription;
return fromResource(
(sink) => {
// sink the current state
sink(dbUserRecord.fields)
// subscribe to the record, invoke the sink callback whenever new data arrives
currentSubscription = dbUserRecord.onUpdated(() => {
sink(dbUserRecord.fields)
})
},
() => {
// the user observable is not in use at the moment, unsubscribe (for now)
dbUserRecord.unsubscribe(currentSubscription)
},
dbUserRecord.fields // optionally, provide initial data
)
}
// usage:
const myUserObservable = createObservableUser(myDatabaseConnector.query("name = 'Michel'"))
autorun(() => {
// printed everytime the database updates its records
console.log(myUserObservable.get().displayName)
})
const userComponent = observer(({ user }) =>
<div>{user.get().displayName}</div>
)