Skip to content

Commit

Permalink
v0.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayMakhonin committed Jun 16, 2022
1 parent d722132 commit 57eaa8d
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 22 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@babel/runtime-corejs3": "^7.18.3",
"@flemist/copy-glob-flat": "^0.0.5",
"@flemist/karma-custom-launcher": "^0.0.0",
"@flemist/test-variants": "^0.0.2",
"@rollup/plugin-alias": "^3.1.9",
"@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-commonjs": "^22.0.0",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/async/priority-queue/PriorityQueue.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {PriorityQueue} from './PriorityQueue'
import {priorityCreate} from 'src/sync/priority'

describe('helpers > PriorityQueue', function () {
describe('priority-queue > PriorityQueue', function () {
it('base', async function () {
const queue = new PriorityQueue()
const log = []
Expand Down
6 changes: 6 additions & 0 deletions src/async/time-controller/contracts.ts
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)
}

17 changes: 17 additions & 0 deletions src/async/time-controller/timeControllerDefault.ts
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)
},
}
82 changes: 82 additions & 0 deletions src/async/time-controller/timeControllerMock.test.ts
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]],
],
})
})
})
81 changes: 81 additions & 0 deletions src/async/time-controller/timeControllerMock.ts
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]
}
}
5 changes: 4 additions & 1 deletion src/async/time-limits/TimeLimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ export class TimeLimit implements ITimeLimit {
private readonly _maxCount: number
private readonly _timeMs: number
private readonly _priorityQueue: PriorityQueue
private readonly _setTimeout: (func: () => void, timeout?: number) => void

constructor({
maxCount,
timeMs,
priorityQueue,
setTimeout,
}: {
maxCount: number,
timeMs: number,
priorityQueue?: PriorityQueue,
setTimeout?: (func: () => void, timeout?: number) => void,
}) {
this._maxCount = maxCount
this._timeMs = timeMs
Expand Down Expand Up @@ -72,7 +75,7 @@ export class TimeLimit implements ITimeLimit {
return result
}
finally {
setTimeout(this._releaseFunc, this._timeMs)
this._setTimeout(this._releaseFunc, this._timeMs)
}
}
}
2 changes: 1 addition & 1 deletion src/async/time-limits/TimeLimits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {delay} from 'src/async/delay'
import {PriorityQueue} from 'src/async/priority-queue'
import {priorityCreate} from 'src/sync/priority'

describe('helpers > TimeLimits', function () {
describe('time-limits > TimeLimits', function () {
this.timeout(300000)
type Mode = 'sync' | 'async' | 'random'

Expand Down
2 changes: 1 addition & 1 deletion src/sync/pairing-heap/PairingHeap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class PairingHeapTester<TItem> {
}
}

describe('helpers > PairingHeap', function () {
describe('pairing-heap > PairingHeap', function () {
this.timeout(6000000)

let totalTests = 0
Expand Down
2 changes: 1 addition & 1 deletion src/sync/priority/Priority.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Priority, priorityCompare, priorityCreate} from './Priority'

describe('helpers > Priority', function () {
describe('priority > Priority', function () {
function test(o1: Priority, o2: Priority, checkResult: -1 | 0 | 1) {
let result = priorityCompare(o1, o2)
assert.strictEqual(result, checkResult)
Expand Down
17 changes: 0 additions & 17 deletions src/test/delay.ts

This file was deleted.

0 comments on commit 57eaa8d

Please sign in to comment.