aneka 0.16.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGES.md +32 -27
  2. package/docs/arrange-array.js.html +2 -2
  3. package/docs/create-class.js.html +2 -2
  4. package/docs/current-loc.js.html +2 -2
  5. package/docs/data/search.json +1 -1
  6. package/docs/defaults-deep.js.html +2 -2
  7. package/docs/delay.js.html +6 -3
  8. package/docs/extract-text.js.html +2 -2
  9. package/docs/fill-object.js.html +2 -2
  10. package/docs/filter-duplicate.js.html +2 -2
  11. package/docs/filter-index.js.html +2 -2
  12. package/docs/format-text.js.html +2 -2
  13. package/docs/generate-id.js.html +2 -2
  14. package/docs/get-caller-filename.js.html +2 -2
  15. package/docs/get-global-module-dir.js.html +23 -0
  16. package/docs/get-index.js.html +2 -2
  17. package/docs/get-key-by-value.js.html +2 -2
  18. package/docs/global.html +4 -4
  19. package/docs/includes.js.html +2 -2
  20. package/docs/index.html +2 -2
  21. package/docs/is-class.js.html +2 -2
  22. package/docs/is-set.js.html +2 -2
  23. package/docs/merge-objects-by-key.js.html +2 -2
  24. package/docs/paginate.js.html +2 -2
  25. package/docs/parse-args-argv.js.html +2 -2
  26. package/docs/parse-duration.js.html +2 -2
  27. package/docs/parse-env.js.html +2 -2
  28. package/docs/parse-kv-string.js.html +2 -2
  29. package/docs/parse-object.js.html +2 -2
  30. package/docs/pascal-case.js.html +2 -2
  31. package/docs/random-range.js.html +2 -2
  32. package/docs/resolve-path.js.html +2 -2
  33. package/docs/round.js.html +2 -2
  34. package/docs/sec-to-hms.js.html +2 -2
  35. package/docs/set-immediate.js.html +2 -2
  36. package/docs/set-interval.js.html +55 -0
  37. package/docs/set-timeout.js.html +37 -0
  38. package/docs/shim.js.html +2 -2
  39. package/docs/text-to-array.js.html +2 -2
  40. package/docs/titleize.js.html +2 -2
  41. package/docs/without.js.html +2 -2
  42. package/index.js +7 -0
  43. package/package.json +2 -1
  44. package/src/delay.js +4 -1
  45. package/src/set-interval.js +52 -0
  46. package/src/set-timeout.js +34 -0
  47. package/test/set-interval.js +180 -0
  48. package/test/set-timeout.js +120 -0
@@ -0,0 +1,120 @@
1
+ /* eslint-env mocha */
2
+ import { expect } from 'chai'
3
+ import lockfile from '@bybrave/proper-lockfile2'
4
+ import fn from '../src/set-timeout.js'
5
+
6
+ const originalSetTimeout = global.setTimeout
7
+ const originalClearTimeout = global.clearTimeout
8
+ const originalLock = lockfile.lock
9
+
10
+ function installTimerMock () {
11
+ const calls = []
12
+
13
+ global.setTimeout = (callback, ms, ...args) => {
14
+ calls.push({ callback, ms, args })
15
+ queueMicrotask(() => callback(...args))
16
+ return calls.length
17
+ }
18
+
19
+ global.clearTimeout = () => {}
20
+
21
+ return calls
22
+ }
23
+
24
+ describe('setTimeout', function () {
25
+ afterEach(function () {
26
+ global.setTimeout = originalSetTimeout
27
+ global.clearTimeout = originalClearTimeout
28
+ lockfile.lock = originalLock
29
+ })
30
+
31
+ it('returns a promise and resolves after the parsed timeout', async function () {
32
+ const timerCalls = installTimerMock()
33
+ const calls = []
34
+
35
+ const result = fn(() => calls.push('done'), '2s')
36
+
37
+ expect(result).to.be.instanceOf(Promise)
38
+ await result
39
+
40
+ expect(timerCalls).to.have.length(1)
41
+ expect(timerCalls[0].ms).to.equal(2000)
42
+ expect(calls).to.deep.equal(['done'])
43
+ })
44
+
45
+ it('passes args to the handler', async function () {
46
+ installTimerMock()
47
+ const calls = []
48
+
49
+ await fn((a, b, c) => calls.push([a, b, c]), 1, { args: [1, 'two', true] })
50
+
51
+ expect(calls).to.deep.equal([[1, 'two', true]])
52
+ })
53
+
54
+ it('binds the provided scope to the handler', async function () {
55
+ installTimerMock()
56
+ const scope = { value: 0 }
57
+
58
+ await fn(function (step) {
59
+ this.value += step
60
+ }, 1, { args: [3], scope })
61
+
62
+ expect(scope.value).to.equal(3)
63
+ })
64
+
65
+ it('locks and releases the lock file around the handler', async function () {
66
+ installTimerMock()
67
+ const calls = []
68
+ lockfile.lock = async (lockFile, lockFileOpts) => {
69
+ calls.push(['lock', lockFile, lockFileOpts])
70
+ return async () => {
71
+ calls.push(['release'])
72
+ }
73
+ }
74
+
75
+ await fn(() => calls.push(['handler']), 1, {
76
+ lockFile: '/tmp/aneka-set-timeout.lock',
77
+ lockFileOpts: { stale: 1000 }
78
+ })
79
+
80
+ expect(calls).to.deep.equal([
81
+ ['lock', '/tmp/aneka-set-timeout.lock', { stale: 1000 }],
82
+ ['handler'],
83
+ ['release']
84
+ ])
85
+ })
86
+
87
+ it('suppresses lock errors when silent is left at the default', async function () {
88
+ installTimerMock()
89
+ const calls = []
90
+ lockfile.lock = async () => {
91
+ throw new Error('lock failed')
92
+ }
93
+
94
+ await fn(() => calls.push('handler'), 1, {
95
+ lockFile: '/tmp/aneka-set-timeout.lock'
96
+ })
97
+
98
+ expect(calls).to.deep.equal([])
99
+ })
100
+
101
+ it('rethrows lock errors when silent is false', async function () {
102
+ installTimerMock()
103
+ const error = new Error('lock failed')
104
+ lockfile.lock = async () => {
105
+ throw error
106
+ }
107
+
108
+ let thrown
109
+ try {
110
+ await fn(() => {}, 1, {
111
+ lockFile: '/tmp/aneka-set-timeout.lock',
112
+ silent: false
113
+ })
114
+ } catch (err) {
115
+ thrown = err
116
+ }
117
+
118
+ expect(thrown).to.equal(error)
119
+ })
120
+ })