當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript mobx.observable函數代碼示例

本文整理匯總了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;
}
開發者ID:pvasek,項目名稱:mobx-app-model,代碼行數:33,代碼來源:model.ts

示例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]);
    });
開發者ID:Microsoft,項目名稱:satcheljs,代碼行數:30,代碼來源:createUndoTests.ts

示例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;
}
開發者ID:eez-open,項目名稱:studio,代碼行數:60,代碼來源:validation.ts

示例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()))
})
開發者ID:lelandyolo,項目名稱:mobx-state-tree,代碼行數:7,代碼來源:array.ts

示例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;
        }
    });
}
開發者ID:agarwalrounak,項目名稱:cbioportal-frontend,代碼行數:28,代碼來源:OncoprinterUtils.ts

示例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();
                });
            });
        });
    });
開發者ID:agarwalrounak,項目名稱:cbioportal-frontend,代碼行數:28,代碼來源:onMobxPromise.spec.ts

示例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");
}
開發者ID:Bobris,項目名稱:Bobril,代碼行數:8,代碼來源:app.ts

示例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)
             }
         }
     }
 })
開發者ID:lelandyolo,項目名稱:mobx-state-tree,代碼行數:15,代碼來源:object.ts


注:本文中的mobx.observable函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。