Skip to content

Commit 7a33b28

Browse files
committed
3rd party improvements
1 parent 931961c commit 7a33b28

File tree

6 files changed

+101
-61
lines changed

6 files changed

+101
-61
lines changed

docs/core.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

esm/exports.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// this file simply exports enough stuff to allow
2+
// 3rd party libraries, including PyScript, to work
3+
export * from './fetch-utils.js';
4+
export * from './index.js';
5+
export * from './script-handler.js';
6+
export * from './utils.js';

esm/index.js

Lines changed: 87 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,106 @@
11
import { $$ } from 'basic-devtools';
22

3-
import XWorker from './xworker.js';
43
import { handle } from './script-handler.js';
5-
import { assign } from './utils.js';
4+
import { assign, defineProperty } from './utils.js';
65
import { selectors, prefixes } from './interpreters.js';
76
import { CUSTOM_SELECTORS, handleCustomType } from './custom.js';
87
import { listener, addAllListeners } from './listeners.js';
98

10-
export { define, whenDefined } from './custom.js';
11-
export { env } from './listeners.js';
9+
import { define as $define, whenDefined as $whenDefined } from './custom.js';
10+
import { env as $env } from './listeners.js';
11+
import { Hook as $Hook } from './worker/hooks.js';
12+
import $XWorker from './xworker.js';
13+
14+
const polyscript = Symbol.for('polyscript');
15+
const alreadyLive = polyscript in globalThis;
16+
17+
// avoid multiple initialization of the same library
18+
/* c8 ignore start */
19+
const { define, whenDefined, env, Hook, XWorker } = (
20+
alreadyLive ?
21+
globalThis[polyscript] :
22+
defineProperty(
23+
globalThis,
24+
polyscript,
25+
{
26+
value: {
27+
define: $define,
28+
whenDefined: $whenDefined,
29+
env: $env,
30+
Hook: $Hook,
31+
XWorker: $XWorker
32+
}
33+
}
34+
)[polyscript]
35+
);
36+
/* c8 ignore stop */
37+
38+
export { define, whenDefined, env, Hook, XWorker };
1239
export * from './errors.js';
13-
export { XWorker };
1440

15-
const mo = new MutationObserver((records) => {
16-
const selector = selectors.join(',');
17-
for (const { type, target, attributeName, addedNodes } of records) {
18-
// attributes are tested via integration / e2e
19-
/* c8 ignore start */
20-
if (type === 'attributes') {
21-
const i = attributeName.lastIndexOf('-') + 1;
22-
if (i) {
23-
const prefix = attributeName.slice(0, i);
24-
for (const p of prefixes) {
25-
if (prefix === p) {
26-
const type = attributeName.slice(i);
27-
if (type !== 'env') {
28-
const method = target.hasAttribute(attributeName)
29-
? 'add'
30-
: 'remove';
31-
target[`${method}EventListener`](type, listener);
41+
if (!alreadyLive) {
42+
const mo = new MutationObserver((records) => {
43+
const selector = selectors.join(',');
44+
for (const { type, target, attributeName, addedNodes } of records) {
45+
// attributes are tested via integration / e2e
46+
/* c8 ignore start */
47+
if (type === 'attributes') {
48+
const i = attributeName.lastIndexOf('-') + 1;
49+
if (i) {
50+
const prefix = attributeName.slice(0, i);
51+
for (const p of prefixes) {
52+
if (prefix === p) {
53+
const type = attributeName.slice(i);
54+
if (type !== 'env') {
55+
const method = target.hasAttribute(attributeName)
56+
? 'add'
57+
: 'remove';
58+
target[`${method}EventListener`](type, listener);
59+
}
60+
break;
3261
}
33-
break;
3462
}
3563
}
64+
continue;
3665
}
37-
continue;
38-
}
39-
for (const node of addedNodes) {
40-
if (node.nodeType === 1) {
41-
addAllListeners(node);
42-
if (selector && node.matches(selector)) handle(node);
43-
else bootstrap(selector, node, true);
66+
for (const node of addedNodes) {
67+
if (node.nodeType === 1) {
68+
addAllListeners(node);
69+
if (selector && node.matches(selector)) handle(node);
70+
else bootstrap(selector, node, true);
71+
}
4472
}
73+
/* c8 ignore stop */
4574
}
46-
/* c8 ignore stop */
47-
}
48-
});
75+
});
4976

50-
/* c8 ignore start */
51-
const bootstrap = (selector, node, shouldHandle) => {
52-
if (selector) $$(selector, node).forEach(handle);
53-
selector = CUSTOM_SELECTORS.join(',');
54-
if (selector) {
55-
if (shouldHandle) handleCustomType(node);
56-
$$(selector, node).forEach(handleCustomType);
57-
}
58-
};
59-
/* c8 ignore stop */
77+
/* c8 ignore start */
78+
const bootstrap = (selector, node, shouldHandle) => {
79+
if (selector) $$(selector, node).forEach(handle);
80+
selector = CUSTOM_SELECTORS.join(',');
81+
if (selector) {
82+
if (shouldHandle) handleCustomType(node);
83+
$$(selector, node).forEach(handleCustomType);
84+
}
85+
};
86+
/* c8 ignore stop */
87+
88+
const observe = (root) => {
89+
mo.observe(root, { childList: true, subtree: true, attributes: true });
90+
return root;
91+
};
6092

61-
const observe = (root) => {
62-
mo.observe(root, { childList: true, subtree: true, attributes: true });
63-
return root;
64-
};
93+
const { attachShadow } = Element.prototype;
94+
assign(Element.prototype, {
95+
attachShadow(init) {
96+
return observe(attachShadow.call(this, init));
97+
},
98+
});
6599

66-
const { attachShadow } = Element.prototype;
67-
assign(Element.prototype, {
68-
attachShadow(init) {
69-
return observe(attachShadow.call(this, init));
70-
},
71-
});
100+
// give 3rd party a chance to apply changes before this happens
101+
queueMicrotask(() => {
102+
addAllListeners(observe(document));
103+
bootstrap(selectors.join(','), document, false);
104+
});
72105

73-
// give 3rd party a chance to apply changes before this happens
74-
queueMicrotask(() => {
75-
addAllListeners(observe(document));
76-
bootstrap(selectors.join(','), document, false);
77-
});
106+
}

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@
6060
"import": "./esm/index.js",
6161
"default": "./cjs/index.js"
6262
},
63+
"./exports": {
64+
"types": "./types/polyscript/esm/exports.d.ts",
65+
"import": "./esm/exports.js",
66+
"default": "./cjs/exports.js"
67+
},
6368
"./package.json": "./package.json"
6469
},
6570
"dependencies": {

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
"emitDeclarationOnly": true,
99
"declarationDir": "types"
1010
},
11-
"include": ["esm/index.js"]
11+
"include": ["esm/index.js", "esm/exports.js"]
1212
}

0 commit comments

Comments
 (0)