atom.io 0.16.1 → 0.16.3

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 (99) hide show
  1. package/data/dist/index.cjs +31 -14
  2. package/data/dist/index.cjs.map +1 -1
  3. package/data/dist/index.js +31 -14
  4. package/data/dist/index.js.map +1 -1
  5. package/data/src/join.ts +31 -14
  6. package/dist/chunk-H4Q5FTPZ.js +11 -0
  7. package/dist/chunk-H4Q5FTPZ.js.map +1 -0
  8. package/dist/index.cjs +7 -14
  9. package/dist/index.cjs.map +1 -1
  10. package/dist/index.d.ts +8 -7
  11. package/dist/index.js +8 -14
  12. package/dist/index.js.map +1 -1
  13. package/internal/dist/index.cjs +240 -193
  14. package/internal/dist/index.cjs.map +1 -1
  15. package/internal/dist/index.d.ts +30 -9
  16. package/internal/dist/index.js +233 -194
  17. package/internal/dist/index.js.map +1 -1
  18. package/internal/src/families/find-in-store.ts +74 -0
  19. package/internal/src/families/index.ts +1 -0
  20. package/internal/src/ingest-updates/ingest-transaction-update.ts +1 -0
  21. package/internal/src/mutable/tracker.ts +37 -32
  22. package/internal/src/mutable/transceiver.ts +1 -1
  23. package/internal/src/not-found-error.ts +14 -3
  24. package/internal/src/operation.ts +2 -1
  25. package/internal/src/selector/create-writable-selector.ts +2 -1
  26. package/internal/src/selector/register-selector.ts +5 -4
  27. package/internal/src/set-state/set-atom.ts +23 -6
  28. package/internal/src/set-state/stow-update.ts +2 -4
  29. package/internal/src/store/store.ts +13 -4
  30. package/internal/src/timeline/add-atom-to-timeline.ts +5 -5
  31. package/internal/src/transaction/abort-transaction.ts +2 -1
  32. package/internal/src/transaction/apply-transaction.ts +5 -3
  33. package/internal/src/transaction/build-transaction.ts +17 -10
  34. package/internal/src/transaction/create-transaction.ts +2 -3
  35. package/internal/src/transaction/index.ts +3 -2
  36. package/internal/src/transaction/is-root-store.ts +23 -0
  37. package/package.json +10 -10
  38. package/react/dist/index.cjs +27 -21
  39. package/react/dist/index.cjs.map +1 -1
  40. package/react/dist/index.d.ts +8 -2
  41. package/react/dist/index.js +27 -21
  42. package/react/dist/index.js.map +1 -1
  43. package/react/src/index.ts +4 -1
  44. package/react/src/use-i.ts +36 -0
  45. package/react/src/use-json.ts +38 -0
  46. package/react/src/use-o.ts +34 -0
  47. package/react/src/use-tl.ts +45 -0
  48. package/realtime-client/dist/index.cjs +163 -62
  49. package/realtime-client/dist/index.cjs.map +1 -1
  50. package/realtime-client/dist/index.d.ts +10 -6
  51. package/realtime-client/dist/index.js +153 -60
  52. package/realtime-client/dist/index.js.map +1 -1
  53. package/realtime-client/src/index.ts +2 -1
  54. package/realtime-client/src/pull-state.ts +4 -3
  55. package/realtime-client/src/{realtime-client-store.ts → realtime-client-stores/client-main-store.ts} +0 -8
  56. package/realtime-client/src/realtime-client-stores/client-sync-store.ts +15 -0
  57. package/realtime-client/src/realtime-client-stores/index.ts +2 -0
  58. package/realtime-client/src/sync-server-action.ts +131 -39
  59. package/realtime-client/src/sync-state.ts +19 -0
  60. package/realtime-react/dist/index.cjs +43 -26
  61. package/realtime-react/dist/index.cjs.map +1 -1
  62. package/realtime-react/dist/index.d.ts +3 -1
  63. package/realtime-react/dist/index.js +41 -25
  64. package/realtime-react/dist/index.js.map +1 -1
  65. package/realtime-react/src/index.ts +1 -0
  66. package/realtime-react/src/on-mount.ts +3 -21
  67. package/realtime-react/src/use-realtime-service.ts +1 -1
  68. package/realtime-react/src/use-server-action.ts +1 -1
  69. package/realtime-react/src/use-single-effect.ts +29 -0
  70. package/realtime-react/src/use-sync-server-action.ts +5 -8
  71. package/realtime-react/src/use-sync.ts +17 -0
  72. package/realtime-server/dist/index.cjs +242 -48
  73. package/realtime-server/dist/index.cjs.map +1 -1
  74. package/realtime-server/dist/index.d.ts +147 -9
  75. package/realtime-server/dist/index.js +232 -51
  76. package/realtime-server/dist/index.js.map +1 -1
  77. package/realtime-server/src/index.ts +2 -0
  78. package/realtime-server/src/realtime-action-receiver.ts +4 -3
  79. package/realtime-server/src/realtime-action-synchronizer.ts +100 -13
  80. package/realtime-server/src/realtime-family-provider.ts +10 -6
  81. package/realtime-server/src/realtime-mutable-family-provider.ts +15 -18
  82. package/realtime-server/src/realtime-mutable-provider.ts +1 -0
  83. package/realtime-server/src/realtime-server-stores/index.ts +2 -0
  84. package/realtime-server/src/realtime-server-stores/server-sync-store.ts +115 -0
  85. package/realtime-server/src/realtime-server-stores/server-user-store.ts +45 -0
  86. package/realtime-server/src/realtime-state-provider.ts +16 -8
  87. package/realtime-server/src/realtime-state-receiver.ts +1 -0
  88. package/realtime-server/src/realtime-state-synchronizer.ts +23 -0
  89. package/realtime-testing/dist/index.cjs +65 -26
  90. package/realtime-testing/dist/index.cjs.map +1 -1
  91. package/realtime-testing/dist/index.d.ts +11 -7
  92. package/realtime-testing/dist/index.js +64 -26
  93. package/realtime-testing/dist/index.js.map +1 -1
  94. package/realtime-testing/src/setup-realtime-test.tsx +83 -43
  95. package/src/find-state.ts +8 -16
  96. package/src/logger.ts +16 -11
  97. package/src/transaction.ts +4 -4
  98. package/react/src/store-hooks.ts +0 -87
  99. package/realtime-server/src/realtime-server-store.ts +0 -39
@@ -4,6 +4,7 @@ import type { Json } from "atom.io/json"
4
4
 
5
5
  import type { ServerConfig } from "."
6
6
 
