rxjs/operators#refCount TypeScript Examples

The following examples show how to use rxjs/operators#refCount. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: choice.ts    From json-schema-form with Apache License 2.0 5 votes vote down vote up
/**
     * load choices
     */
    load(value: any, schema: Schema): Observable<Choice[]> {

        if (!this.cache) {
            if (schema.choices) {
                // static choices are given, convert them to Choice and merge the result
                const arr: Observable<Choice>[] = [];
                for (const s of schema.choices) {
                    arr.push(this.choice(s, schema));
                }
                this.cache = forkJoin(arr);
            } else if (schema.choicesUrl) {
                // load choices from URL
                this.cache = this.getChoices(schema.choicesUrl, schema.choicesUrlArgs, schema.choicesVerb).pipe(
                    switchMap(res => {
                        if (schema.jsonata) {
                            res = jsonata(schema.jsonata).evaluate(res);
                            if (!Array.isArray(res)) {
                                res = [res];

                                // introduce jsonName, jsonValue
                            }
                        }
                        const obs: Observable<Choice>[] = [];
                        for (const r of res) {
                            obs.push(this.choice(r, schema));
                        }
                        return forkJoin(obs);
                    }),

                    // setup caching
                    publishReplay(1),
                    refCount()
                );
            }
        }
        return this.cache;
    }