Skip to content

Commit 1f68dc4

Browse files
committed
feat: use patched @wessberg/connection-observer
Until wessberg/connection-observer#9
1 parent 6d6655f commit 1f68dc4

4 files changed

Lines changed: 72 additions & 12 deletions

File tree

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,10 @@
6464
},
6565
"dependencies": {
6666
"@wessberg/connection-observer": "^1.0.5"
67+
},
68+
"pnpm": {
69+
"patchedDependencies": {
70+
"@wessberg/connection-observer@1.0.5": "patches/@wessberg__connection-observer@1.0.5.patch"
71+
}
6772
}
6873
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
diff --git a/CHANGELOG.md b/CHANGELOG.md
2+
deleted file mode 100644
3+
index 8caf391e7549ee0e3b34c975ed1f1696c00daf68..0000000000000000000000000000000000000000
4+
diff --git a/dist/index.js b/dist/index.js
5+
index d0b60e342f33ff6fc42536e7b29b6d634e5f2894..dfa9948eb5a67950204cd00e002f1f276669e174 100644
6+
--- a/dist/index.js
7+
+++ b/dist/index.js
8+
@@ -1,4 +1,4 @@
9+
-const ORIGINAL_ATTACH_SHADOW = Element.prototype.attachShadow;
10+
+const ORIGINAL_ATTACH_SHADOW = typeof Element !== 'undefined' ? Element.prototype.attachShadow : undefined;
11+
12+
/**
13+
* Returns true if the environment is relying on the ShadyDOM polyfill.
14+
@@ -18,7 +18,7 @@ function supportsShadowRoots() {
15+
*/
16+
function patchElementPrototypeAttachShadow(callback) {
17+
// If Shadow DOM is not available, or if it is based on the ShadyDOM polyfill, there's nothing (or no need) to patch
18+
- if (ORIGINAL_ATTACH_SHADOW == null || isShady())
19+
+ if (ORIGINAL_ATTACH_SHADOW == null || isShady() || typeof Element === 'undefined')
20+
return;
21+
Element.prototype.attachShadow = function (shadowRootInitDict) {
22+
const shadowRoot = ORIGINAL_ATTACH_SHADOW.call(this, shadowRootInitDict);
23+
@@ -329,7 +329,7 @@ const observeRoot = (() => {
24+
})();
25+
26+
// Creates a pausable queue and pass document.documentElement as the initial queue payload
27+
-const rootObserverQueue = createPausableQueue(observeRoot, document.documentElement);
28+
+const rootObserverQueue = createPausableQueue(observeRoot, typeof document !== 'undefined' ? document.documentElement : undefined);
29+
30+
/**
31+
* An Observer that tracks the DOM-insertion state of observed nodes across Shadow boundaries.
32+
@@ -347,6 +347,9 @@ class ConnectionObserver {
33+
else if (typeof callback !== "function") {
34+
throw new TypeError(`Failed to construct '${ConnectionObserver.name}': The callback provided as parameter 1 is not a function.`);
35+
}
36+
+ if (typeof document === 'undefined') {
37+
+ return;
38+
+ }
39+
// Add this ConnectionObserver to the Set of ConnectionObservers
40+
initializeConnectionObserver(this, callback);
41+
}

pnpm-lock.yaml

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/widget.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* eslint-disable unicorn/no-null */
2+
import { ConnectionObserver } from '@wessberg/connection-observer';
23
import type * as ReactType from 'react';
34
import type * as ReactDomType from 'react-dom';
45
import type * as ReactDomClientType from 'react-dom/client';
5-
import type { IChangedTiddlers } from 'tiddlywiki';
6+
import type { IChangedTiddlers, IParseTreeNode, IWidgetInitialiseOptions } from 'tiddlywiki';
67
import type { IReactWidget, ITWReactProps, ITWReactPropsDefault } from './widget-type';
7-
import { ConnectionObserver } from '@wessberg/connection-observer';
88

99
import { widget as Widget } from '$:/core/modules/widgets/widget.js';
1010
const ReactDom = require('react-dom') as typeof ReactDomType & typeof ReactDomClientType;
@@ -20,15 +20,23 @@ class ReactWidgetImpl<
2020
> extends Widget implements IReactWidget<IProps> {
2121
root: ReturnType<typeof ReactDom.createRoot> | undefined;
2222
containerElement: HTMLDivElement | undefined;
23+
connectionObserver: ConnectionObserver | undefined;
2324

24-
connectionObserver = new ConnectionObserver(entries => {
25-
for (const { connected } of entries) {
26-
if (!connected) {
27-
this.destroy();
28-
this.connectionObserver?.disconnect?.();
29-
}
25+
constructor(parseTreeNode: IParseTreeNode, options?: IWidgetInitialiseOptions | undefined) {
26+
super(parseTreeNode, options);
27+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
28+
if (!$tw.browser) {
29+
return;
3030
}
31-
});
31+
this.connectionObserver = new ConnectionObserver(entries => {
32+
for (const { connected } of entries) {
33+
if (!connected) {
34+
this.destroy();
35+
this.connectionObserver?.disconnect?.();
36+
}
37+
}
38+
});
39+
}
3240

3341
refresh(changedTiddlers: IChangedTiddlers) {
3442
// we don't need to refresh mount point of react-dom
@@ -67,7 +75,7 @@ class ReactWidgetImpl<
6775
this.connectionObserver.observe(this.parentDomNode);
6876
}
6977
this.domNodes.push(this.containerElement);
70-
parent.append(this.containerElement);
78+
nextSibling === null ? parent.append(this.containerElement) : nextSibling.before(this.containerElement);
7179
const reactElement = React.createElement(this.reactComponent, currentProps);
7280
this.root.render(reactElement);
7381
}

0 commit comments

Comments
 (0)