New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

timesimmer

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

timesimmer

Handles simulated time running at a different rate to real time

latest
Source
npmnpm
Version
1.0.4
Version published
Maintainers
1
Created
Source

timesimmer

A sim time simulation that supports

  • adjustable rate and tickrate
  • events
  • cycling within a fixed period
  • resetting of rates within a period

Timesimmer will create a continuous timer that runs at any rate x real time. Because it keeps a separate clock going, you can change the rate while it's running without affecting elapsed time that has gone before. An example use case might be

  • your app runs a simulation at 60 * real time - so 1 hour passes in 1 minute
  • it also provides a slider to adjust the rate
  • the timer needs to remember how long has passed at every rate, so it's not just a matter of multiplying the elapsed time ny the new rate.

More details on qottle can be found at https://ramblings.mcpher.com/gassnippets2/timesimmer/

Installation

yarn add timesimmer

Usage

const TimeSimmer = require('timesimmer');

You can use the default options or add options on the constructor such as

const s = new TimeSimmer({
    // speed up time so 1 second = 10 minutes
    rate: 60 * 10,
    // tick every second
    tickRate: 1000,
    // when the timer should start from
    startedAt: new Date(2001, 0,1).getTime(),
    // restart time every hour
    cycle: 60 * 60 * 1000
})

Cycling

Normally time is counted from the startedAt option (or now by default). For some use cases, it's possible that the time cycles within a given period (for example a weekly schedule), so you can use the cycle option to say at what point to reset the time back to the startedAt value

Options

Most options can be applied when the timer is initialized, or set while the timer is running

optiondefaultpurpose
rate60Factor for how much to speed up time by - so 60 makes a minute pass in a second
tickRate1000How often to update the simulated time. This also controls how often the tick event is fired
startedAtnew Date().getTime()The time to start the timer from
immediatetruewhether to start the timer immediately on instanciation
cycle0if non-zero, the timer will be restarted to startedAt on the tick event that detects these number of ms have passed
carryForwardOnCyclefalseif cycling is happening, if the cycle time isnt a multiple of the rate, you might have some excess time on a tick. Normally a recycle will reset the timer to 0, but you can carry forward any excess time by setting this value

Events

Events are checked for triggering at every tick event. Adding events like this adds to existing events - so multiple listeners can be activated by a single event.

  s.on("tick", (simPack) => {
    console.log(`tick number ${simPack.ticker} was triggered at sim time ${simPack.date.toString()}`)
  });

or

  s.on("cycle", (simPack) => {
    console.log(`recycled to sim time ${simPack.date.toString()}`)
  });

To clear all registered events for a given eventName use .off, eg..

  s.off("cycle")
eventNametriggered on
tickevery tickRate ms
cyclethere's been a reset of the timer back to start. A tick event will also be fired just before a recycle

Simpack

A simPack is retured as an event payload

propertycontent
timethe current sim time in ms
ratethe current rate
tickerhow many times there's been a tick event. This is reset on a cycle event and will be 0
startedAtwhen the current cycle started in real time
cyclethe current cycle option value
kickoffthe original startedAt time. It will be equal to startedAt if cycle is 0
tickRatehow often the time is updated in ms
eventNamethe event
allTimethe sim time passed since instanciation
realTimeStartedAtwhen it actually started
realTimeElapsedhow long its been running for in real time
carryFowardOnCyclewhether to carry forward any excess sim time when a recycle happens
datethe sim time as a date

properties

A simPack (described earlier) is a collection of properties returned in a single object. Most properties can also be set or get individually, for example

  s.rate = 20
  console.log(s.date)

methods

methodcontentreturns
stop ()stop the timerself
start()start the timerself
on(eventName: string, listener: function)add a listener to be executed when a given eventName triggersself
off(eventName: string)remove all listeners for the given eventNameself
ms(conversionName: string, [value=1])convert to and from msnumber

convenience time conversion

Since there's a lot of conversions, a convenience ms to to other measures are provided as a static method, but also accessible from an instance. For example to get one day in ms

  s.ms ('days')

or 10 hours in ms

  s.ms ('hours', 10)

or can also be called as a static method

  TimeSimmer.ms('weeks', 3)

To convert back the other way, just stick 'ms' in front of the conversion name. For example to convert 200000ms to weeks.

  s.ms('msWeeks', 200000)

It's not rocket science, but it does help to document when instead of defining a simmer like this

const s = new TimeSimmer({
    // speed up time to run at 100 times normal speed
    rate: 100,
    // tick every second
    tickRate: 1000,
    // restart time every 2 hours
    cycle: 60 * 60 * 1000 * 2
})

You can do this

const ms = {TimeSimmer}
const s = new TimeSimmer({
    rate: 100,
    tickRate: ms('seconds'),
    cycle: ms('hours', 2)
})

and you can interpret results like this

  const hours = ms('msHours', s.time)

Here's the full list of conversions

conversion namereturns
secondsms
minutesms
hoursms
daysms
weeksms
msSecondsseconds
msMinutesminutes
msHourshours
msDaysdays
msWeeksweeks

Examples

See the test.js for many examples

Simple time speed up

This example makes an hour pass in a second, and you can use the ticker event to update a simulated clock every 500ms, where the start date is now

set up the timer

  const s = new TimeSimmer({
    rate: 60 * 60,
    tickRate: 500
  });

add a tick event

  s.on("tick", simPack => {
    updateYourUiClock(simPack.date)
  })

recycler

This example could be used to re-use a hourly schedule. A minute will pass in a second, and it will reset every hour, and tick every 2 seconds

set up the timer

  const ms = {TimeSimmer}
  const s = new TimeSimmer({
    rate: 60,
    tickRate: ms("seconds", 2)
    cycle: ms ("hours"),
    startedAt: new Date(2001, 11, 17, 8, 0, 0).getTime()
  });

add tick events

  s.on("tick", simPack => {
    // will fire every 2 secs, and 2 minutes will have passed
    reuseYourSchedule(simPack.date)
  })
  s.on("cycle", ({date})=> {
    console.log(`the timer restarted back to ${date.toString()})
  })

FAQs

Package last updated on 16 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts