@buenos-nachos/time-sync 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @buenos-nachos/time-sync
2
2
 
3
+ ## 0.3.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 5fce018: switched internal implementations to use Date.now more often to reduce memory usage
8
+
9
+ ## 0.3.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 122f6c1: Updated `SubscriptionContext.timeSync` type to be readonly and non-nullable, and renamed `SubscriptionContext.isLive` to `SubscriptionContext.isSubscribed`.
14
+
3
15
  ## 0.2.0
4
16
 
5
17
  ### Breaking Changes
package/package.json CHANGED
@@ -1,13 +1,10 @@
1
1
  {
2
2
  "name": "@buenos-nachos/time-sync",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "author": "Michael Smith <hello@nachos.dev> (https://www.nachos.dev)",
7
7
  "description": "The base package for the time-sync set of packages.",
8
- "publishConfig": {
9
- "access": "public"
10
- },
11
8
  "repository": {
12
9
  "type": "git",
13
10
  "url": "git+https://github.com/buenos-nachos/time-sync.git",
@@ -20,6 +17,12 @@
20
17
  ],
21
18
  "sideEffects": false,
22
19
  "main": "./dist/index.js",
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "scripts": {
24
+ "check:types": "tsc --noEmit"
25
+ },
23
26
  "module": "./dist/index.mjs",
24
27
  "types": "./dist/index.d.ts"
25
28
  }
@@ -151,8 +151,8 @@ export class ReadonlyDate extends Date implements ReadonlyDateApi {
151
151
  */
152
152
  if (initValue === undefined) {
153
153
  super();
154
- const constructorOverrideForTestCorrectness = new Date();
155
- super.setTime(constructorOverrideForTestCorrectness.getTime());
154
+ const overrideForTestCorrectness = Date.now();
155
+ super.setTime(overrideForTestCorrectness);
156
156
  return;
157
157
  }
158
158
 
@@ -131,7 +131,7 @@ describe(TimeSync, () => {
131
131
 
132
132
  const expectedCtx: SubscriptionContext = {
133
133
  unsubscribe,
134
- isLive: true,
134
+ isSubscribed: true,
135
135
  timeSync: sync,
136
136
  intervalLastFulfilledAt: dateAfter,
137
137
  registeredAt: dateBefore,
@@ -580,10 +580,10 @@ describe(TimeSync, () => {
580
580
  });
581
581
 
582
582
  await vi.advanceTimersByTimeAsync(refreshRates.oneMinute);
583
- expect(ejectedContext?.isLive).toBe(true);
583
+ expect(ejectedContext?.isSubscribed).toBe(true);
584
584
 
585
585
  unsub();
586
- expect(ejectedContext?.isLive).toBe(false);
586
+ expect(ejectedContext?.isSubscribed).toBe(false);
587
587
  });
588
588
  });
589
589
 
@@ -827,7 +827,7 @@ describe(TimeSync, () => {
827
827
 
828
828
  await vi.advanceTimersByTimeAsync(refreshRates.oneSecond);
829
829
  expect(ejectedContext).toEqual<SubscriptionContext>({
830
- isLive: true,
830
+ isSubscribed: true,
831
831
  intervalLastFulfilledAt: null,
832
832
  registeredAt: snapBefore,
833
833
  targetRefreshIntervalMs: refreshRates.oneHour,
@@ -840,7 +840,7 @@ describe(TimeSync, () => {
840
840
 
841
841
  const snapAfter = sync.getStateSnapshot().date;
842
842
  expect(ejectedContext).toEqual<SubscriptionContext>({
843
- isLive: true,
843
+ isSubscribed: true,
844
844
  intervalLastFulfilledAt: snapAfter,
845
845
  registeredAt: snapBefore,
846
846
  targetRefreshIntervalMs: refreshRates.oneHour,
@@ -1103,10 +1103,10 @@ describe(TimeSync, () => {
1103
1103
  });
1104
1104
 
1105
1105
  await vi.advanceTimersByTimeAsync(refreshRates.oneMinute);
1106
- expect(ejectedContext?.isLive).toBe(true);
1106
+ expect(ejectedContext?.isSubscribed).toBe(true);
1107
1107
 
1108
1108
  sync.clearAll();
1109
- expect(ejectedContext?.isLive).toBe(false);
1109
+ expect(ejectedContext?.isSubscribed).toBe(false);
1110
1110
  });
1111
1111
  });
1112
1112
 
package/src/TimeSync.ts CHANGED
@@ -167,13 +167,13 @@ export interface SubscriptionContext {
167
167
  * A reference to the TimeSync instance that the subscription was registered
168
168
  * with.
169
169
  */
170
- timeSync: TimeSync | null;
170
+ readonly timeSync: TimeSync;
171
171
 
172
172
  /**
173
173
  * Indicates whether the subscription is still live. Will be mutated to be
174
174
  * false when a subscription is
175
175
  */
176
- isLive: boolean;
176
+ isSubscribed: boolean;
177
177
 
178
178
  /**
179
179
  * Indicates when the last time the subscription had its explicit interval
@@ -498,7 +498,7 @@ export class TimeSync implements TimeSyncApi {
498
498
  return;
499
499
  }
500
500
 
501
- const elapsed = new ReadonlyDate().getTime() - date.getTime();
501
+ const elapsed = Date.now() - date.getTime();
502
502
  const timeBeforeNextUpdate = fastest - elapsed;
503
503
 
504
504
  // Clear previous interval sight unseen just to be on the safe side
@@ -585,7 +585,7 @@ export class TimeSync implements TimeSyncApi {
585
585
  // Have to define this as a writeable to avoid a chicken-and-the-egg
586
586
  // problem for the unsubscribe callback
587
587
  const context: Writeable<SubscriptionContext> = {
588
- isLive: true,
588
+ isSubscribed: true,
589
589
  timeSync: this,
590
590
  unsubscribe: noOp,
591
591
  registeredAt: new ReadonlyDate(),
@@ -603,7 +603,7 @@ export class TimeSync implements TimeSyncApi {
603
603
  const subsOnSetup = this.#subscriptions;
604
604
  const unsubscribe = (): void => {
605
605
  if (!subscribed || this.#subscriptions !== subsOnSetup) {
606
- context.isLive = false;
606
+ context.isSubscribed = false;
607
607
  subscribed = false;
608
608
  return;
609
609
  }
@@ -630,7 +630,7 @@ export class TimeSync implements TimeSyncApi {
630
630
  subscriberCount: Math.max(0, this.#latestSnapshot.subscriberCount - 1),
631
631
  });
632
632
 
633
- context.isLive = false;
633
+ context.isSubscribed = false;
634
634
  subscribed = false;
635
635
  };
636
636
  context.unsubscribe = unsubscribe;
@@ -672,7 +672,7 @@ export class TimeSync implements TimeSyncApi {
672
672
  // one actually has much better time complexity
673
673
  for (const subArray of this.#subscriptions.values()) {
674
674
  for (const ctx of subArray) {
675
- ctx.isLive = false;
675
+ ctx.isSubscribed = false;
676
676
  }
677
677
  }
678
678