本文整理汇总了TypeScript中xstream.fromObservable函数的典型用法代码示例。如果您正苦于以下问题:TypeScript fromObservable函数的具体用法?TypeScript fromObservable怎么用?TypeScript fromObservable使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了fromObservable函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: timeSource
return function throttleAnimation<T>(inputStream: Stream<T>): Stream<T> {
const source = timeSource();
const stream = xs.fromObservable(inputStream);
let animationListener: any = null;
const throttledStream = xs.create<T>({
start(listener) {
let lastValue: any = null;
let emittedLastValue = true;
const frame$ = xs.fromObservable(source.animationFrames());
const frameListener = {
next(event: any) {
if (!emittedLastValue) {
listener.next(lastValue);
emittedLastValue = true;
}
},
};
animationListener = {
next(event: T) {
lastValue = event;
emittedLastValue = false;
},
error(err: Error) {
listener.error(err);
},
complete() {
frame$.removeListener(frameListener);
listener.complete();
},
};
stream.addListener(animationListener);
frame$.addListener(frameListener);
},
stop() {
if (animationListener) {
stream.removeListener(animationListener);
}
},
});
return adapt(throttledStream);
};
示例2: adapt
return function debounceOperator<T>(inputStream: Stream<T>): Stream<T> {
const state = {scheduledEntry: null};
const stream = xs.fromObservable(inputStream);
let debounceListener: any = null;
const debouncedStream = xs.create<T>({
start(listener: Listener<T>) {
debounceListener = makeDebounceListener<T>(
schedule,
currentTime,
debounceInterval,
listener,
state
);
stream.addListener(debounceListener);
},
stop() {
if (debounceListener) {
stream.removeListener(debounceListener);
}
},
});
return adapt(debouncedStream);
};
示例3: sourcesWrapper
return gear => {
let state = cacheModel ? modelCache!.get(gear) : null
if (!state) {
const wrappedSources = sourcesWrapper(sources, gear)
const actions = gear.intent ? gear.intent(wrappedSources) : defaultIntent(wrappedSources)
state = xs.fromObservable(gear.model ? gear.model(actions) : defaultModel(actions))
.replaceError((err: any) => {
if (cacheModel && modelCache) {
modelCache.delete(gear)
}
return xs.fromObservable(gear.catch ? gear.catch(err, actions) : defaultCatch(err, actions))
})
.remember()
if (cacheModel) {
modelCache!.set(gear, state)
}
}
const views = teeth.reduce((accum, tooth) => {
let view = state!.filter(toothFilter(tooth, (gear.teeth || {})[tooth])).map(toothView(tooth, (gear.teeth || {})[tooth]))
const isolator = connectors.has(tooth)
? connectors.get(tooth)!.isolate || defaultConnector.isolate
: defaultConnector.isolate
if (isolator) {
view = xs.fromObservable(isolator(sources, view, gear))
}
if (toothCombineGear) {
view = view.map(v => [v, gear])
}
return Object.assign(accum, {
[tooth]: view
})
},
{})
return views
}
示例4: return
return (sources: any) => {
let gears: Observable<Iterable<Gear<any, any>>>
if (gearbox instanceof Function) {
gears = gearbox(sources)
} else {
gears = gearbox
}
let spin = xs.fromObservable<Iterable<Gear<any, any>>>(gears)
.map(spinGears(sources,
defaultIntent,
defaultModel,
cacheModel,
defaultCatch,
teeth,
toothFilter,
toothView,
cumulative,
sourcesWrapper,
defaultConnector,
connectors))
.startWith([])
if (cumulative) {
spin = spin
.map(spins => xs.fromArray(spins))
.compose(flattenConcurrently)
.remember()
} else {
spin = spin.remember()
}
const sinks = teeth.reduce((accum, tooth) => {
let view: xs<any>
if (cumulative) {
view = spin
.map((gear: any) => gear[tooth])
.filter(toothView => !!toothView)
.compose(flattenConcurrently)
} else {
view = spin.map(spins => xs.fromArray(spins)
.map(gear => gear[tooth])
.filter(toothView => !!toothView)
.compose(flattenConcurrently))
.flatten()
}
const connector = connectors.get(tooth) || defaultConnector
if (connector.fold) {
view = view.fold(connector.reduce || defaultReduce, connector.init ? connector.init() : {})
} else {
view = view.map(([cur]: [any]) => cur)
}
return Object.assign(accum, {
[sinkMap.has(tooth) ? sinkMap.get(tooth) : tooth]: adapt(view)
})
},
{})
return sinks
}
示例5: collectionComponent
return function collectionComponent(sources: any) {
const name = opts.channel || 'state';
const itemKey = opts.itemKey;
const itemScope = opts.itemScope || defaultItemScope;
const itemComp = opts.item;
const state$ = xs.fromObservable((sources[name] as StateSource<S>).stream);
const instances$ = state$.fold(
(acc: InternalInstances<Si>, nextState: Array<any> | any) => {
const dict = acc.dict;
if (Array.isArray(nextState)) {
const nextInstArray = Array(nextState.length) as Array<
Si & {_key: string}
>;
const nextKeys = new Set<string>();
// add
for (let i = 0, n = nextState.length; i < n; ++i) {
const key = `${itemKey ? itemKey(nextState[i], i) : i}`;
nextKeys.add(key);
if (!dict.has(key)) {
const stateScope = itemKey ? instanceLens(itemKey, key) : `${i}`;
const otherScopes = itemScope(key);
const scopes =
typeof otherScopes === 'string'
? {'*': otherScopes, [name]: stateScope}
: {...otherScopes, [name]: stateScope};
const sinks: any = isolate(itemComp, scopes)(sources);
dict.set(key, sinks);
nextInstArray[i] = sinks;
} else {
nextInstArray[i] = dict.get(key) as any;
}
nextInstArray[i]._key = key;
}
// remove
dict.forEach((_, key) => {
if (!nextKeys.has(key)) {
dict.delete(key);
}
});
nextKeys.clear();
return {dict: dict, arr: nextInstArray};
} else {
dict.clear();
const key = `${itemKey ? itemKey(nextState, 0) : 'this'}`;
const stateScope = identityLens;
const otherScopes = itemScope(key);
const scopes =
typeof otherScopes === 'string'
? {'*': otherScopes, [name]: stateScope}
: {...otherScopes, [name]: stateScope};
const sinks: any = isolate(itemComp, scopes)(sources);
dict.set(key, sinks);
return {dict: dict, arr: [sinks]};
}
},
{dict: new Map(), arr: []} as InternalInstances<Si>
);
return opts.collectSinks(new Instances<Si>(instances$));
};
示例6: isolateSink
public isolateSink(sink: any, scope: string): any {
return adapt(
xs.fromObservable<any>(sink).map((vnode: VNode) => {
if (vnode.sel && vnode.sel.indexOf(SCOPE_PREFIX + scope) !== -1) {
return vnode;
} else {
vnode.sel += `.${SCOPE_PREFIX}${scope}`;
return vnode;
}
})
);
}
示例7: record
return function record(stream: Stream<any>): Stream<any> {
const recordedStream = xs.createWithMemory({
start(listener) {
xs.fromObservable(stream).addListener(
recordListener(currentTime, listener)
);
},
stop() {},
});
return adapt(recordedStream);
};