@yaks/sync 0.0.0-stage → 0.2.36

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 (75) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +729 -2
  3. package/_dist/coverage.d.ts +7 -0
  4. package/_dist/coverage.d.ts.map +1 -0
  5. package/_dist/inbound.d.ts +43 -0
  6. package/_dist/inbound.d.ts.map +1 -0
  7. package/_dist/mark.d.ts +29 -0
  8. package/_dist/mark.d.ts.map +1 -0
  9. package/_dist/mod.d.ts +78 -0
  10. package/_dist/mod.d.ts.map +1 -0
  11. package/_dist/outbound.d.ts +55 -0
  12. package/_dist/outbound.d.ts.map +1 -0
  13. package/_dist/outbox.d.ts +48 -0
  14. package/_dist/outbox.d.ts.map +1 -0
  15. package/_dist/pace.d.ts +9 -0
  16. package/_dist/pace.d.ts.map +1 -0
  17. package/_dist/port.d.ts +31 -0
  18. package/_dist/port.d.ts.map +1 -0
  19. package/_dist/revision.d.ts +60 -0
  20. package/_dist/revision.d.ts.map +1 -0
  21. package/_dist/saying.d.ts +12 -0
  22. package/_dist/saying.d.ts.map +1 -0
  23. package/_dist/socket.d.ts +151 -0
  24. package/_dist/socket.d.ts.map +1 -0
  25. package/_dist/sync.d.ts +97 -0
  26. package/_dist/sync.d.ts.map +1 -0
  27. package/_dist/tier.d.ts +62 -0
  28. package/_dist/tier.d.ts.map +1 -0
  29. package/clock.js +43 -0
  30. package/clock.js.map +1 -0
  31. package/clock.ts +56 -0
  32. package/coverage.js +13 -0
  33. package/coverage.js.map +1 -0
  34. package/coverage.ts +26 -0
  35. package/deno.json +1 -0
  36. package/inbound.js +217 -0
  37. package/inbound.js.map +1 -0
  38. package/inbound.ts +251 -0
  39. package/mark.js +87 -0
  40. package/mark.js.map +1 -0
  41. package/mark.ts +108 -0
  42. package/mod.js +78 -0
  43. package/mod.js.map +1 -0
  44. package/mod.ts +153 -0
  45. package/outbound.js +170 -0
  46. package/outbound.js.map +1 -0
  47. package/outbound.ts +221 -0
  48. package/outbox.js +281 -0
  49. package/outbox.js.map +1 -0
  50. package/outbox.ts +343 -0
  51. package/pace.js +96 -0
  52. package/pace.js.map +1 -0
  53. package/pace.ts +91 -0
  54. package/package.json +32 -4
  55. package/port.js +128 -0
  56. package/port.js.map +1 -0
  57. package/port.ts +176 -0
  58. package/rebase.js +93 -0
  59. package/rebase.js.map +1 -0
  60. package/rebase.ts +77 -0
  61. package/revision.js +104 -0
  62. package/revision.js.map +1 -0
  63. package/revision.ts +146 -0
  64. package/saying.js +62 -0
  65. package/saying.js.map +1 -0
  66. package/saying.ts +69 -0
  67. package/socket.js +694 -0
  68. package/socket.js.map +1 -0
  69. package/socket.ts +927 -0
  70. package/sync.js +542 -0
  71. package/sync.js.map +1 -0
  72. package/sync.ts +705 -0
  73. package/tier.js +176 -0
  74. package/tier.js.map +1 -0
  75. package/tier.ts +211 -0
