-
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
Showing
3 changed files
with
76 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Utility mechanisms to manage the time-wise execution of functions. | ||
*/ | ||
export abstract class Schedule { | ||
/** | ||
* Gives back a modification of the passed function, that whenever called will only execute after | ||
* a specified time period has elapsed. | ||
* @param callback The function to be modified | ||
* @param wait The waiting time period in milliseconds | ||
* @param context The this-context of the function | ||
* @returns The modified function | ||
*/ | ||
public static postpone( | ||
callback: ( ...p: any[] ) => void, wait: number, context: object | null = null | ||
): ( ...q: any[] ) => void { | ||
return ( ...args: any[] ): void => { | ||
setTimeout( (): void => { | ||
callback.apply( context, args ); | ||
}, wait ); | ||
}; | ||
} | ||
} |
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,53 @@ | ||
import {expect} from 'chai'; | ||
|
||
import {Schedule} from '../lib/schedule'; | ||
|
||
describe( 'Schedule', () => { | ||
describe( 'postpone', () => { | ||
it( 'should call the callback with the passed parameters', ( done: any ) => { | ||
// arrange | ||
const inputs = [ 1, 2, 3 ]; | ||
const callback = ( ...args: any[] ): void => { | ||
// assert | ||
expect( args ).to.deep.equal( inputs ); | ||
done(); | ||
}; | ||
|
||
// act | ||
const postponedFunction = Schedule.postpone( callback, 40 ); | ||
postponedFunction( ...inputs ); | ||
} ); | ||
|
||
it( 'should call with context null if not passed', ( done: any ) => { | ||
// arrange | ||
class TestClass { | ||
public method(): void { | ||
// assert | ||
expect( this ).to.be.null; | ||
done(); | ||
} | ||
} | ||
const test = new TestClass(); | ||
|
||
// act | ||
const postponedFunction = Schedule.postpone( test.method, 40 ); | ||
postponedFunction(); | ||
} ); | ||
|
||
it( 'should call with context of test class when passed', ( done: any ) => { | ||
// arrange | ||
class TestClass { | ||
public method(): void { | ||
// assert | ||
expect( this ).to.be.instanceof( TestClass ); | ||
done(); | ||
} | ||
} | ||
const test = new TestClass(); | ||
|
||
// act | ||
const postponedFunction = Schedule.postpone( test.method, 40, test ); | ||
postponedFunction(); | ||
} ); | ||
} ); | ||
} ); |