本文整理汇总了TypeScript中mobx.observable函数的典型用法代码示例。如果您正苦于以下问题:TypeScript observable函数的具体用法?TypeScript observable怎么用?TypeScript observable使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了observable函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: observable
export function model<TState, TTargets>(
template: IModelTemplate<TState>,
options?: IModelOptions): IModel<TState, TTargets> {
options = options || { key: 'model' };
const state = observable(Object.assign({}, template.state));
const services = Object.assign({}, defaultServices, options.services);
const result = Object.assign({
key: options.key,
state,
targets: null,
template,
services,
}, template.extensions);
if (template.actions || options.actions) {
result.targets = actionsToTargets(result, Object.assign({}, template.actions, options.actions));
}
if (template.inputs) {
const inputs = template.inputs(result, services);
result.outputs = inputs;
result.targets = Object.assign(result.targets, inputToTargets(inputs));
}
if (template.init) {
template.init(result, template);
}
return result;
}
示例2: it
it('handles nested undo windows called out of order', () => {
let array = observable([1, 2, 3, 4, 5]);
let innerUndo: Function;
let outerUndo = createUndo('outerUndo')(
action('outerUndo')(() => {
array[0] = 0;
expect(array.slice(0)).toEqual([0, 2, 3, 4, 5]);
innerUndo = createUndo('innerUndo')(
action('innerUndo')(() => {
array[1] = 0;
})
);
array[2] = 0;
expect(array.slice(0)).toEqual([0, 0, 0, 4, 5]);
})
);
outerUndo();
expect(array.slice(0)).toEqual([1, 2, 3, 4, 5]);
innerUndo && innerUndo();
expect(array.slice(0)).toEqual([1, 2, 3, 4, 5]);
});
示例3: observable
export function makeValidator<T extends Rules>(rules: T) {
type Errors = Partial<{ [ruleName in keyof T]: string[] }>;
const self = observable({
errors: {} as Errors,
checkValidity(object: any) {
return new Promise<boolean>(resolve => {
let isValid = true;
let errors = {} as Errors;
let promises: Promise<void>[] = [];
function setError(ruleName: string, error: string | null) {
if (error !== null) {
isValid = false;
const existingError = errors[ruleName];
if (existingError) {
existingError.push(error);
} else {
errors[ruleName] = [error] as any;
}
}
}
function checkRule(ruleName: string, rule: Rule) {
const error = rule(object, ruleName);
if (error instanceof Promise) {
promises.push(
new Promise(resolve => {
error.then(error => {
setError(ruleName, error);
resolve();
});
})
);
} else {
setError(ruleName, error);
}
}
Object.keys(rules).forEach(ruleName => {
const rule = rules[ruleName];
if (Array.isArray(rule)) {
rule.forEach(rule => checkRule(ruleName, rule));
} else {
checkRule(ruleName, rule);
}
});
Promise.all(promises).then(() => {
runInAction(() => (self.errors = errors));
resolve(isValid);
});
});
}
});
return self;
}
示例4: test
test("it should support observable arrays", t => {
const TestArray = types.array(types.number)
const testArray = TestArray.create(observable([1, 2]))
t.true(testArray[0] === 1)
t.true(testArray.length === 2)
t.true(Array.isArray(testArray.slice()))
})
示例5: initDriverAnnotationSettings
export function initDriverAnnotationSettings(store:OncoprinterStore) {
return observable({
cbioportalCount: false,
cbioportalCountThreshold: 0,
_oncoKb:true,
_ignoreUnknown: false,
hotspots: false, // for now
set oncoKb(val:boolean) {
this._oncoKb = val;
},
get oncoKb() {
return AppConfig.serverConfig.show_oncokb && this._oncoKb && !store.didOncoKbFail;
},
set ignoreUnknown(val:boolean) {
this._ignoreUnknown = val;
},
get ignoreUnknown() {
return this._ignoreUnknown && this.driversAnnotated;
},
get driversAnnotated() {
const anySelected = this.oncoKb ||
this.cbioportalCount || this.hotspots;
return anySelected;
}
});
}
示例6: it
it('executes immediately if the promise is already resolved, does not execute again', (done)=>{
let handlerInvokeCount = 0;
let promiseResult = observable(0);
let lastInvokedPromiseResult = 0;
let promiseResultIncrementerDisposer:IReactionDisposer;
let promise = remoteData({
invoke:async ()=>{
lastInvokedPromiseResult = promiseResult.get();
return lastInvokedPromiseResult;
}
});
onMobxPromise(promise, ()=>{
// ensure the promise is already resolved by this point
onMobxPromise(promise, (result:number)=>{
assert.equal(result, 0, "promise invoked with result = 0");
handlerInvokeCount += 1;
promiseResult.set(promiseResult.get() + 1);
}, 1, ()=>{
promiseResultIncrementerDisposer = onMobxPromise(promise, ()=>{
assert.equal(handlerInvokeCount, 1, "never invoked again");
promiseResult.set(promiseResult.get() + 1);
}, 10, ()=>{
done();
});
});
});
});
示例7: mobxArrayPush
function mobxArrayPush() {
console.time("Mobx push 1000 empty objects into array");
let a = mobx.observable([]);
for (let i = 0; i < 1000; i++) {
a.push({ b: i });
}
console.timeEnd("Mobx push 1000 empty objects into array");
}
示例8: observable
const A = types.model({}).extend(self => {
const localState = observable(3)
return {
views: {
get x() {
return localState.get()
}
},
actions: {
setX(value) {
localState.set(value)
}
}
}
})