@@ -0,0 +1,7 @@
1
+ import type { Bundle, Coverage } from "@yaks/graph";
2
+ export type { Coverage };
3
+ /** Whether a delivery covers a component's presence or a particular
4
+ * property. */ export declare let covers: (scope: Coverage, name: string, prop?: string) => boolean;
5
+ /** The coverage of a rider bundle that declared none: exactly what it
6
+ * carried. */ export declare let delivered: (b: Bundle) => Coverage;
7
+ //# sourceMappingURL=coverage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"coverage.d.ts","sources":["../coverage.ts"],"names":[],"mappings":"AAIA,cAAc,MAAM,EAAE,QAAQ,2BAAgC;AAG9D,cAAc,QAAQ,GAAE;AAExB;aACa,GACb,OAAO,YAAI,SAAU,OAAO,UAAU,MAAM,MAAM,EAAE,OAAO,MAAM,KAAG,OAAO,CAIV;AAEjE;YACY,GACZ,OAAO,YAAI,YAAa,GAAG,WAAS,SAMjC"}
@@ -0,0 +1,43 @@
1
+ import type { Bundle, Eid, Graph } from "@yaks/graph";
2
+ import type { Mine } from "./saying.js";
3
+ import type { Frame } from "./socket.js";
4
+ import { type Coverage } from "./coverage.js";
5
+ /**
6
+ * Take these entities out of the local graph: their server-owned components are
7
+ * dropped, their identity stays. This is what a subscription's `gone` list
8
+ * means.
9
+ */ export declare let strip: (graph: Graph, eids: Eid[]) => Bundle[] | Promise<Bundle[]>;
10
+ /**
11
+ * One frame from the server, applied: the bundles it carries go in whole and
12
+ * trusted, and the entities it lists as gone have their components removed. A
13
+ * frame with `coverage` is a {@link snapshot} within it, so what the answer
14
+ * covers and no longer carries is cleared. Riders need a Replica to own them.
15
+ * A refused subscription changes nothing in the graph — it is reported, not
16
+ * applied. `mine` is what this node is saying itself, as {@link hear} takes it.
17
+ */ export declare let land: (graph: Graph, frame: Frame, mine?: Mine) => Bundle[] | Promise<Bundle[]>;
18
+ /**
19
+ * The relayed values a frame carries, applied: each peer's `sync: peers`
20
+ * components, and nothing else, as the patches they were sent as. A reset
21
+ * frame is the whole set as it now stands, relay included, so a peer value
22
+ * this copy still holds for one of its members and the frame does not repeat
23
+ * went away while this connection was not listening, and is cleared. What
24
+ * this node is saying itself (`mine`) is the exception: a connection is never
25
+ * told its own values, so the frame leaving them out says nothing about them.
26
+ */ export declare let hear: (graph: Graph, frame: Frame, mine?: Mine) => Bundle[] | Promise<Bundle[]>;
27
+ /** Whether a patch would change the row held: an entity field, component or
28
+ * property it sets that does not already read so, or one it clears that is
29
+ * there. A whole answer heard again after a reconnect is mostly rows that did
30
+ * not move, and applying those would redo every reader's work for nothing. */ export declare let changes: (held: Bundle | undefined, patch: Bundle) => boolean;
31
+ /** Replace the components the server stores with a query's whole rows,
32
+ * including the properties it reports as absent. A raw feed carries patches
33
+ * instead, and must use land(). Local components and unsaved peer components
34
+ * never ride a stored row, so a snapshot never removes them. Saved peer values
35
+ * initialize readers while this node's own live writes remain ahead of them.
36
+ * A row that already reads as its snapshot says is left alone, and is not in
37
+ * what this returns: landing a whole answer again costs a comparison per row,
38
+ * and only what moved is applied. */ export declare let snapshot: (graph: Graph, bundles: Bundle[], opts?: {
39
+ coverage?: Record<Eid, Coverage>;
40
+ /** Peer values this node is writing itself, ahead of its saved snapshot. */ mine?: Mine;
41
+ /** Lets another owner of a property keep it when this snapshot omits it. */ preserve?: (eid: Eid, name: string, prop?: string) => boolean;
42
+ }) => Bundle[] | Promise<Bundle[]>;
43
+ //# sourceMappingURL=inbound.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inbound.d.ts","sources":["../inbound.ts"],"names":[],"mappings":"AAmBA,cAAc,MAAM,EAAQ,GAAG,EAAE,KAAK,2BAAgC;AAKtE,cAAc,IAAI,sBAAqB;AACvC,cAAc,KAAK,sBAAqB;AACxC,SAAS,KAAK,QAAQ,wBAA+B;AA4BrD;;;;CAIC,GACD,OAAO,YAAI,QACT,OAAO,OACP,MAAM,UACL,WAAW,QAAQ,UAIlB;AAEJ;;;;;;;CAOC,GACD,OAAO,YAAI,OACT,OAAO,OACP,OAAO,OACP,OAAM,SACL,WAAW,QAAQ,UA2BrB;AAKD;;;;;;;;CAQC,GACD,OAAO,YAAI,OACT,OAAO,OACP,OAAO,OACP,OAAM,SACL,WAAW,QAAQ,UAuCrB;AAED;;;4EAG4E,GAC5E,OAAO,YAAI,UAAW,MAAM,SAAS,SAAS,EAAE,OAAO,WAAS,OAAO,CAetE;AAED;;;;;;;mCAOmC,GACnC,OAAO,YAAI,WACT,OAAO,OACP,SAAS,UACT;EACE,WAAW,OAAO,KAAK;EACvB,0EAA0E,GAC1E,OAAO;EACP,0EAA0E,GAC1E,YAAY,KAAK,KAAK,MAAM,MAAM,EAAE,OAAO,MAAM,KAAK,OAAO;MAE9D,WAAW,QAAQ,UA6CnB"}
@@ -0,0 +1,29 @@
1
+ import type { Bundle, Graph, Plugin } from "@yaks/graph";
2
+ /** The mark on a bundle a caller sent, carrying the entity as it then stood. */ export declare let SENT: string;
3
+ /** The mark on a bundle a rule of the write added, carrying the entity as it
4
+ * stood before the write. */ export declare let RULED: string;
5
+ /** The mark on a batch that arrived from the server and must not go back. */ export declare let ECHO: string;
6
+ /** Mark a bundle as the caller's, with a copy of the entity it patches
7
+ * (`null` when there was no such entity yet). */ export declare let asking: (b: Bundle, was: Bundle | null) => Bundle;
8
+ /** Mark a bundle as a rule's, with a copy of the entity before the write
9
+ * (`null` when there was no such entity yet). */ export declare let ruling: (b: Bundle, was: Bundle | null) => Bundle;
10
+ /** Whether a caller passed this bundle in — as opposed to a later phase
11
+ * adding it. */ export declare let asked: (b: Bundle) => boolean;
12
+ /** Whether a rule of the write added this bundle. */ export declare let ruled: (b: Bundle) => boolean;
13
+ /** The entity as it stood before this write: the bundle for it, `null` if it
14
+ * did not exist, `undefined` if neither a caller nor a rule wrote this
15
+ * bundle. */ export declare let before: (b: Bundle) => Bundle | null | undefined;
16
+ /** The plugin a graph registers to accept these two marks when it carries no
17
+ * sync plugin of its own — a replica that only lands what a server sent
18
+ * (`land`, `snapshot`) and never posts anything back. `apply()` refuses a `$`
19
+ * key no plugin declared, and these are ours; it declares them and does nothing
20
+ * else. */ export declare let marks: Plugin;
21
+ /** Mark bundles as the server's, so the outbound hook lets them pass. */ export declare let echo: (bundles: Bundle[]) => Bundle[];
22
+ /** Whether a bundle came from the server. */ export declare let echoed: (b: Bundle) => boolean;
23
+ /** What the server sent, applied to this replica: marked as an echo so it never
24
+ * goes back, trusted since the server admitted it already, unstamped since its
25
+ * stamps are the server's, and as a replica so a component this copy was not
26
+ * loaded with is left out rather than refused. */ export declare let replicate: (graph: Graph, bundles: Bundle[]) => Bundle[] | Promise<Bundle[]>;
27
+ /** A bundle with the marks removed: what a caller gets back from `apply()`,
28
+ * which is their data and not this package's bookkeeping. */ export declare let clean: (b: Bundle) => Bundle;
29
+ //# sourceMappingURL=mark.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mark.d.ts","sources":["../mark.ts"],"names":[],"mappings":"AAwBA,cAAc,MAAM,EAAE,KAAK,EAAE,MAAM,2BAAgC;AAEnE,8EAA8E,GAC9E,OAAO,YAAI,aAAc;AAEzB;2BAC2B,GAC3B,OAAO,YAAI,cAAgB;AAE3B,2EAA2E,GAC3E,OAAO,YAAI,aAAc;AAMzB;+CAC+C,GAC/C,OAAO,YAAI,SAAU,GAAG,QAAQ,KAAK,SAAS,IAAI,KAAG,OAGnD;AAEF;+CAC+C,GAC/C,OAAO,YAAI,SAAU,GAAG,QAAQ,KAAK,SAAS,IAAI,KAAG,OAGnD;AAEF;cACc,GACd,OAAO,YAAI,QAAS,GAAG,WAAS,OAAO,CAAyB;AAEhE,mDAAmD,GACnD,OAAO,YAAI,QAAS,GAAG,WAAS,OAAO,CAA0B;AAEjE;;WAEW,GACX,OAAO,YAAI,SAAU,GAAG,WAAS,SAAS,IAAI,GAAG,SAAS,CACgB;AAE1E;;;;SAIS,GACT,OAAO,YAAI,OAAO,OAGjB;AAED,uEAAuE,GACvE,OAAO,YAAI,OAAQ,SAAS,aAAW,SACO;AAE9C,2CAA2C,GAC3C,OAAO,YAAI,SAAU,GAAG,WAAS,OAAO,CAAoB;AAE5D;;;gDAGgD,GAChD,OAAO,YAAI,YACT,OAAO,OACP,SAAS,aACR,WAAW,QAAQ,UACsD;AAE5E;2DAC2D,GAC3D,OAAO,YAAI,QAAS,GAAG,WAAS,OAW/B"}
package/_dist/mod.d.ts ADDED
@@ -0,0 +1,78 @@
1
+ /**
2
+ * @yaks/sync — connects a client {@link https://jsr.io/@yaks/graph |
3
+ * @yaks/graph} to a server: a plugin that sends a local graph's writes to the
4
+ * server over HTTP and applies the server's writes back over a WebSocket.
5
+ *
6
+ * A graph in a page over {@link https://jsr.io/@yaks/ram | @yaks/ram} is
7
+ * a complete graph — same `apply()`, same queries, same bundles — but it is
8
+ * the only participant. This package adds the other one.
9
+ *
10
+ * ```ts
11
+ * import { graph } from '@yaks/graph'
12
+ * import { ram } from '@yaks/ram'
13
+ * import { sync } from '@yaks/sync'
14
+ *
15
+ * // let g = graph({ storage: ram(vocab, { adopt: true }), vocab })
16
+ * // let link = sync(g, { url: 'https://recipes.example' })
17
+ * // link.subscribe('.recipe.course=dinner&.recipe.serves>4')
18
+ * ```
19
+ *
20
+ * ## Writes are optimistic A write commits locally first — the page renders it
21
+ * before anything crosses the network — and is then sent as `POST /apply`. The
22
+ * list of bundles the server responds with is applied back through the same
23
+ * local graph, which is how the numbers it assigned, the properties it stamped
24
+ * and the entities it deleted reach the client. If the server refuses the
25
+ * write, the optimistic batch is undone from the copy {@link sync} took of
26
+ * those entities beforehand, along with whatever the write's rules added, and
27
+ * the refusal is reported. A rule the page ran itself is a guess the server's
28
+ * answer replaces: what it added is undone as the answer lands. If the server
29
+ * is merely unreachable, nothing is undone: the write may have been applied
30
+ * there and only the response lost.
31
+ *
32
+ * ## Two keywords, one apply()
33
+ * A client holds state the server owns, state this browser owns, and state
34
+ * that dies with the tab. Each component declares which it is, using the two
35
+ * core vocabulary keywords:
36
+ *
37
+ * ```json
38
+ * { "$defs": { "draft": { "type": "object", "sync": "none",
39
+ * "properties": { "text": { "type": "string" } } } } }
40
+ * ```
41
+ *
42
+ * `sync` declares who is told about a write — `server` (the default) and
43
+ * `peers` are sent, `none` stays here — and `durable` declares how long the
44
+ * value lives: `forever` (the default) is the vault, `connection` or a
45
+ * duration is memory. All of them go through the same `apply()`. A `peers`
46
+ * component may also declare a `pace` (`"100ms"`): each write lands here at
47
+ * once, and the latest leaves at most once a pace, the last one always.
48
+ *
49
+ * ## Reading is a subscription
50
+ * {@link Sync.subscribe} opens a stored query on the server's `/ws`. Its
51
+ * answer — and every later change to it, including what left the set — is
52
+ * applied to the local graph, so a render reads the local store and never
53
+ * awaits. A dropped socket reconnects with a widening backoff, sends every
54
+ * subscription again, and treats the first frame after a reopen as the whole
55
+ * set. A drop the wire resumes from (open again, every query answered again)
56
+ * missed nothing and says nothing; an outage still unresumed `most` after it
57
+ * began is reported once as a {@link Dropped}.
58
+ *
59
+ * ## Both transports are injected
60
+ * {@link SyncOpts.fetch} and {@link SyncOpts.connect} default to the
61
+ * platform's `fetch` and `WebSocket`, and either can be passed in — which is
62
+ * how this package is tested against an in-process handler with no network at
63
+ * all.
64
+ *
65
+ * @module
66
+ */ export { type Replica, type SubscribeOpts, type Sync, sync, type SyncOpts } from "./sync.js";
67
+ export { type Fetch, MOVED, post, type PostOpts, type Refusal, type Report, type Trouble, WriteRefused, WriteUnknown } from "./outbound.js";
68
+ export { changes, hear, land, snapshot, strip } from "./inbound.js";
69
+ export { pacer } from "./pace.js";
70
+ export { type Mine, saying } from "./saying.js";
71
+ export { type Ask, backoff, type Connect, Dropped, type Frame, type Network, type Socket, type Tab, type Timer, type Wire, wire, type WireOpts } from "./socket.js";
72
+ export { durableOf, guessed, inverse, local, outbound, outward, stays, stored, syncOf } from "./tier.js";
73
+ export { asked, asking, before, clean, ECHO, echo, echoed, marks, replicate, RULED, ruled, ruling, SENT } from "./mark.js";
74
+ export { type Port, type PortLink, portLink, type RequestOptions } from "./port.js";
75
+ export { type Coverage, covers, delivered } from "./coverage.js";
76
+ export { older, type Revision, REVISIONS, type Revisions, revisions, type Stamped, type Verdict } from "./revision.js";
77
+ export { durableOutbox, type Outbox, outboxIdb, type OutboxStore, type Pending } from "./outbox.js";
78
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sources":["../mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiEC,GAED,SACE,KAAK,OAAO,EACZ,KAAK,aAAa,EAClB,KAAK,IAAI,EACT,IAAI,EACJ,KAAK,QAAQ,oBACG;AAClB,SACE,KAAK,KAAK,EACV,KAAK,EACL,IAAI,EACJ,KAAK,QAAQ,EACb,KAAK,OAAO,EACZ,KAAK,MAAM,EACX,KAAK,OAAO,EACZ,YAAY,EACZ,YAAY,wBACQ;AACtB,SAAS,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,uBAAsB;AACnE,SAAS,KAAK,oBAAmB;AACjC,SAAS,KAAK,IAAI,EAAE,MAAM,sBAAqB;AAC/C,SACE,KAAK,GAAG,EACR,OAAO,EACP,KAAK,OAAO,EACZ,OAAO,EACP,KAAK,KAAK,EACV,KAAK,OAAO,EACZ,KAAK,MAAM,EACX,KAAK,GAAG,EACR,KAAK,KAAK,EACV,KAAK,IAAI,EACT,IAAI,EACJ,KAAK,QAAQ,sBACK;AACpB,SACE,SAAS,EACT,OAAO,EACP,OAAO,EACP,KAAK,EACL,QAAQ,EACR,OAAO,EACP,KAAK,EACL,MAAM,EACN,MAAM,oBACU;AAClB,SACE,KAAK,EACL,MAAM,EACN,MAAM,EACN,KAAK,EACL,IAAI,EACJ,IAAI,EACJ,MAAM,EACN,KAAK,EACL,SAAS,EACT,KAAK,EACL,KAAK,EACL,MAAM,EACN,IAAI,oBACY;AAClB,SACE,KAAK,IAAI,EACT,KAAK,QAAQ,EACb,QAAQ,EACR,KAAK,cAAc,oBACH;AAElB,SAAS,KAAK,QAAQ,EAAE,MAAM,EAAE,SAAS,wBAAuB;AAChE,SACE,KAAK,EACL,KAAK,QAAQ,EACb,SAAS,EACT,KAAK,SAAS,EACd,SAAS,EACT,KAAK,OAAO,EACZ,KAAK,OAAO,wBACQ;AAEtB,SACE,aAAa,EACb,KAAK,MAAM,EACX,SAAS,EACT,KAAK,WAAW,EAChB,KAAK,OAAO,sBACM"}
@@ -0,0 +1,55 @@
1
+ import type { Bundle, Graph } from "@yaks/graph";
2
+ /** The body of a server's refusal: the error's own name, its message, and
3
+ * whatever fields it carried — a `Stale` names the property and the value the
4
+ * graph holds. */ export type Refusal = {
5
+ error: string;
6
+ message: string;
7
+ [k: string]: unknown;
8
+ };
9
+ /** The server deliberately refused the batch; none of it was accepted. */ export declare class WriteRefused extends Error {
10
+ sent: Bundle[];
11
+ refusal: Refusal;
12
+ readonly outcome: 'refused';
13
+ constructor(sent: Bundle[], refusal: Refusal);
14
+ }
15
+ /** No usable acceptance or refusal arrived. Reconcile or retry `sent`;
16
+ * the server may already have committed it. */ export declare class WriteUnknown extends Error {
17
+ sent: Bundle[];
18
+ readonly outcome: 'unknown';
19
+ retry?: {
20
+ after: number;
21
+ };
22
+ constructor(sent: Bundle[], cause: unknown);
23
+ }
24
+ /** How a request is sent. The global `fetch` satisfies it, and so does an
25
+ * in-process handler, which is how this package is tested with no network. */ export type Fetch = (request: Request) => Response | Promise<Response>;
26
+ /** One outgoing write, or one subscription, the server did not take. */ export type Trouble = {
27
+ /** the bundles as they were sent */ sent: Bundle[];
28
+ /** the server's refusal, when it responded with one */ refused?: Refusal;
29
+ /** a transport or response failure: the server may have committed */ error?: unknown;
30
+ /** whether the optimistic local change was undone */ reverted: boolean;
31
+ /** the subscription whose query the server refused, when what it refused
32
+ * was a subscription rather than a write */ subscription?: string;
33
+ };
34
+ /** Where a failure is reported: a page shows it, a test collects it. */ export type Report = (trouble: Trouble) => void;
35
+ /** The header on a write's answer saying the address now reaches another
36
+ * store: a server that gave this browser a store of its own at this write
37
+ * says it, and the client opens its socket again. */ export declare let MOVED: string;
38
+ /** What {@link post} needs: where to send, how, and where to put the answer. */ export type PostOpts = {
39
+ graph: Graph;
40
+ url: string;
41
+ fetch: Fetch;
42
+ headers?: Record<string, string>;
43
+ report: Report;
44
+ /** A server-authoritative write already split from browser-owned state. */ sent?: Bundle[];
45
+ /** the write was held out of the local graph, not applied: a refusal has
46
+ * nothing to revert */ held?: boolean;
47
+ /** The write's idempotency key. Sending the same batch again under it is
48
+ * the same write: a server that kept or applied it answers as it did the
49
+ * first time, rather than taking it again. */ key?: string;
50
+ /** told when the answer says the address moved ({@link MOVED}) */ moved?: () => void;
51
+ /** Adapt a host's write envelope and credentials at the transport boundary. */ request?: (key: string, sent: Bundle[]) => Promise<Bundle[]>;
52
+ land?: (bundles: Bundle[]) => Bundle[] | Promise<Bundle[]>;
53
+ };
54
+ /** Send an optimistic or held write and say whether its outcome is known. */ export declare let post: (bundles: Bundle[], opts: PostOpts) => Promise<boolean>;
55
+ //# sourceMappingURL=outbound.d.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,OAAO,YAAI,OAAc,SAAS,UAAU,MAAM,aAAW,QAAQ,OAAO,EACnC"}
@@ -0,0 +1,48 @@
1
+ export type Pending<T> = {
2
+ key: string;
3
+ sent: T;
4
+ state: 'pending' | 'refused';
5
+ /** Admission order, retained across pages independently of random keys. */ at?: number;
6
+ refusal?: {
7
+ name: string;
8
+ message: string;
9
+ eid?: string;
10
+ comp?: string;
11
+ prop?: string;
12
+ was?: string | null;
13
+ };
14
+ };
15
+ /** The disk seam. A completed put/remove means the transaction committed. */ export type OutboxStore<T> = {
16
+ load: () => Promise<Pending<T>[]>;
17
+ put: (entry: Pending<T>) => Promise<void>;
18
+ remove: (key: string) => Promise<void>;
19
+ close?: () => void;
20
+ };
21
+ /** One database per origin, with app/store and credentials in the scope. */ export declare let outboxIdb: <T>(scope: string) => OutboxStore<T>;
22
+ /** The observable delivery handle. Only an acceptance removes pending intent. */ export type Outbox<T, R> = {
23
+ ready: Promise<void>;
24
+ submit: (sent: T, key?: string) => Promise<R>;
25
+ pending: () => Pending<T>[];
26
+ onPending: (fn: () => void) => () => void;
27
+ retry: (key: string) => Promise<void>;
28
+ forget: (key: string) => Promise<void>;
29
+ refuse: (sent: T, refusal: NonNullable<Pending<T>['refusal']>, key: string) => Promise<void>;
30
+ close: () => void;
31
+ };
32
+ /** A durable, ordered queue. An unknown answer rejects that caller (unsaved),
33
+ * but retains and retries the exact intent/key. A refusal is retained for the
34
+ * person to inspect, never silently discarded or automatically resubmitted.
35
+ * When IndexedDB is absent or the browser denies access at startup, the queue
36
+ * lasts for this instance. A failed disk write prevents sending. */ export declare let durableOutbox: <T, R>(opts: {
37
+ scope: string;
38
+ send: (key: string, sent: T) => Promise<R>;
39
+ accepted?: (entry: Pending<T>, receipt: R) => void;
40
+ restored?: (entries: Pending<T>[]) => void | Promise<void>;
41
+ waitForRetry?: (sent: T) => boolean;
42
+ store?: OutboxStore<T>;
43
+ report?: (error: unknown) => void;
44
+ timer?: (fn: () => void, ms: number) => unknown;
45
+ cancelTimer?: (handle: unknown) => void;
46
+ wait?: number;
47
+ }) => Outbox<T, R>;
48
+ //# sourceMappingURL=outbox.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"outbox.d.ts","sources":["../outbox.ts"],"names":[],"mappings":"AAIA,YAAY,QAAQ;EAClB,KAAK,MAAM;EACX,MAAM;EACN,OAAO,YAAY;EACnB,yEAAyE,GACzE,KAAK,MAAM;EACX;IACE,MAAM,MAAM;IACZ,SAAS,MAAM;IACf,MAAM,MAAM;IACZ,OAAO,MAAM;IACb,OAAO,MAAM;IACb,MAAM,MAAM,GAAG,IAAI;;;AAIvB,2EAA2E,GAC3E,YAAY,YAAY;EACtB,YAAY,QAAQ,QAAQ;EAC5B,MAAM,OAAO,QAAQ,OAAO,QAAQ,IAAI;EACxC,SAAS,KAAK,MAAM,KAAK,QAAQ,IAAI;EACrC,cAAc,IAAI;;AAGpB,0EAA0E,GAC1E,OAAO,YAAI,YAAa,GAAG,OAAO,MAAM,KAAG,YAAY,GA2CtD;AAED,+EAA+E,GAC/E,YAAY,OAAO,GAAG;EACpB,OAAO,QAAQ,IAAI;EACnB,SAAS,MAAM,GAAG,MAAM,MAAM,KAAK,QAAQ;EAC3C,eAAe,QAAQ;EACvB,YAAY,UAAU,IAAI,WAAW,IAAI;EACzC,QAAQ,KAAK,MAAM,KAAK,QAAQ,IAAI;EACpC,SAAS,KAAK,MAAM,KAAK,QAAQ,IAAI;EACrC,SACE,MAAM,GACN,SAAS,YAAY,QAAQ,GAAG,aAChC,KAAK,MAAM,KACR,QAAQ,IAAI;EACjB,aAAa,IAAI;;AAGnB;;;;kEAIkE,GAClE,OAAO,YAAI,gBAAiB,GAAG,GAAG;EAChC,OAAO,MAAM;EACb,OAAO,KAAK,MAAM,EAAE,MAAM,MAAM,QAAQ;EACxC,YAAY,OAAO,QAAQ,IAAI,SAAS,MAAM,IAAI;EAClD,YAAY,SAAS,QAAQ,SAAS,IAAI,GAAG,QAAQ,IAAI;EACzD,gBAAgB,MAAM,MAAM,OAAO;EACnC,QAAQ,YAAY;EACpB,UAAU,OAAO,OAAO,KAAK,IAAI;EACjC,SAAS,UAAU,IAAI,EAAE,IAAI,MAAM,KAAK,OAAO;EAC/C,eAAe,QAAQ,OAAO,KAAK,IAAI;EACvC,OAAO,MAAM;MACX,OAAO,GAAG,GA4Ob"}
@@ -0,0 +1,9 @@
1
+ import type { Bundle } from "@yaks/graph";
2
+ import { type Vocab } from "@yaks/vocab";
3
+ import type { Timer } from "./socket.js";
4
+ /**
5
+ * The relay, paced. Returns what the sync hands each committed write's relayed
6
+ * bundles to; `send` is the socket's relay, and `timer` is how a pace is timed.
7
+ * A component with no pace is sent as it is written.
8
+ */ export declare let pacer: (vocab: Pick<Vocab, 'comp'> | (() => Pick<Vocab, 'comp'>), send: (bundles: Bundle[]) => void, timer: Timer) => (bundles: Bundle[]) => void;
9
+ //# sourceMappingURL=pace.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pace.d.ts","sources":["../pace.ts"],"names":[],"mappings":"AAgBA,cAAc,MAAM,2BAA2C;AAE/D,SAAiB,KAAK,KAAK,2BAAgC;AAC3D,cAAc,KAAK,sBAAqB;AAmBxC;;;;CAIC,GACD,OAAO,YAAI,QACT,OAAO,KAAK,OAAO,iBAAiB,KAAK,OAAO,UAChD,OAAO,SAAS,aAAa,IAAI,EACjC,OAAO,WACL,SAAS,aAAa,IAAI,CA2C7B"}
@@ -0,0 +1,31 @@
1
+ /** Ordered request/reply calls and subscription frames over a Worker or a
2
+ * MessagePort. The port belongs to the caller; closing the link removes the
3
+ * listeners it added but does not close the port.
4
+ */ import { type Clock } from "@yaks/fp";
5
+ import type { Frame } from "./socket.js";
6
+ export type Port = Pick<MessagePort, 'postMessage' | 'addEventListener' | 'removeEventListener'>;
7
+ export type RequestOptions = {
8
+ /** Override the link's timeout for this request; `null` waits until a reply
9
+ * or a disconnection. */ timeout?: number | null;
10
+ };
11
+ /** One end of a link: what it has carried, and the three things it can do. */ export type PortLink = {
12
+ stats: {
13
+ sent: number;
14
+ received: number;
15
+ frames: number;
16
+ };
17
+ request: (method: string, value?: unknown, options?: RequestOptions) => Promise<unknown>;
18
+ frame: (frame: Frame) => void;
19
+ close: (reason?: Error) => void;
20
+ };
21
+ export declare let portLink: (port: Port, opts?: {
22
+ receive?: (method: string, value: unknown) => unknown | Promise<unknown>;
23
+ frame?: (frame: Frame) => void;
24
+ report?: (error: unknown) => void;
25
+ /** Encode a request failure as a structured-cloneable value. */ encodeError?: (error: unknown) => unknown;
26
+ /** Restore a received failure, such as a domain-specific Error subclass. */ decodeError?: (value: unknown) => Error;
27
+ timeout?: number;
28
+ /** Maximum outstanding requests before a further one is rejected. */ maxPending?: number;
29
+ /** The clock request deadlines are kept by (default the runtime's). */ clock?: Clock;
30
+ }) => PortLink;
31
+ //# sourceMappingURL=port.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"port.d.ts","sources":["../port.ts"],"names":[],"mappings":"AAAA;;;CAGC,GACD,SAAS,KAAK,KAAK,wBAAqC;AACxD,cAAc,KAAK,sBAAqB;AACxC,YAAY,OAAO,KACjB,aACA,gBAAgB,qBAAqB;AAWvC,YAAY;EACV;yBACuB,GACvB,UAAU,MAAM,GAAG,IAAI;;AAEzB,4EAA4E,GAC5E,YAAY;EACV;IAAS,MAAM,MAAM;IAAE,UAAU,MAAM;IAAE,QAAQ,MAAM;;EACvD,UACE,QAAQ,MAAM,EACd,QAAQ,OAAO,EACf,UAAU,mBACP,QAAQ,OAAO;EACpB,QAAQ,OAAO,UAAU,IAAI;EAC7B,QAAQ,SAAS,UAAU,IAAI;;AAEjC,OAAO,YAAI,WAAY,MAAM,MAAM;EACjC,WAAW,QAAQ,MAAM,EAAE,OAAO,OAAO,KAAK,OAAO,GAAG,QAAQ,OAAO;EACvE,SAAS,OAAO,UAAU,IAAI;EAC9B,UAAU,OAAO,OAAO,KAAK,IAAI;EACjC,8DAA8D,GAC9D,eAAe,OAAO,OAAO,KAAK,OAAO;EACzC,0EAA0E,GAC1E,eAAe,OAAO,OAAO,KAAK;EAClC,UAAU,MAAM;EAChB,mEAAmE,GACnE,aAAa,MAAM;EACnB,qEAAqE,GACrE,QAAQ;MACD,SA+HR"}
@@ -0,0 +1,60 @@
1
+ import type { Eid } from "@yaks/graph";
2
+ /** A store's revision: one number, or each store's under its name. */ export type Revision = number | Record<string, number>;
3
+ /** What a frame says of its revision, as {@link revisions} reads it. */ export type Stamped = {
4
+ id: string;
5
+ revision?: Revision;
6
+ since?: Revision;
7
+ bundles?: {
8
+ entity: {
9
+ eid: Eid;
10
+ };
11
+ }[];
12
+ peers?: {
13
+ entity: {
14
+ eid: Eid;
15
+ };
16
+ }[];
17
+ gone?: Eid[];
18
+ peerGone?: Eid[];
19
+ };
20
+ /** Why a frame does not land: `skipped`, its subscription's frame before it
21
+ * never arrived; `stale`, a row in it is older than the one held; `asked`,
22
+ * it was on its way before its subscription was asked again. */ export type Verdict = 'skipped' | 'stale' | 'asked';
23
+ /** How many rows' revisions are kept, the least recently landed let go
24
+ * first: more than a client keeps rows (@yaks/client `RETENTION_ROWS`), so a
25
+ * row's revision outlives the row in memory. */ export declare const REVISIONS: 50_000;
26
+ /** Whether `a` is older than `b` on a store both name.
27
+ *
28
+ * ```ts
29
+ * import { older } from '@yaks/sync'
30
+ * import { equal } from '@yaks/testing'
31
+ *
32
+ * equal(older(4, 5), true)
33
+ * equal(older({ books: 9, loans: 1 }, { loans: 2 }), true)
34
+ * equal(older({ books: 1 }, { loans: 2 }), false)
35
+ * ```
36
+ */ export declare let older: (a: Revision, b: Revision) => boolean;
37
+ /** The revisions a copy holds, of rows and of subscriptions. */ export type Revisions = {
38
+ /** why this frame must not land, or nothing when it may */ check: (frame: Stamped) => Verdict | undefined;
39
+ /** this frame landed: its rows and its subscription are at its revision */ landed: (frame: Stamped) => void;
40
+ /** this subscription was asked, again or for the first time */ asked: (id: string) => void;
41
+ /** this subscription is closed */ closed: (id: string) => void;
42
+ };
43
+ /**
44
+ * A copy's revisions. A frame without a revision, from a store that says
45
+ * none, is never held back.
46
+ *
47
+ * ```ts
48
+ * import { revisions } from '@yaks/sync'
49
+ * import { equal } from '@yaks/testing'
50
+ *
51
+ * let held = revisions()
52
+ * let row = { entity: { eid: 'dal' } }
53
+ * held.asked('a')
54
+ * held.landed({ id: 'a', revision: 5, bundles: [row] })
55
+ * equal(held.check({ id: 'b', revision: 4, bundles: [row] }), 'stale')
56
+ * equal(held.check({ id: 'a', revision: 7, since: 6 }), 'skipped')
57
+ * equal(held.check({ id: 'a', revision: 6, since: 5 }), undefined)
58
+ * ```
59
+ */ export declare let revisions: (limit: typeof REVISIONS) => Revisions;
60
+ //# sourceMappingURL=revision.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"revision.d.ts","sources":["../revision.ts"],"names":[],"mappings":"AAgBA,cAAc,GAAG,2BAAgC;AAEjD,oEAAoE,GACpE,YAAY,WAAW,MAAM,GAAG,OAAO,MAAM,EAAE,MAAM;AAErD,sEAAsE,GACtE,YAAY;EACV,IAAI,MAAM;EACV,WAAW;EACX,QAAQ;EACR;IAAY;MAAU,KAAK;;;EAC3B;IAAU;MAAU,KAAK;;;EACzB,OAAO;EACP,WAAW;;AAGb;;8DAE8D,GAC9D,YAAY,UAAU,YAAY,UAAU;AAE5C;;8CAE8C,GAC9C,OAAO,cAAM,WAAY,OAAM;AAM/B;;;;;;;;;;CAUC,GACD,OAAO,YAAI,QAAS,GAAG,UAAU,GAAG,aAAW,OAAO,CAGrD;AAsBD,8DAA8D,GAC9D,YAAY;EACV,yDAAyD,GACzD,QAAQ,OAAO,YAAY,UAAU,SAAS;EAC9C,yEAAyE,GACzE,SAAS,OAAO,YAAY,IAAI;EAChC,6DAA6D,GAC7D,QAAQ,IAAI,MAAM,KAAK,IAAI;EAC3B,gCAAgC,GAChC,SAAS,IAAI,MAAM,KAAK,IAAI;;AAG9B;;;;;;;;;;;;;;;;CAgBC,GACD,OAAO,YAAI,YAAa,cAAQ,cAAY,UAkC3C"}
@@ -0,0 +1,12 @@
1
+ import type { Bundle, Eid } from "@yaks/graph";
2
+ /** Whether this node is saying one component of one entity. */ export type Mine = (eid: Eid, comp: string) => boolean;
3
+ /** What this node is saying, kept across its connections. */ export type Saying = {
4
+ /** a committed write's relayed bundles; `carried` is whether a connection
5
+ * is open to take them now */ wrote: (bundles: Bundle[], carried: boolean) => void;
6
+ /** relayed bundles heard from another connection */ heard: (bundles: Bundle[]) => void;
7
+ /** whether this node is saying this component of this entity */ mine: Mine;
8
+ /** the current patches, without consuming any waiting clears */ held: () => Bundle[];
9
+ /** everything to say again on a new connection */ again: () => Bundle[];
10
+ };
11
+ export declare let saying: () => Saying;
12
+ //# sourceMappingURL=saying.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"saying.d.ts","sources":["../saying.ts"],"names":[],"mappings":"AAgBA,cAAc,MAAM,EAAE,GAAG,2BAAgC;AAIzD,6DAA6D,GAC7D,YAAY,QAAQ,KAAK,KAAK,MAAM,MAAM,KAAK,OAAO;AAEtD,2DAA2D,GAC3D,YAAY;EACV;8BAC4B,GAC5B,QAAQ,SAAS,UAAU,SAAS,OAAO,KAAK,IAAI;EACpD,kDAAkD,GAClD,QAAQ,SAAS,aAAa,IAAI;EAClC,8DAA8D,GAC9D,MAAM;EACN,8DAA8D,GAC9D,YAAY;EACZ,gDAAgD,GAChD,aAAa;;AAGf,OAAO,YAAI,cAAa,OA8BvB"}
@@ -0,0 +1,151 @@
1
+ import type { Bundle, Eid } from "@yaks/graph";
2
+ import type { Vocab } from "@yaks/vocab";
3
+ import type { Coverage } from "./coverage.js";
4
+ import { type Revision } from "./revision.js";
5
+ import type { Fetch } from "./outbound.js";
6
+ /** The part of a WebSocket this package uses. The standard `WebSocket`
7
+ * satisfies it, and so does any stand-in a test or a caller provides. */ export type Socket = {
8
+ /** `0` connecting, `1` open, `2` closing, `3` closed */ readyState: number;
9
+ /** bytes already handed to the transport but not yet sent */ bufferedAmount?: number;
10
+ /** send one frame, already serialized */ send(data: string): void;
11
+ /** close the connection, saying why with a close code */ close(code?: number): void;
12
+ /** listen for `open`, `message`, `close` and `error` */ addEventListener(type: string, listener: (event: Event & {
13
+ data?: unknown;
14
+ }) => void): void;
15
+ };
16
+ /** How a socket is made: the `WebSocket` constructor, or a stand-in with the
17
+ * same call shape. */ export type Connect = (url: string) => Socket;
18
+ /** How a retry is scheduled. Defaults to `setTimeout`; a test passes its own
19
+ * and fires the reconnect when it wants one. */ export type Timer = (fn: () => void, ms: number) => void;
20
+ /** Whether this machine has a network, and how to hear that it has one again.
21
+ * A browser says both, with `navigator.onLine` and the `online` event, and is
22
+ * the default; a runtime that says neither is always online. */ export type Network = {
23
+ /** false while this machine has no network at all */ online: () => boolean;
24
+ /** call `fn` each time the network comes back; returns its removal */ back: (fn: () => void) => () => void;
25
+ };
26
+ /** What a subscriber asks for: a query, or `true` for the raw feed of every
27
+ * committed write. */ export type Ask = string | true;
28
+ /** One push from the server: the entities now in the set, the ones that left
29
+ * it, or the refusal that closed the subscription. */ export type Frame = {
30
+ /** A host can attach display names for referenced people. */ refs?: string[];
31
+ names?: Record<string, string>;
32
+ /** an opted-in subscriber echoes this after applying the frame */ ack?: string;
33
+ transient?: import("@yaks/graph").TransientFrame[];
34
+ transientReset?: string[];
35
+ /** the subscription this frame answers */ id: string;
36
+ /** the store's revision its rows were read at (./revision.ts) */ revision?: Revision;
37
+ /** the revision its subscription's frame before it said */ since?: Revision;
38
+ /** the entities now in the set (whole rows, unless `coverage` says what
39
+ * each answers for), or, for a raw feed, the bundles as applied */ bundles?: Bundle[];
40
+ /** Per-row coverage for result bundles. Omitted entries are full rows. Each delivery
41
+ * replaces that role's coverage. Adapters must repeat projected coverage on
42
+ * deltas, including exclusions for deliberately omitted bodies. */ coverage?: Record<Eid, Coverage>;
43
+ /** Rider coverage, independent even when an eid has both roles in a frame.
44
+ * Omitted entries cover only delivered properties. */ peerCoverage?: Record<Eid, Coverage>;
45
+ /** Payload riders, pinned by this subscription but never query members:
46
+ * the entities a `.fields` projection's paths reach (@yaks/api). */ peers?: Bundle[];
47
+ /** Rider departures, independent of result membership. reset replaces both. */ peerGone?: Eid[];
48
+ /** entities that left the set — deleted, or no longer matching */ gone?: Eid[];
49
+ /**
50
+ * `sync: peers` components the server is relaying: somebody's cursor, a
51
+ * caret, a presence dot. The server keeps none of it, and a client holds it
52
+ * in its graph's memory until it is cleared: a value cleared by its writer —
53
+ * or by that writer's connection closing — arrives as the component set to
54
+ * `null`.
55
+ *
56
+ * Not to be confused with `peers` above, the payload riders. The two are
57
+ * unrelated; this one carries the `sync: peers` components.
58
+ */ relay?: Bundle[];
59
+ /** why the subscription was refused, when it was */ refused?: {
60
+ error: string;
61
+ message: string;
62
+ [k: string]: unknown;
63
+ };
64
+ /** an aggregate query's answer, in place of bundles: `.count`,
65
+ * `.distinct=prop` and `.tally=prop` (@yaks/api) */ count?: number;
66
+ distinct?: string[];
67
+ tally?: Record<string, number>;
68
+ /** the first frame after a reopen: the set as it now stands, whole */ reset?: boolean;
69
+ };
70
+ /** The socket's half of a sync: what it has to be told, and what it reports. */ export type WireOpts = {
71
+ /** Interpret binary properties only when their vocabulary declares them. */ vocab?: Vocab;
72
+ /** Decode a packet when the vocabulary must be loaded on demand. */ decode?: (value: unknown, payload?: Uint8Array, text?: string) => unknown | Promise<unknown>;
73
+ /** Resend connection-owned values after the server restores a connection. */ resay?: () => void;
74
+ /** the server's base URL — `https://…` or `http://…` */ url: string;
75
+ /** the socket constructor (default: the global `WebSocket`) */ connect?: Connect;
76
+ /** Local epoch milliseconds for clock measurements (default: a monotonic epoch). */ time?: () => number;
77
+ /** how a reconnect is scheduled (default: `setTimeout`) */ timer?: Timer;
78
+ /** the reconnect delay in ms after a failed attempt, doubling to `most`
79
+ * (default: 250), each drawn between half of it and all of it */ wait?: number;
80
+ /** how long in ms a connection holds before its loss starts the backoff
81
+ * again rather than going on with it (default: 10_000) */ held?: number;
82
+ /** the window in ms a connection that held is opened again within, at a
83
+ * moment drawn at random in it (default: 1_000) */ spread?: number;
84
+ /** each draw in [0, 1) a reconnect's moment is taken from (default:
85
+ * `Math.random`) */ random?: () => number;
86
+ /** the longest reconnect delay in ms, and how long an outage may go
87
+ * unresumed before it is reported (default: 30_000) */ most?: number;
88
+ /** how long in ms an open socket may say nothing after its clock probe,
89
+ * and a new one wait on its handshake, before it is given up as dead and
90
+ * replaced (default: 10_000) */ quiet?: number;
91
+ /** whether this machine has a network (default: the browser's say, where
92
+ * there is one) */ network?: Network;
93
+ /** how the server is asked whether it answers at all, before a socket that
94
+ * never reopened is said dropped (default: the global `fetch`) */ fetch?: Fetch;
95
+ /** each frame, once the reset bookkeeping has been done for it */ land: (frame: Frame) => unknown | Promise<unknown>;
96
+ /** a subscription needs a fresh answer: opened, re-pointed or disconnected */ pending?: (id: string) => void;
97
+ /** the `sync: peers` values this side is saying, said again on every
98
+ * connection before its subscriptions (./saying.ts) */ again?: () => Bundle[];
99
+ /** anything that went wrong on the socket */ report: (err: unknown) => void;
100
+ };
101
+ /** One page's tab: whether it is visible, focused, and idle. */ export type Tab = {
102
+ visible: boolean;
103
+ focused: boolean;
104
+ idle?: boolean;
105
+ };
106
+ /** A live WebSocket with subscriptions held open on it. */ export type Wire = {
107
+ /** open the socket if it is not already opening or open */ open: () => void;
108
+ /** subscribe (or re-point an existing id); returns the id */ subscribe: (query: Ask, id?: string) => string;
109
+ /** drop one subscription */ unsubscribe: (id: string) => void;
110
+ /** ask a subscription again, whole: what it holds stays until the answer
111
+ * replaces it, and its frames still on their way from before are let go */ ask: (id: string) => void;
112
+ /**
113
+ * Send `sync: peers` components to the server to relay. It is the only write
114
+ * sent over the socket rather than posted, and it goes here because its
115
+ * lifetime is this socket's: the server holds it under this connection and
116
+ * clears it when the connection closes. A relay message sent while the socket
117
+ * is down is dropped, never queued: there is no backlog worth replaying, and
118
+ * the next connection is told what is being said as it stands (`again`).
119
+ */ relay: (bundles: Bundle[]) => void;
120
+ /** Say whether this page's tab is visible and focused, which is where its
121
+ * person is (@yaks/notify/whereabouts). Said now when the socket is open,
122
+ * and again on every connection; it never opens one. */ tab: (tab: Tab) => void;
123
+ /** Store epoch milliseconds; first use opens the socket. Every open socket
124
+ * calibrates; until the first reply this is the local epoch, and across
125
+ * outages it holds the offset. */ now: () => number;
126
+ /** whether the socket is open right now */ connected: () => boolean;
127
+ /** be called with true each time the socket opens, and with false each
128
+ * time an open one closes under it; not called immediately. The returned
129
+ * function removes the listener. */ onConnection: (fn: (open: boolean) => void) => () => void;
130
+ /** open a new socket in place of the one open now, at once and with no
131
+ * outage: the server said its address now reaches another store
132
+ * (outbound.ts `MOVED`), which the new socket subscribes to afresh */ reopen: () => void;
133
+ /** close the socket and stop reconnecting */ close: () => void;
134
+ };
135
+ /** A connection that closed under this wire and has not resumed `most` after
136
+ * it began: what closed it (its close code, when it gave one) and how long it
137
+ * has been down. Resumed is open again with every subscription answered again,
138
+ * so a drop that resumes in time, whatever its close code (a restart, a proxy
139
+ * reloading, a laptop waking), is the system working and is never said. Said
140
+ * once per outage, while it lasts, and never while this machine has no
141
+ * network. Its `cause` says what it was waiting on: a socket that never
142
+ * reopened, or the queries a reopened one has not had answered. */ export declare class Dropped extends Error {
143
+ constructor(url: string, code: number | undefined, ms: number, waiting?: string);
144
+ }
145
+ /** The next reconnect delay: double it, but never past the ceiling. */ export declare let backoff: (wait: number, most: number) => number;
146
+ /**
147
+ * The socket half of a sync: subscriptions that survive a disconnection, one
148
+ * reconnect timer, and a reset frame after each reopen so a client can work out
149
+ * what left the set while it was away.
150
+ */ export declare let wire: (opts: WireOpts) => Wire;
151
+ //# sourceMappingURL=socket.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"socket.d.ts","sources":["../socket.ts"],"names":[],"mappings":"AAqCA,cAAc,MAAM,EAAE,GAAG,2BAAgC;AAIzD,cAAc,KAAK,2BAAgC;AAGnD,cAAc,QAAQ,wBAAuB;AAE7C,SAAS,KAAK,QAAQ,wBAAgD;AACtE,c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