@dotinc/ogre 0.15.1 → 0.15.2

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/lib/repository.js CHANGED
@@ -6,18 +6,21 @@ import * as jsondiffpatch from 'jsondiffpatch';
6
6
  */
7
7
  export class Repository {
8
8
  constructor(obj, options) {
9
- var _a, _b, _c, _d, _e, _f, _g;
10
- this._isReady = false;
9
+ var _a, _b, _c, _d, _e, _f, _g, _h;
10
+ this._readyPromise = new Promise((resolve) => {
11
+ this._resolveReady = resolve;
12
+ });
11
13
  this.hashFn = (_a = options.overrides) === null || _a === void 0 ? void 0 : _a.calculateCommitHashFn;
12
14
  this.serializeObjectFn = (_b = options.overrides) === null || _b === void 0 ? void 0 : _b.serializeObjectFn;
13
15
  this.deserializeObjectFn = (_c = options.overrides) === null || _c === void 0 ? void 0 : _c.deserializeObjectFn;
16
+ this.timestampFn = (_d = options.overrides) === null || _d === void 0 ? void 0 : _d.timestampFn;
14
17
  options.history && this.storeRemoteState(options.history);
15
18
  this.original = deepClone(obj);
16
19
  // store js ref, so obj can still be modified without going through repo.data
17
20
  this.data = obj;
18
21
  this.observer = observe(obj);
19
- this.refs = ((_d = options.history) === null || _d === void 0 ? void 0 : _d.refs)
20
- ? mutableMapCopy((_e = options.history) === null || _e === void 0 ? void 0 : _e.refs)
22
+ this.refs = ((_e = options.history) === null || _e === void 0 ? void 0 : _e.refs)
23
+ ? mutableMapCopy((_f = options.history) === null || _f === void 0 ? void 0 : _f.refs)
21
24
  : new Map([
22
25
  [
23
26
  REFS_HEAD_KEY,
@@ -27,19 +30,19 @@ export class Repository {
27
30
  },
28
31
  ],
29
32
  ]);
30
- this.commits = (_g = (_f = options.history) === null || _f === void 0 ? void 0 : _f.commits) !== null && _g !== void 0 ? _g : [];
33
+ this.commits = (_h = (_g = options.history) === null || _g === void 0 ? void 0 : _g.commits) !== null && _h !== void 0 ? _h : [];
31
34
  if (!options.history) {
32
- this._isReady = true;
35
+ this._resolveReady(true);
33
36
  return;
34
37
  }
35
38
  // restore history
36
39
  const commit = this.commitAtHead();
37
40
  if (!commit) {
38
- this._isReady = true;
41
+ this._resolveReady(true);
39
42
  return;
40
43
  }
41
44
  this.moveTo(commit).then(() => {
42
- this._isReady = true;
45
+ this._resolveReady(true);
43
46
  });
44
47
  }
45
48
  storeRemoteState(history) {
@@ -48,18 +51,7 @@ export class Repository {
48
51
  this.remoteCommits = immutableArrayCopy(history.commits, (c) => c.hash);
49
52
  }
50
53
  isReady() {
51
- const self = this;
52
- function checkFlag(callback) {
53
- if (self._isReady === true) {
54
- callback(true);
55
- }
56
- else {
57
- setTimeout(() => checkFlag(callback), 10);
58
- }
59
- }
60
- return new Promise((resolve) => {
61
- checkFlag(resolve);
62
- });
54
+ return this._readyPromise;
63
55
  }
64
56
  async push(pushToBackendFn) {
65
57
  const success = await pushToBackendFn();
@@ -286,7 +278,7 @@ export class Repository {
286
278
  (amend && message === (parent === null || parent === void 0 ? void 0 : parent.message))) {
287
279
  throw new Error(`no changes to commit`);
288
280
  }
289
- const timestamp = new Date();
281
+ const timestamp = this.timestampFn ? this.timestampFn() : new Date();
290
282
  const parentChanges = amend && parent && ((_b = (_a = parent.changes) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0 > 0)
291
283
  ? parent.changes
292
284
  : [];
@@ -352,8 +344,7 @@ export class Repository {
352
344
  return cleanRefValue(ref.value);
353
345
  }
354
346
  // region History functions
355
- collectCommits() {
356
- const commit = this.commitAtHead();
347
+ collectCommits(commit) {
357
348
  if (!commit) {
358
349
  return [];
359
350
  }
@@ -369,7 +360,7 @@ export class Repository {
369
360
  getHistory() {
370
361
  return {
371
362
  refs: new Map(this.refs),
372
- commits: this.collectCommits(),
363
+ commits: this.collectCommits(this.commitAtHead()),
373
364
  };
374
365
  }
375
366
  logs(numberOfCommits) {
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "type": "git",
5
5
  "url": "https://github.com/dotindustries/ogre.git"
6
6
  },
7
- "version": "0.15.1",
7
+ "version": "0.15.2",
8
8
  "description": "Git-like repository for in-memory object versioning",
9
9
  "private": false,
10
10
  "main": "lib/index.js",
@@ -49,5 +49,5 @@
49
49
  "registry": "https://registry.npmjs.org/",
50
50
  "access": "public"
51
51
  },
52
- "gitHead": "ca49fdcdec161e884acc33515925f3dbcd0fd8ca"
52
+ "gitHead": "a7153783f07fc368cb31cac9dd2662e05d3cfb29"
53
53
  }
@@ -843,4 +843,42 @@ test("pending changes - push helpers", async (t) => {
843
843
  "wrong pending ref update",
844
844
  );
845
845
  });
846
+
847
+ t.test("local remote state is updated after push", async (t) => {
848
+ const [repo, obj] = await getBaseline();
849
+ updateHeaderData(obj);
850
+ const c1 = await repo.commit("header data", testAuthor);
851
+ addOneNested(obj);
852
+ const c2 = await repo.commit("first nested", testAuthor);
853
+ repo.tag("v0.1.0");
854
+ const history = repo.getHistory();
855
+ const cm1 = history.commits.find((c) => c.hash === c1);
856
+ const cm2 = history.commits.find((c) => c.hash === c2);
857
+
858
+ const pending = repo.cherry();
859
+ t.equal(pending.commits.length, 2, "incorrect number of pending commits");
860
+ t.equal(pending.refs.size, 2, `incorrect number of pending ref updates`);
861
+ t.matchOnlyStrict(
862
+ pending.commits,
863
+ [cm2, cm1],
864
+ "wrong pending commits",
865
+ );
866
+ t.matchOnlyStrict(
867
+ pending.refs.keys(),
868
+ ["refs/heads/main", "refs/tags/v0.1.0"],
869
+ "wrong pending ref update",
870
+ );
871
+
872
+ const remote = repo.remote()
873
+ console.log("local remote state is updated after push");
874
+ await repo.push(() => Promise.resolve(true))
875
+ const remote2 = repo.remote()
876
+ t.notMatchOnlyStrict(remote, remote2, "remote should be different after push")
877
+ // now test push
878
+
879
+ })
880
+
881
+ // t.test("history returns all commits from all branches", async t => {
882
+ // t.fail("TODO: implement commits to be gathered from all refs, not just from the HEAD backwards")
883
+ // })
846
884
  });
package/src/repository.ts CHANGED
@@ -40,6 +40,7 @@ export interface RepositoryOptions {
40
40
  calculateCommitHashFn?: (content: CommitHashContent) => Promise<string>;
41
41
  serializeObjectFn?: (obj: any) => Promise<string>;
42
42
  deserializeObjectFn?: <T>(str: string) => Promise<T>;
43
+ timestampFn?: () => Date;
43
44
  };
44
45
  }
45
46
 
@@ -121,9 +122,13 @@ export class Repository<T extends { [k: PropertyKey]: any }>
121
122
  implements RepositoryObject<T>
122
123
  {
123
124
  constructor(obj: Partial<T>, options: RepositoryOptions) {
125
+ this._readyPromise = new Promise<boolean>((resolve) => {
126
+ this._resolveReady = resolve;
127
+ });
124
128
  this.hashFn = options.overrides?.calculateCommitHashFn;
125
129
  this.serializeObjectFn = options.overrides?.serializeObjectFn;
126
130
  this.deserializeObjectFn = options.overrides?.deserializeObjectFn;
131
+ this.timestampFn = options.overrides?.timestampFn;
127
132
  options.history && this.storeRemoteState(options.history);
128
133
  this.original = deepClone(obj);
129
134
  // store js ref, so obj can still be modified without going through repo.data
@@ -144,18 +149,18 @@ export class Repository<T extends { [k: PropertyKey]: any }>
144
149
  this.commits = options.history?.commits ?? [];
145
150
 
146
151
  if (!options.history) {
147
- this._isReady = true;
152
+ this._resolveReady(true);
148
153
  return;
149
154
  }
150
155
 
151
156
  // restore history
152
157
  const commit = this.commitAtHead();
153
158
  if (!commit) {
154
- this._isReady = true;
159
+ this._resolveReady(true);
155
160
  return;
156
161
  }
157
162
  this.moveTo(commit).then(() => {
158
- this._isReady = true;
163
+ this._resolveReady(true);
159
164
  });
160
165
  }
161
166
 
@@ -169,22 +174,11 @@ export class Repository<T extends { [k: PropertyKey]: any }>
169
174
  }
170
175
 
171
176
  private readonly original: T;
172
- private _isReady = false;
177
+ private _readyPromise: Promise<boolean>;
178
+ private _resolveReady!: (value: boolean) => void;
173
179
 
174
180
  isReady(): Promise<boolean> {
175
- const self = this;
176
-
177
- function checkFlag(callback: (ok: boolean) => void) {
178
- if (self._isReady === true) {
179
- callback(true);
180
- } else {
181
- setTimeout(() => checkFlag(callback), 10);
182
- }
183
- }
184
-
185
- return new Promise((resolve) => {
186
- checkFlag(resolve);
187
- });
181
+ return this._readyPromise;
188
182
  }
189
183
 
190
184
  data: T;
@@ -200,6 +194,8 @@ export class Repository<T extends { [k: PropertyKey]: any }>
200
194
  | (<T>(str: string) => Promise<T>)
201
195
  | undefined;
202
196
 
197
+ private readonly timestampFn: (() => Date) | undefined;
198
+
203
199
  // stores the remote state upon initialization
204
200
  private remoteRefs:
205
201
  | ReadonlyMap<string, Readonly<Reference>>
@@ -486,7 +482,7 @@ export class Repository<T extends { [k: PropertyKey]: any }>
486
482
  throw new Error(`no changes to commit`);
487
483
  }
488
484
 
489
- const timestamp = new Date();
485
+ const timestamp = this.timestampFn ? this.timestampFn() : new Date();
490
486
  const parentChanges =
491
487
  amend && parent && (parent.changes?.length ?? 0 > 0)
492
488
  ? parent.changes
@@ -564,8 +560,7 @@ export class Repository<T extends { [k: PropertyKey]: any }>
564
560
  }
565
561
 
566
562
  // region History functions
567
- private collectCommits() {
568
- const commit = this.commitAtHead();
563
+ private collectCommits(commit?: Commit) {
569
564
  if (!commit) {
570
565
  return [];
571
566
  }
@@ -582,7 +577,7 @@ export class Repository<T extends { [k: PropertyKey]: any }>
582
577
  getHistory(): History {
583
578
  return {
584
579
  refs: new Map(this.refs),
585
- commits: this.collectCommits(),
580
+ commits: this.collectCommits(this.commitAtHead()),
586
581
  };
587
582
  }
588
583