Skip to content

Commit

Permalink
Vendor rfc6902 package (#173) (#177)
Browse files Browse the repository at this point in the history
Co-authored-by: Marco Gomez <[email protected]>
  • Loading branch information
MarcusLongmuir and TheCodeTherapy authored Jun 18, 2024
1 parent 5cac31d commit 9ae0a23
Show file tree
Hide file tree
Showing 22 changed files with 2,370 additions and 13 deletions.
8 changes: 1 addition & 7 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions packages/networked-dom-document/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
},
"dependencies": {
"@mml-io/networked-dom-protocol": "^0.15.0",
"@mml-io/observable-dom-common": "^0.15.0",
"rfc6902": "5.1.1"
"@mml-io/observable-dom-common": "^0.15.0"
}
}
2 changes: 1 addition & 1 deletion packages/networked-dom-document/src/NetworkedDOM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
StaticVirtualDOMElement,
StaticVirtualDOMMutationIdsRecord,
} from "@mml-io/observable-dom-common";
import { applyPatch } from "rfc6902";

import { StaticVirtualDOMMutationRecord, VirtualDOMDiffStruct } from "./common";
import {
Expand All @@ -25,6 +24,7 @@ import {
findParentNodeOfNodeId,
virtualDOMDiffToVirtualDOMMutationRecord,
} from "./diffing";
import { applyPatch } from "./rfc6902";

export const networkedDOMProtocolSubProtocol_v0_1 = "networked-dom-v0.1";
export const defaultWebsocketSubProtocol = networkedDOMProtocolSubProtocol_v0_1;
Expand Down
3 changes: 2 additions & 1 deletion packages/networked-dom-document/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { StaticVirtualDOMElement } from "@mml-io/observable-dom-common";
import * as rfc6902 from "rfc6902";

import * as rfc6902 from "./rfc6902";

export type NodeMapping = {
clientFacingNodeId: number;
Expand Down
2 changes: 1 addition & 1 deletion packages/networked-dom-document/src/diffing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
TextNodeDescription,
} from "@mml-io/networked-dom-protocol";
import { StaticVirtualDOMElement } from "@mml-io/observable-dom-common";
import * as rfc6902 from "rfc6902";

import { NodeMapping, StaticVirtualDOMMutationRecord, VirtualDOMDiffStruct } from "./common";
import * as rfc6902 from "./rfc6902";

export const visibleToAttrName = "visible-to";
export const hiddenFromAttrName = "hidden-from";
Expand Down
9 changes: 9 additions & 0 deletions packages/networked-dom-document/src/rfc6902/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Originally from https://github.com/chbrown/rfc6902

Combined with this contribution from supermeng: https://github.com/chbrown/rfc6902/pull/88/files

Vendored here to avoid depending on a unpublished git repository.

# License

Copyright 2014-2021 Christopher Brown. MIT Licensed.
41 changes: 41 additions & 0 deletions packages/networked-dom-document/src/rfc6902/deepEqual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export function deepEqual(a: any, b: any): boolean {
if (a === b) {
return true;
}

if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) {
return false;
}

return a.every((elem, index) => {
return deepEqual(elem, b[index]);
});
}

if (typeof a === "object" && typeof b === "object" && a !== null && b !== null) {
if (Array.isArray(a) || Array.isArray(b)) {
return false;
}

const keys1 = Object.keys(a);
const keys2 = Object.keys(b);
if (keys1.length !== keys2.length || !keys1.every((key) => keys2.includes(key))) {
return false;
}

for (const key in a) {
if (!deepEqual(a[key], b[key])) {
return false;
}
}

if (keys1.length === 0 && a instanceof Date && b instanceof Date) {
// Only need to check the length of 1 as they are already known to be equal
return a.getTime() === b.getTime();
}

return true;
}
return false;
}
Loading

0 comments on commit 9ae0a23

Please sign in to comment.