Skip to content

Commit

Permalink
feat(task): add start, success & failure methods (#8061)
Browse files Browse the repository at this point in the history
* feat(task): add start, success & failure methods

Allow managing the task manually instead of using `.run()` and `.runInside()`.
  • Loading branch information
julien-f authored Oct 22, 2024
1 parent a71e78f commit ba3753a
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 5 deletions.
9 changes: 9 additions & 0 deletions @vates/task/.USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ const result = await task.runInside(fn)
// if fn rejects, the task will be marked as failed
// if fn resolves, the task will be marked as succeeded
const result = await task.run(fn)

// manually starts the task
task.start()

// manually finishes the task in success state
task.success(result)

// manually finishes the task in failure state
task.failure(error)
```

Inside a task:
Expand Down
9 changes: 9 additions & 0 deletions @vates/task/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ const result = await task.runInside(fn)
// if fn rejects, the task will be marked as failed
// if fn resolves, the task will be marked as succeeded
const result = await task.run(fn)

// manually starts the task
task.start()

// manually finishes the task in success state
task.success(result)

// manually finishes the task in failure state
task.failure(error)
```

Inside a task:
Expand Down
24 changes: 19 additions & 5 deletions @vates/task/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,22 @@ class Task {
#end(status, result) {
assert.equal(this.#status, PENDING)

this.#status = status
this.#emit('end', { status, result })
this.#onProgress = alreadyEnded
this.#status = status
}

failure(error) {
this.#end(FAILURE, error)
}

info(message, data) {
this.#emit('info', { data, message })
}

#maybeStart() {
const startData = this.#startData
if (startData !== undefined) {
this.#startData = undefined
this.#emit('start', startData)
if (this.#startData !== undefined) {
this.start()
}
}

Expand Down Expand Up @@ -181,6 +183,18 @@ class Task {
this.#emit('property', { name, value })
}

start() {
const startData = this.#startData
assert.notEqual(startData, undefined, 'task has already started')

this.#startData = undefined
this.#emit('start', startData)
}

success(result) {
this.#end(SUCCESS, result)
}

warning(message, data) {
assert.equal(this.status, PENDING)

Expand Down
76 changes: 76 additions & 0 deletions @vates/task/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,36 @@ describe('Task', function () {
})
})

describe('#failure()', function () {
const error = new Error()

it('throws if the task has not started yet', function () {
const task = createTask()

assert.throws(() => task.failure(error), { message: 'task has not started yet' })
})

it('throws if the task has already finished', function () {
const task = createTask()
task.start()
task.success()

assert.throws(() => task.failure(error), { code: 'ERR_ASSERTION' })
})

it('finishes the task and mark it as failed', function () {
const task = createTask()
task.start()
task.failure(error)

assertEvent(task, {
status: 'failure',
result: error,
type: 'end',
})
})
})

describe('.info()', function () {
it('does nothing when run outside a task', function () {
Task.info('foo')
Expand Down Expand Up @@ -244,6 +274,52 @@ describe('Task', function () {
})
})

describe('#start', function () {
it('starts the task', function () {
const task = createTask()
task.start()

assertEvent(task, { type: 'start' })
})

it('throws when the task has already started', function () {
const task = createTask()
task.start()

assert.throws(() => task.start(), { message: 'task has already started' })
})
})

describe('#success()', function () {
const result = {}

it('throws if the task has not started yet', function () {
const task = createTask()

assert.throws(() => task.success(result), { message: 'task has not started yet' })
})

it('throws if the task has already finished', function () {
const task = createTask()
task.start()
task.success()

assert.throws(() => task.success(result), { code: 'ERR_ASSERTION' })
})

it('finishes the task and mark it as successful', function () {
const task = createTask()
task.start()
task.success(result)

assertEvent(task, {
status: 'success',
result,
type: 'end',
})
})
})

describe('.warning()', function () {
it('does nothing when run outside a task', function () {
Task.warning('foo')
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
<!--packages-start-->


- @vates/task minor
- @xen-orchestra/backups patch
- @xen-orchestra/fs minor
- @xen-orchestra/log minor
Expand Down

0 comments on commit ba3753a

Please sign in to comment.