@celljs/ta-sync 4.18.0 → 4.19.0

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.
Files changed (2) hide show
  1. package/host/index.mjs +106 -3
  2. package/package.json +4 -4
package/host/index.mjs CHANGED
@@ -2381,6 +2381,87 @@ function writeStream(stream, bytes) {
2381
2381
  });
2382
2382
  }
2383
2383
 
2384
+ // web-src/sync-account.ts
2385
+ class SyncAccount {
2386
+ options;
2387
+ identities = new Set;
2388
+ told = new Map;
2389
+ tried = new Set;
2390
+ timer;
2391
+ stopHearing;
2392
+ running;
2393
+ constructor(options) {
2394
+ this.options = options;
2395
+ }
2396
+ start() {
2397
+ this.stopHearing = this.options.account.onTold((_from, value) => void this.heard(value));
2398
+ this.timer = setInterval(() => void this.round(), this.options.every ?? 30000);
2399
+ this.round();
2400
+ }
2401
+ dispose() {
2402
+ clearInterval(this.timer);
2403
+ this.stopHearing?.();
2404
+ }
2405
+ round() {
2406
+ this.running ??= this.look().catch(() => {
2407
+ return;
2408
+ }).finally(() => {
2409
+ this.running = undefined;
2410
+ });
2411
+ return this.running;
2412
+ }
2413
+ async look() {
2414
+ const { account, sync } = this.options;
2415
+ const computers = await account.computers();
2416
+ if (computers.length === 0 || await this.options.off()) {
2417
+ return;
2418
+ }
2419
+ let status = await sync.status();
2420
+ if (status.state === "off") {
2421
+ const self = await account.self();
2422
+ const first = self && computers.filter((one) => one.online).every((one) => self.id < one.id);
2423
+ if (!first) {
2424
+ return;
2425
+ }
2426
+ status = await sync.enable();
2427
+ }
2428
+ const word = { identity: await this.options.identity() };
2429
+ if (status.state === "on" && (status.owner || status.where !== "relay")) {
2430
+ word.link = (await sync.invite()).link;
2431
+ }
2432
+ const text = JSON.stringify(word);
2433
+ for (const computer of computers) {
2434
+ if (this.told.get(computer.id) !== text) {
2435
+ await account.tell(computer.id, word);
2436
+ this.told.set(computer.id, text);
2437
+ }
2438
+ }
2439
+ if (status.owner) {
2440
+ for (const request of await this.options.requests()) {
2441
+ if (this.identities.has(request.id) && !request.guest) {
2442
+ await sync.answer(request.ask, true);
2443
+ }
2444
+ }
2445
+ }
2446
+ }
2447
+ async heard(word) {
2448
+ if (typeof word?.identity === "string") {
2449
+ this.identities.add(word.identity);
2450
+ }
2451
+ if (typeof word?.link !== "string" || this.tried.has(word.link) || await this.options.off()) {
2452
+ return;
2453
+ }
2454
+ const status = await this.options.sync.status();
2455
+ if (status.state === "off" || status.state === "gone" || status.state === "refused") {
2456
+ this.tried.add(word.link);
2457
+ await this.options.sync.join(word.link).catch(() => {
2458
+ return;
2459
+ });
2460
+ }
2461
+ this.round();
2462
+ }
2463
+ }
2464
+
2384
2465
  // web-src/sync-protocol.ts
2385
2466
  var SYNC_KIND = "ta-sync";
2386
2467
  var SYNC_PACKAGE = "@celljs/ta-sync";
@@ -4280,12 +4361,33 @@ function activate(host) {
4280
4361
  });
4281
4362
  const stopData = host.onExtensionData((name, key, cwd) => cwd === undefined && service.dataChanged(name, key));
4282
4363
  service.start();
4364
+ const OFF = "account.off";
4365
+ const off = (value) => host.setExtensionData(SYNC_PACKAGE, OFF, value || null).catch(() => {
4366
+ return;
4367
+ });
4368
+ const account = host.account ? new SyncAccount({
4369
+ account: host.account,
4370
+ sync: service,
4371
+ identity: async () => (await host.rooms.getIdentity()).id,
4372
+ requests: () => host.rooms.listRequests(),
4373
+ off: async () => await host.getExtensionData(SYNC_PACKAGE, OFF) === true
4374
+ }) : undefined;
4375
+ account?.start();
4283
4376
  return {
4284
4377
  status: () => service.status(),
4285
- enable: () => service.enable(),
4378
+ enable: async () => {
4379
+ await off(false);
4380
+ return service.enable();
4381
+ },
4286
4382
  invite: (renew) => service.invite(renew === true),
4287
- join: (link) => service.join(String(link ?? "")),
4288
- leave: () => service.leave(),
4383
+ join: async (link) => {
4384
+ await off(false);
4385
+ return service.join(String(link ?? ""));
4386
+ },
4387
+ leave: async () => {
4388
+ await off(true);
4389
+ return service.leave();
4390
+ },
4289
4391
  answer: (ask, accept) => service.answer(String(ask ?? ""), accept === true),
4290
4392
  removeDevice: (member) => service.removeDevice(String(member ?? "")),
4291
4393
  setResources: (change) => service.setResources(change ?? {}),
@@ -4306,6 +4408,7 @@ function activate(host) {
4306
4408
  migrateRelocate: (from, to) => migrate.relocate(String(from ?? ""), String(to ?? "")),
4307
4409
  dispose: () => {
4308
4410
  stopData();
4411
+ account?.dispose();
4309
4412
  service.dispose();
4310
4413
  migrate.dispose();
4311
4414
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-sync",
3
- "version": "4.18.0",
3
+ "version": "4.19.0",
4
4
  "description": "A pi package for Ta: the user's settings, instructions, skills, extensions and the data extensions declare, the same on each of their computers, through a room of the relay sealed end to end, without an account",
5
5
  "keywords": [
6
6
  "pi-package"
@@ -26,9 +26,9 @@
26
26
  "watch": "tsc -b -w --preserveWatchOutput"
27
27
  },
28
28
  "devDependencies": {
29
- "@celljs/ta-protocol": "4.18.0",
30
- "@celljs/ta-relay": "4.18.0",
31
- "@celljs/ta-web": "4.18.0",
29
+ "@celljs/ta-protocol": "4.19.0",
30
+ "@celljs/ta-relay": "4.19.0",
31
+ "@celljs/ta-web": "4.19.0",
32
32
  "@types/react": "^19.3.0"
33
33
  },
34
34
  "publishConfig": {