@simpleplatform/sdk 3.0.0 → 3.0.2

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/README.md CHANGED
@@ -25,6 +25,10 @@ One of those changes renamed how a Space connects, which is why this is a major.
25
25
  | `ConnectSpaceOptions` | `ConnectOptions` |
26
26
  | `SpaceWindowLike`, `SpaceMessageEvent` | No longer exported. A test declares its own. |
27
27
 
28
+ **A test that builds the client by hand** adds what the client gained, or it
29
+ stops type checking: `navigation`, `ui`, and `open` beside `current` under
30
+ `records` (for example `records: { current: vi.fn(), open: vi.fn() }`).
31
+
28
32
  **Two behaviours change with no change in your code:**
29
33
 
30
34
  - `connect()` sets `overscroll-behavior-y: none` on the Space's root element,
@@ -354,6 +358,9 @@ while `simple.records.current()` rejects with `SpaceProtocolError` code
354
358
  In an older host, `records.current()` can remain available through the `record`
355
359
  capability while `records.open()` requires the newer `records` capability.
356
360
 
361
+ A Space has one connection per page. The first `connect()` opens it, and every
362
+ later call returns the same client. A call with a different `targetOrigin` is rejected.
363
+
357
364
  When `connect()` runs in the Space document, it disables vertical root
358
365
  overscroll bounce. Normal scrolling inside the Space remains enabled.
359
366
 
@@ -2,6 +2,7 @@ import { createMessagePortTransport } from './browser-transport.js';
2
2
  import { createSimpleClient, HEADER_ACTIONS_PROTOCOL_VERSION, HEADER_STATUS_PROTOCOL_VERSION, isSpaceContext, MANAGED_RECORD_FORM_PROTOCOL_VERSION, PROTOCOL_VERSION, RECORDS_PROTOCOL_VERSION, SpaceDataError, SpaceProtocolError, TABS_PROTOCOL_VERSION, TOAST_PROTOCOL_VERSION, } from './core.js';
3
3
  import { isObjectRecord } from './protocol.js';
4
4
  export { HEADER_ACTIONS_PROTOCOL_VERSION, HEADER_STATUS_PROTOCOL_VERSION, PROTOCOL_VERSION, RECORDS_PROTOCOL_VERSION, SpaceDataError, SpaceProtocolError, TABS_PROTOCOL_VERSION, TOAST_PROTOCOL_VERSION };
5
+ const connections = new WeakMap();
5
6
  /** Connects this Space to its host using the explicitly supplied host origin. */
6
7
  export function connect({ targetOrigin }) {
7
8
  if (!isOrigin(targetOrigin)) {
@@ -17,9 +18,18 @@ export function connect({ targetOrigin }) {
17
18
  message: 'The Space SDK requires a browser window.',
18
19
  }));
19
20
  }
21
+ const existing = connections.get(window);
22
+ if (existing) {
23
+ if (existing.targetOrigin === targetOrigin)
24
+ return existing.promise;
25
+ return Promise.reject(new SpaceProtocolError({
26
+ code: 'invalid_request',
27
+ message: 'connect() already opened this Space\'s connection to a different targetOrigin.',
28
+ }));
29
+ }
20
30
  // The host cannot style a cross-origin frame's viewport, where edge bounce occurs.
21
31
  window.document.documentElement.style.overscrollBehaviorY = 'none';
22
- return new Promise((resolve, reject) => {
32
+ const promise = new Promise((resolve, reject) => {
23
33
  const onMessage = (event) => {
24
34
  if (event.origin !== targetOrigin || !isInitializationMessage(event.data))
25
35
  return;
@@ -78,7 +88,12 @@ export function connect({ targetOrigin }) {
78
88
  },
79
89
  type: 'SPACE_READY',
80
90
  }, targetOrigin);
91
+ }).catch((error) => {
92
+ connections.delete(window);
93
+ throw error;
81
94
  });
95
+ connections.set(window, { promise, targetOrigin });
96
+ return promise;
82
97
  }
83
98
  function isOrigin(targetOrigin) {
84
99
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simpleplatform/sdk",
3
- "version": "3.0.0",
3
+ "version": "3.0.2",
4
4
  "description": "Simple Platform Typescript SDK",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://docs.simple.dev",