How to use the xstream/extra/fromevent function in xstream

To help you get started, we’ve selected a few xstream examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github aronallen / cycle-sandbox / src / dom / worker.ts View on Github external
start(observer) {
              const attachMessage = {
                cmd: WorkerDOMMessageCommand.attach,
                payload: {
                  selector: selector,
                  events: events,
                  options: options,
                  listenerId
                }
              }
              tx.postMessage(attachMessage);
              subscription = fromEvent(rx, 'message')
                .filter(e => (e.data as WorkerDOMEvent).listenerId === listenerId)
                .subscribe({
                  next (event) {
                    const payload = event.data.payload as EventSynthesis;
                    observer.next(payload);
                  },
                  error (error) {
                    console.error(error)
                  },
                  complete () {

                  }
                })
            },
            stop() {
github aronallen / cycle-sandbox / src / sandbox.ts View on Github external
sources[key]
                  )
                }
              } else {
                throw Error('We dont know how to handle this with a bridge');
              }
            }, {});
            observer.next(sinks);
            const startMessage: SandboxMessage = {
              cmd: SandboxMessageCommand.start,
              instanceId,
            }
            worker.postMessage(startMessage);
          }
          
          subscription = fromEvent(worker, 'message')
            .map((event: MessageEvent) => event.data as SandboxMessage)
            .filter(message => message.cmd === SandboxMessageCommand.init && message.instanceId === instanceId)
            .take(1)
            .subscribe({
              next: listener,
              error: (e) => console.error(e),
              complete: () => null
            })
        },
        stop () {
github aronallen / cycle-sandbox / src / main.ts View on Github external
[key]: connectors[key](
                      receivePorts[key],
                      sendPorts[key]
                    )(sources[key])
                  }
                }
              }, {});
            observer.next(sinks);
            const startMessage: SandboxMessage = {
              cmd: SandboxMessageCommand.start,
              instanceId,
            }
            worker.postMessage(startMessage);
          }

          subscription = fromEvent(worker, 'message')
            .map((event: MessageEvent) => event.data as SandboxMessage)
            .filter(message => message.cmd === SandboxMessageCommand.init && message.instanceId === instanceId)
            .take(1)
            .subscribe({
              next: listener,
              error(error) { console.error(error) },
              complete() { }
            })
        },
        stop() {
github aronallen / cycle-sandbox / src / default / main.ts View on Github external
if (stream$) {
            tx.start();
            sender = stream$.subscribe({
            next(event) {
              tx.postMessage(event);
            },
            error(error) {
              console.error(error);
            },
            complete() {

            }
          });
        } {
          rx.start();
          receiver = fromEvent(rx, 'message')
          .subscribe({
            next(event: MessageEvent) {
              observer.next(event.data);
            },
            error(error) {
              console.error(error);
            },
            complete() {

            }
          });
        }
      },
      stop() {
github aronallen / cycle-sandbox / src / worker.ts View on Github external
export function setup (
  component: (Sources: Sources) => FantasySinks,
  drivers: Drivers,
  connectors: WorkerConnectors = {},
  run = defaultRun
) {
  fromEvent(self, 'message')
    .map((event: MessageEvent) => event.data as SandboxMessage)
    .filter(message => message.cmd === SandboxMessageCommand.init)
    .subscribe({
      next: message => {
      let dispose: DisposeFunction;
      const instanceId = message.instanceId;
      const receivePorts = message.ports;
      let channels = createChannels(Object.keys(receivePorts));
      const transferPorts = portMap(channels, 2);
      const sendPorts = portMap(channels, 1);
      const initMessage: SandboxMessage = {
        cmd: SandboxMessageCommand.init,
        ports: transferPorts,
        instanceId
      };
      self.postMessage(initMessage, Object.values(transferPorts));
github aronallen / cycle-sandbox / src / sandbox.ts View on Github external
start(observer) {
      rx.start();
      tx.start();
      listener = fromEvent(rx, 'message')
        .map(e => e.data as WorkerDOMMessage)
        .subscribe({
          next (message) {
            if (message.cmd === WorkerDOMMessageCommand.vnode) {
              const vnode = message.payload as VNode;
              observer.next(vnode);
            } else if (message.cmd === WorkerDOMMessageCommand.attach) {
              const payload = message.payload as WorkerDOMAttachMessage;
              const options = payload.options || {};
              attachments[payload.listenerId] = (xs.from(source
                .select(payload.selector)
                .events(payload.events, options.useCapture)) as FantasyObservable)
                .subscribe({
                  next (event: Event) {
                    if (options.preventDefault) {
                      event.preventDefault();
github aronallen / cycle-sandbox / src / dom / main.ts View on Github external
start(observer) {
        rx.start();
        tx.start();
        listener = fromEvent(rx, 'message')
          .map(e => e.data as WorkerDOMMessage)
          .subscribe({
            next(message) {
              if (message.cmd === WorkerDOMMessageCommand.vnode) {
                const vnode = message.payload as VNode;
                observer.next(vnode);
              } else if (message.cmd === WorkerDOMMessageCommand.attach) {
                const payload = message.payload as WorkerDOMAttachMessage;
                const options = payload.options || {};
                attachments[payload.listenerId] = (xs.from(source
                  .select(payload.selector)
                  .events(payload.events, options.useCapture)) as FantasyObservable)
                  .subscribe({
                    next(event: Event) {
                      if (options.preventDefault) {
                        event.preventDefault();
github aronallen / cycle-sandbox / src / worker.ts View on Github external
next: message => {
      let dispose: DisposeFunction;
      const instanceId = message.instanceId;
      const receivePorts = message.ports;
      let channels = createChannels(Object.keys(receivePorts));
      const transferPorts = portMap(channels, 2);
      const sendPorts = portMap(channels, 1);
      const initMessage: SandboxMessage = {
        cmd: SandboxMessageCommand.init,
        ports: transferPorts,
        instanceId
      };
      self.postMessage(initMessage, Object.values(transferPorts));

      const start$ = fromEvent(self, 'message')
        .map((event: MessageEvent) => event.data as SandboxMessage)
        .filter(message => message.instanceId === instanceId && message.cmd === SandboxMessageCommand.start)
        .mapTo(SandboxMessageCommand.start);
        
        
      
      const stop$ = fromEvent(self, 'message')
        .map((event: MessageEvent) => event.data as SandboxMessage)
        .filter(message => message.instanceId === instanceId && message.cmd === SandboxMessageCommand.stop)
        .mapTo(SandboxMessageCommand.stop);
        
      
      start$
        .endWhen(stop$)
        .subscribe({
          next () {