本文整理汇总了TypeScript中mobx.autorun函数的典型用法代码示例。如果您正苦于以下问题:TypeScript autorun函数的具体用法?TypeScript autorun怎么用?TypeScript autorun使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了autorun函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: autorun
export function onMobxPromise<T>(promise:MobxPromise<T>|Array<MobxPromise<T>>,
onComplete:(...results:T[])=>void,
times:number = 1,
onDispose?:()=>void):IReactionDisposer {
let disposer:IReactionDisposer;
let count:number = 0;
let promiseArray:Array<MobxPromise<T>>;
if (promise instanceof Array) {
promiseArray = promise;
} else {
promiseArray = [promise];
}
disposer = autorun((reaction)=>{
if (promiseArray.reduce((acc, next)=>(acc && next.isComplete), true)) {
// if all complete
onComplete(...promiseArray.map(x=>(x.result as T)));
count += 1;
}
if (count >= times) {
reaction.dispose();
onDispose && onDispose();
}
});
return disposer;
}
示例2: it
it('should trigger autorun on change', (done) => {
const store = new TestStore();
store.sync({
data: {
attributes: {
name: 'Demo',
},
id: 1,
type: 'events',
},
});
let name = 'Demo';
const event = store.find<Event>('events', 1);
expect(event.name).to.equal('Demo');
autorun(() => {
expect(event.name).to.equal(name);
if (name === 'Foo') {
done();
}
});
name = 'Foo';
event.name = 'Foo';
});
示例3: ensureValidConfig
ensureValidConfig() {
const {chart} = this
// Validate the map variable id selection to something on the chart
autorun(() => {
const hasVariable = chart.map.variableId && chart.vardata.variablesById[chart.map.variableId]
if (!hasVariable && chart.data.primaryVariable) {
const variableId = chart.data.primaryVariable.id
runInAction(() => chart.map.props.variableId = variableId)
}
})
// When automatic classification is turned off, assign defaults
reaction(
() => this.map.props.isManualBuckets,
() => {
if (this.map.props.isManualBuckets) {
const { autoBinMaximums } = this
const colorSchemeValues = toJS(this.map.props.colorSchemeValues) || []
for (let i = 0; i < autoBinMaximums.length; i++) {
if (i >= colorSchemeValues.length)
colorSchemeValues.push(autoBinMaximums[i])
}
this.map.props.colorSchemeValues = colorSchemeValues
}
}
)
}
示例4: modelDriver
export function modelDriver(callback: (subject: Subject<any>) => void): Observable<any> {
const result = new Subject();
autorun(() => {
callback(result);
});
return result;
};
示例5: it
it('Adding variable whose formula references a non-existent symbol', () => {
const remath = new Remath();
let view: ObservableMap<string> = observable.map<string>();
const render = sinon.spy(() => {
renderCells(remath, view);
});
autorun(render);
// add a
const a = remath.addCell({
symbol: 'a',
formula: '= b + 10'
});
expect(render.callCount).to.equal(2);
expect(view.get('a')).to.equal('sym:a,formula:b + 10,val:NaN,disp:#REF?');
// add b = 30
runInAction(() => {
const b = remath.addCell({
symbol: 'b',
formula: '=30'
});
});
expect(view.get('b')).to.equal('sym:b,formula:30,val:30,disp:30');
expect(view.get('a')).to.equal('sym:a,formula:b + 10,val:40,disp:40');
});
示例6: it
it('loggedIn', () => {
authStore.sessionToken = '';
let ar: boolean[] = [];
autorun(() => {
ar.push(authStore.loggedIn);
});
expect(ar[ar.length-1]).to.equal(false);
authStore.sessionToken = '#token';
expect(ar[ar.length-1]).to.equal(true);
});
示例7: it
it('is observable', () => {
const lock = new Lockable({locked: false});
let v;
const renderSpy = sinon.spy(() => {
v = `${lock.locked}`;
});
autorun(renderSpy);
expect(v).to.equal('false');
lock.lock();
expect(v).to.equal('true');
lock.unlock();
expect(v).to.equal('false');
});
示例8: constructor
constructor() {
this.token = localStorage.getItem(AUTH_STORAGE_NAME) || "";
if (this.token) {
// Decode the token without verifying it. Verification will happen on the server.
this.session = decode(this.token, undefined, true);
}
autorun("AuthStore-autorun", (runner) => {
// Persist the auth changes to localstorage
localStorage.setItem(AUTH_STORAGE_NAME, this.token);
});
}
示例9: it
it("triggers any mobx reaction that touches the cache when its updated", (done)=>{
let peekFn = sinon.spy(()=>{
cache.peek({numAsString:"2"});
if (peekFn.callCount === 2 && cacheFn.callCount === 2 && getFn.callCount === 2) {
peekReaction();
cacheReaction();
getReaction();
done();
}
});
let cacheFn = sinon.spy(()=>{
cache.cache;
if (peekFn.callCount === 2 && cacheFn.callCount === 2 && getFn.callCount === 2) {
peekReaction();
cacheReaction();
getReaction();
done();
}
});
let getFn = sinon.spy(()=>{
cache.get({numAsString:"2"});
if (peekFn.callCount === 2 && cacheFn.callCount === 2 && getFn.callCount === 2) {
peekReaction();
cacheReaction();
getReaction();
done();
}
});
assert.equal(peekFn.callCount, 0);
assert.equal(cacheFn.callCount, 0);
assert.equal(getFn.callCount, 0);
let peekReaction = mobx.autorun(peekFn);
let cacheReaction = mobx.autorun(cacheFn);
let getReaction = mobx.autorun(getFn);
assert.equal(peekFn.callCount, 1);
assert.equal(cacheFn.callCount, 1);
assert.equal(getFn.callCount, 1);
});