Gathering detailed insights and metrics for @teamteanpm2024/occaecati-quas-maxime
Gathering detailed insights and metrics for @teamteanpm2024/occaecati-quas-maxime
npm install @teamteanpm2024/occaecati-quas-maxime
Typescript
Module System
Node Version
NPM Version
54.5
Supply Chain
94.4
Quality
80.3
Maintenance
100
Vulnerability
100
License
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
Latest Version
1.0.6
Package Id
@teamteanpm2024/occaecati-quas-maxime@1.0.6
Unpacked Size
21.48 kB
Size
7.90 kB
File Count
6
NPM Version
10.5.0
Node Version
20.12.2
Publised On
29 Apr 2024
Cumulative downloads
Total Downloads
Last day
0%
0
Compared to previous day
Last week
0%
0
Compared to previous week
Last month
0%
0
Compared to previous month
Last year
0%
0
Compared to previous year
37
An object-focused alternative to Publisher / Subscriber models.
To offer a simple means of tracking a variable's initialization and subsequent changes.
The variable you're tracking can be a primitive or an object. This library is not opinionated about the data types you use.
1npm install @teamteanpm2024/occaecati-quas-maxime
1import ChangeTracker from '@teamteanpm2024/occaecati-quas-maxime'; 2 3// Init the tracker 4const myTrackedObject = new ChangeTracker(); 5 6// Set a value. This may be done at any time. We track the parent object reference. 7myTrackedObject.setValue({ name: 'John' }); 8 9// Log the value if already set; else, log it as soon as it's set. 10myTrackedObject.getOnce(({ name }) => { 11 console.log('-> [Logged once] Name:', name); 12}); 13 14// Log the value every time it changes. Log immediately if it's already been set. 15myTrackedObject.getEveryChange(({ name }) => { 16 console.log('-> [Logged every time] Name:', name); 17}); 18 19// Log the value next time it's set. Don't log if it's already been set. 20myTrackedObject.getNext(({ name }) => { 21 console.log('-> [Logged on next change] Name:', name); 22}); 23 24// You can directly check the last known value. This is undefined if not yet 25// set, else it contains the most recently set value. 26console.log('Cached name:', myTrackedObject.cachedValue?.name);
We are making a 3D video game and need to keep track of a spaceship. Game boot is asynchronous and spaceship load time cannot be determined. Additionally, some parts of our game need to be notified when the player switches to a different spaceship. To complicate matters, our main game loop runs at 60 frames per second from early on and cannot use asynchronous callbacks, but needs to render the ship as soon as it's ready.
Let's assume we have a spaceship class for loading our 3D spaceship. For the sake of demonstration, we'll define our ship as:
1class PlayerShip { 2 constructor() { console.log('New ship loaded.'); } 3 get name() { return 'Friday'; } 4 render() { /* do gfx magic */ } 5}
Let's write some code to track our game objects:
1// core.js 2 3import ChangeTracker from '@teamteanpm2024/occaecati-quas-maxime'; 4 5const gameObjects = { 6 playerShipTracker: new ChangeTracker(), 7}; 8 9export { 10 gameObjects, 11}
1// shipLoader.js 2 3// Create the spaceship: 4gameObjects.playerShipTracker.setValue(new PlayerShip()); 5// ^^ message is logged: 'New ship loaded.'
On first init, welcome the player:
1// boot.js 2 3// This is called only after initialization. If the player ship has already 4// been initialized, it'll call back immediately. 5gameObjects.playerShipTracker.getOnce((shipInstance) => { 6 alert('Welcome to The Space Game!'); 7 console.log('Player loaded', shipInstance.name); 8});
Every time the ship is loaded (including init), update the UI:
1// ui.js 2 3gameObjects.playerShipTracker.getEveryChange((shipInstance) => { 4 const shipNameDiv = document.getElementById('ship-name'); 5 if (shipNameDiv) { 6 shipNameDiv.innerHTML = shipInstance.name; 7 } 8});
From very early on, our GFX engine renders anything that can be rendered. It should render the ship as soon as it's loaded, but cannot use delayed callbacks for obvious timing reasons (it would queue a new callback 60 times a second, indefinitely, and then eventually have thousands of old requests trigger at once). Instead of keeping track of boot manually, you can get the latest known value of the ship. It will be undefined until the spaceship has loaded. Thereafter, we'll have a valid value.
1function renderGame() { 2 requestAnimationFrame(renderGame); 3 4 const ship = gameObjects.playerShipTracker.cachedValue; 5 if (ship) { 6 ship.render(); 7 } 8}
If you want better IDE autocompletion, you can instead use TypeScript in the initial definition. Here's an example of how you could rewrite the definition in the first example code block:
1interface TrackedPlayerShip extends ChangeTracker { cachedValue: PlayerShip; } 2 3const playerShipTracker: TrackedPlayerShip = new ChangeTracker(); 4 5const gameObjects = { playerShipTracker }; 6 7// gameObjects.playerShipTracker.cachedValue.<< auto-completes properties >>
Note that your transpiler will already need to be set up to take advantage of this. For reference, the config we used to build Change Tracker can be found here.
This library supports all modern browsers, and Node.js (with your choice of
import
or require
).
You may install the library like so:
1npm install @teamteanpm2024/occaecati-quas-maxime
Node.js:
1const ChangeTracker = require('@teamteanpm2024/occaecati-quas-maxime');
Browsers and modern Node.js:
1import ChangeTracker from '@teamteanpm2024/occaecati-quas-maxime';
For browsers, it is recommended you use a bundler like Webpack. If you insist on using the library in a browser without a bundler, you can source it from a CDN such UNPKG which will store the main class in window scope:
1<script src="https://unpkg.com/@teamteanpm2024/occaecati-quas-maxime@^1/browser.js"></script> 2<script> 3 const myVar = new ChangeTracker(); 4</script>
All functions are annotated with JSDoc comments so that your IDE can feed you manuals on the fly (triggered by Ctrl+Q in IntelliJ, for example).
Examples:
1import ChangeTracker from '@teamteanpm2024/occaecati-quas-maxime'; 2const trackedValue = new ChangeTracker();
1trackedValue.setValue({ 2 name: 'Joe', 3 updated: Date.now(), 4});
getOnce
will be triggered immediately:1trackedValue.getOnce((trackedValue) => { 2 console.log('Value has changed to:', trackedValue); 3 console.log('This function will not be notified again.'); 4});
getOnce
that has not yet been triggered, so long as you
keep a reference to the original function:1function onValueChange(trackedValue) { 2 console.log('Value has changed to:', trackedValue); 3 console.log('This function will not be notified again.'); 4} 5 6trackedValue.getOnce(onValueChange); 7trackedValue.removeGetOnceListener(onValueChange);
1function onValueChange(trackedValue) { 2 console.log('Value has changed to:', trackedValue); 3} 4 5trackedValue.getEveryChange(onValueChange); 6 7// [...] 8 9trackedValue.removeGetEveryChangeListener(onValueChange);
1function onValueChange(trackedValue) { 2 console.log('Value has changed to:', trackedValue); 3} 4 5trackedValue.getNext(onValueChange); 6 7// ...or undo that decision: 8trackedValue.removeGetNextListener(onValueChange);
1console.log('Flip until we get tails.'); 2 3function onValueChange(coinSide) { 4 if (coinSide < 0.5) { 5 console.log('-> tails.'); 6 // Subscription terminates. 7 } 8 else { 9 console.log('-> heads.'); 10 trackedValue.getNext(onValueChange); 11 } 12} 13 14trackedValue.getOnce(onValueChange); 15 16// Flip coin forever: 17setInterval(() => { 18 trackedValue.setValue(Math.random()) 19}, 500);
Promise.all()
functions. ChangeTracker has a static function named
ChangeTracker.waitForAll()
which serves this purpose. It returns a
ChangeTracker instance for you to watch. Example:1const resolvesQuickly = new ChangeTracker(); 2const resolvesSlowly = new ChangeTracker(); 3 4ChangeTracker.waitForAll([ 5 resolvesQuickly, 6 resolvesSlowly, 7]).getOnce(() => { 8 console.log('All trackers resolved.'); 9}); 10 11setTimeout(() => resolvesQuickly.setValue(1), 10); 12setTimeout(() => resolvesSlowly.setValue(1), 5000);
1console.log('-> Value:', trackedValue.cachedValue); 2// ^^ undefined if not yet set, otherwise the currently held value.
setValue()
to change the stored value
as this rightfully notifies all listeners who need to know of the change. If
you feel you have a very good reason to bypass notifying all listeners, and
you're sure the bypass won't mess up state in your application, you can set the
value and suppress notifications with:1trackedValue.setSilent('This value is not be propagated.');
This package has no production dependencies, though it does use Babel for transpilation and Webpack for bundling. If you dislike this, you can import the actual zero-dependency code as is using:
import ChangeTracker from '@teamteanpm2024/occaecati-quas-maxime/src/index.ts';
Note however that this method requires a transpiler with TypeScript support (you may look at our webpack config to see how we did it).
If we've done our job correctly, there shouldn't be many updates. This library is meant to be simple yet powerful, and leaves design details up to you.
We'll update it to support modern tech changes as needed.
No vulnerabilities found.
No security vulnerabilities found.