npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

task-collector

v0.0.4

Published

A typescript library providing abstraction layer for task collector system

Downloads

56

Readme

task-collector

简介

任务中断回溯机制数据抽象层 package。此包负责:

  • 统一 Task 抽象类,包括提供 TaskStatus 任务状态类。
  • 统一 Progress 抽象类,并提供两个常用的子类:
    • FakeProgress 提供假任务进度的能力
    • TimeProgress 提供时间进度条的能力
  • TaskGenerator 工厂方法,以及可被依赖注入的 TaskManager 基类服务。

使用方法

以 UploadTask 为例,我们需要实现一个上传任务。

首先,通过继承 Task 抽象类,以及通用的一些任务 interfaces(目前提供 RestorableTask,CancellableTask 和 NavigableTask),定义一个 UploadTask 类:

import {Task, CancellableTask} from 'task-collector';

export class UploadTask extends Task implements CancellableTask {
  //...
  
  constructor(
    private options: UploadTaskOptions,
    private dependencyService: DependencyService,
    private taskManager: TaskManager
  ) {
    super();
  }

  // 实现 start()、check() 等抽象方法
}

start() 方法里可直接开启 check() 进行接口轮询,以此获取任务最新状态。但需要注意数据多播的情况,这里建议返回 Observable 时使用 share 操作符。

Task 类里已经提前定义好 setProgress() 方法。在任务轮询接口没有提供进度信息的情况下,可以直接使用假进度;否则,请直接使用 this.progress$.next(progress),或继承抽象类 Progress 自定义进度信息计算方法 computeProgress 并重载 progressFactory

然后,根据 UploadTask 的依赖,我们需要实现 UploadTask 的工厂方法:

@Injectable({
  providedIn: 'root'
})
export class UploadTaskGenerator implements TaskGenerator {
  constructor(
    private dependencyService: DependencyService,
    private taskManager: TaskManager
  ) {}

  createTask(options: UploadTaskOptions): UploadTask {
    const task = new UploadArtifactTask(options, this.dependencyService, this.taskManager);
    this.taskManager.register(task);
    return task;
  }
}

最后一步,根据 TaskManagerBase 基类,定义 TaskManager 服务。

@Injectable({
  providedIn: 'root'
})
export class TaskManager extends TaskManagerBase {
  //...
  
  constructor(private injector: Injector) {
    super();
  }

  createTaskByGenerator<T extends TaskGenerator>(
    token: Type<T>,
    options: ParameterType<T['createTask']>
  ): ReturnType<T['createTask']> {
    try {
      const taskGenerator = this.injector.get<T>(token);
      return taskGenerator.createTask(options) as ReturnType<T['createTask']>;
    } catch (e) {
      throw e;
    }
  }
  
  //...
}

这里需要着重注意实现 createTaskByGenerator() 方法,外部需要通过该方法创建具体的 UploadTask 实例:

// 此时 task 是类型安全的
const uploadTask = this.taskManager.createTaskByGenerator(UploadTaskGenerator, {...});

uploadTask.start().subscribe(...);
uploadTask.statusChanged().subscribe(...);
uploadTask.progressChanged().subscribe(...);