Skip to content

Commit

Permalink
improve connectElement to take livestate confit options
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Nelson committed Aug 7, 2024
1 parent 2c6775d commit 73d24cc
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 24 deletions.
96 changes: 85 additions & 11 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "phx-live-state",
"version": "0.11.2",
"version": "0.12.0",
"description": "Front end library for live_state",
"main": "build/src/index.js",
"module": "build/src/index.js",
Expand Down Expand Up @@ -35,7 +35,7 @@
"typescript": "^4.2.2"
},
"dependencies": {
"json-joy": "^1.18.1",
"json-joy": ">= 11.0",
"lit": "^2.2.6",
"phoenix": "1.6.15",
"reflect-metadata": "^0.1.13",
Expand Down
35 changes: 24 additions & 11 deletions src/connectElement.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import LiveState from "./LiveState";
import liveState from "./liveStateDecorator";
import LiveState, {LiveStateConfig} from "./LiveState";
import liveState, { liveStateConfig } from "./liveStateDecorator";
import subscript from 'subscript';

export type ConnectOptions = {
export type ConnectOptions = LiveStateConfig & {
properties?: Array<LiveStateProperty>;
attributes?: Array<string>;
events?: {
Expand All @@ -19,17 +19,30 @@ export type ConnectOptions = {
* - connect properties and attributes
* - call `sendEvent` and `receiveEvent` for each specified event
*/
export const connectElement = (liveState: LiveState, el: HTMLElement, { properties, attributes, events }: ConnectOptions) => {
if (el['liveState'] !== liveState) {
liveState.connect();
connectProperties(liveState, el, properties);
attributes?.forEach((attr) => connectAtttribute(liveState, el, attr));
events?.send?.forEach((eventName) => sendEvent(liveState, el, eventName));
events?.receive?.forEach((eventName) => receiveEvent(liveState, el, eventName));
el['liveState'] = liveState;
export const connectElement = (liveStateOrEl: LiveState | HTMLElement, elOrOptions: HTMLElement | ConnectOptions, options?: ConnectOptions) => {
if (liveStateOrEl instanceof LiveState) {
const liveState = liveStateOrEl as LiveState;
const el = elOrOptions as HTMLElement;
if (el['liveState'] !== liveState) {
doConnect(el, liveState, options);
}
} else {
const liveState = new LiveState(elOrOptions as ConnectOptions);
doConnect(liveStateOrEl as HTMLElement, liveState, elOrOptions as ConnectOptions);
}
}

const doConnect = (el: HTMLElement, liveState: LiveState, options: ConnectOptions) => {
const { properties, attributes, events } = options;
liveState.connect();
connectProperties(liveState, el, properties);
attributes?.forEach((attr) => connectAtttribute(liveState, el, attr));
events?.send?.forEach((eventName) => sendEvent(liveState, el, eventName));
events?.receive?.forEach((eventName) => receiveEvent(liveState, el, eventName));
el['liveState'] = liveState;

}

const connectProperties = (liveState, el, properties) => {
let mergedProperties = properties || [];
mergedProperties = el._liveStateProperties ? mergedProperties.concat(el._liveStateProperties) : mergedProperties;
Expand Down
11 changes: 11 additions & 0 deletions test/connect-element-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ describe('connectElement', () => {
socketMock.expects('connect').exactly(1);
});

it('creates livestate instance with arity 2 version', async() => {
const el: TestElement = await fixture('<test-element></test-element>');
connectElement(el, {
url: 'ws://foo.bar',
topic: 'howdy',
properties: ['bar'],
attributes: ['foo']
});
expect(el['liveState']).to.be.instanceOf(LiveState);
});

it('updates on state changes', async () => {
const el: TestElement = await fixture('<test-element></test-element>');
connectElement(liveState, el, {
Expand Down

0 comments on commit 73d24cc

Please sign in to comment.