@use-everywhere/core 0.5.0 → 0.7.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.
- package/dist/index.cjs +379 -117
- package/dist/index.d.cts +112 -43
- package/dist/index.d.ts +112 -43
- package/dist/index.js +373 -112
- package/dist/testing.cjs +85 -0
- package/dist/testing.d.cts +31 -0
- package/dist/testing.d.ts +31 -0
- package/dist/testing.js +57 -0
- package/dist/transport.types-CV1WZOhy.d.cts +20 -0
- package/dist/transport.types-CV1WZOhy.d.ts +20 -0
- package/package.json +32 -14
package/dist/testing.cjs
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/testing.ts
|
|
21
|
+
var testing_exports = {};
|
|
22
|
+
__export(testing_exports, {
|
|
23
|
+
MemoryHub: () => MemoryHub,
|
|
24
|
+
MemoryTransport: () => MemoryTransport
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(testing_exports);
|
|
27
|
+
|
|
28
|
+
// src/transport/memory-transport.ts
|
|
29
|
+
var MemoryTransport = class {
|
|
30
|
+
constructor(hub) {
|
|
31
|
+
this.hub = hub;
|
|
32
|
+
this.kind = "memory";
|
|
33
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
34
|
+
this.closed = false;
|
|
35
|
+
}
|
|
36
|
+
post(data) {
|
|
37
|
+
if (this.closed) return;
|
|
38
|
+
this.hub.broadcast(this, data);
|
|
39
|
+
}
|
|
40
|
+
subscribe(listener) {
|
|
41
|
+
this.listeners.add(listener);
|
|
42
|
+
return () => this.listeners.delete(listener);
|
|
43
|
+
}
|
|
44
|
+
close() {
|
|
45
|
+
this.closed = true;
|
|
46
|
+
this.listeners.clear();
|
|
47
|
+
this.hub.disconnect(this);
|
|
48
|
+
}
|
|
49
|
+
/** @internal */
|
|
50
|
+
deliver(data) {
|
|
51
|
+
if (this.closed) return;
|
|
52
|
+
for (const listener of this.listeners) listener(data);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// src/transport/memory-hub.ts
|
|
57
|
+
var MemoryHub = class {
|
|
58
|
+
constructor() {
|
|
59
|
+
this.transports = /* @__PURE__ */ new Set();
|
|
60
|
+
}
|
|
61
|
+
connect() {
|
|
62
|
+
const transport = new MemoryTransport(this);
|
|
63
|
+
this.transports.add(transport);
|
|
64
|
+
return transport;
|
|
65
|
+
}
|
|
66
|
+
/** @internal */
|
|
67
|
+
broadcast(from, data) {
|
|
68
|
+
structuredClone(data);
|
|
69
|
+
for (const transport of this.transports) {
|
|
70
|
+
if (transport !== from) {
|
|
71
|
+
const copy = structuredClone(data);
|
|
72
|
+
queueMicrotask(() => transport.deliver(copy));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** @internal */
|
|
77
|
+
disconnect(transport) {
|
|
78
|
+
this.transports.delete(transport);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
82
|
+
0 && (module.exports = {
|
|
83
|
+
MemoryHub,
|
|
84
|
+
MemoryTransport
|
|
85
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { T as Transport, a as TransportKind } from './transport.types-CV1WZOhy.cjs';
|
|
2
|
+
|
|
3
|
+
/** One simulated client on a MemoryHub. Create via hub.connect(). */
|
|
4
|
+
declare class MemoryTransport implements Transport {
|
|
5
|
+
private hub;
|
|
6
|
+
readonly kind: TransportKind;
|
|
7
|
+
private listeners;
|
|
8
|
+
private closed;
|
|
9
|
+
constructor(hub: MemoryHub);
|
|
10
|
+
post(data: unknown): void;
|
|
11
|
+
subscribe(listener: (data: unknown) => void): () => void;
|
|
12
|
+
close(): void;
|
|
13
|
+
/** @internal */
|
|
14
|
+
deliver(data: unknown): void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* In-memory hub for tests: N transports attached to one hub, each post is
|
|
19
|
+
* delivered to every *other* transport on a microtask (mirrors BroadcastChannel's
|
|
20
|
+
* async, no-self-echo delivery).
|
|
21
|
+
*/
|
|
22
|
+
declare class MemoryHub {
|
|
23
|
+
private transports;
|
|
24
|
+
connect(): MemoryTransport;
|
|
25
|
+
/** @internal */
|
|
26
|
+
broadcast(from: MemoryTransport, data: unknown): void;
|
|
27
|
+
/** @internal */
|
|
28
|
+
disconnect(transport: MemoryTransport): void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { MemoryHub, MemoryTransport };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { T as Transport, a as TransportKind } from './transport.types-CV1WZOhy.js';
|
|
2
|
+
|
|
3
|
+
/** One simulated client on a MemoryHub. Create via hub.connect(). */
|
|
4
|
+
declare class MemoryTransport implements Transport {
|
|
5
|
+
private hub;
|
|
6
|
+
readonly kind: TransportKind;
|
|
7
|
+
private listeners;
|
|
8
|
+
private closed;
|
|
9
|
+
constructor(hub: MemoryHub);
|
|
10
|
+
post(data: unknown): void;
|
|
11
|
+
subscribe(listener: (data: unknown) => void): () => void;
|
|
12
|
+
close(): void;
|
|
13
|
+
/** @internal */
|
|
14
|
+
deliver(data: unknown): void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* In-memory hub for tests: N transports attached to one hub, each post is
|
|
19
|
+
* delivered to every *other* transport on a microtask (mirrors BroadcastChannel's
|
|
20
|
+
* async, no-self-echo delivery).
|
|
21
|
+
*/
|
|
22
|
+
declare class MemoryHub {
|
|
23
|
+
private transports;
|
|
24
|
+
connect(): MemoryTransport;
|
|
25
|
+
/** @internal */
|
|
26
|
+
broadcast(from: MemoryTransport, data: unknown): void;
|
|
27
|
+
/** @internal */
|
|
28
|
+
disconnect(transport: MemoryTransport): void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { MemoryHub, MemoryTransport };
|
package/dist/testing.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// src/transport/memory-transport.ts
|
|
2
|
+
var MemoryTransport = class {
|
|
3
|
+
constructor(hub) {
|
|
4
|
+
this.hub = hub;
|
|
5
|
+
this.kind = "memory";
|
|
6
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
7
|
+
this.closed = false;
|
|
8
|
+
}
|
|
9
|
+
post(data) {
|
|
10
|
+
if (this.closed) return;
|
|
11
|
+
this.hub.broadcast(this, data);
|
|
12
|
+
}
|
|
13
|
+
subscribe(listener) {
|
|
14
|
+
this.listeners.add(listener);
|
|
15
|
+
return () => this.listeners.delete(listener);
|
|
16
|
+
}
|
|
17
|
+
close() {
|
|
18
|
+
this.closed = true;
|
|
19
|
+
this.listeners.clear();
|
|
20
|
+
this.hub.disconnect(this);
|
|
21
|
+
}
|
|
22
|
+
/** @internal */
|
|
23
|
+
deliver(data) {
|
|
24
|
+
if (this.closed) return;
|
|
25
|
+
for (const listener of this.listeners) listener(data);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// src/transport/memory-hub.ts
|
|
30
|
+
var MemoryHub = class {
|
|
31
|
+
constructor() {
|
|
32
|
+
this.transports = /* @__PURE__ */ new Set();
|
|
33
|
+
}
|
|
34
|
+
connect() {
|
|
35
|
+
const transport = new MemoryTransport(this);
|
|
36
|
+
this.transports.add(transport);
|
|
37
|
+
return transport;
|
|
38
|
+
}
|
|
39
|
+
/** @internal */
|
|
40
|
+
broadcast(from, data) {
|
|
41
|
+
structuredClone(data);
|
|
42
|
+
for (const transport of this.transports) {
|
|
43
|
+
if (transport !== from) {
|
|
44
|
+
const copy = structuredClone(data);
|
|
45
|
+
queueMicrotask(() => transport.deliver(copy));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/** @internal */
|
|
50
|
+
disconnect(transport) {
|
|
51
|
+
this.transports.delete(transport);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
export {
|
|
55
|
+
MemoryHub,
|
|
56
|
+
MemoryTransport
|
|
57
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which mechanism a transport actually uses. Worth surfacing, because they are
|
|
3
|
+
* not equivalent: `storage` is a fallback with lower fidelity than
|
|
4
|
+
* `broadcast-channel`, and `none` means nothing is being shared at all.
|
|
5
|
+
*/
|
|
6
|
+
type TransportKind = 'broadcast-channel' | 'storage' | 'memory' | 'none' | (string & {});
|
|
7
|
+
/**
|
|
8
|
+
* Minimal message bus. Implementations: BroadcastChannelTransport (same-origin),
|
|
9
|
+
* StorageTransport (fallback), NoopTransport (SSR / local-only), MemoryTransport
|
|
10
|
+
* (tests). A transport never echoes a client's own posts back to it.
|
|
11
|
+
*/
|
|
12
|
+
interface Transport {
|
|
13
|
+
/** What this transport is. Optional so a custom transport need not declare one. */
|
|
14
|
+
readonly kind?: TransportKind;
|
|
15
|
+
post(data: unknown): void;
|
|
16
|
+
subscribe(listener: (data: unknown) => void): () => void;
|
|
17
|
+
close(): void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type { Transport as T, TransportKind as a };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which mechanism a transport actually uses. Worth surfacing, because they are
|
|
3
|
+
* not equivalent: `storage` is a fallback with lower fidelity than
|
|
4
|
+
* `broadcast-channel`, and `none` means nothing is being shared at all.
|
|
5
|
+
*/
|
|
6
|
+
type TransportKind = 'broadcast-channel' | 'storage' | 'memory' | 'none' | (string & {});
|
|
7
|
+
/**
|
|
8
|
+
* Minimal message bus. Implementations: BroadcastChannelTransport (same-origin),
|
|
9
|
+
* StorageTransport (fallback), NoopTransport (SSR / local-only), MemoryTransport
|
|
10
|
+
* (tests). A transport never echoes a client's own posts back to it.
|
|
11
|
+
*/
|
|
12
|
+
interface Transport {
|
|
13
|
+
/** What this transport is. Optional so a custom transport need not declare one. */
|
|
14
|
+
readonly kind?: TransportKind;
|
|
15
|
+
post(data: unknown): void;
|
|
16
|
+
subscribe(listener: (data: unknown) => void): () => void;
|
|
17
|
+
close(): void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type { Transport as T, TransportKind as a };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@use-everywhere/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Cross-tab shared state, events, presence, and cross-origin window channels",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Jonatan Kruszewski <jonakrusze@gmail.com>",
|
|
@@ -38,6 +38,16 @@
|
|
|
38
38
|
"types": "./dist/index.d.cts",
|
|
39
39
|
"default": "./dist/index.cjs"
|
|
40
40
|
}
|
|
41
|
+
},
|
|
42
|
+
"./testing": {
|
|
43
|
+
"import": {
|
|
44
|
+
"types": "./dist/testing.d.ts",
|
|
45
|
+
"default": "./dist/testing.js"
|
|
46
|
+
},
|
|
47
|
+
"require": {
|
|
48
|
+
"types": "./dist/testing.d.cts",
|
|
49
|
+
"default": "./dist/testing.cjs"
|
|
50
|
+
}
|
|
41
51
|
}
|
|
42
52
|
},
|
|
43
53
|
"files": [
|
|
@@ -48,49 +58,43 @@
|
|
|
48
58
|
"name": "everything (import *)",
|
|
49
59
|
"path": "dist/index.js",
|
|
50
60
|
"import": "*",
|
|
51
|
-
"limit": "
|
|
61
|
+
"limit": "7.2 kB"
|
|
52
62
|
},
|
|
53
63
|
{
|
|
54
64
|
"name": "createLeader",
|
|
55
65
|
"path": "dist/index.js",
|
|
56
66
|
"import": "{ createLeader }",
|
|
57
|
-
"limit": "
|
|
67
|
+
"limit": "3.33 kB"
|
|
58
68
|
},
|
|
59
69
|
{
|
|
60
70
|
"name": "createSharedStore",
|
|
61
71
|
"path": "dist/index.js",
|
|
62
72
|
"import": "{ createSharedStore }",
|
|
63
|
-
"limit": "
|
|
73
|
+
"limit": "3.3 kB"
|
|
64
74
|
},
|
|
65
75
|
{
|
|
66
76
|
"name": "createChannel",
|
|
67
77
|
"path": "dist/index.js",
|
|
68
78
|
"import": "{ createChannel }",
|
|
69
|
-
"limit": "1
|
|
79
|
+
"limit": "2.1 kB"
|
|
70
80
|
},
|
|
71
81
|
{
|
|
72
82
|
"name": "createPresence",
|
|
73
83
|
"path": "dist/index.js",
|
|
74
84
|
"import": "{ createPresence }",
|
|
75
|
-
"limit": "
|
|
85
|
+
"limit": "2.28 kB"
|
|
76
86
|
},
|
|
77
87
|
{
|
|
78
88
|
"name": "openWindow (opener side)",
|
|
79
89
|
"path": "dist/index.js",
|
|
80
90
|
"import": "{ openWindow }",
|
|
81
|
-
"limit": "1.
|
|
91
|
+
"limit": "1.49 kB"
|
|
82
92
|
},
|
|
83
93
|
{
|
|
84
94
|
"name": "connectToOpener (child side)",
|
|
85
95
|
"path": "dist/index.js",
|
|
86
96
|
"import": "{ connectToOpener }",
|
|
87
|
-
"limit": "1.
|
|
88
|
-
},
|
|
89
|
-
{
|
|
90
|
-
"name": "MemoryHub (test transport)",
|
|
91
|
-
"path": "dist/index.js",
|
|
92
|
-
"import": "{ MemoryHub }",
|
|
93
|
-
"limit": "300 B"
|
|
97
|
+
"limit": "1.32 kB"
|
|
94
98
|
},
|
|
95
99
|
{
|
|
96
100
|
"name": "observeBus + enableDebug",
|
|
@@ -103,12 +107,19 @@
|
|
|
103
107
|
"path": "dist/index.js",
|
|
104
108
|
"import": "{ localStorageAdapter }",
|
|
105
109
|
"limit": "500 B"
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
"name": "MemoryHub (testing subpath)",
|
|
113
|
+
"path": "dist/testing.js",
|
|
114
|
+
"import": "{ MemoryHub }",
|
|
115
|
+
"limit": "350 B"
|
|
106
116
|
}
|
|
107
117
|
],
|
|
108
118
|
"devDependencies": {
|
|
109
119
|
"@arethetypeswrong/cli": "^0.18.5",
|
|
110
120
|
"@size-limit/preset-small-lib": "^13.0.1",
|
|
111
121
|
"@vitest/coverage-v8": "^4.1.10",
|
|
122
|
+
"fast-check": "^4.9.0",
|
|
112
123
|
"happy-dom": "^20.11.1",
|
|
113
124
|
"publint": "^0.3.21",
|
|
114
125
|
"size-limit": "^13.0.1",
|
|
@@ -116,6 +127,13 @@
|
|
|
116
127
|
"typescript": "^6.0.3",
|
|
117
128
|
"vitest": "^4.1.10"
|
|
118
129
|
},
|
|
130
|
+
"typesVersions": {
|
|
131
|
+
"*": {
|
|
132
|
+
"testing": [
|
|
133
|
+
"./dist/testing.d.ts"
|
|
134
|
+
]
|
|
135
|
+
}
|
|
136
|
+
},
|
|
119
137
|
"scripts": {
|
|
120
138
|
"build": "tsup",
|
|
121
139
|
"test": "vitest run --coverage",
|