7
+ export type StateProvider = ReturnType<typeof realtimeStateProvider>
7
8
  export function realtimeStateProvider({
8
9
  socket,
9
10
  store = IMPLICIT.STORE,
@@ -11,16 +12,11 @@ export function realtimeStateProvider({
11
12
  return function stateProvider<J extends Json.Serializable>(
12
13
  token: AtomIO.WritableToken<J>,
13
14
  ): () => void {
14
- let unsubscribeFromStateUpdates: (() => void) | null = null
15
-
16
- const fillUnsubRequest = () => {
17
- socket.off(`unsub:${token.key}`, fillUnsubRequest)
18
- unsubscribeFromStateUpdates?.()
19
- unsubscribeFromStateUpdates = null
20
- }
15
+ let unsubscribeFromStateUpdates: (() => void) | undefined
21
16
 
22
17
  const fillSubRequest = () => {
23
18
  socket.emit(`serve:${token.key}`, AtomIO.getState(token, store))
19
+
24
20
  unsubscribeFromStateUpdates = subscribeToState(
25
21
  token,
26
22
  ({ newValue }) => {
@@ -29,6 +25,15 @@ export function realtimeStateProvider({
29
25
  `expose-single:${socket.id}`,
30
26
  store,
31
27
  )
28
+
29
+ const fillUnsubRequest = () => {
30
+ socket.off(`unsub:${token.key}`, fillUnsubRequest)
31
+ if (unsubscribeFromStateUpdates) {
32
+ unsubscribeFromStateUpdates()
33
+ unsubscribeFromStateUpdates = undefined
34
+ }
35
+ }
36
+
32
37
  socket.on(`unsub:${token.key}`, fillUnsubRequest)
33
38
  }
34
39
 
@@ -36,7 +41,10 @@ export function realtimeStateProvider({
36
41
 
37
42
  return () => {
38
43
  socket.off(`sub:${token.key}`, fillSubRequest)
39
- unsubscribeFromStateUpdates?.()
44
+ if (unsubscribeFromStateUpdates) {
45
+ unsubscribeFromStateUpdates()
46
+ unsubscribeFromStateUpdates = undefined
47
+ }
40
48
  }
41
49
  }
42
50
  }
@@ -5,6 +5,7 @@ import type { Json } from "atom.io/json"
5
5
 
6
6
  import type { ServerConfig } from "."
7
7
 
8
+ export type StateReceiver = ReturnType<typeof realtimeStateReceiver>
8
9
  export function realtimeStateReceiver({
9
10
  socket,
10
11
  store = IMPLICIT.STORE,
@@ -0,0 +1,23 @@
1
+ import * as AtomIO from "atom.io"
2
+ import { IMPLICIT } from "atom.io/internal"
3
+ import type { Json } from "atom.io/json"
4
+
5
+ import type { ServerConfig } from "."
6
+
7
+ export function realtimeStateSynchronizer({
8
+ socket,
9
+ store = IMPLICIT.STORE,
10
+ }: ServerConfig) {
11
+ return function stateSynchronizer<J extends Json.Serializable>(
12
+ token: AtomIO.WritableToken<J>,
13
+ ): () => void {
14
+ const fillGetRequest = () => {
15
+ socket.emit(`value:${token.key}`, AtomIO.getState(token, store))
16
+ }
17
+
18
+ socket.on(`get:${token.key}`, fillGetRequest)
19
+ return () => {
20
+ socket.off(`get:${token.key}`, fillGetRequest)
21
+ }
22
+ }
23
+ }
@@ -6,6 +6,7 @@ var AtomIO = require('atom.io');
6
6
  var Internal = require('atom.io/internal');
7
7
  var AR = require('atom.io/react');
8
8
  var RTR = require('atom.io/realtime-react');
9
+ var RTS = require('atom.io/realtime-server');
9
10
  var Happy = require('happy-dom');
10
11
  var SocketIO = require('socket.io');
11
12
  var socket_ioClient = require('socket.io-client');
@@ -34,6 +35,7 @@ var AtomIO__namespace = /*#__PURE__*/_interopNamespace(AtomIO);
34
35
  var Internal__namespace = /*#__PURE__*/_interopNamespace(Internal);
35
36
  var AR__namespace = /*#__PURE__*/_interopNamespace(AR);
36
37
  var RTR__namespace = /*#__PURE__*/_interopNamespace(RTR);
38
+ var RTS__namespace = /*#__PURE__*/_interopNamespace(RTS);
37
39
  var Happy__namespace = /*#__PURE__*/_interopNamespace(Happy);
38
40
  var SocketIO__namespace = /*#__PURE__*/_interopNamespace(SocketIO);
39
41
 
@@ -60,13 +62,41 @@ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
60
62
  // ../anvl/src/object/entries.ts
61
63
  var recordToEntries = (obj) => Object.entries(obj);
62
64
  var setupRealtimeTestServer = (options) => {
65
+ const silo = new AtomIO__namespace.Silo(`SERVER`, Internal__namespace.IMPLICIT.STORE);
63
66
  const httpServer = http__namespace.createServer((_, res) => res.end(`Hello World!`));
64
67
  const address = httpServer.listen().address();
65
68
  const port = typeof address === `string` ? 80 : address === null ? null : address.port;
66
69
  if (port === null)
67
70
  throw new Error(`Could not determine port for test server`);
68
- const server = new SocketIO__namespace.Server(httpServer);
69
- const silo = new AtomIO__namespace.Silo(`SERVER`, Internal__namespace.IMPLICIT.STORE);
71
+ const server = new SocketIO__namespace.Server(httpServer).use((socket, next) => {
72
+ const { token, username } = socket.handshake.auth;
73
+ if (token === `test` && socket.id) {
74
+ const socketRelatedKeysState = Internal__namespace.findInStore(
75
+ RTS__namespace.usersOfSockets.core.findRelatedKeysState,
76
+ socket.id,
77
+ silo.store
78
+ );
79
+ const clientRelatedKeysState = Internal__namespace.findInStore(
80
+ RTS__namespace.usersOfSockets.core.findRelatedKeysState,
81
+ username,
82
+ silo.store
83
+ );
84
+ AtomIO__namespace.setState(
85
+ socketRelatedKeysState,
86
+ (keys) => (keys.clear(), keys.add(username)),
87
+ silo.store
88
+ );
89
+ AtomIO__namespace.setState(
90
+ clientRelatedKeysState,
91
+ (keys) => (keys.clear(), keys.add(socket.id)),
92
+ silo.store
93
+ );
94
+ console.log(`${username} connected on ${socket.id}`);
95
+ next();
96
+ } else {
97
+ next(new Error(`Authentication error`));
98
+ }
99
+ });
70
100
  server.on(`connection`, (socket) => {
71
101
  options.server({ socket, silo });
72
102
  });
@@ -82,32 +112,41 @@ var setupRealtimeTestServer = (options) => {
82
112
  };
83
113
  };
84
114
  var setupRealtimeTestClient = (options, name, port) => {
85
- const socket = socket_ioClient.io(`http://localhost:${port}/`);
86
- const silo = new AtomIO__namespace.Silo(name, Internal__namespace.IMPLICIT.STORE);
87
- const { document } = new Happy__namespace.Window();
88
- document.body.innerHTML = `<div id="app"></div>`;
89
- const renderResult = react.render(
90
- /* @__PURE__ */ jsxRuntime.jsx(AR__namespace.StoreProvider, { store: silo.store, children: /* @__PURE__ */ jsxRuntime.jsx(RTR__namespace.RealtimeProvider, { socket, children: /* @__PURE__ */ jsxRuntime.jsx(options.client, {}) }) }),
91
- {
92
- container: document.querySelector(`#app`)
115
+ const testClient = { dispose: () => {
116
+ } };
117
+ const init = () => {
118
+ const socket = socket_ioClient.io(`http://localhost:${port}/`, {
119
+ auth: { token: `test`, username: name }
120
+ });
121
+ const silo = new AtomIO__namespace.Silo(name, Internal__namespace.IMPLICIT.STORE);
122
+ for (const [key, value] of silo.store.valueMap.entries()) {
123
+ if (Array.isArray(value)) {
124
+ silo.store.valueMap.set(key, [...value]);
125
+ }
93
126
  }
94
- );
95
- const prettyPrint = () => console.log(react.prettyDOM(renderResult.container));
96
- const disconnect = () => socket.disconnect();
97
- const reconnect = () => socket.connect();
98
- const dispose = () => {
99
- socket.disconnect();
100
- Internal__namespace.clearStore(silo.store);
101
- };
102
- return {
103
- name,
104
- silo,
105
- renderResult,
106
- prettyPrint,
107
- disconnect,
108
- reconnect,
109
- dispose
127
+ const { document } = new Happy__namespace.Window();
128
+ document.body.innerHTML = `<div id="app"></div>`;
129
+ const renderResult = react.render(
130
+ /* @__PURE__ */ jsxRuntime.jsx(AR__namespace.StoreProvider, { store: silo.store, children: /* @__PURE__ */ jsxRuntime.jsx(RTR__namespace.RealtimeProvider, { socket, children: /* @__PURE__ */ jsxRuntime.jsx(options.client, {}) }) }),
131
+ {
132
+ container: document.querySelector(`#app`)
133
+ }
134
+ );
135
+ const prettyPrint = () => console.log(react.prettyDOM(renderResult.container));
136
+ const dispose = () => {
137
+ socket.disconnect();
138
+ Internal__namespace.clearStore(silo.store);
139
+ };
140
+ testClient.dispose = dispose;
141
+ return {
142
+ name,
143
+ silo,
144
+ socket,
145
+ renderResult,
146
+ prettyPrint
147
+ };
110
148
  };
149
+ return Object.assign(testClient, { init });
111
150
  };
112
151
  var singleClient = (options) => {
113
152
  const server = setupRealtimeTestServer(options);
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/setup-realtime-test.tsx","../../../anvl/src/object/entries.ts"],"names":["clients"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA,YAAY,UAAU;AAEtB,SAA4B,WAAW,cAAc;AACrD,YAAY,YAAY;AACxB,YAAY,cAAc;AAC1B,YAAY,QAAQ;AACpB,YAAY,SAAS;AACrB,YAAY,WAAW;AAEvB,YAAY,cAAc;AAE1B,SAAS,UAAU;;;ACTZ,IAAM,kBAAkB,CAC9B,QACmB,OAAO,QAAQ,GAAG;;;AD4FlC;AAzCG,IAAM,0BAA0B,CACtC,YACwB;AACxB,QAAM,aAAkB,kBAAa,CAAC,GAAG,QAAQ,IAAI,IAAI,cAAc,CAAC;AACxE,QAAM,UAAU,WAAW,OAAO,EAAE,QAAQ;AAC5C,QAAM,OACL,OAAO,YAAY,WA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wB,OAAO;AAC9C,QAAM,SAAS,wBAAwB,SAAS,UAAU,OAAO,IAAI;AAErE,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA,UAAU,MAAM;AACf,aAAO,QAAQ;AACf,aAAO,QAAQ;AAAA,IAChB;AAAA,EACD;AACD;AAEO,IAAM,cAAc,CAC1B,YAC+C;AAC/C,QAAM,SAAS,wBAAwB,OAAO;AAC9C,QAAM,UAAU,gBAAgB,QAAQ,OAAO,EAAE;AAAA,IAChD,CAACA,UAAS,CAAC,MAAM,MAAM,MAAM;AAC5B,MAAAA,SAAQ,IAAI,IAAI;AAAA,QACf,iCAAK,UAAL,EAAc,OAAO;AAAA,QACrB;AAAA,QACA,OAAO;AAAA,MACR;AACA,aAAOA;AAAA,IACR;AAAA,IACA,CAAC;AAAA,EACF;AAEA,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA,UAAU,MAAM;AACf,iBAAW,CAAC,EAAE,MAAM,KAAK,gBAAgB,OAAO,GAAG;AAClD,eAAO,QAAQ;AAAA,MAChB;AACA,aAAO,QAAQ;AAAA,IAChB;AAAA,EACD;AACD","sourcesContent":["import * as http from \"http\"\n\nimport { type RenderResult, prettyDOM, render } from \"@testing-library/react\"\nimport * as AtomIO from \"atom.io\"\nimport * as Internal from \"atom.io/internal\"\nimport * as AR from \"atom.io/react\"\nimport * as RTR from \"atom.io/realtime-react\"\nimport * as Happy from \"happy-dom\"\nimport * as React from \"react\"\nimport * as SocketIO from \"socket.io\"\nimport type { Socket as ClientSocket } from \"socket.io-client\"\nimport { io } from \"socket.io-client\"\n\nimport { recordToEntries } from \"~/packages/anvl/src/object\"\n\nexport type TestSetupOptions = {\n\tserver: (tools: { socket: SocketIO.Socket; silo: AtomIO.Silo }) => void\n}\nexport type TestSetupOptions__SingleClient = TestSetupOptions & {\n\tclient: React.FC\n}\nexport type TestSetupOptions__MultiClient<ClientNames extends string> =\n\tTestSetupOptions & {\n\t\tclients: {\n\t\t\t[K in ClientNames]: React.FC\n\t\t}\n\t}\n\nexport type RealtimeTestTools = {\n\tname: string\n\tsilo: AtomIO.Silo\n\tdispose: () => void\n}\nexport type RealtimeTestClient = RealtimeTestTools & {\n\trenderResult: RenderResult\n\tprettyPrint: () => void\n\treconnect: () => void\n\tdisconnect: () => void\n}\nexport type RealtimeTestServer = RealtimeTestTools & {\n\tport: number\n}\n\nexport type RealtimeTestAPI = {\n\tserver: RealtimeTestServer\n\tteardown: () => void\n}\nexport type RealtimeTestAPI__SingleClient = RealtimeTestAPI & {\n\tclient: RealtimeTestClient\n}\nexport type RealtimeTestAPI__MultiClient<ClientNames extends string> =\n\tRealtimeTestAPI & {\n\t\tclients: Record<ClientNames, RealtimeTestClient>\n\t}\n\nexport const setupRealtimeTestServer = (\n\toptions: TestSetupOptions,\n): RealtimeTestServer => {\n\tconst httpServer = http.createServer((_, res) => res.end(`Hello World!`))\n\tconst address = httpServer.listen().address()\n\tconst port =\n\t\ttypeof address === `string` ? 80 : address === null ? null : address.port\n\tif (port === null) throw new Error(`Could not determine port for test server`)\n\tconst server = new SocketIO.Server(httpServer)\n\n\tconst silo = new AtomIO.Silo(`SERVER`, Internal.IMPLICIT.STORE)\n\n\tserver.on(`connection`, (socket: SocketIO.Socket) => {\n\t\toptions.server({ socket, silo })\n\t})\n\n\tconst dispose = () => {\n\t\tserver.close()\n\t\tInternal.clearStore(silo.store)\n\t}\n\n\treturn {\n\t\tname: `SERVER`,\n\t\tsilo,\n\t\tdispose,\n\t\tport,\n\t}\n}\nexport const setupRealtimeTestClient = (\n\toptions: TestSetupOptions__SingleClient,\n\tname: string,\n\tport: number,\n): RealtimeTestClient => {\n\tconst socket: ClientSocket = io(`http://localhost:${port}/`)\n\tconst silo = new AtomIO.Silo(name, Internal.IMPLICIT.STORE)\n\n\tconst { document } = new Happy.Window()\n\tdocument.body.innerHTML = `<div id=\"app\"></div>`\n\tconst renderResult = render(\n\t\t<AR.StoreProvider store={silo.store}>\n\t\t\t<RTR.RealtimeProvider socket={socket}>\n\t\t\t\t<options.client />\n\t\t\t</RTR.RealtimeProvider>\n\t\t</AR.StoreProvider>,\n\t\t{\n\t\t\tcontainer: document.querySelector(`#app`) as unknown as HTMLElement,\n\t\t},\n\t)\n\n\tconst prettyPrint = () => console.log(prettyDOM(renderResult.container))\n\n\tconst disconnect = () => socket.disconnect()\n\tconst reconnect = () => socket.connect()\n\n\tconst dispose = () => {\n\t\tsocket.disconnect()\n\t\tInternal.clearStore(silo.store)\n\t}\n\n\treturn {\n\t\tname,\n\t\tsilo,\n\t\trenderResult,\n\t\tprettyPrint,\n\t\tdisconnect,\n\t\treconnect,\n\t\tdispose,\n\t}\n}\n\nexport const singleClient = (\n\toptions: TestSetupOptions__SingleClient,\n): RealtimeTestAPI__SingleClient => {\n\tconst server = setupRealtimeTestServer(options)\n\tconst client = setupRealtimeTestClient(options, `CLIENT`, server.port)\n\n\treturn {\n\t\tclient,\n\t\tserver,\n\t\tteardown: () => {\n\t\t\tclient.dispose()\n\t\t\tserver.dispose()\n\t\t},\n\t}\n}\n\nexport const multiClient = <ClientNames extends string>(\n\toptions: TestSetupOptions__MultiClient<ClientNames>,\n): RealtimeTestAPI__MultiClient<ClientNames> => {\n\tconst server = setupRealtimeTestServer(options)\n\tconst clients = recordToEntries(options.clients).reduce(\n\t\t(clients, [name, client]) => {\n\t\t\tclients[name] = setupRealtimeTestClient(\n\t\t\t\t{ ...options, client },\n\t\t\t\tname,\n\t\t\t\tserver.port,\n\t\t\t)\n\t\t\treturn clients\n\t\t},\n\t\t{} as Record<ClientNames, RealtimeTestClient>,\n\t)\n\n\treturn {\n\t\tclients,\n\t\tserver,\n\t\tteardown: () => {\n\t\t\tfor (const [, client] of recordToEntries(clients)) {\n\t\t\t\tclient.dispose()\n\t\t\t}\n\t\t\tserver.dispose()\n\t\t},\n\t}\n}\n","export type Entries<K extends keyof any, V> = [key: K, value: V][]\n\nexport const recordToEntries = <K extends keyof any, V>(\n\tobj: Record<K, V>,\n): Entries<K, V> => Object.entries(obj) as Entries<K, V>\n\nexport const entriesToRecord = <K extends keyof any, V>(\n\tentries: Entries<K, V>,\n): Record<K, V> => Object.fromEntries(entries) as Record<K, V>\n"]}
1
+ {"version":3,"sources":["../src/setup-realtime-test.tsx","../../../anvl/src/object/entries.ts"],"names":["clients"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA,YAAY,UAAU;AAEtB,SAA4B,WAAW,cAAc;AACrD,YAAY,YAAY;AACxB,YAAY,cAAc;AAC1B,YAAY,QAAQ;AACpB,YAAY,SAAS;AACrB,YAAY,SAAS;AACrB,YAAY,WAAW;AAEvB,YAAY,cAAc;AAE1B,SAAS,UAAU;;;ACVZ,IAAM,kBAAkB,CAC9B,QACmB,OAAO,QAAQ,GAAG;;;ADsIjC;AA9EE,IAAM,0BAA0B,CACtC,YACwB;AACxB,QAAM,OAAO,IAAW,YAAK,UAAmB,kBAAS,KAAK;AAE9D,QAAM,aAAkB,kBAAa,CAAC,GAAG,QAAQ,IAAI,IAAI,cAAc,CAAC;AACxE,QAAM,UAAU,WAAW,OAAO,EAAE,QAAQ;AAC5C,QAAM,OACL,OAAO,YAAY,WAAW,KAAK,YAAY,OAAO,OAAO,QAAQ;AACtE,MAAI,SAAS;AAAM,UAAM,IAAI,MAAM,0CAA0C;AAC7E,QAAM,SAAS,IAAa,gBAAO,UAAU,EAAE,IAAI,CAAC,QAAQ,SAAS;AACpE,UAAM,EAAE,OAAO,SAAS,IAAI,OAAO,UAAU;AAC7C,QAAI,UAAU,UAAU,OAAO,IAAI;AAClC,YAAM,yBAAkC;AAAA,QACnC,mBAAe,KAAK;AAAA,QACxB,OAAO;AAAA,QACP,KAAK;AAAA,MACN;AACA,YAAM,yBAAkC;AAAA,QACnC,mBAAe,KAAK;AAAA,QACxB;AAAA,QACA,KAAK;AAAA,MACN;AACA,MAAO;AAAA,QACN;AAAA,QACA,CAAC,UAAU,KAAK,MAAM,GAAG,KAAK,IAAI,QAAQ;AAAA,QAC1C,KAAK;AAAA,MACN;AAC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SAAS,cAAc,MAAM;AAAA,MACzC;AAAA,IACD;AAEA,UAAM,cAAc,MAAM,QAAQ,IAAI,UAAU,aAAa,SAAS,CAAC;AAEvE,UAAM,UAAU,MAAM;AACrB,aAAO,WAAW;AAClB,MAAS,oBAAW,KAAK,KAAK;AAAA,IAC/B;AACA,eAAW,UAAU;AAErB,WAAO;AAAA,MACN;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD;AACA,SAAO,OAAO,OAAO,YAAY,EAAE,KAAK,CAAC;AAC1C;AAEO,IAAM,eAAe,CAC3B,YACmC;AACnC,QAAM,SAAS,wBAAwB,OAAO;AAC9C,QAAM,SAAS,wBAAwB,SAAS,UAAU,OAAO,IAAI;AAErE,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA,UAAU,MAAM;AACf,aAAO,QAAQ;AACf,aAAO,QAAQ;AAAA,IAChB;AAAA,EACD;AACD;AAEO,IAAM,cAAc,CAC1B,YAC+C;AAC/C,QAAM,SAAS,wBAAwB,OAAO;AAC9C,QAAM,UAAU,gBAAgB,QAAQ,OAAO,EAAE;AAAA,IAChD,CAACA,UAAS,CAAC,MAAM,MAAM,MAAM;AAC5B,MAAAA,SAAQ,IAAI,IAAI;AAAA,QACf,iCAAK,UAAL,EAAc,OAAO;AAAA,QACrB;AAAA,QACA,OAAO;AAAA,MACR;AACA,aAAOA;AAAA,IACR;AAAA,IACA,CAAC;AAAA,EACF;AAEA,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA,UAAU,MAAM;AACf,iBAAW,CAAC,EAAE,MAAM,KAAK,gBAAgB,OAAO,GAAG;AAClD,eAAO,QAAQ;AAAA,MAChB;AACA,aAAO,QAAQ;AAAA,IAChB;AAAA,EACD;AACD","sourcesContent":["import * as http from \"http\"\n\nimport { type RenderResult, prettyDOM, render } from \"@testing-library/react\"\nimport * as AtomIO from \"atom.io\"\nimport * as Internal from \"atom.io/internal\"\nimport * as AR from \"atom.io/react\"\nimport * as RTR from \"atom.io/realtime-react\"\nimport * as RTS from \"atom.io/realtime-server\"\nimport * as Happy from \"happy-dom\"\nimport * as React from \"react\"\nimport * as SocketIO from \"socket.io\"\nimport type { Socket as ClientSocket } from \"socket.io-client\"\nimport { io } from \"socket.io-client\"\n\nimport { recordToEntries } from \"~/packages/anvl/src/object\"\n\nexport type TestSetupOptions = {\n\tserver: (tools: { socket: SocketIO.Socket; silo: AtomIO.Silo }) => void\n}\nexport type TestSetupOptions__SingleClient = TestSetupOptions & {\n\tclient: React.FC\n}\nexport type TestSetupOptions__MultiClient<ClientNames extends string> =\n\tTestSetupOptions & {\n\t\tclients: {\n\t\t\t[K in ClientNames]: React.FC\n\t\t}\n\t}\n\nexport type RealtimeTestTools = {\n\tname: string\n\tsilo: AtomIO.Silo\n}\nexport type RealtimeTestClient = RealtimeTestTools & {\n\trenderResult: RenderResult\n\tprettyPrint: () => void\n\tsocket: ClientSocket\n}\nexport type RealtimeTestClientBuilder = {\n\tdispose: () => void\n\tinit: () => RealtimeTestClient\n}\n\nexport type RealtimeTestServer = RealtimeTestTools & {\n\tdispose: () => void\n\tport: number\n}\n\nexport type RealtimeTestAPI = {\n\tserver: RealtimeTestServer\n\tteardown: () => void\n}\nexport type RealtimeTestAPI__SingleClient = RealtimeTestAPI & {\n\tclient: RealtimeTestClientBuilder\n}\nexport type RealtimeTestAPI__MultiClient<ClientNames extends string> =\n\tRealtimeTestAPI & {\n\t\tclients: Record<ClientNames, RealtimeTestClientBuilder>\n\t}\n\nexport const setupRealtimeTestServer = (\n\toptions: TestSetupOptions,\n): RealtimeTestServer => {\n\tconst silo = new AtomIO.Silo(`SERVER`, Internal.IMPLICIT.STORE)\n\n\tconst httpServer = http.createServer((_, res) => res.end(`Hello World!`))\n\tconst address = httpServer.listen().address()\n\tconst port =\n\t\ttypeof address === `string` ? 80 : address === null ? null : address.port\n\tif (port === null) throw new Error(`Could not determine port for test server`)\n\tconst server = new SocketIO.Server(httpServer).use((socket, next) => {\n\t\tconst { token, username } = socket.handshake.auth\n\t\tif (token === `test` && socket.id) {\n\t\t\tconst socketRelatedKeysState = Internal.findInStore(\n\t\t\t\tRTS.usersOfSockets.core.findRelatedKeysState,\n\t\t\t\tsocket.id,\n\t\t\t\tsilo.store,\n\t\t\t)\n\t\t\tconst clientRelatedKeysState = Internal.findInStore(\n\t\t\t\tRTS.usersOfSockets.core.findRelatedKeysState,\n\t\t\t\tusername,\n\t\t\t\tsilo.store,\n\t\t\t)\n\t\t\tAtomIO.setState(\n\t\t\t\tsocketRelatedKeysState,\n\t\t\t\t(keys) => (keys.clear(), keys.add(username)),\n\t\t\t\tsilo.store,\n\t\t\t)\n\t\t\tAtomIO.setState(\n\t\t\t\tclientRelatedKeysState,\n\t\t\t\t(keys) => (keys.clear(), keys.add(socket.id)),\n\t\t\t\tsilo.store,\n\t\t\t)\n\t\t\tconsole.log(`${username} connected on ${socket.id}`)\n\t\t\tnext()\n\t\t} else {\n\t\t\tnext(new Error(`Authentication error`))\n\t\t}\n\t})\n\n\tserver.on(`connection`, (socket: SocketIO.Socket) => {\n\t\toptions.server({ socket, silo })\n\t})\n\n\tconst dispose = () => {\n\t\tserver.close()\n\t\tInternal.clearStore(silo.store)\n\t}\n\n\treturn {\n\t\tname: `SERVER`,\n\t\tsilo,\n\t\tdispose,\n\t\tport,\n\t}\n}\nexport const setupRealtimeTestClient = (\n\toptions: TestSetupOptions__SingleClient,\n\tname: string,\n\tport: number,\n): RealtimeTestClientBuilder => {\n\tconst testClient = { dispose: () => {} }\n\tconst init = () => {\n\t\tconst socket: ClientSocket = io(`http://localhost:${port}/`, {\n\t\t\tauth: { token: `test`, username: name },\n\t\t})\n\t\tconst silo = new AtomIO.Silo(name, Internal.IMPLICIT.STORE)\n\t\tfor (const [key, value] of silo.store.valueMap.entries()) {\n\t\t\tif (Array.isArray(value)) {\n\t\t\t\tsilo.store.valueMap.set(key, [...value])\n\t\t\t}\n\t\t}\n\n\t\tconst { document } = new Happy.Window()\n\t\tdocument.body.innerHTML = `<div id=\"app\"></div>`\n\t\tconst renderResult = render(\n\t\t\t<AR.StoreProvider store={silo.store}>\n\t\t\t\t<RTR.RealtimeProvider socket={socket}>\n\t\t\t\t\t<options.client />\n\t\t\t\t</RTR.RealtimeProvider>\n\t\t\t</AR.StoreProvider>,\n\t\t\t{\n\t\t\t\tcontainer: document.querySelector(`#app`) as unknown as HTMLElement,\n\t\t\t},\n\t\t)\n\n\t\tconst prettyPrint = () => console.log(prettyDOM(renderResult.container))\n\n\t\tconst dispose = () => {\n\t\t\tsocket.disconnect()\n\t\t\tInternal.clearStore(silo.store)\n\t\t}\n\t\ttestClient.dispose = dispose\n\n\t\treturn {\n\t\t\tname,\n\t\t\tsilo,\n\t\t\tsocket,\n\t\t\trenderResult,\n\t\t\tprettyPrint,\n\t\t}\n\t}\n\treturn Object.assign(testClient, { init })\n}\n\nexport const singleClient = (\n\toptions: TestSetupOptions__SingleClient,\n): RealtimeTestAPI__SingleClient => {\n\tconst server = setupRealtimeTestServer(options)\n\tconst client = setupRealtimeTestClient(options, `CLIENT`, server.port)\n\n\treturn {\n\t\tclient,\n\t\tserver,\n\t\tteardown: () => {\n\t\t\tclient.dispose()\n\t\t\tserver.dispose()\n\t\t},\n\t}\n}\n\nexport const multiClient = <ClientNames extends string>(\n\toptions: TestSetupOptions__MultiClient<ClientNames>,\n): RealtimeTestAPI__MultiClient<ClientNames> => {\n\tconst server = setupRealtimeTestServer(options)\n\tconst clients = recordToEntries(options.clients).reduce(\n\t\t(clients, [name, client]) => {\n\t\t\tclients[name] = setupRealtimeTestClient(\n\t\t\t\t{ ...options, client },\n\t\t\t\tname,\n\t\t\t\tserver.port,\n\t\t\t)\n\t\t\treturn clients\n\t\t},\n\t\t{} as Record<ClientNames, RealtimeTestClientBuilder>,\n\t)\n\n\treturn {\n\t\tclients,\n\t\tserver,\n\t\tteardown: () => {\n\t\t\tfor (const [, client] of recordToEntries(clients)) {\n\t\t\t\tclient.dispose()\n\t\t\t}\n\t\t\tserver.dispose()\n\t\t},\n\t}\n}\n","export type Entries<K extends keyof any, V> = [key: K, value: V][]\n\nexport const recordToEntries = <K extends keyof any, V>(\n\tobj: Record<K, V>,\n): Entries<K, V> => Object.entries(obj) as Entries<K, V>\n\nexport const entriesToRecord = <K extends keyof any, V>(\n\tentries: Entries<K, V>,\n): Record<K, V> => Object.fromEntries(entries) as Record<K, V>\n"]}
@@ -2,6 +2,7 @@ import { RenderResult } from '@testing-library/react';
2
2
  import * as AtomIO from 'atom.io';
3
3
  import * as React from 'react';
4
4
  import * as SocketIO from 'socket.io';
5
+ import { Socket } from 'socket.io-client';
5
6
 
6
7
  type TestSetupOptions = {
7
8
  server: (tools: {
@@ -20,15 +21,18 @@ type TestSetupOptions__MultiClient<ClientNames extends string> = TestSetupOption
20
21
  type RealtimeTestTools = {
21
22
  name: string;
22
23
  silo: AtomIO.Silo;
23
- dispose: () => void;
24
24
  };
25
25
  type RealtimeTestClient = RealtimeTestTools & {
26
26
  renderResult: RenderResult;
27
27
  prettyPrint: () => void;
28
- reconnect: () => void;
29
- disconnect: () => void;
28
+ socket: Socket;
29
+ };
30
+ type RealtimeTestClientBuilder = {
31
+ dispose: () => void;
32
+ init: () => RealtimeTestClient;
30
33
  };
31
34
  type RealtimeTestServer = RealtimeTestTools & {
35
+ dispose: () => void;
32
36
  port: number;
33
37
  };
34
38
  type RealtimeTestAPI = {
@@ -36,14 +40,14 @@ type RealtimeTestAPI = {
36
40
  teardown: () => void;
37
41
  };
38
42
  type RealtimeTestAPI__SingleClient = RealtimeTestAPI & {
39
- client: RealtimeTestClient;
43
+ client: RealtimeTestClientBuilder;
40
44
  };
41
45
  type RealtimeTestAPI__MultiClient<ClientNames extends string> = RealtimeTestAPI & {
42
- clients: Record<ClientNames, RealtimeTestClient>;
46
+ clients: Record<ClientNames, RealtimeTestClientBuilder>;
43
47
  };
44
48
  declare const setupRealtimeTestServer: (options: TestSetupOptions) => RealtimeTestServer;
45
- declare const setupRealtimeTestClient: (options: TestSetupOptions__SingleClient, name: string, port: number) => RealtimeTestClient;
49
+ declare const setupRealtimeTestClient: (options: TestSetupOptions__SingleClient, name: string, port: number) => RealtimeTestClientBuilder;
46
50
  declare const singleClient: (options: TestSetupOptions__SingleClient) => RealtimeTestAPI__SingleClient;
47
51
  declare const multiClient: <ClientNames extends string>(options: TestSetupOptions__MultiClient<ClientNames>) => RealtimeTestAPI__MultiClient<ClientNames>;
48
52
 
49
- export { type RealtimeTestAPI, type RealtimeTestAPI__MultiClient, type RealtimeTestAPI__SingleClient, type RealtimeTestClient, type RealtimeTestServer, type RealtimeTestTools, type TestSetupOptions, type TestSetupOptions__MultiClient, type TestSetupOptions__SingleClient, multiClient, setupRealtimeTestClient, setupRealtimeTestServer, singleClient };
53
+ export { type RealtimeTestAPI, type RealtimeTestAPI__MultiClient, type RealtimeTestAPI__SingleClient, type RealtimeTestClient, type RealtimeTestClientBuilder, type RealtimeTestServer, type RealtimeTestTools, type TestSetupOptions, type TestSetupOptions__MultiClient, type TestSetupOptions__SingleClient, multiClient, setupRealtimeTestClient, setupRealtimeTestServer, singleClient };
@@ -6,19 +6,48 @@ import * as AtomIO from 'atom.io';
6
6
  import * as Internal from 'atom.io/internal';
7
7
  import * as AR from 'atom.io/react';
8
8
  import * as RTR from 'atom.io/realtime-react';
9
+ import * as RTS from 'atom.io/realtime-server';
9
10
  import * as Happy from 'happy-dom';
10
11
  import * as SocketIO from 'socket.io';
11
12
  import { io } from 'socket.io-client';
12
13
  import { jsx } from 'react/jsx-runtime';
13
14
 
14
15
  var setupRealtimeTestServer = (options) => {
16
+ const silo = new AtomIO.Silo(`SERVER`, Internal.IMPLICIT.STORE);
15
17
  const httpServer = http.createServer((_, res) => res.end(`Hello World!`));
16
18
  const address = httpServer.listen().address();
17
19
  const port = typeof address === `string` ? 80 : address === null ? null : address.port;
18
20
  if (port === null)
19
21
  throw new Error(`Could not determine port for test server`);
20
- const server = new SocketIO.Server(httpServer);
21
- const silo = new AtomIO.Silo(`SERVER`, Internal.IMPLICIT.STORE);
22
+ const server = new SocketIO.Server(httpServer).use((socket, next) => {
23
+ const { token, username } = socket.handshake.auth;
24
+ if (token === `test` && socket.id) {
25
+ const socketRelatedKeysState = Internal.findInStore(
26
+ RTS.usersOfSockets.core.findRelatedKeysState,
27
+ socket.id,
28
+ silo.store
29
+ );
30
+ const clientRelatedKeysState = Internal.findInStore(
31
+ RTS.usersOfSockets.core.findRelatedKeysState,
32
+ username,
33
+ silo.store
34
+ );
35
+ AtomIO.setState(
36
+ socketRelatedKeysState,
37
+ (keys) => (keys.clear(), keys.add(username)),
38
+ silo.store
39
+ );
40
+ AtomIO.setState(
41
+ clientRelatedKeysState,
42
+ (keys) => (keys.clear(), keys.add(socket.id)),
43
+ silo.store
44
+ );
45
+ console.log(`${username} connected on ${socket.id}`);
46
+ next();
47
+ } else {
48
+ next(new Error(`Authentication error`));
49
+ }
50
+ });
22
51
  server.on(`connection`, (socket) => {
23
52
  options.server({ socket, silo });
24
53
  });
@@ -34,32 +63,41 @@ var setupRealtimeTestServer = (options) => {
34
63
  };
35
64
  };
36
65
  var setupRealtimeTestClient = (options, name, port) => {
37
- const socket = io(`http://localhost:${port}/`);
38
- const silo = new AtomIO.Silo(name, Internal.IMPLICIT.STORE);
39
- const { document } = new Happy.Window();
40
- document.body.innerHTML = `<div id="app"></div>`;
41
- const renderResult = render(
42
- /* @__PURE__ */ jsx(AR.StoreProvider, { store: silo.store, children: /* @__PURE__ */ jsx(RTR.RealtimeProvider, { socket, children: /* @__PURE__ */ jsx(options.client, {}) }) }),
43
- {
44
- container: document.querySelector(`#app`)
66
+ const testClient = { dispose: () => {
67
+ } };
68
+ const init = () => {
69
+ const socket = io(`http://localhost:${port}/`, {
70
+ auth: { token: `test`, username: name }
71
+ });
72
+ const silo = new AtomIO.Silo(name, Internal.IMPLICIT.STORE);
73
+ for (const [key, value] of silo.store.valueMap.entries()) {
74
+ if (Array.isArray(value)) {
75
+ silo.store.valueMap.set(key, [...value]);
76
+ }
45
77
  }
46
- );
47
- const prettyPrint = () => console.log(prettyDOM(renderResult.container));
48
- const disconnect = () => socket.disconnect();
49
- const reconnect = () => socket.connect();
50
- const dispose = () => {
51
- socket.disconnect();
52
- Internal.clearStore(silo.store);
53
- };
54
- return {
55
- name,
56
- silo,
57
- renderResult,
58
- prettyPrint,
59
- disconnect,
60
- reconnect,
61
- dispose
78
+ const { document } = new Happy.Window();
79
+ document.body.innerHTML = `<div id="app"></div>`;
80
+ const renderResult = render(
81
+ /* @__PURE__ */ jsx(AR.StoreProvider, { store: silo.store, children: /* @__PURE__ */ jsx(RTR.RealtimeProvider, { socket, children: /* @__PURE__ */ jsx(options.client, {}) }) }),
82
+ {
83
+ container: document.querySelector(`#app`)
84
+ }
85
+ );
86
+ const prettyPrint = () => console.log(prettyDOM(renderResult.container));
87
+ const dispose = () => {
88
+ socket.disconnect();
89
+ Internal.clearStore(silo.store);
90
+ };
91
+ testClient.dispose = dispose;
92
+ return {
93
+ name,
94
+ silo,
95
+ socket,
96
+ renderResult,
97
+ prettyPrint
98
+ };
62
99
  };
100
+ return Object.assign(testClient, { init });
63
101
  };
64
102
  var singleClient = (options) => {
65
103
  const server = setupRealtimeTestServer(options);
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/setup-realtime-test.tsx"],"names":["clients"],"mappings":";;;;;;;;;AAAA,YAAY,UAAU;AAEtB,SAA4B,WAAW,cAAc;AACrD,YAAY,YAAY;AACxB,YAAY,cAAc;AAC1B,YAAY,QAAQ;AACpB,YAAY,SAAS;AACrB,YAAY,WAAW;AAEvB,YAAY,cAAc;AAE1B,SAAS,UAAU;AAqFf;AAzCG,IAAM,0BAA0B,CACtC,YACwB;AACxB,QAAM,aAAkB,kBAAa,CAAC,GAAG,QAAQ,IAAI,IAAI,cAAc,CAAC;AACxE,QAAM,UAAU,WAAW,OAAO,EAAE,QAAQ;AAC5C,QAAM,OACL,OAAO,YAAY,WAAW,KAAK,YAAY,OAAO,OAAO,QAAQ;AACt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AAS,UAAU,OAAO,IAAI;AAErE,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA,UAAU,MAAM;AACf,aAAO,QAAQ;AACf,aAAO,QAAQ;AAAA,IAChB;AAAA,EACD;AACD;AAEO,IAAM,cAAc,CAC1B,YAC+C;AAC/C,QAAM,SAAS,wBAAwB,OAAO;AAC9C,QAAM,UAAU,gBAAgB,QAAQ,OAAO,EAAE;AAAA,IAChD,CAACA,UAAS,CAAC,MAAM,MAAM,MAAM;AAC5B,MAAAA,SAAQ,IAAI,IAAI;AAAA,QACf,iCAAK,UAAL,EAAc,OAAO;AAAA,QACrB;AAAA,QACA,OAAO;AAAA,MACR;AACA,aAAOA;AAAA,IACR;AAAA,IACA,CAAC;AAAA,EACF;AAEA,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA,UAAU,MAAM;AACf,iBAAW,CAAC,EAAE,MAAM,KAAK,gBAAgB,OAAO,GAAG;AAClD,eAAO,QAAQ;AAAA,MAChB;AACA,aAAO,QAAQ;AAAA,IAChB;AAAA,EACD;AACD","sourcesContent":["import * as http from \"http\"\n\nimport { type RenderResult, prettyDOM, render } from \"@testing-library/react\"\nimport * as AtomIO from \"atom.io\"\nimport * as Internal from \"atom.io/internal\"\nimport * as AR from \"atom.io/react\"\nimport * as RTR from \"atom.io/realtime-react\"\nimport * as Happy from \"happy-dom\"\nimport * as React from \"react\"\nimport * as SocketIO from \"socket.io\"\nimport type { Socket as ClientSocket } from \"socket.io-client\"\nimport { io } from \"socket.io-client\"\n\nimport { recordToEntries } from \"~/packages/anvl/src/object\"\n\nexport type TestSetupOptions = {\n\tserver: (tools: { socket: SocketIO.Socket; silo: AtomIO.Silo }) => void\n}\nexport type TestSetupOptions__SingleClient = TestSetupOptions & {\n\tclient: React.FC\n}\nexport type TestSetupOptions__MultiClient<ClientNames extends string> =\n\tTestSetupOptions & {\n\t\tclients: {\n\t\t\t[K in ClientNames]: React.FC\n\t\t}\n\t}\n\nexport type RealtimeTestTools = {\n\tname: string\n\tsilo: AtomIO.Silo\n\tdispose: () => void\n}\nexport type RealtimeTestClient = RealtimeTestTools & {\n\trenderResult: RenderResult\n\tprettyPrint: () => void\n\treconnect: () => void\n\tdisconnect: () => void\n}\nexport type RealtimeTestServer = RealtimeTestTools & {\n\tport: number\n}\n\nexport type RealtimeTestAPI = {\n\tserver: RealtimeTestServer\n\tteardown: () => void\n}\nexport type RealtimeTestAPI__SingleClient = RealtimeTestAPI & {\n\tclient: RealtimeTestClient\n}\nexport type RealtimeTestAPI__MultiClient<ClientNames extends string> =\n\tRealtimeTestAPI & {\n\t\tclients: Record<ClientNames, RealtimeTestClient>\n\t}\n\nexport const setupRealtimeTestServer = (\n\toptions: TestSetupOptions,\n): RealtimeTestServer => {\n\tconst httpServer = http.createServer((_, res) => res.end(`Hello World!`))\n\tconst address = httpServer.listen().address()\n\tconst port =\n\t\ttypeof address === `string` ? 80 : address === null ? null : address.port\n\tif (port === null) throw new Error(`Could not determine port for test server`)\n\tconst server = new SocketIO.Server(httpServer)\n\n\tconst silo = new AtomIO.Silo(`SERVER`, Internal.IMPLICIT.STORE)\n\n\tserver.on(`connection`, (socket: SocketIO.Socket) => {\n\t\toptions.server({ socket, silo })\n\t})\n\n\tconst dispose = () => {\n\t\tserver.close()\n\t\tInternal.clearStore(silo.store)\n\t}\n\n\treturn {\n\t\tname: `SERVER`,\n\t\tsilo,\n\t\tdispose,\n\t\tport,\n\t}\n}\nexport const setupRealtimeTestClient = (\n\toptions: TestSetupOptions__SingleClient,\n\tname: string,\n\tport: number,\n): RealtimeTestClient => {\n\tconst socket: ClientSocket = io(`http://localhost:${port}/`)\n\tconst silo = new AtomIO.Silo(name, Internal.IMPLICIT.STORE)\n\n\tconst { document } = new Happy.Window()\n\tdocument.body.innerHTML = `<div id=\"app\"></div>`\n\tconst renderResult = render(\n\t\t<AR.StoreProvider store={silo.store}>\n\t\t\t<RTR.RealtimeProvider socket={socket}>\n\t\t\t\t<options.client />\n\t\t\t</RTR.RealtimeProvider>\n\t\t</AR.StoreProvider>,\n\t\t{\n\t\t\tcontainer: document.querySelector(`#app`) as unknown as HTMLElement,\n\t\t},\n\t)\n\n\tconst prettyPrint = () => console.log(prettyDOM(renderResult.container))\n\n\tconst disconnect = () => socket.disconnect()\n\tconst reconnect = () => socket.connect()\n\n\tconst dispose = () => {\n\t\tsocket.disconnect()\n\t\tInternal.clearStore(silo.store)\n\t}\n\n\treturn {\n\t\tname,\n\t\tsilo,\n\t\trenderResult,\n\t\tprettyPrint,\n\t\tdisconnect,\n\t\treconnect,\n\t\tdispose,\n\t}\n}\n\nexport const singleClient = (\n\toptions: TestSetupOptions__SingleClient,\n): RealtimeTestAPI__SingleClient => {\n\tconst server = setupRealtimeTestServer(options)\n\tconst client = setupRealtimeTestClient(options, `CLIENT`, server.port)\n\n\treturn {\n\t\tclient,\n\t\tserver,\n\t\tteardown: () => {\n\t\t\tclient.dispose()\n\t\t\tserver.dispose()\n\t\t},\n\t}\n}\n\nexport const multiClient = <ClientNames extends string>(\n\toptions: TestSetupOptions__MultiClient<ClientNames>,\n): RealtimeTestAPI__MultiClient<ClientNames> => {\n\tconst server = setupRealtimeTestServer(options)\n\tconst clients = recordToEntries(options.clients).reduce(\n\t\t(clients, [name, client]) => {\n\t\t\tclients[name] = setupRealtimeTestClient(\n\t\t\t\t{ ...options, client },\n\t\t\t\tname,\n\t\t\t\tserver.port,\n\t\t\t)\n\t\t\treturn clients\n\t\t},\n\t\t{} as Record<ClientNames, RealtimeTestClient>,\n\t)\n\n\treturn {\n\t\tclients,\n\t\tserver,\n\t\tteardown: () => {\n\t\t\tfor (const [, client] of recordToEntries(clients)) {\n\t\t\t\tclient.dispose()\n\t\t\t}\n\t\t\tserver.dispose()\n\t\t},\n\t}\n}\n"]}
1
+ {"version":3,"sources":["../src/setup-realtime-test.tsx"],"names":["clients"],"mappings":";;;;;;;;;AAAA,YAAY,UAAU;AAEtB,SAA4B,WAAW,cAAc;AACrD,YAAY,YAAY;AACxB,YAAY,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AC1C;AAEO,IAAM,eAAe,CAC3B,YACmC;AACnC,QAAM,SAAS,wBAAwB,OAAO;AAC9C,QAAM,SAAS,wBAAwB,SAAS,UAAU,OAAO,IAAI;AAErE,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA,UAAU,MAAM;AACf,aAAO,QAAQ;AACf,aAAO,QAAQ;AAAA,IAChB;AAAA,EACD;AACD;AAEO,IAAM,cAAc,CAC1B,YAC+C;AAC/C,QAAM,SAAS,wBAAwB,OAAO;AAC9C,QAAM,UAAU,gBAAgB,QAAQ,OAAO,EAAE;AAAA,IAChD,CAACA,UAAS,CAAC,MAAM,MAAM,MAAM;AAC5B,MAAAA,SAAQ,IAAI,IAAI;AAAA,QACf,iCAAK,UAAL,EAAc,OAAO;AAAA,QACrB;AAAA,QACA,OAAO;AAAA,MACR;AACA,aAAOA;AAAA,IACR;AAAA,IACA,CAAC;AAAA,EACF;AAEA,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA,UAAU,MAAM;AACf,iBAAW,CAAC,EAAE,MAAM,KAAK,gBAAgB,OAAO,GAAG;AAClD,eAAO,QAAQ;AAAA,MAChB;AACA,aAAO,QAAQ;AAAA,IAChB;AAAA,EACD;AACD","sourcesContent":["import * as http from \"http\"\n\nimport { type RenderResult, prettyDOM, render } from \"@testing-library/react\"\nimport * as AtomIO from \"atom.io\"\nimport * as Internal from \"atom.io/internal\"\nimport * as AR from \"atom.io/react\"\nimport * as RTR from \"atom.io/realtime-react\"\nimport * as RTS from \"atom.io/realtime-server\"\nimport * as Happy from \"happy-dom\"\nimport * as React from \"react\"\nimport * as SocketIO from \"socket.io\"\nimport type { Socket as ClientSocket } from \"socket.io-client\"\nimport { io } from \"socket.io-client\"\n\nimport { recordToEntries } from \"~/packages/anvl/src/object\"\n\nexport type TestSetupOptions = {\n\tserver: (tools: { socket: SocketIO.Socket; silo: AtomIO.Silo }) => void\n}\nexport type TestSetupOptions__SingleClient = TestSetupOptions & {\n\tclient: React.FC\n}\nexport type TestSetupOptions__MultiClient<ClientNames extends string> =\n\tTestSetupOptions & {\n\t\tclients: {\n\t\t\t[K in ClientNames]: React.FC\n\t\t}\n\t}\n\nexport type RealtimeTestTools = {\n\tname: string\n\tsilo: AtomIO.Silo\n}\nexport type RealtimeTestClient = RealtimeTestTools & {\n\trenderResult: RenderResult\n\tprettyPrint: () => void\n\tsocket: ClientSocket\n}\nexport type RealtimeTestClientBuilder = {\n\tdispose: () => void\n\tinit: () => RealtimeTestClient\n}\n\nexport type RealtimeTestServer = RealtimeTestTools & {\n\tdispose: () => void\n\tport: number\n}\n\nexport type RealtimeTestAPI = {\n\tserver: RealtimeTestServer\n\tteardown: () => void\n}\nexport type RealtimeTestAPI__SingleClient = RealtimeTestAPI & {\n\tclient: RealtimeTestClientBuilder\n}\nexport type RealtimeTestAPI__MultiClient<ClientNames extends string> =\n\tRealtimeTestAPI & {\n\t\tclients: Record<ClientNames, RealtimeTestClientBuilder>\n\t}\n\nexport const setupRealtimeTestServer = (\n\toptions: TestSetupOptions,\n): RealtimeTestServer => {\n\tconst silo = new AtomIO.Silo(`SERVER`, Internal.IMPLICIT.STORE)\n\n\tconst httpServer = http.createServer((_, res) => res.end(`Hello World!`))\n\tconst address = httpServer.listen().address()\n\tconst port =\n\t\ttypeof address === `string` ? 80 : address === null ? null : address.port\n\tif (port === null) throw new Error(`Could not determine port for test server`)\n\tconst server = new SocketIO.Server(httpServer).use((socket, next) => {\n\t\tconst { token, username } = socket.handshake.auth\n\t\tif (token === `test` && socket.id) {\n\t\t\tconst socketRelatedKeysState = Internal.findInStore(\n\t\t\t\tRTS.usersOfSockets.core.findRelatedKeysState,\n\t\t\t\tsocket.id,\n\t\t\t\tsilo.store,\n\t\t\t)\n\t\t\tconst clientRelatedKeysState = Internal.findInStore(\n\t\t\t\tRTS.usersOfSockets.core.findRelatedKeysState,\n\t\t\t\tusername,\n\t\t\t\tsilo.store,\n\t\t\t)\n\t\t\tAtomIO.setState(\n\t\t\t\tsocketRelatedKeysState,\n\t\t\t\t(keys) => (keys.clear(), keys.add(username)),\n\t\t\t\tsilo.store,\n\t\t\t)\n\t\t\tAtomIO.setState(\n\t\t\t\tclientRelatedKeysState,\n\t\t\t\t(keys) => (keys.clear(), keys.add(socket.id)),\n\t\t\t\tsilo.store,\n\t\t\t)\n\t\t\tconsole.log(`${username} connected on ${socket.id}`)\n\t\t\tnext()\n\t\t} else {\n\t\t\tnext(new Error(`Authentication error`))\n\t\t}\n\t})\n\n\tserver.on(`connection`, (socket: SocketIO.Socket) => {\n\t\toptions.server({ socket, silo })\n\t})\n\n\tconst dispose = () => {\n\t\tserver.close()\n\t\tInternal.clearStore(silo.store)\n\t}\n\n\treturn {\n\t\tname: `SERVER`,\n\t\tsilo,\n\t\tdispose,\n\t\tport,\n\t}\n}\nexport const setupRealtimeTestClient = (\n\toptions: TestSetupOptions__SingleClient,\n\tname: string,\n\tport: number,\n): RealtimeTestClientBuilder => {\n\tconst testClient = { dispose: () => {} }\n\tconst init = () => {\n\t\tconst socket: ClientSocket = io(`http://localhost:${port}/`, {\n\t\t\tauth: { token: `test`, username: name },\n\t\t})\n\t\tconst silo = new AtomIO.Silo(name, Internal.IMPLICIT.STORE)\n\t\tfor (const [key, value] of silo.store.valueMap.entries()) {\n\t\t\tif (Array.isArray(value)) {\n\t\t\t\tsilo.store.valueMap.set(key, [...value])\n\t\t\t}\n\t\t}\n\n\t\tconst { document } = new Happy.Window()\n\t\tdocument.body.innerHTML = `<div id=\"app\"></div>`\n\t\tconst renderResult = render(\n\t\t\t<AR.StoreProvider store={silo.store}>\n\t\t\t\t<RTR.RealtimeProvider socket={socket}>\n\t\t\t\t\t<options.client />\n\t\t\t\t</RTR.RealtimeProvider>\n\t\t\t</AR.StoreProvider>,\n\t\t\t{\n\t\t\t\tcontainer: document.querySelector(`#app`) as unknown as HTMLElement,\n\t\t\t},\n\t\t)\n\n\t\tconst prettyPrint = () => console.log(prettyDOM(renderResult.container))\n\n\t\tconst dispose = () => {\n\t\t\tsocket.disconnect()\n\t\t\tInternal.clearStore(silo.store)\n\t\t}\n\t\ttestClient.dispose = dispose\n\n\t\treturn {\n\t\t\tname,\n\t\t\tsilo,\n\t\t\tsocket,\n\t\t\trenderResult,\n\t\t\tprettyPrint,\n\t\t}\n\t}\n\treturn Object.assign(testClient, { init })\n}\n\nexport const singleClient = (\n\toptions: TestSetupOptions__SingleClient,\n): RealtimeTestAPI__SingleClient => {\n\tconst server = setupRealtimeTestServer(options)\n\tconst client = setupRealtimeTestClient(options, `CLIENT`, server.port)\n\n\treturn {\n\t\tclient,\n\t\tserver,\n\t\tteardown: () => {\n\t\t\tclient.dispose()\n\t\t\tserver.dispose()\n\t\t},\n\t}\n}\n\nexport const multiClient = <ClientNames extends string>(\n\toptions: TestSetupOptions__MultiClient<ClientNames>,\n): RealtimeTestAPI__MultiClient<ClientNames> => {\n\tconst server = setupRealtimeTestServer(options)\n\tconst clients = recordToEntries(options.clients).reduce(\n\t\t(clients, [name, client]) => {\n\t\t\tclients[name] = setupRealtimeTestClient(\n\t\t\t\t{ ...options, client },\n\t\t\t\tname,\n\t\t\t\tserver.port,\n\t\t\t)\n\t\t\treturn clients\n\t\t},\n\t\t{} as Record<ClientNames, RealtimeTestClientBuilder>,\n\t)\n\n\treturn {\n\t\tclients,\n\t\tserver,\n\t\tteardown: () => {\n\t\t\tfor (const [, client] of recordToEntries(clients)) {\n\t\t\t\tclient.dispose()\n\t\t\t}\n\t\t\tserver.dispose()\n\t\t},\n\t}\n}\n"]}
@@ -5,6 +5,7 @@ import * as AtomIO from "atom.io"
5
5
  import * as Internal from "atom.io/internal"
6
6
  import * as AR from "atom.io/react"
7
7
  import * as RTR from "atom.io/realtime-react"
8
+ import * as RTS from "atom.io/realtime-server"
8
9
  import * as Happy from "happy-dom"
9
10
  import * as React from "react"
10
11
  import * as SocketIO from "socket.io"
@@ -29,15 +30,19 @@ export type TestSetupOptions__MultiClient<ClientNames extends string> =
29
30
  export type RealtimeTestTools = {
30
31
  name: string
31
32
  silo: AtomIO.Silo
32
- dispose: () => void
33
33
  }
34
34
  export type RealtimeTestClient = RealtimeTestTools & {
35
35
  renderResult: RenderResult
36
36
  prettyPrint: () => void
37
- reconnect: () => void
38
- disconnect: () => void
37
+ socket: ClientSocket
38
+ }
39
+ export type RealtimeTestClientBuilder = {
40
+ dispose: () => void
41
+ init: () => RealtimeTestClient
39
42
  }
43
+
40
44
  export type RealtimeTestServer = RealtimeTestTools & {
45
+ dispose: () => void
41
46
  port: number
42
47
  }
43
48
 
@@ -46,24 +51,52 @@ export type RealtimeTestAPI = {
46
51
  teardown: () => void
47
52
  }
48
53
  export type RealtimeTestAPI__SingleClient = RealtimeTestAPI & {
49
- client: RealtimeTestClient
54
+ client: RealtimeTestClientBuilder
50
55
  }
51
56
  export type RealtimeTestAPI__MultiClient<ClientNames extends string> =
52
57
  RealtimeTestAPI & {
53
- clients: Record<ClientNames, RealtimeTestClient>
58
+ clients: Record<ClientNames, RealtimeTestClientBuilder>
54
59
  }
55
60
 
56
61
  export const setupRealtimeTestServer = (
57
62
  options: TestSetupOptions,
58
63
  ): RealtimeTestServer => {
64
+ const silo = new AtomIO.Silo(`SERVER`, Internal.IMPLICIT.STORE)
65
+
59
66
  const httpServer = http.createServer((_, res) => res.end(`Hello World!`))
60
67
  const address = httpServer.listen().address()
61
68
  const port =
62
69
  typeof address === `string` ? 80 : address === null ? null : address.port
63
70
  if (port === null) throw new Error(`Could not determine port for test server`)
64
- const server = new SocketIO.Server(httpServer)
65
-
66
- const silo = new AtomIO.Silo(`SERVER`, Internal.IMPLICIT.STORE)
71
+ const server = new SocketIO.Server(httpServer).use((socket, next) => {
72
+ const { token, username } = socket.handshake.auth
73
+ if (token === `test` && socket.id) {
74
+ const socketRelatedKeysState = Internal.findInStore(
75
+ RTS.usersOfSockets.core.findRelatedKeysState,
76
+ socket.id,
77
+ silo.store,
78
+ )
79
+ const clientRelatedKeysState = Internal.findInStore(
80
+ RTS.usersOfSockets.core.findRelatedKeysState,
81
+ username,
82
+ silo.store,
83
+ )
84
+ AtomIO.setState(
85
+ socketRelatedKeysState,
86
+ (keys) => (keys.clear(), keys.add(username)),
87
+ silo.store,
88
+ )
89
+ AtomIO.setState(
90
+ clientRelatedKeysState,
91
+ (keys) => (keys.clear(), keys.add(socket.id)),
92
+ silo.store,
93
+ )
94
+ console.log(`${username} connected on ${socket.id}`)
95
+ next()
96
+ } else {
97
+ next(new Error(`Authentication error`))
98
+ }
99
+ })
67
100
 
68
101
  server.on(`connection`, (socket: SocketIO.Socket) => {
69
102
  options.server({ socket, silo })
@@ -85,42 +118,49 @@ export const setupRealtimeTestClient = (
85
118
  options: TestSetupOptions__SingleClient,
86
119
  name: string,
87
120
  port: number,
88
- ): RealtimeTestClient => {
89
- const socket: ClientSocket = io(`http://localhost:${port}/`)
90
- const silo = new AtomIO.Silo(name, Internal.IMPLICIT.STORE)
91
-
92
- const { document } = new Happy.Window()
93
- document.body.innerHTML = `<div id="app"></div>`
94
- const renderResult = render(
95
- <AR.StoreProvider store={silo.store}>
96
- <RTR.RealtimeProvider socket={socket}>
97
- <options.client />
98
- </RTR.RealtimeProvider>
99
- </AR.StoreProvider>,
100
- {
101
- container: document.querySelector(`#app`) as unknown as HTMLElement,
102
- },
103
- )
104
-
105
- const prettyPrint = () => console.log(prettyDOM(renderResult.container))
106
-
107
- const disconnect = () => socket.disconnect()
108
- const reconnect = () => socket.connect()
109
-
110
- const dispose = () => {
111
- socket.disconnect()
112
- Internal.clearStore(silo.store)
113
- }
121
+ ): RealtimeTestClientBuilder => {
122
+ const testClient = { dispose: () => {} }
123
+ const init = () => {
124
+ const socket: ClientSocket = io(`http://localhost:${port}/`, {
125
+ auth: { token: `test`, username: name },
126
+ })
127
+ const silo = new AtomIO.Silo(name, Internal.IMPLICIT.STORE)
128
+ for (const [key, value] of silo.store.valueMap.entries()) {
129
+ if (Array.isArray(value)) {
130
+ silo.store.valueMap.set(key, [...value])
131
+ }
132
+ }
114
133
 
115
- return {
116
- name,
117
- silo,
118
- renderResult,
119
- prettyPrint,
120
- disconnect,
121
- reconnect,
122
- dispose,
134
+ const { document } = new Happy.Window()
135
+ document.body.innerHTML = `<div id="app"></div>`
136
+ const renderResult = render(
137
+ <AR.StoreProvider store={silo.store}>
138
+ <RTR.RealtimeProvider socket={socket}>
139
+ <options.client />
140
+ </RTR.RealtimeProvider>
141
+ </AR.StoreProvider>,
142
+ {
143
+ container: document.querySelector(`#app`) as unknown as HTMLElement,
144
+ },
145
+ )
146
+
147
+ const prettyPrint = () => console.log(prettyDOM(renderResult.container))
148
+
149
+ const dispose = () => {
150
+ socket.disconnect()
151
+ Internal.clearStore(silo.store)
152
+ }
153
+ testClient.dispose = dispose
154
+
155
+ return {
156
+ name,
157
+ silo,
158
+ socket,
159
+ renderResult,
160
+ prettyPrint,
161
+ }
123
162
  }
163
+ return Object.assign(testClient, { init })
124
164
  }
125
165
 
126
166
  export const singleClient = (
@@ -152,7 +192,7 @@ export const multiClient = <ClientNames extends string>(
152
192
  )
153
193
  return clients
154
194
  },
155
- {} as Record<ClientNames, RealtimeTestClient>,
195
+ {} as Record<ClientNames, RealtimeTestClientBuilder>,
156
196
  )
157
197
 
158
198
  return {