@teambit/pubsub 0.0.551 → 0.0.556

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.
@@ -1,5 +1,9 @@
1
1
  import React, { RefObject, ReactNode } from 'react';
2
2
  export interface PubSubRegistry {
3
+ /**
4
+ * starts a connection to an iframe child.
5
+ * Returns a destroy() function that will break the connection.
6
+ */
3
7
  connect(ref: HTMLIFrameElement): () => void;
4
8
  }
5
9
  export declare const pubsubRegistry: React.Context<PubSubRegistry | undefined>;
@@ -1,3 +1,6 @@
1
+ /**
2
+ * Please Notice: This file will run in the preview iframe.
3
+ */
1
4
  import { BitBaseEvent } from './bit-base-event';
2
5
  import { Callback } from './types';
3
6
  export declare class PubsubPreview {
@@ -2,15 +2,28 @@ import { UiUI } from '@teambit/ui';
2
2
  import { BitBaseEvent } from './bit-base-event';
3
3
  import { Callback } from './types';
4
4
  declare type PubOptions = {
5
+ /** forward the event to adjacent windows (including the preview iframe) */
5
6
  propagate?: boolean;
6
7
  };
7
8
  export declare class PubsubUI {
8
9
  private childApi?;
9
10
  private events;
11
+ /**
12
+ * subscribe to events
13
+ */
10
14
  sub: (topic: string, callback: Callback) => () => void;
15
+ /**
16
+ * publish event to all subscribers, including nested iframes.
17
+ */
11
18
  pub: (topic: string, event: BitBaseEvent<any>, { propagate }?: PubOptions) => void;
12
19
  private connectToIframe;
20
+ /**
21
+ * publish event to all subscribers in this window
22
+ */
13
23
  private emitEvent;
24
+ /**
25
+ * publish event to nested iframes
26
+ */
14
27
  private pubToChild;
15
28
  static runtime: import("@teambit/harmony").RuntimeDefinition;
16
29
  static dependencies: import("@teambit/harmony").Aspect[];
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@teambit/pubsub",
3
- "version": "0.0.551",
3
+ "version": "0.0.556",
4
4
  "homepage": "https://bit.dev/teambit/harmony/pubsub",
5
5
  "main": "dist/index.js",
6
6
  "componentId": {
7
7
  "scope": "teambit.harmony",
8
8
  "name": "pubsub",
9
- "version": "0.0.551"
9
+ "version": "0.0.556"
10
10
  },
11
11
  "dependencies": {
12
12
  "@teambit/harmony": "0.2.11",
@@ -14,10 +14,10 @@
14
14
  "penpal": "5.3.0",
15
15
  "@babel/runtime": "7.12.18",
16
16
  "core-js": "^3.0.0",
17
- "@teambit/cli": "0.0.382",
18
- "@teambit/preview": "0.0.551",
19
- "@teambit/ui-foundation.ui.is-browser": "0.0.459",
20
- "@teambit/ui": "0.0.551"
17
+ "@teambit/cli": "0.0.387",
18
+ "@teambit/preview": "0.0.556",
19
+ "@teambit/ui-foundation.ui.is-browser": "0.0.464",
20
+ "@teambit/ui": "0.0.556"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@types/react": "^17.0.8",
@@ -28,7 +28,7 @@
28
28
  "@types/node": "12.20.4"
29
29
  },
30
30
  "peerDependencies": {
31
- "@teambit/legacy": "1.0.169",
31
+ "@teambit/legacy": "1.0.173",
32
32
  "react-dom": "^16.8.0 || ^17.0.0",
33
33
  "react": "^16.8.0 || ^17.0.0"
34
34
  },
@@ -56,12 +56,18 @@
56
56
  "react": "-"
57
57
  },
58
58
  "peerDependencies": {
59
- "@teambit/legacy": "1.0.169",
59
+ "@teambit/legacy": "1.0.173",
60
60
  "react-dom": "^16.8.0 || ^17.0.0",
61
61
  "react": "^16.8.0 || ^17.0.0"
62
62
  }
63
63
  }
64
64
  },
65
+ "files": [
66
+ "dist",
67
+ "!dist/tsconfig.tsbuildinfo",
68
+ "README.md",
69
+ "README.mdx"
70
+ ],
65
71
  "private": false,
