@dxos/web-context-react 0.8.4-main.c85a9c8dae → 0.8.4-main.dd46787728
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/index.mjs +15 -5
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +15 -5
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/provider.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -6
- package/src/consumer.test.tsx +5 -5
- package/src/provider.tsx +15 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/web-context-react",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.dd46787728",
|
|
4
4
|
"description": "React integration with web context protocol",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -21,15 +21,12 @@
|
|
|
21
21
|
}
|
|
22
22
|
},
|
|
23
23
|
"types": "dist/types/src/index.d.ts",
|
|
24
|
-
"typesVersions": {
|
|
25
|
-
"*": {}
|
|
26
|
-
},
|
|
27
24
|
"files": [
|
|
28
25
|
"dist",
|
|
29
26
|
"src"
|
|
30
27
|
],
|
|
31
28
|
"dependencies": {
|
|
32
|
-
"@dxos/web-context": "0.8.4-main.
|
|
29
|
+
"@dxos/web-context": "0.8.4-main.dd46787728"
|
|
33
30
|
},
|
|
34
31
|
"devDependencies": {
|
|
35
32
|
"@testing-library/react": "^16.3.0",
|
|
@@ -37,7 +34,7 @@
|
|
|
37
34
|
"@types/react-dom": "~19.2.3",
|
|
38
35
|
"react": "~19.2.3",
|
|
39
36
|
"react-dom": "~19.2.3",
|
|
40
|
-
"vitest": "
|
|
37
|
+
"vitest": "4.1.5"
|
|
41
38
|
},
|
|
42
39
|
"peerDependencies": {
|
|
43
40
|
"react": "~19.2.3"
|
package/src/consumer.test.tsx
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// @vitest-environment jsdom
|
|
6
6
|
|
|
7
7
|
import { act, cleanup, render, screen } from '@testing-library/react';
|
|
8
|
-
import React from 'react';
|
|
8
|
+
import React, { ReactNode, useEffect, useRef, useState } from 'react';
|
|
9
9
|
import { afterEach, describe, expect, test } from 'vitest';
|
|
10
10
|
|
|
11
11
|
import { CONTEXT_REQUEST_EVENT, createContext } from '@dxos/web-context';
|
|
@@ -37,7 +37,7 @@ describe('useWebComponentContext', () => {
|
|
|
37
37
|
|
|
38
38
|
test('consumes context from updates (subscription)', async () => {
|
|
39
39
|
const Container = () => {
|
|
40
|
-
const [val, setVal] =
|
|
40
|
+
const [val, setVal] = useState('initial');
|
|
41
41
|
return (
|
|
42
42
|
<>
|
|
43
43
|
<button onClick={() => setVal('updated')}>Update</button>
|
|
@@ -59,9 +59,9 @@ describe('useWebComponentContext', () => {
|
|
|
59
59
|
});
|
|
60
60
|
|
|
61
61
|
test('consumes context from DOM parent (outside React tree)', () => {
|
|
62
|
-
const Wrapper = ({ children }: { children:
|
|
63
|
-
const ref =
|
|
64
|
-
|
|
62
|
+
const Wrapper = ({ children }: { children: ReactNode }) => {
|
|
63
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
64
|
+
useEffect(() => {
|
|
65
65
|
const handler = (e: Event) => {
|
|
66
66
|
const event = e as any;
|
|
67
67
|
if (event.context === ctx) {
|
package/src/provider.tsx
CHANGED
|
@@ -149,8 +149,12 @@ export const ContextProtocolProvider = <T extends UnknownContext>({
|
|
|
149
149
|
const handleContextProviderEvent = useCallback(
|
|
150
150
|
(e: Event) => {
|
|
151
151
|
const event = e as ContextProviderEvent<UnknownContext>;
|
|
152
|
-
if (event.context !== context)
|
|
153
|
-
|
|
152
|
+
if (event.context !== context) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (containerRef.current && event.contextTarget === containerRef.current) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
154
158
|
|
|
155
159
|
const seen = new Set<ContextCallback<ContextType<T>>>();
|
|
156
160
|
for (const ref of subscriptionRefs) {
|
|
@@ -161,9 +165,13 @@ export const ContextProtocolProvider = <T extends UnknownContext>({
|
|
|
161
165
|
}
|
|
162
166
|
|
|
163
167
|
const info = subscriptions.get(callback);
|
|
164
|
-
if (!info)
|
|
168
|
+
if (!info) {
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
165
171
|
|
|
166
|
-
if (seen.has(callback))
|
|
172
|
+
if (seen.has(callback)) {
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
167
175
|
seen.add(callback);
|
|
168
176
|
|
|
169
177
|
info.consumerHost.dispatchEvent(
|
|
@@ -198,7 +206,9 @@ export const ContextProtocolProvider = <T extends UnknownContext>({
|
|
|
198
206
|
|
|
199
207
|
useEffect(() => {
|
|
200
208
|
const el = containerRef.current;
|
|
201
|
-
if (!el)
|
|
209
|
+
if (!el) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
202
212
|
|
|
203
213
|
el.addEventListener(CONTEXT_REQUEST_EVENT, handleContextRequestEvent);
|
|
204
214
|
el.addEventListener(CONTEXT_PROVIDER_EVENT, handleContextProviderEvent);
|