[RxJS] Schedular basic

复制代码
  subscribeOn() {
    // Changes source execution
    // only used once
    of(1).pipe(
      subscribeOn(async)
    )
    .subscribe({
      next: x => console.log(x),
      complete: () => console.log('3')
    })
    console.log('2')

    //2 1 3
  }

  observeOn() {
    // Changes notifications execution  (next, error, complete)
    // Can be used before each operators
  }

  queueSchedular() {
    // execute synchronously
    // tasks execute in order
    // waits until current task ends before exeucting next one
    // performant (precedes event loop)

    queue.schedule(() => console.log(1)) // sync
    console.log(2) // sync
    queue.schedule(() => console.log(3)) // sync
    
    // 1 2 3

    queue.schedule(() => {
      // when nested queue, change behavior
      queue.schedule(() => console.log(1)) // async
      console.log(2) // sync
      queue.schedule(() => console.log(3)) // async
    })

    // 2 1 3
  }

  asapSchedular() {
    // executes asynchronously (micro)
    // Tasks execute before next tick
    // Relays on Promises
    // Performant (precedes event loop)
    setTimeout(() => console.log(1)) // macro
    asap.schedule(() => console.log(2)) // micro
    queue.schedule(() => console.log(3)) // snyc

    // 3 2 1
  }

  asyncSchedular() {
    // Executes asynchronously (macro)
    // Relays on setInterval
    // less performant (use event loop)
    async.schedule(() => console.log(1)) // macro
    asap.schedule(() => console.log(2)) // micro
    queue.schedule(() => console.log(3)) // snyc

    // 3 2 1
  }

  cancelling() {
    const s = new AsyncScheduler(AsyncAction)
    const DELAY = 0;
    let subscription = s.schedule(v => console.log(v), DELAY, '1')
    s.schedule(v => console.log(v), DELAY, '2')
    console.log('3')
    subscription.unsubscribe();

    // 3 2 
    // 1 got cancelled
  }

  internalTime() {
    const s = new AsyncScheduler(AsyncAction)
    const DELAY = 2000; 
    const start = Date.now()

    s.schedule(v => console.log(v), DELAY, 1)
    s.schedule(v => console.log(v), DELAY, 2)
    s.schedule(v => console.log(`${s.now() - start}ms`), DELAY)
    console.log(3)

    // 3 1 2 2008ms
  }
复制代码

 

posted @   Zhentiw  阅读(183)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-04-28 [Vuex] Lazy Load a Vuex Module at Runtime using TypeScript
2019-04-28 [Vuex] Split Vuex Store into Modules using TypeScript
2019-04-28 [Vuex] Perform Async Updates using Vuex Actions with TypeScript
2019-04-28 [Vuex] Modify State by using Vuex Mutations with TypeScript
2019-04-28 [Vuex] Access State by using Vuex getter functions with TypeScript
2017-04-28 [TypeScript] Find the repeated item in an array using TypeScript
2016-04-28 [AngularJS] angular-md-table for Angular material design
点击右上角即可分享
微信分享提示