66
72
  "engines": {
67
73
  "node": ">=12.22.0"
package/bit-base-event.ts DELETED
@@ -1,3 +0,0 @@
1
- export class BitBaseEvent<T> {
2
- constructor(readonly type: string, readonly version: string, readonly timestamp: number, readonly data: T) {}
3
- }
package/index.ts DELETED
@@ -1,7 +0,0 @@
1
- export { PubsubAspect, PubsubAspect as default } from './pubsub.aspect';
2
-
3
- export type { PubsubMain } from './pubsub.main.runtime';
4
- export type { PubsubPreview } from './pubsub.preview.runtime';
5
- export type { PubsubUI } from './pubsub.ui.runtime';
6
- export { BitBaseEvent } from './bit-base-event';
7
- export { usePubSub, PubSubRegistry, usePubSubIframe } from './pubsub-context';
@@ -1,6 +0,0 @@
1
- export class PubSubNoParentError extends Error {
2
- constructor() {
3
- super('could not connect to parent window');
4
- this.name = 'PubSubNoParentError';
5
- }
6
- }
@@ -1,34 +0,0 @@
1
- import React, { createContext, useContext, useEffect, RefObject, ReactNode } from 'react';
2
-
3
- export interface PubSubRegistry {
4
- /**
5
- * starts a connection to an iframe child.
6
- * Returns a destroy() function that will break the connection.
7
- */
8
- connect(ref: HTMLIFrameElement): () => void;
9
- }
10
-
11
- export const pubsubRegistry = createContext<PubSubRegistry | undefined>(undefined);
12
-
13
- export function createProvider(pubSubContext: PubSubRegistry) {
14
- const PubSubProvider = ({ children }: { children: ReactNode }) => (
15
- <pubsubRegistry.Provider value={pubSubContext}>{children}</pubsubRegistry.Provider>
16
- );
17
-
18
- return PubSubProvider;
19
- }
20
-
21
- export function usePubSub() {
22
- return useContext(pubsubRegistry);
23
- }
24
-
25
- export function usePubSubIframe(ref: RefObject<HTMLIFrameElement>) {
26
- const pubSub = usePubSub();
27
-
28
- useEffect(() => {
29
- if (!ref.current || !pubSub) return () => {};
30
-
31
- const destroyConnection = pubSub.connect(ref.current);
32
- return () => destroyConnection();
33
- }, [ref.current, pubSub]);
34
- }
package/pubsub.aspect.ts DELETED
@@ -1,8 +0,0 @@
1
- import { Aspect } from '@teambit/harmony';
2
-
3
- export const PubsubAspect = Aspect.create({
4
- id: 'teambit.harmony/pubsub',
5
- dependencies: [],
6
- });
7
-
8
- export default PubsubAspect;
@@ -1,34 +0,0 @@
1
- import { MainRuntime } from '@teambit/cli';
2
-
3
- import { BitBaseEvent } from './bit-base-event';
4
- import { PubsubAspect } from './pubsub.aspect';
5
-
6
- export class PubsubMain {
7
- private topicMap = {};
8
-
9
- private createOrGetTopic = (topicUUID) => {
10
- this.topicMap[topicUUID] = this.topicMap[topicUUID] || [];
11
- };
12
-
13
- public sub(topicUUID, callback) {
14
- this.createOrGetTopic(topicUUID);
15
- this.topicMap[topicUUID].push(callback);
16
- }
17
-
18
- public pub(topicUUID, event: BitBaseEvent<any>) {
19
- this.createOrGetTopic(topicUUID);
20
- this.topicMap[topicUUID].forEach((callback) => callback(event));
21
- }
22
-
23
- unsubscribeAll(topicId: string) {
24
- delete this.topicMap[topicId];
25
- }
26
-
27
- static runtime = MainRuntime;
28
-
29
- static async provider() {
30
- return new PubsubMain();
31
- }
32
- }
33
-
34
- PubsubAspect.addRuntime(PubsubMain);
@@ -1,90 +0,0 @@
1
- /**
2
- * Please Notice: This file will run in the preview iframe.
3
- */
4
-
5
- import { PreviewRuntime } from '@teambit/preview';
6
- import { isBrowser } from '@teambit/ui-foundation.ui.is-browser';
7
-
8
- import { EventEmitter2 } from 'eventemitter2';
9
- import { connectToParent, ErrorCode } from 'penpal';
10
-
11
- import { BitBaseEvent } from './bit-base-event';
12
- import { PubSubNoParentError } from './no-parent-error';
13
- import { PubsubAspect } from './pubsub.aspect';
14
- import { Callback } from './types';
15
-
16
- type ParentMethods = {
17
- pub: (topic: string, event: BitBaseEvent<any>) => Promise<any>;
18
- };
19
-
20
- export class PubsubPreview {
21
- private _parentPubsub?: ParentMethods;
22
- private events = new EventEmitter2();
23
-
24
- public sub(topic: string, callback: Callback) {
25
- const emitter = this.events;
26
- emitter.on(topic, callback);
27
-
28
- const unSub = () => {
29
- emitter.off(topic, callback);
30
- };
31
- return unSub;
32
- }
33
-
34
- public pub(topic: string, event: BitBaseEvent<any>) {
35
- this.events.emit(topic, event);
36
- this._parentPubsub?.pub(topic, event).catch((err) => {
37
- // eslint-disable-next-line no-console
38
- console.error('[Pubsub.preview]', err);
39
- });
40
- }
41
-
42
- private inIframe() {
43
- try {
44
- return isBrowser && window.self !== window.top;
45
- } catch (e: any) {
46
- return false;
47
- }
48
- }
49
-
50
- private connectToParentPubSub = (retries = 10): Promise<ParentMethods | undefined> => {
51
- if (retries <= 0) throw new PubSubNoParentError();
52
-
53
- return connectToParent<ParentMethods>({
54
- timeout: 300,
55
- methods: {
56
- pub: this.handleMessageFromParent,
57
- },
58
- })
59
- .promise.then((parentPubsub) => (this._parentPubsub = parentPubsub))
60
- .catch((e: any) => {
61
- if (e.code !== ErrorCode.ConnectionTimeout) throw e;
62
-
63
- return this.connectToParentPubSub(retries - 1);
64
- });
65
- };
66
-
67
- private handleMessageFromParent = (topic: string, message: BitBaseEvent<any>) => {
68
- this.events.emit(topic, message);
69
- };
70
-
71
- static runtime = PreviewRuntime;
72
-
73
- static async provider(): Promise<PubsubPreview> {
74
- const pubsubPreview = new PubsubPreview();
75
-
76
- if (pubsubPreview.inIframe()) {
77
- pubsubPreview.connectToParentPubSub().catch((err) => {
78
- // parent window is not required to accept connections
79
- if (err instanceof PubSubNoParentError) return;
80
-
81
- // eslint-disable-next-line no-console
82
- console.error('[Pubsub.preview]', err);
83
- });
84
- }
85
-
86
- return pubsubPreview;
87
- }
88
- }
89
-
90
- PubsubAspect.addRuntime(PubsubPreview);
@@ -1,100 +0,0 @@
1
- import { UIRuntime, UIAspect, UiUI } from '@teambit/ui';
2
- import { EventEmitter2 } from 'eventemitter2';
3
- import { connectToChild } from 'penpal';
4
- import type { AsyncMethodReturns } from 'penpal/lib/types';
5
- import { BitBaseEvent } from './bit-base-event';
6
- import { PubsubAspect } from './pubsub.aspect';
7
- import { createProvider } from './pubsub-context';
8
- import { Callback } from './types';
9
-
10
- type PubOptions = {
11
- /** forward the event to adjacent windows (including the preview iframe) */
12
- propagate?: boolean;
13
- };
14
-
15
- type ChildMethods = {
16
- pub: (topic: string, event: BitBaseEvent<any>) => any;
17
- };
18
- export class PubsubUI {
19
- private childApi?: AsyncMethodReturns<ChildMethods>;
20
- private events = new EventEmitter2();
21
-
22
- /**
23
- * subscribe to events
24
- */
25
- public sub = (topic: string, callback: Callback) => {
26
- const events = this.events;
27
- events.on(topic, callback);
28
-
29
- const unSub = () => {
30
- events.off(topic, callback);
31
- };
32
-
33
- return unSub;
34
- };
35
-
36
- /**
37
- * publish event to all subscribers, including nested iframes.
38
- */
39
- public pub = (topic: string, event: BitBaseEvent<any>, { propagate }: PubOptions = {}) => {
40
- this.emitEvent(topic, event);
41
-
42
- // opt-in to forward to iframe, as we would not want 'private' messages automatically passing to iframe
43
- if (propagate) {
44
- this.pubToChild(topic, event);
45
- }
46
- };
47
-
48
- private connectToIframe = (iframe: HTMLIFrameElement) => {
49
- const connection = connectToChild<ChildMethods>({
50
- iframe,
51
- methods: {
52
- pub: this.emitEvent,
53
- },
54
- });
55
-
56
- // absorb valid errors like 'connection destroyed'
57
- connection.promise
58
- .then((childConnection) => (this.childApi = childConnection))
59
- .catch(() => {
60
- // eslint-disable-next-line no-console
61
- console.error('[Pubsub.ui]', 'failed connecting to child iframe');
62
- });
63
-
64
- const destroy = () => {
65
- connection && connection.destroy();
66
- };
67
- return destroy;
68
- };
69
-
70
- /**
71
- * publish event to all subscribers in this window
72
- */
73
- private emitEvent = (topic: string, event: BitBaseEvent<any>) => {
74
- this.events.emit(topic, event);
75
- };
76
-
77
- /**
78
- * publish event to nested iframes
79
- */
80
- private pubToChild = (topic: string, event: BitBaseEvent<any>) => {
81
- return this.childApi?.pub(topic, event);
82
- };
83
-
84
- static runtime = UIRuntime;
85
- static dependencies = [UIAspect];
86
-
87
- static async provider([uiUI]: [UiUI]) {
88
- const pubsubUI = new PubsubUI();
89
-
90
- const reactContext = createProvider({
91
- connect: pubsubUI.connectToIframe,
92
- });
93
-
94
- uiUI.registerRenderHooks({ reactContext });
95
-
96
- return pubsubUI;
97
- }
98
- }
99
-
100
- PubsubAspect.addRuntime(PubsubUI);
package/tsconfig.json DELETED
@@ -1,35 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "lib": [
4
- "es2019",
5
- "DOM",
6
- "ES6",
7
- "DOM.Iterable",
8
- "ScriptHost"
9
- ],
10
- "target": "es2015",
11
- "module": "commonjs",
12
- "jsx": "react",
13
- "declaration": true,
14
- "sourceMap": true,
15
- "skipLibCheck": true,
16
- "moduleResolution": "node",
17
- "esModuleInterop": true,
18
- "outDir": "dist",
19
- "composite": true,
20
- "emitDeclarationOnly": true,
21
- "experimentalDecorators": true,
22
- "emitDecoratorMetadata": true,
23
- "allowSyntheticDefaultImports": true,
24
- "strictPropertyInitialization": false,
25
- "strict": true,
26
- "noImplicitAny": false,
27
- "rootDir": ".",
28
- "removeComments": true,
29
- "preserveConstEnums": true,
30
- "resolveJsonModule": true
31
- },
32
- "exclude": [
33
- "dist"
34
- ]
35
- }
package/types/asset.d.ts DELETED
@@ -1,29 +0,0 @@
1
- declare module '*.png' {
2
- const value: any;
3
- export = value;
4
- }
5
- declare module '*.svg' {
6
- import type { FunctionComponent, SVGProps } from 'react';
7
-
8
- export const ReactComponent: FunctionComponent<SVGProps<SVGSVGElement> & { title?: string }>;
9
- const src: string;
10
- export default src;
11
- }
12
-
13
- // @TODO Gilad
14
- declare module '*.jpg' {
15
- const value: any;
16
- export = value;
17
- }
18
- declare module '*.jpeg' {
19
- const value: any;
20
- export = value;
21
- }
22
- declare module '*.gif' {
23
- const value: any;
24
- export = value;
25
- }
26
- declare module '*.bmp' {
27
- const value: any;
28
- export = value;
29
- }
package/types/style.d.ts DELETED
@@ -1,42 +0,0 @@
1
- declare module '*.module.css' {
2
- const classes: { readonly [key: string]: string };
3
- export default classes;
4
- }
5
- declare module '*.module.scss' {
6
- const classes: { readonly [key: string]: string };
7
- export default classes;
8
- }
9
- declare module '*.module.sass' {
10
- const classes: { readonly [key: string]: string };
11
- export default classes;
12
- }
13
-
14
- declare module '*.module.less' {
15
- const classes: { readonly [key: string]: string };
16
- export default classes;
17
- }
18
-
19
- declare module '*.less' {
20
- const classes: { readonly [key: string]: string };
21
- export default classes;
22
- }
23
-
24
- declare module '*.css' {
25
- const classes: { readonly [key: string]: string };
26
- export default classes;
27
- }
28
-
29
- declare module '*.sass' {
30
- const classes: { readonly [key: string]: string };
31
- export default classes;
32
- }
33
-
34
- declare module '*.scss' {
35
- const classes: { readonly [key: string]: string };
36
- export default classes;
37
- }
38
-
39
- declare module '*.mdx' {
40
- const component: any;
41
- export default component;
42
- }
package/types.ts DELETED
@@ -1,3 +0,0 @@
1
- import { BitBaseEvent } from './bit-base-event';
2
-
3
- export type Callback = (event: BitBaseEvent<any>) => void;