|
| 1 | +import { Database } from "./database"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Represents an async job in a {@link database.Database}. |
| 5 | + */ |
| 6 | +export class Job<T = any> { |
| 7 | + protected _id: string; |
| 8 | + protected _db: Database; |
| 9 | + protected _loaded: boolean = false; |
| 10 | + protected _result: T | undefined; |
| 11 | + |
| 12 | + /** |
| 13 | + * @internal |
| 14 | + */ |
| 15 | + constructor(db: Database, id: string) { |
| 16 | + this._db = db; |
| 17 | + this._id = id; |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Whether the job's results have been loaded. If set to `true`, the job's |
| 22 | + * result can be accessed from {@link Job.result}. |
| 23 | + */ |
| 24 | + get isLoaded(): boolean { |
| 25 | + return this._loaded; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * The job's result if it has been loaded or `undefined` otherwise. |
| 30 | + */ |
| 31 | + get result(): T | undefined { |
| 32 | + return this._result; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Loads the job's result from the database if it is not already loaded. |
| 37 | + * |
| 38 | + * @example |
| 39 | + * ```js |
| 40 | + * // poll for the job to complete |
| 41 | + * while (!job.isLoaded) { |
| 42 | + * await timeout(1000); |
| 43 | + * const result = await job.load(); |
| 44 | + * console.log(result); |
| 45 | + * } |
| 46 | + * // job result is now loaded and can also be accessed from job.result |
| 47 | + * console.log(job.result); |
| 48 | + * ``` |
| 49 | + */ |
| 50 | + async load(): Promise<T | undefined> { |
| 51 | + if (!this.isLoaded) { |
| 52 | + const res = await this._db.request( |
| 53 | + { |
| 54 | + method: "PUT", |
| 55 | + path: `/_api/job/${this._id}`, |
| 56 | + }, |
| 57 | + false |
| 58 | + ); |
| 59 | + if (res.statusCode !== 204) { |
| 60 | + this._loaded = true; |
| 61 | + this._result = res.body; |
| 62 | + } |
| 63 | + } |
| 64 | + return this._result; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Cancels the job if it is still running. Note that it may take some time to |
| 69 | + * actually cancel the job. |
| 70 | + */ |
| 71 | + cancel(): Promise<void> { |
| 72 | + return this._db.request( |
| 73 | + { |
| 74 | + method: "PUT", |
| 75 | + path: `/_api/job/${this._id}/cancel`, |
| 76 | + }, |
| 77 | + () => undefined |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Deletes the result if it has not already been retrieved or deleted. |
| 83 | + */ |
| 84 | + deleteResult(): Promise<void> { |
| 85 | + return this._db.request( |
| 86 | + { |
| 87 | + method: "DELETE", |
| 88 | + path: `/_api/job/${this._id}`, |
| 89 | + }, |
| 90 | + () => undefined |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Fetches the job's completion state. |
| 96 | + * |
| 97 | + * Returns `true` if the job has completed, `false` otherwise. |
| 98 | + * |
| 99 | + * @example |
| 100 | + * ```js |
| 101 | + * // poll for the job to complete |
| 102 | + * while (!(await job.getCompleted())) { |
| 103 | + * await timeout(1000); |
| 104 | + * } |
| 105 | + * // job result is now available and can be loaded |
| 106 | + * await job.load(); |
| 107 | + * console.log(job.result); |
| 108 | + * ``` |
| 109 | + */ |
| 110 | + getCompleted(): Promise<boolean> { |
| 111 | + return this._db.request( |
| 112 | + { |
| 113 | + path: `/_api/job/${this._id}`, |
| 114 | + }, |
| 115 | + (res) => res.statusCode !== 204 |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
0 commit comments