Skip to content

Commit

Permalink
fix: avoid using localstorage polyfill to support SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
eliias committed Feb 3, 2023
1 parent 2872f3f commit 5aeb5c0
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 43 deletions.
11 changes: 11 additions & 0 deletions docs/release.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Release

## Release npm package

```bash
yarn changeset
yarn changeset version

git add -A . && git commit
git push
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"private": true,
"devDependencies": {
"turbo": "^1.6.3"
"turbo": "^1.7.2"
},
"scripts": {
"release": "turbo run build lint test && changeset version && changeset publish"
Expand Down
6 changes: 6 additions & 0 deletions packages/yrb-actioncable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# yrb-actioncable

## 0.1.4

### Patch Changes

- cde080e: Fix an issue with SSR and the lib0/broadcastchannel implementation of the LocalStorage polyfill.

## 0.1.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/yrb-actioncable/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.3",
"version": "0.1.4",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
64 changes: 64 additions & 0 deletions packages/yrb-actioncable/src/broadcastchannel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as map from 'lib0/map';

/**
* Helpers for cross-tab communication using broadcastchannel with LocalStorage
* fallback. This is a copy of
* https://github.com/dmonad/lib0/blob/main/broadcastchannel.js that does not
* fall back to LocalStore to prevent SSR issues.
*
* ```js
* // In browser window A:
* broadcastchannel.subscribe('my events', data => console.log(data))
* broadcastchannel.publish('my events', 'Hello world!') // => A: 'Hello world!' fires synchronously in same tab
*
* // In browser window B:
* broadcastchannel.publish('my events', 'hello from tab B') // => A: 'hello from tab B'
* ```
*/

const channels = new Map();

type Sub = (e: MessageEvent, origin: any) => void;

const getChannel = (room: string) =>
map.setIfUndefined(channels, room, () => {
const subs = new Set<Sub>();
const bc = new BroadcastChannel(room);

bc.onmessage = e => {
subs.forEach((sub: Sub) => {
sub(e.data, 'broadcastchannel');
});
};

return {
bc,
subs,
};
});

export const subscribe = (
room: string,
f: (data: any, origin: any) => void
) => {
getChannel(room).subs.add(f);

return f;
};

export const unsubscribe = (room: string, f: any) => {
const channel = getChannel(room);
const unsubscribed = channel.subs.delete(f);
/* istanbul ignore else */
if (unsubscribed && channel.subs.size === 0) {
channel.bc.close();
channels.delete(room);
}
return unsubscribed;
};

export const publish = (room: string, data: any, origin: any = null) => {
const c = getChannel(room);
c.bc.postMessage(data);
c.subs.forEach((sub: Sub) => sub(data, origin));
};
3 changes: 2 additions & 1 deletion packages/yrb-actioncable/src/websocket-provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Doc } from 'yjs';
import { publish, subscribe, unsubscribe } from 'lib0/broadcastchannel';
import {
Encoder,
createEncoder,
Expand Down Expand Up @@ -29,6 +28,8 @@ import {
} from 'y-protocols/awareness';
import { readAuthMessage } from 'y-protocols/auth';

import { publish, subscribe, unsubscribe } from './broadcastchannel';

type MessageHandler = (
encoder: Encoder,
decoder: Decoder,
Expand Down
80 changes: 40 additions & 40 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7756,47 +7756,47 @@ tunnel-agent@^0.6.0:
dependencies:
safe-buffer "^5.0.1"

[email protected].0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/turbo-darwin-64/-/turbo-darwin-64-1.7.0.tgz#e53ed2e791ce4d146a76aa7c276c84d590134550"
integrity sha512-hSGAueSf5Ko8J67mpqjpt9FsP6ePn1nMcl7IVPoJq5dHsgX3anCP/BPlexJ502bNK+87DDyhQhJ/LPSJXKrSYQ==

[email protected].0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/turbo-darwin-arm64/-/turbo-darwin-arm64-1.7.0.tgz#d5fdeccb38f1092b5c888f83a2dcd9ae97068dad"
integrity sha512-BLLOW5W6VZxk5+0ZOj5AO1qjM0P5isIgjbEuyAl8lHZ4s9antUbY4CtFrspT32XxPTYoDl4UjviPMcSsbcl3WQ==

[email protected].0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/turbo-linux-64/-/turbo-linux-64-1.7.0.tgz#48cf63aa51cde67d5a56eb70df6d7a93548376fa"
integrity sha512-aw2qxmfZa+kT87SB3GNUoFimqEPzTlzlRqhPgHuAAT6Uf0JHnmebPt4K+ZPtDNl5yfVmtB05bhHPqw+5QV97Yg==

[email protected].0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/turbo-linux-arm64/-/turbo-linux-arm64-1.7.0.tgz#1b9b047d72071f9b8090057ede40ca54db945ae7"
integrity sha512-AJEx2jX+zO5fQtJpO3r6uhTabj4oSA5ZhB7zTs/rwu/XqoydsvStA4X8NDW4poTbOjF7DcSHizqwi04tSMzpJw==

[email protected].0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/turbo-windows-64/-/turbo-windows-64-1.7.0.tgz#4683b84c5ba806cba8ab3ec28e4a62f46bdc9901"
integrity sha512-ewj7PPv2uxqv0r31hgnBa3E5qwUu7eyVRP5M1gB/TJXfSHduU79gbxpKCyxIZv2fL/N2/3U7EPOQPSZxBAoljA==

[email protected].0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/turbo-windows-arm64/-/turbo-windows-arm64-1.7.0.tgz#4cdcae60868df73e2af06dcaf39e1c725176cf7f"
integrity sha512-LzjOUzveWkvTD0jP8DBMYiAnYemmydsvqxdSmsUapHHJkl6wKZIOQNSO7pxsy+9XM/1/+0f9Y9F9ZNl5lePTEA==

turbo@^1.6.3:
version "1.7.0"
resolved "https://registry.yarnpkg.com/turbo/-/turbo-1.7.0.tgz#8e7d2fbc0b57f8835d51195e957766969929616f"
integrity sha512-cwympNwQNnQZ/TffBd8yT0i0O10Cf/hlxccCYgUcwhcGEb9rDjE5thDbHoHw1hlJQUF/5ua7ERJe7Zr0lNE/ww==
[email protected].2:
version "1.7.2"
resolved "https://registry.yarnpkg.com/turbo-darwin-64/-/turbo-darwin-64-1.7.2.tgz#ee79dad3009122e5754433df348f51f93f461ae1"
integrity sha512-Sml3WR8MSu80W+gS8SnoKNImcDOlIX7zlvezzds65mW11yGniIFfZ18aKWGOm92Nj2SvXCQ2+UmyGghbFaHNmQ==

[email protected].2:
version "1.7.2"
resolved "https://registry.yarnpkg.com/turbo-darwin-arm64/-/turbo-darwin-arm64-1.7.2.tgz#a5999de8a5d8d91a27a0dd9f71cc83ec2298e4d8"
integrity sha512-JnlgGLScboUJGJxvmSsF+5xkImEDTMPg2FHzX4n8AMB9az9ZlPQAMtc+xu4p6Xp9eaykKiV2RG81YS3H0fxDLA==

[email protected].2:
version "1.7.2"
resolved "https://registry.yarnpkg.com/turbo-linux-64/-/turbo-linux-64-1.7.2.tgz#fefdb94d248ff88aafa5276dcf2350e792cb2b29"
integrity sha512-vbLJw6ovG+lpiPqxniscBjljKJ2jbsHuKp8uK4j/wqgp68wAVKeAZW77GGDAUgDb88XH6Kvhh2hcizL+iWduww==

[email protected].2:
version "1.7.2"
resolved "https://registry.yarnpkg.com/turbo-linux-arm64/-/turbo-linux-arm64-1.7.2.tgz#70e2770a5915c3db5a7c324c7b6cf4efc9b8f867"
integrity sha512-zLnuS8WdHonKL74KqOopOH/leBOWumlVGF8/8hldbDPq0mwY+6myRR5/5LdveB51rkG4UJh/sQ94xV67tjBoyw==

[email protected].2:
version "1.7.2"
resolved "https://registry.yarnpkg.com/turbo-windows-64/-/turbo-windows-64-1.7.2.tgz#21e0008a28bff275de1e3ba281ec399ccc863a97"
integrity sha512-oE5PMoXjmR09okvVzteFb6FjA6yo+nMsacsgKH2yLNq4sOrVo9tG98JkRurOv5+L6ZQ3yGXPxWHiqeH7hLkAVQ==

[email protected].2:
version "1.7.2"
resolved "https://registry.yarnpkg.com/turbo-windows-arm64/-/turbo-windows-arm64-1.7.2.tgz#873afbd6e1170d9f8d4c758ab3fbc9f7a0cd9dd7"
integrity sha512-mdTUJk23acRv5qxA/yEstYhM1VFenVE3FDrssxGRFq7S80smtCGK1xUd4BEDDzDlVXOqBohmM5jRh9516rcjPQ==

turbo@^1.7.2:
version "1.7.2"
resolved "https://registry.yarnpkg.com/turbo/-/turbo-1.7.2.tgz#ed7394631fac523fd0654f9c81306d40ef61014a"
integrity sha512-YR/x3GZEx0C1RV6Yvuw/HB1Ixx3upM6ZTTa4WqKz9WtLWN8u2g+u2h5KpG5YtjCS3wl/8zVXgHf2WiMK6KIghg==
optionalDependencies:
turbo-darwin-64 "1.7.0"
turbo-darwin-arm64 "1.7.0"
turbo-linux-64 "1.7.0"
turbo-linux-arm64 "1.7.0"
turbo-windows-64 "1.7.0"
turbo-windows-arm64 "1.7.0"
turbo-darwin-64 "1.7.2"
turbo-darwin-arm64 "1.7.2"
turbo-linux-64 "1.7.2"
turbo-linux-arm64 "1.7.2"
turbo-windows-64 "1.7.2"
turbo-windows-arm64 "1.7.2"

tweetnacl@^0.14.3, tweetnacl@~0.14.0:
version "0.14.5"
Expand Down

0 comments on commit 5aeb5c0

Please sign in to comment.