Skip to content

Commit

Permalink
Update live.js.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed May 5, 2024
1 parent 3a45db3 commit 7224cf3
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 16 deletions.
6 changes: 3 additions & 3 deletions node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions node_modules/@socketry/live/Live.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion node_modules/@socketry/live/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions node_modules/@socketry/live/test/Live.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"dependencies": {
"@socketry/live": "^0.7.0"
"@socketry/live": "^0.8"
},
"scripts": {
"postinstall": "bundle exec bake utopia:node:update"
Expand Down
8 changes: 5 additions & 3 deletions public/_components/@socketry/live/Live.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ export class Live {
}
}

bindElementsByClassName(selector = 'live') {
bindElementsByClassName(parent = this.document, className = 'live') {
this.bind(
this.document.getElementsByClassName(selector)
parent.getElementsByClassName(className)
);

this.flush();
Expand All @@ -126,7 +126,7 @@ export class Live {
}

reply(options) {
if (options && options.reply) {
if (options?.reply) {
this.send(JSON.stringify({reply: options.reply}));
}
}
Expand All @@ -139,6 +139,8 @@ export class Live {

morphdom(element, fragment);

if (options?.bind) this.bindElementsByClassName(element);

this.reply(options);
}

Expand Down
2 changes: 1 addition & 1 deletion public/_components/@socketry/live/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@socketry/live",
"type": "module",
"version": "0.7.0",
"version": "0.8.0",
"description": "Live HTML tags for Ruby.",
"main": "Live.js",
"repository": {
Expand Down
31 changes: 31 additions & 0 deletions public/_components/@socketry/live/test/Live.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,38 @@ describe('Live', function () {
live.disconnect();
});

it('should handle updates with child live elements', async function () {
const live = new Live(dom.window, webSocketServerURL);

live.connect();

const connected = new Promise(resolve => {
webSocketServer.on('connection', resolve);
});

let socket = await connected;

const reply = new Promise((resolve, reject) => {
socket.on('message', message => {
let payload = JSON.parse(message);
if (payload.bind) resolve(payload);
});
});

socket.send(
JSON.stringify(['update', 'my', '<div id="my"><div id="mychild" class="live"></div></div>', {bind: true}])
);

let binding = await reply;

strictEqual(binding.bind, 'mychild');

live.disconnect();
});

it('should handle replacements', async function () {
dom.window.document.body.innerHTML = '<div id="my"><p>Hello World</p></div>';

const live = new Live(dom.window, webSocketServerURL);

live.connect();
Expand Down

0 comments on commit 7224cf3

Please sign in to comment.