当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript mobx.reaction函数代码示例

本文整理汇总了TypeScript中mobx.reaction函数的典型用法代码示例。如果您正苦于以下问题:TypeScript reaction函数的具体用法?TypeScript reaction怎么用?TypeScript reaction使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了reaction函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: constructor

    constructor(private queryToKey:(q:Query)=>string, // query maps to the key of the datum it will fill
                private dataToKey:(d:Data, m?:Metadata)=>string, // should uniquely identify the data - for indexing in cache
                private fetch:(queries:Query[], ...staticDependencies:any[])=>Promise<FetchResult<Data, Metadata>>,
                ...staticDependencies:any[]) {
        this.init();
        this.staticDependencies = staticDependencies;
        this.debouncedPopulate = accumulatingDebounce<QueryKeyToQuery<Query>, Query>(
            (queryMap:QueryKeyToQuery<Query>)=>{
                const queries:Query[] = Object.keys(queryMap).map(k=>queryMap[k]);
                this.populate(queries);
            },
            (queryMap:QueryKeyToQuery<Query>, newQuery:Query)=>{
                queryMap[this.queryToKey(newQuery)] = newQuery;
                return queryMap;
            },
            ()=>{return {};},
            0
        );

        reaction(
            ()=>this._cache,
            (cache:Cache<Data, Metadata>)=>{
                // filter out completed promises, we dont listen on them anymore
                this.promises = this.promises.filter(promise=>!this.tryTrigger(promise));
            }
        );
    }
开发者ID:agarwalrounak,项目名称:cbioportal-frontend,代码行数:27,代码来源:LazyMobXCache.ts

示例2: 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
                }
            }
        )
    }
开发者ID:OurWorldInData,项目名称:owid-grapher,代码行数:28,代码来源:MapData.ts

示例3: flow

test.cb("flow happens in single ticks", t => {
    const X = types
        .model({
            y: 1
        })
        .actions(self => ({
            p: flow(function*() {
                self.y++
                self.y++
                yield delay(1, true, false)
                self.y++
                self.y++
            })
        }))

    const x = X.create()
    const values: number[] = []
    reaction(() => x.y, v => values.push(v))

    debugger
    x.p().then(() => {
        t.is(x.y, 5)
        t.deepEqual(values, [3, 5])
        t.end()
    })
})
开发者ID:lelandyolo,项目名称:mobx-state-tree,代码行数:26,代码来源:async.ts

示例4: test

test("it should be possible to share states between views and actions using enhance", t => {
    const A = types.model({}).extend(self => {
        const localState = observable(3)
        return {
            views: {
                get x() {
                    return localState.get()
                }
            },
            actions: {
                setX(value) {
                    localState.set(value)
                }
            }
        }
    })

    let x = 0
    let a = A.create()
    const d = reaction(
        () => a.x,
        v => {
            x = v
        }
    )
    a.setX(7)
    t.is(a.x, 7)
    t.is(x, 7)
    d()
})
开发者ID:lelandyolo,项目名称:mobx-state-tree,代码行数:30,代码来源:object.ts

示例5: test

test("it should resolve refs during creation, when using generic reference", t => {
    const values: number[] = []
    const Book = types.model({
        id: types.identifier(),
        price: types.number
    })
    const BookEntry = types
        .model({
            book: types.reference(Book)
        })
        .views(self => ({
            get price() {
                return self.book.price * 2
            }
        }))
    const Store = types.model({
        books: types.array(Book),
        entries: types.optional(types.array(BookEntry), [])
    })
    const s = Store.create({
        books: [{ id: "3", price: 2 }]
    })
    unprotect(s)
    reaction(() => s.entries.reduce((a, e) => a + e.price, 0), v => values.push(v))
    s.entries.push({ book: s.books[0] } as any)
    t.is(s.entries[0].price, 4)
    t.is(s.entries.reduce((a, e) => a + e.price, 0), 4)
    const entry = BookEntry.create({ book: s.books[0] }) // can refer to book, even when not part of tree yet
    t.deepEqual(getSnapshot(entry), { book: "3" })
    s.entries.push(entry)
    t.deepEqual(values, [4, 8])
})
开发者ID:lelandyolo,项目名称:mobx-state-tree,代码行数:32,代码来源:reference.ts

示例6: ngOnInit

 ngOnInit() {
   reaction(() => this.authStore.authError, (authError) => {
     if (authError) {
       this.snackBar.open(authError, '', {
         duration: 3000
       });
     }
   });
 }
开发者ID:reisub0,项目名称:mainflux,代码行数:9,代码来源:app.component.ts

示例7: test

test("VS be observable", t => {
    const promises: Promise<any>[] = []
    const i = Todo.create()
    const d = reaction(() => i.state, p => promises.push(p))

    i.reload()
    i.reload()

    t.is(promises.length, 2)

    d()
})
开发者ID:lelandyolo,项目名称:mobx-state-tree,代码行数:12,代码来源:volatile.ts

示例8: testCoffeeTodo

function testCoffeeTodo(
    t: CallbackTestContext & Context<any>,
    generator: (self: any) => (x: string) => IterableIterator<any>,
    shouldError: boolean,
    resultValue: any,
    producedCoffees: string[]
) {
    useStrict(true)
    const Todo = types
        .model({
            title: "get coffee"
        })
        .actions(self => ({
            startFetch: flow(generator(self))
        }))
    const events: any[] = []
    const coffees: string[] = []
    const t1 = Todo.create({})
    unprotect(t1)
    addMiddleware(t1, (c, next) => {
        events.push(c)
        return next(c)
    })
    reaction(() => t1.title, coffee => coffees.push(coffee))

    function handleResult(res) {
        t.is(res, resultValue)
        t.deepEqual(coffees, producedCoffees)
        const filtered = filterRelevantStuff(events)
        t.snapshot(filtered, "Wrong events, expected\n" + JSON.stringify(filtered, null, 2))
        useStrict(false)
        t.end()
    }

    t1.startFetch("black").then(
        r => {
            t.is(shouldError, false, "Ended up in OK handler")
            handleResult(r)
        },
        r => {
            t.is(shouldError, true, "Ended up in ERROR handler")
            console.error(r)
            handleResult(r)
        }
    )
}
开发者ID:lelandyolo,项目名称:mobx-state-tree,代码行数:46,代码来源:async.ts

示例9: reaction

export default function reactionWithPrev<D>(
        dataFn:()=>D,
        effectFn:(data:D, prevData?:D)=>void,
        opts?:IReactionOptions
    ) {
    let prevData:D|undefined = undefined;
    let currData:D|undefined = undefined;
    return reaction(
        ()=>{
            prevData = currData;
            currData = dataFn();
            return currData;
        },
        (data:D)=>{
            effectFn(data, prevData);
        },
        opts
    );
}
开发者ID:agarwalrounak,项目名称:cbioportal-frontend,代码行数:19,代码来源:reactionWithPrev.ts


注:本文中的mobx.reaction函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。