https://github.com/faressoft/ticks-tracer
Takes snapshots for an object across event loop ticks
https://github.com/faressoft/ticks-tracer
async control debug fake flow nexttick process promise setimmediate snapshot test tick timer trace unit-testing
Last synced: about 1 year ago
JSON representation
Takes snapshots for an object across event loop ticks
- Host: GitHub
- URL: https://github.com/faressoft/ticks-tracer
- Owner: faressoft
- License: mit
- Created: 2018-07-02T14:40:53.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-09-01T15:27:57.000Z (almost 8 years ago)
- Last Synced: 2025-03-24T02:37:20.039Z (about 1 year ago)
- Topics: async, control, debug, fake, flow, nexttick, process, promise, setimmediate, snapshot, test, tick, timer, trace, unit-testing
- Language: JavaScript
- Size: 12.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Ticks Tracer
[](https://www.npmjs.com/package/ticks-tracer)
[](https://github.com/faressoft/ticks-tracer/blob/master/LICENSE)
> Takes snapshots for an object across event loop ticks.
# Hint
Good for testing flow control packages like `flowa`, `async`, `q`, etc.
# Table of Contents
* [Installation](#installation)
* [Usage](#usage)
* [API](#api)
* [License](#license)
## Installation
```
npm install --save ticks-tracer
```
## Usage
Take snapshot on every `check` phase of the event loop (using `setImmediate` internally).
```js
var TicksTracer = require('ticks-tracer');
// A flow control package
var Flowa = require('flowa');
// The object to trace
var context = {};
// A dummy flow
var flow = new Flowa({
type: 'serial',
task1: generateDummyTask(1),
task2: generateDummyTask(2),
group1: {
type: 'parallel',
task3: generateDummyTask(3),
task4: generateDummyTask(4)
},
task5: generateDummyTask(5)
});
// Start tracing
var ticksTracer = new TicksTracer(context);
// Execute the tasks
flow.run(context).then(function(result) {
// Get the taken snapshots
console.log(ticksTracer.getSnapshots());
console.log(ticksTracer.getSnapshotsDiffs());
// Stop the tracing
ticksTracer.stop();
});
// Don't worry about this
function generateDummyTask(id) {
return function(context, callback) {
context['task' + id] = true;
setImmediate(callback);
};
}
```
The output is:
```
[
{},
{ task1: true },
{ task1: true, task2: true },
{ task1: true, task2: true, task3: true, task4: true },
{ task1: true, task2: true, task3: true, task4: true, task5: true }
]
```
## API
- TicksTracer(tracedObject)
To create a TicksTracer object and start tracing
- stop()
Stop tracing
-
getTicksCount() ⇒Number Get the current tick number
-
getSnapshotAt(tick) ⇒Object Get a taken snapshot by a tick number
-
getSnapshots() ⇒Array Get all taken snapshots indexed by ticks numbers
-
getSnapshotsDiffs() ⇒Array Get a list of snapshots that represent only the diffs
## TicksTracer(tracedObject)
To create a TicksTracer object and start tracing.
| Param | Type | Description |
|--------------|---------------------|---------------------------------|
| tracedObject | Object | The object to take snapshots of |
## stop()
Stop tracing.
## getTicksCount() ⇒ Number
Get the current tick number.
**Returns**: Number
## getSnapshotAt(tick) ⇒ Object
Get a taken snapshot by a tick number.
| Param | Type | Description |
|-------|---------------------|---------------------------------|
| tick | Number | The tick number |
**Returns**: Object
## getSnapshots() ⇒ Array
Get all taken snapshots indexed by ticks numbers.
**Returns**: Array
## getSnapshotsDiffs() ⇒ Array
Get a list of snapshots that represent only the diffs
**Returns**: Array
# License
This project is under the MIT license.