-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
5,695 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
module.exports = { | ||
root: true, | ||
parser: "babel-eslint", | ||
parserOptions: { | ||
ecmaVersion: 2017, | ||
sourceType: 'module' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { task as ecTask } from 'ember-concurrency'; | ||
|
||
function extractValue(desc) { | ||
return desc.value || | ||
(typeof desc.initializer === 'function' && desc.initializer()); | ||
} | ||
|
||
function isDescriptor(item) { | ||
return item && | ||
typeof item === 'object' && | ||
'writable' in item && | ||
'enumerable' in item && | ||
'configurable' in item; | ||
} | ||
|
||
function handleDescriptor(callback, target, key, desc, params = []) { | ||
return { | ||
enumerable: desc.enumerable, | ||
configurable: desc.configurable, | ||
writeable: desc.writeable, | ||
initializer: function() { | ||
if (!desc.writable) { | ||
throw new Error('ember-concurrency-decorators does not support using getters and setters'); | ||
} | ||
|
||
let value = extractValue(desc); | ||
return callback(value, params); | ||
} | ||
}; | ||
} | ||
|
||
function decorator(callback) { | ||
return function(...params) { | ||
// determine if user called as @task('blah', 'blah') or @task | ||
if (isDescriptor(params[params.length - 1])) { | ||
return handleDescriptor(callback, ...arguments); | ||
} else { | ||
return function(/* target, key, desc */) { | ||
return handleDescriptor(callback, ...arguments, params); | ||
}; | ||
} | ||
}; | ||
} | ||
|
||
|
||
function taskify(value) { | ||
return (typeof value === 'function') ? ecTask(value) : value; | ||
} | ||
|
||
export const task = decorator(v => taskify(v)); | ||
export const restartableTask = decorator(v => taskify(v).restartable()); | ||
export const dropTask = decorator(v => taskify(v).drop()); | ||
export const keepLatestTask = decorator(v => taskify(v).keepLatest()); | ||
export const enqueueTask = decorator(v => taskify(v).enqueue()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/*eslint-disable*/ | ||
|
||
import Ember from 'ember'; | ||
import { timeout } from 'ember-concurrency'; | ||
import { | ||
task, | ||
restartableTask, | ||
dropTask, | ||
keepLatestTask, | ||
enqueueTask, | ||
maxConcurrency | ||
} from 'ember-concurrency-decorators'; | ||
import { module, test } from 'qunit'; | ||
|
||
module('Unit: decorators'); | ||
|
||
test("a plethora of decorators", function(assert) { | ||
assert.expect(1); | ||
|
||
let Obj = Ember.Object.extend({ | ||
@task | ||
doStuff: function * () { | ||
return 123; | ||
}, | ||
|
||
@restartableTask | ||
a: function * () { }, | ||
|
||
@keepLatestTask | ||
b: function * () { }, | ||
|
||
@dropTask | ||
c: function * () { }, | ||
|
||
@enqueueTask | ||
d: function * () { }, | ||
}); | ||
|
||
let obj; | ||
Ember.run(() => { | ||
obj = Obj.create(); | ||
obj.get('doStuff').perform(); | ||
}); | ||
assert.equal(obj.get('doStuff.last.value'), 123); | ||
}); | ||
|
Oops, something went wrong.