@rljson/fs-agent 0.0.26 → 0.0.27

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.
@@ -274,6 +274,17 @@ export declare class FsAgent {
274
274
  * must never be worth failing a sync over.
275
275
  * @param ref - The ref the folder is now at.
276
276
  */
277
+ /**
278
+ * Starts the watcher unless it is already running.
279
+ *
280
+ * Both `syncToDb` and `syncFromDb` need a live watcher and either may be
281
+ * started first. `syncToDb` used to call `watch()` unconditionally, so
282
+ * starting them in the order `syncFromDb` then `syncToDb` threw "Already
283
+ * watching" — which quietly forced every caller into push-first, the order
284
+ * that lets a reconnecting client overwrite the network with a stale tree.
285
+ * A crash is a poor reason to choose an unsafe order.
286
+ */
287
+ private _ensureWatching;
277
288
  private _persistCurrentRef;
278
289
  /**
279
290
  * The ref this folder was last recorded at, from a previous run.
package/dist/fs-agent.js CHANGED
@@ -458,6 +458,10 @@ class FsScanner {
458
458
  get tree() {
459
459
  return this._tree;
460
460
  }
461
+ /** Whether a watcher is currently installed. */
462
+ get isWatching() {
463
+ return this._watcher !== null;
464
+ }
461
465
  get rootPath() {
462
466
  return this._rootPath;
463
467
  }
@@ -1161,6 +1165,21 @@ class FsAgent {
1161
1165
  * must never be worth failing a sync over.
1162
1166
  * @param ref - The ref the folder is now at.
1163
1167
  */
1168
+ /**
1169
+ * Starts the watcher unless it is already running.
1170
+ *
1171
+ * Both `syncToDb` and `syncFromDb` need a live watcher and either may be
1172
+ * started first. `syncToDb` used to call `watch()` unconditionally, so
1173
+ * starting them in the order `syncFromDb` then `syncToDb` threw "Already
1174
+ * watching" — which quietly forced every caller into push-first, the order
1175
+ * that lets a reconnecting client overwrite the network with a stale tree.
1176
+ * A crash is a poor reason to choose an unsafe order.
1177
+ */
1178
+ async _ensureWatching() {
1179
+ if (!this._scanner.isWatching) {
1180
+ await this._scanner.watch();
1181
+ }
1182
+ }
1164
1183
  _persistCurrentRef(ref) {
1165
1184
  try {
1166
1185
  writeFileSync(
@@ -1927,7 +1946,7 @@ ${err.stack}` : String(err);
1927
1946
  }, this._timeouts.debounceMs);
1928
1947
  };
1929
1948
  this._scanner.onChange(debouncedSync);
1930
- await this._scanner.watch();
1949
+ await this._ensureWatching();
1931
1950
  return () => {
1932
1951
  if (debounceTimer) clearTimeout(debounceTimer);
1933
1952
  this._scanner.offChange(debouncedSync);
@@ -2170,9 +2189,7 @@ ${err.stack}` : String(err);
2170
2189
  * @returns Function to stop watching
2171
2190
  */
2172
2191
  async syncFromDb(db, connector, treeKey, restoreOptions) {
2173
- if (!this._scanner["_watcher"]) {
2174
- await this._scanner.watch();
2175
- }
2192
+ await this._ensureWatching();
2176
2193
  let pendingRef = null;
2177
2194
  let fromDbTimer = null;
2178
2195
  let pendingRecoveryAttempt = 0;
@@ -121,6 +121,8 @@ export declare class FsScanner {
121
121
  private _vanishedDuringScan;
122
122
  constructor(rootPath: string, options?: FsScanOptions);
123
123
  get tree(): FsTree | null;
124
+ /** Whether a watcher is currently installed. */
125
+ get isWatching(): boolean;
124
126
  get rootPath(): string;
125
127
  get bs(): Bs;
126
128
  scan(): Promise<FsTree>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rljson/fs-agent",
3
- "version": "0.0.26",
3
+ "version": "0.0.27",
4
4
  "description": "Rljson fs-agent description",
5
5
  "homepage": "https://github.com/rljson/fs-agent",
6
6
  "bugs": "https://github.com/rljson/fs-agent/issues",