本文整理汇总了TypeScript中xstream.Stream.create方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Stream.create方法的具体用法?TypeScript Stream.create怎么用?TypeScript Stream.create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xstream.Stream
的用法示例。
在下文中一共展示了Stream.create方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: fromEvent
export function fromEvent(
element: Element | Document,
eventName: string,
useCapture = false,
preventDefault: PreventDefaultOpt = false,
passive = false,
): Stream<Event> {
return Stream.create<Event>({
element: element,
next: null,
start: function start(listener: Listener<Event>) {
if (preventDefault) {
this.next = function next(event: Event) {
preventDefaultConditional(event, preventDefault);
listener.next(event);
};
} else {
this.next = function next(event: Event) {
listener.next(event);
};
}
this.element.addEventListener(eventName, this.next, {
capture: useCapture,
passive,
});
},
stop: function stop() {
this.element.removeEventListener(eventName, this.next, useCapture);
},
} as Producer<Event>);
}
示例2: start
export default function createEventHandler<T>() {
interface HandlerProducer<T> extends Producer<T> {
sendValue: (T) => void,
_listener: Listener<T>
}
const producer: HandlerProducer<T> = {
start(listener) {
this._listener = listener
},
stop() {
this._listener = null
},
sendValue(val: T) {
if (this._listener) {
this._listener.next(val)
}
},
_listener: null as Listener<T>
}
const stream = Stream.create(producer)
return {
stream,
handler: (val: T) => {
producer.sendValue(val)
}
}
}
示例3: fromEvent
export function fromEvent(
element: Element | Document,
eventName: string,
useCapture = false,
preventDefault: PreventDefaultOpt = false,
passive = false
): Stream<Event> {
let next: ((e: Event) => void) | null = null;
return Stream.create<Event>({
start: function start(listener: Listener<Event>) {
if (preventDefault) {
next = function _next(event: Event) {
preventDefaultConditional(event, preventDefault);
listener.next(event);
};
} else {
next = function _next(event: Event) {
listener.next(event);
};
}
element.addEventListener(eventName, next, {
capture: useCapture,
passive,
});
},
stop: function stop() {
element.removeEventListener(eventName, next as any, useCapture);
next = null;
},
} as Producer<Event>);
}
示例4: fromEvent
export function fromEvent(element: Element | Document,
eventName: string,
useCapture = false): Stream<Event> {
return Stream.create<Event>({
element: element,
next: null,
start: function start(listener: Listener<Event>) {
this.next = function next(event: Event) { listener.next(event); };
this.element.addEventListener(eventName, this.next, useCapture);
},
stop: function stop() {
this.element.removeEventListener(eventName, this.next, useCapture);
},
} as Producer<Event>);
}
示例5: complete
export const withCompletionMarble$ = (some$: Stream<Marble>, completion_time?: number): Stream<Marble> => {
const return$ = Stream.create<Marble>();
let lastTime = 0;
const getCompletionTime = () => completion_time == undefined ? lastTime : completion_time;
setTimeout(() =>
some$.addListener({
next: marble => {
lastTime = marble.time;
if (!marble.complete) return$.shamefullySendNext(marble);
else complete(return$, getCompletionTime());
},
complete: () => complete(return$, getCompletionTime()),
error: err => return$.shamefullySendError(err)
}), 30);
return return$;
};
示例6: start
const getMessageStream = (type: string, socket: SocketIOClient.Socket)
: Stream<IncomingMessage> => {
const producer : Producer<IncomingMessage> = {
start(listener) {
socket.on(type, (data) => {
listener.next({
type,
data
} as IncomingMessage)
})
},
stop() {
}
}
return Stream.create(producer)
}