-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
d722132
commit 57eaa8d
Showing
12 changed files
with
204 additions
and
22 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,6 @@ | ||
export interface ITimeController { | ||
now(): number | ||
setTimeout(handler: () => void, timeout: number): number | ||
clearTimeout(handle: number) | ||
} | ||
|
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,17 @@ | ||
import {ITimeController} from './contracts' | ||
|
||
export const timeControllerDefault: ITimeController = { | ||
now: function now() { | ||
return Date.now() | ||
}, | ||
setTimeout: typeof window === 'undefined' | ||
? setTimeout | ||
: function setTimeout() { | ||
return setTimeout.apply(window, arguments) | ||
}, | ||
clearTimeout: typeof window === 'undefined' | ||
? clearTimeout | ||
: function clearTimeout() { | ||
return clearTimeout.apply(window, arguments) | ||
}, | ||
} |
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,82 @@ | ||
import {createTestVariantsSync} from '@flemist/test-variants' | ||
import {TimeControllerMock} from './timeControllerMock' | ||
import {ITimeController} from './contracts' | ||
import {timeControllerDefault} from './timeControllerDefault' | ||
|
||
describe('time-controller > timeControllerMock', function () { | ||
function test({ | ||
timeController, | ||
times, | ||
postDelay, | ||
}: { | ||
timeController: ITimeController, | ||
times: [start: number|null, timeout: number, abort: number|null][], | ||
postDelay: number, | ||
}) { | ||
return new Promise<string[]>((resolve, reject) => { | ||
const result: string[] = [] | ||
let timeMax = 0 | ||
for (let i = 0, len = times.length; i < len; i++) { | ||
const time = times[i] | ||
const index = i | ||
timeMax = Math.max((time[0] || 0) + Math.max(time[1] || 0, time[2] || 0)) | ||
|
||
const start = () => { | ||
const handle = timeController.setTimeout(() => { | ||
result.push(`completed: ${index}`) | ||
}, time[1]) | ||
|
||
if (time[2] != null) { | ||
if (time[2] < 0) { | ||
timeController.clearTimeout(handle) | ||
} | ||
else { | ||
timeController.setTimeout(() => { | ||
timeController.clearTimeout(handle) | ||
result.push(`aborted: ${index}`) | ||
}, time[2]) | ||
} | ||
} | ||
} | ||
|
||
if (time[0] == null) { | ||
start() | ||
} | ||
else { | ||
timeController.setTimeout(start, time[0]) | ||
} | ||
} | ||
|
||
timeController.setTimeout(() => { | ||
resolve(result) | ||
}, timeMax + postDelay) | ||
}) | ||
} | ||
|
||
const testVariants = createTestVariantsSync(({ | ||
times, | ||
}: { | ||
times: [start: number|null, timeout: number, abort: number|null][], | ||
}) => { | ||
const timeController = new TimeControllerMock() | ||
const actual = test({ | ||
timeController, | ||
times, | ||
postDelay: 100000000, | ||
}) | ||
const expected = test({ | ||
timeController: timeControllerDefault, | ||
times, | ||
postDelay : 100, | ||
}) | ||
assert.deepStrictEqual(actual, expected) | ||
}) | ||
|
||
it('base', async function () { | ||
testVariants({ | ||
times: [ | ||
[[0, 0, null]], | ||
], | ||
}) | ||
}) | ||
}) |
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,81 @@ | ||
import {ITimeController} from './contracts' | ||
|
||
interface IHandler { | ||
id: number, | ||
time: number, | ||
handler: () => void, | ||
} | ||
|
||
function compareHandlers(o1: IHandler, o2: IHandler): number { | ||
if (o1.time > o2.time) { | ||
return 1 | ||
} | ||
if (o1.time < o2.time) { | ||
return -1 | ||
} | ||
if (o1.id > o2.id) { | ||
return 1 | ||
} | ||
if (o1.id < o2.id) { | ||
return -1 | ||
} | ||
|
||
throw new Error('Duplicate timing handlers') | ||
} | ||
|
||
export class TimeControllerMock implements ITimeController { | ||
private _handlers: IHandler[] = [] | ||
private _now: number = 1 | ||
private _nextId: number = 0 | ||
|
||
addTime(time: number) { | ||
this.setTime(this._now + time) | ||
} | ||
|
||
setTime(time: number) { | ||
if (time <= 0) { | ||
throw new Error(`time (${time} should be > 0)`) | ||
} | ||
const {_handlers, _now: now} = this | ||
|
||
while (true) { | ||
let minHandler: IHandler | ||
for (const id in _handlers) { | ||
if (Object.prototype.hasOwnProperty.call(_handlers, id)) { | ||
const handler = _handlers[id] | ||
if (handler.time <= time && (!minHandler || compareHandlers(handler, minHandler) < 0)) { | ||
minHandler = handler | ||
} | ||
} | ||
} | ||
|
||
if (!minHandler) { | ||
break | ||
} | ||
|
||
delete _handlers[minHandler.id] | ||
this._now = minHandler.time | ||
minHandler.handler() | ||
} | ||
|
||
this._now = time | ||
} | ||
|
||
now(): number { | ||
return this._now | ||
} | ||
|
||
setTimeout(handler: () => void, timeout: number): number { | ||
const id = this._nextId++ | ||
this._handlers[id] = { | ||
id, | ||
time: this._now + timeout, | ||
handler, | ||
} | ||
return id | ||
} | ||
|
||
clearTimeout(handle: number) { | ||
delete this._handlers[handle] | ||
} | ||
} |
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
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 was deleted.
Oops, something went wrong.