diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d3756ee..fdf7ea0 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -27,6 +27,12 @@ jobs: - name: Install dependencies run: npm install + - name: Build project + run: npm run build + + - name: Generate docs + run: npm run docs + - name: Run semantic-release env: GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} diff --git a/.gitignore b/.gitignore index 878b001..3b0b403 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ pnpm-debug.log* lerna-debug.log* node_modules +dist dist-ssr *.local diff --git a/README.md b/README.md index ae1233d..52dafc8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # @rob-long/app-bridge -**Version**: 0.0.4 **Description**: App bridge for Angular and React ## Overview @@ -16,7 +15,7 @@ npm install @rob-long/app-bridge ## Features - **State Management**: Utilize RxJS `BehaviorSubject` for managing state. -- **Framework Integration**: Seamless integration with Angular and React. +- **Framework Integration**: Seamless integration with Angular and React; additional framework support can be added. - **TypeScript Support**: Fully typed with TypeScript for better developer experience. ## Usage diff --git a/dist/index.d.ts b/dist/index.d.ts index 6b7eb3d..81e3274 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,6 +1,22 @@ import { BehaviorSubject, Subscription } from 'rxjs'; import angular from 'angular'; +/** + * Interface representing a collection of BehaviorSubject instances. + * + * @template T - The type of the values managed by the BehaviorSubjects. + */ +type SubjectEntries = { + [K in SubjectKey]: BehaviorSubject; +}; +/** + * Interface extending the Window object to include a subject manager. + * + * @template T - The type of the values managed by the subject manager. + */ +interface WindowWithSubjectManager extends Window { + _subjectManager: SubjectEntries; +} /** * Options for configuring the AppBridge instance. */ @@ -14,6 +30,7 @@ interface AppBridgeOptions { * Type alias for the keys of a given type T. */ type SubjectKey = keyof T & string; + /** * AppBridge class provides a bridge for state management using RxJS BehaviorSubjects. * It manages a collection of BehaviorSubject instances, allowing for state updates, @@ -164,4 +181,4 @@ declare function createAppBridgeService(applicationName: string): angular.IMo */ declare const useAppBridge: (subjectName: SubjectKey) => readonly [T[SubjectKey] | null, (newState: T[SubjectKey]) => void]; -export { createAppBridgeService as appBridgeService, createAppBridge, useAppBridge }; +export { type AppBridgeOptions, type SubjectEntries, type SubjectKey, type WindowWithSubjectManager, createAppBridgeService as appBridgeService, createAppBridge, useAppBridge }; diff --git a/dist/index.js.map b/dist/index.js.map index cc4457f..794701d 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sources":["../src/AppBridge.ts","../src/AppBridgeService.ts","../src/useAppBridge.ts"],"sourcesContent":["import { BehaviorSubject, Subscription } from 'rxjs';\n\n/**\n * Interface representing a collection of BehaviorSubject instances.\n *\n * @template T - The type of the values managed by the BehaviorSubjects.\n */\nexport type SubjectEntries = {\n [K in SubjectKey]: BehaviorSubject;\n};\n\n/**\n * Interface extending the Window object to include a subject manager.\n *\n * @template T - The type of the values managed by the subject manager.\n */\nexport interface WindowWithSubjectManager extends Window {\n _subjectManager: SubjectEntries;\n}\n\n/**\n * Options for configuring the AppBridge instance.\n */\nexport interface AppBridgeOptions {\n /**\n * If true, resets the AppBridge instance.\n */\n reset?: boolean;\n}\n\ndeclare const window: WindowWithSubjectManager;\n\n/**\n * Type alias for the keys of a given type T.\n */\nexport type SubjectKey = keyof T & string;\n\n/**\n * AppBridge class provides a bridge for state management using RxJS BehaviorSubjects.\n * It manages a collection of BehaviorSubject instances, allowing for state updates,\n * retrievals, and subscriptions.\n *\n * @template T - The type of the subjects managed by AppBridge.\n */\nexport class AppBridge {\n private static instance: AppBridge;\n\n /**\n * Private constructor to enforce the singleton pattern.\n */\n private constructor() {\n if (!window._subjectManager) {\n window._subjectManager = {} as SubjectEntries;\n }\n }\n\n /**\n * Retrieves the singleton instance of the AppBridge.\n * If the instance does not exist or the reset option is provided, a new instance is created.\n *\n * @template T - The type of the subjects managed by AppBridge.\n * @param options - Options to configure the instance.\n * @returns The singleton instance of AppBridge.\n */\n public static getInstance(options: AppBridgeOptions = {}): AppBridge {\n if (!AppBridge.instance || options.reset) {\n AppBridge.instance = new AppBridge();\n }\n return AppBridge.instance;\n }\n\n /**\n * Clears all BehaviorSubjects from the subject manager, completing and removing them.\n *\n * @example\n * ```typescript\n * const appBridge = AppBridge.getInstance();\n * appBridge.clearAllSubjects();\n * ```\n */\n public clearAllSubjects(): void {\n for (const subjectKey in window._subjectManager) {\n if (window._subjectManager.hasOwnProperty(subjectKey)) {\n window._subjectManager[subjectKey].complete();\n delete window._subjectManager[subjectKey];\n }\n }\n }\n\n /**\n * Retrieves a BehaviorSubject by its name, creating it if it does not exist.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @returns The BehaviorSubject instance.\n *\n * @example\n * ```typescript\n * const subject = appBridge.getSubject('mySubject');\n * subject.subscribe(value => console.log(value));\n * ```\n */\n public getSubject>(\n name: K,\n ): BehaviorSubject {\n if (!window._subjectManager[name]) {\n window._subjectManager[name] = new BehaviorSubject(null);\n }\n return window._subjectManager[name];\n }\n\n /**\n * Updates the value of a BehaviorSubject by its name, creating it if it does not exist.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @param newState - The new state to update.\n *\n * @example\n * ```typescript\n * appBridge.updateSubject('mySubject', { key: 'value' });\n * ```\n */\n public updateSubject>(name: K, newState: T[K]): void {\n this.getSubject(name).next(newState);\n }\n\n /**\n * Emits an error in the BehaviorSubject by its name.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @param error - The error to emit.\n *\n * @example\n * ```typescript\n * appBridge.errorSubject('mySubject', new Error('Something went wrong'));\n * ```\n */\n public errorSubject>(name: K, error: any): void {\n this.getSubject(name).error(error);\n }\n\n /**\n * Retrieves the current value of a BehaviorSubject by its name.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @returns The current value of the subject.\n *\n * @example\n * ```typescript\n * const currentValue = appBridge.getValue('mySubject');\n * console.log(currentValue);\n * ```\n */\n public getValue>(name: K): T[K] | null {\n return this.getSubject(name).getValue();\n }\n\n /**\n * Subscribes to a BehaviorSubject by its name with an observer.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @param observer - The observer object with next, error, and complete callbacks.\n * @returns The subscription to the subject.\n *\n * @example\n * ```typescript\n * const subscription = appBridge.subscribe('mySubject', {\n * next: value => console.log(value),\n * error: error => console.error(error),\n * complete: () => console.log('Completed')\n * });\n * ```\n */\n public subscribe>(\n name: K,\n observer: {\n next?: (value: T[K] | null) => void;\n error?: (error: any) => void;\n complete?: () => void;\n },\n ): Subscription {\n return this.getSubject(name).subscribe(observer);\n }\n}\n\n/**\n * Factory function for creating or retrieving the singleton instance of the AppBridge class.\n *\n * @template T - The type of the subjects managed by AppBridge.\n * @param options - Options to configure the instance.\n * @returns The singleton instance of AppBridge.\n *\n * @example\n * ```typescript\n * const appBridge = createAppBridge({ reset: true });\n * ```\n */\nexport function createAppBridge(options: AppBridgeOptions = {}) {\n return AppBridge.getInstance(options);\n}\n","import angular from 'angular';\nimport { createAppBridge, SubjectKey } from './AppBridge.ts';\n\n/**\n * Function to create a generic AppBridgeService for Angular.\n *\n * @returns The Angular module with the AppBridgeService factory.\n */\nexport function createAppBridgeService(applicationName: string) {\n return angular.module(applicationName, []).factory('AppBridgeService', [\n '$rootScope',\n function ($rootScope: angular.IRootScopeService) {\n const appBridge = createAppBridge();\n return {\n getSubject: >(name: K) =>\n appBridge.getSubject(name),\n getValue: >(name: K) =>\n appBridge.getValue(name),\n updateSubject: >(name: K, newState: T[K]) =>\n appBridge.updateSubject(name, newState),\n subscribe: >(\n name: K,\n next: (newState: T[K] | null) => void,\n ) => {\n const subscription = appBridge.subscribe(name, {\n next: (newState) => {\n $rootScope.$apply(() => {\n next(newState);\n });\n },\n });\n return () => subscription.unsubscribe();\n },\n };\n },\n ]);\n}\n\nexport default createAppBridgeService;\n","import { useEffect, useState } from 'react';\nimport { createAppBridge } from './AppBridge.ts';\nimport type { SubjectKey } from './AppBridge.ts';\n\n/**\n * Custom hook that subscribes to a subject from the AppBridge and provides state management.\n *\n * @template T - The type of the subjects managed by AppBridge.\n * @param subjectName - The name of the subject to subscribe to.\n * @returns A tuple containing the current state of the subject and a function to update the state.\n *\n * @example\n * ```typescript\n * const [state, updateState] = useAppBridge('mySubject');\n *\n * // Use the state\n * console.log(state);\n *\n * // Update the state\n * updateState({ key: 'value' });\n * ```\n */\nconst useAppBridge = (subjectName: SubjectKey) => {\n const appBridge = createAppBridge();\n const [state, setState] = useState(appBridge.getValue(subjectName));\n\n useEffect(() => {\n const subscription = appBridge.subscribe(subjectName, {\n next: (newState) => {\n setState(newState);\n },\n });\n\n return () => {\n subscription.unsubscribe();\n };\n }, [subjectName]);\n\n /**\n * Updates the state of the subject.\n *\n * @param newState - The new state to set.\n */\n const updateState = (newState: T[SubjectKey]) => {\n appBridge.updateSubject(subjectName, newState);\n };\n\n return [state, updateState] as const;\n};\n\nexport default useAppBridge;\n"],"names":["BehaviorSubject","useState","useEffect"],"mappings":";;;;;;;;;AA4CO,MAAM,UAAA,GAAN,MAAM,UAAa,CAAA;AAAA;AAAA;AAAA;AAAA,EAMhB,WAAc,GAAA;AACpB,IAAI,IAAA,CAAC,OAAO,eAAiB,EAAA;AAC3B,MAAA,MAAA,CAAO,kBAAkB,EAAC,CAAA;AAAA,KAC5B;AAAA,GACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,OAAc,WAAA,CAAe,OAA4B,GAAA,EAAkB,EAAA;AACzE,IAAA,IAAI,CAAC,UAAA,CAAU,QAAY,IAAA,OAAA,CAAQ,KAAO,EAAA;AACxC,MAAU,UAAA,CAAA,QAAA,GAAW,IAAI,UAAa,EAAA,CAAA;AAAA,KACxC;AACA,IAAA,OAAO,UAAU,CAAA,QAAA,CAAA;AAAA,GACnB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWO,gBAAyB,GAAA;AAC9B,IAAW,KAAA,M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A,CAAA;AACD,UAAO,OAAA,MAAM,aAAa,WAAY,EAAA,CAAA;AAAA,SACxC;AAAA,OACF,CAAA;AAAA,KACF;AAAA,GACD,CAAA,CAAA;AACH;;ACdM,MAAA,YAAA,GAAe,CAAI,WAA+B,KAAA;AACtD,EAAA,MAAM,YAAY,eAAmB,EAAA,CAAA;AACrC,EAAM,MAAA,CAAC,OAAO,QAAQ,CAAA,GAAIC,eAAS,SAAU,CAAA,QAAA,CAAS,WAAW,CAAC,CAAA,CAAA;AAElE,EAAAC,eAAA,CAAU,MAAM;AACd,IAAM,MAAA,YAAA,GAAe,SAAU,CAAA,SAAA,CAAU,WAAa,EAAA;AAAA,MACpD,IAAA,EAAM,CAAC,QAAa,KAAA;AAClB,QAAA,QAAA,CAAS,QAAQ,CAAA,CAAA;AAAA,OACnB;AAAA,KACD,CAAA,CAAA;AAED,IAAA,OAAO,MAAM;AACX,MAAA,YAAA,CAAa,WAAY,EAAA,CAAA;AAAA,KAC3B,CAAA;AAAA,GACF,EAAG,CAAC,WAAW,CAAC,CAAA,CAAA;AAOhB,EAAM,MAAA,WAAA,GAAc,CAAC,QAA+B,KAAA;AAClD,IAAU,SAAA,CAAA,aAAA,CAAc,aAAa,QAAQ,CAAA,CAAA;AAAA,GAC/C,CAAA;AAEA,EAAO,OAAA,CAAC,OAAO,WAAW,CAAA,CAAA;AAC5B;;;;;;"} \ No newline at end of file +{"version":3,"file":"index.js","sources":["../src/AppBridge.ts","../src/AppBridgeService.ts","../src/useAppBridge.ts"],"sourcesContent":["import { BehaviorSubject, Subscription } from 'rxjs';\nimport {\n WindowWithSubjectManager,\n SubjectEntries,\n AppBridgeOptions,\n SubjectKey,\n} from './types.ts';\n\ndeclare const window: WindowWithSubjectManager;\n\n/**\n * AppBridge class provides a bridge for state management using RxJS BehaviorSubjects.\n * It manages a collection of BehaviorSubject instances, allowing for state updates,\n * retrievals, and subscriptions.\n *\n * @template T - The type of the subjects managed by AppBridge.\n */\nexport class AppBridge {\n private static instance: AppBridge;\n\n /**\n * Private constructor to enforce the singleton pattern.\n */\n private constructor() {\n if (!window._subjectManager) {\n window._subjectManager = {} as SubjectEntries;\n }\n }\n\n /**\n * Retrieves the singleton instance of the AppBridge.\n * If the instance does not exist or the reset option is provided, a new instance is created.\n *\n * @template T - The type of the subjects managed by AppBridge.\n * @param options - Options to configure the instance.\n * @returns The singleton instance of AppBridge.\n */\n public static getInstance(options: AppBridgeOptions = {}): AppBridge {\n if (!AppBridge.instance || options.reset) {\n AppBridge.instance = new AppBridge();\n }\n return AppBridge.instance;\n }\n\n /**\n * Clears all BehaviorSubjects from the subject manager, completing and removing them.\n *\n * @example\n * ```typescript\n * const appBridge = AppBridge.getInstance();\n * appBridge.clearAllSubjects();\n * ```\n */\n public clearAllSubjects(): void {\n for (const subjectKey in window._subjectManager) {\n if (window._subjectManager.hasOwnProperty(subjectKey)) {\n window._subjectManager[subjectKey].complete();\n delete window._subjectManager[subjectKey];\n }\n }\n }\n\n /**\n * Retrieves a BehaviorSubject by its name, creating it if it does not exist.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @returns The BehaviorSubject instance.\n *\n * @example\n * ```typescript\n * const subject = appBridge.getSubject('mySubject');\n * subject.subscribe(value => console.log(value));\n * ```\n */\n public getSubject>(\n name: K,\n ): BehaviorSubject {\n if (!window._subjectManager[name]) {\n window._subjectManager[name] = new BehaviorSubject(null);\n }\n return window._subjectManager[name];\n }\n\n /**\n * Updates the value of a BehaviorSubject by its name, creating it if it does not exist.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @param newState - The new state to update.\n *\n * @example\n * ```typescript\n * appBridge.updateSubject('mySubject', { key: 'value' });\n * ```\n */\n public updateSubject>(name: K, newState: T[K]): void {\n this.getSubject(name).next(newState);\n }\n\n /**\n * Emits an error in the BehaviorSubject by its name.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @param error - The error to emit.\n *\n * @example\n * ```typescript\n * appBridge.errorSubject('mySubject', new Error('Something went wrong'));\n * ```\n */\n public errorSubject>(name: K, error: any): void {\n this.getSubject(name).error(error);\n }\n\n /**\n * Retrieves the current value of a BehaviorSubject by its name.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @returns The current value of the subject.\n *\n * @example\n * ```typescript\n * const currentValue = appBridge.getValue('mySubject');\n * console.log(currentValue);\n * ```\n */\n public getValue>(name: K): T[K] | null {\n return this.getSubject(name).getValue();\n }\n\n /**\n * Subscribes to a BehaviorSubject by its name with an observer.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @param observer - The observer object with next, error, and complete callbacks.\n * @returns The subscription to the subject.\n *\n * @example\n * ```typescript\n * const subscription = appBridge.subscribe('mySubject', {\n * next: value => console.log(value),\n * error: error => console.error(error),\n * complete: () => console.log('Completed')\n * });\n * ```\n */\n public subscribe>(\n name: K,\n observer: {\n next?: (value: T[K] | null) => void;\n error?: (error: any) => void;\n complete?: () => void;\n },\n ): Subscription {\n return this.getSubject(name).subscribe(observer);\n }\n}\n\n/**\n * Factory function for creating or retrieving the singleton instance of the AppBridge class.\n *\n * @template T - The type of the subjects managed by AppBridge.\n * @param options - Options to configure the instance.\n * @returns The singleton instance of AppBridge.\n *\n * @example\n * ```typescript\n * const appBridge = createAppBridge({ reset: true });\n * ```\n */\nexport function createAppBridge(options: AppBridgeOptions = {}) {\n return AppBridge.getInstance(options);\n}\n","import angular from 'angular';\nimport { createAppBridge } from './AppBridge.ts';\nimport { SubjectKey } from './types.ts';\n\n/**\n * Function to create a generic AppBridgeService for Angular.\n *\n * @returns The Angular module with the AppBridgeService factory.\n */\nexport function createAppBridgeService(applicationName: string) {\n return angular.module(applicationName, []).factory('AppBridgeService', [\n '$rootScope',\n function ($rootScope: angular.IRootScopeService) {\n const appBridge = createAppBridge();\n return {\n getSubject: >(name: K) =>\n appBridge.getSubject(name),\n getValue: >(name: K) =>\n appBridge.getValue(name),\n updateSubject: >(name: K, newState: T[K]) =>\n appBridge.updateSubject(name, newState),\n subscribe: >(\n name: K,\n next: (newState: T[K] | null) => void,\n ) => {\n const subscription = appBridge.subscribe(name, {\n next: (newState) => {\n $rootScope.$apply(() => {\n next(newState);\n });\n },\n });\n return () => subscription.unsubscribe();\n },\n };\n },\n ]);\n}\n\nexport default createAppBridgeService;\n","import { useEffect, useState } from 'react';\nimport { createAppBridge } from './AppBridge.ts';\nimport type { SubjectKey } from './types.ts';\n\n/**\n * Custom hook that subscribes to a subject from the AppBridge and provides state management.\n *\n * @template T - The type of the subjects managed by AppBridge.\n * @param subjectName - The name of the subject to subscribe to.\n * @returns A tuple containing the current state of the subject and a function to update the state.\n *\n * @example\n * ```typescript\n * const [state, updateState] = useAppBridge('mySubject');\n *\n * // Use the state\n * console.log(state);\n *\n * // Update the state\n * updateState({ key: 'value' });\n * ```\n */\nconst useAppBridge = (subjectName: SubjectKey) => {\n const appBridge = createAppBridge();\n const [state, setState] = useState(appBridge.getValue(subjectName));\n\n useEffect(() => {\n const subscription = appBridge.subscribe(subjectName, {\n next: (newState) => {\n setState(newState);\n },\n });\n\n return () => {\n subscription.unsubscribe();\n };\n }, [subjectName]);\n\n /**\n * Updates the state of the subject.\n *\n * @param newState - The new state to set.\n */\n const updateState = (newState: T[SubjectKey]) => {\n appBridge.updateSubject(subjectName, newState);\n };\n\n return [state, updateState] as const;\n};\n\nexport default useAppBridge;\n"],"names":["BehaviorSubject","useState","useEffect"],"mappings":";;;;;;;;;AAiBO,MAAM,UAAA,GAAN,MAAM,UAAa,CAAA;AAAA;AAAA;AAAA;AAAA,EAMhB,WAAc,GAAA;AACpB,IAAI,IAAA,CAAC,OAAO,eAAiB,EAAA;AAC3B,MAAA,MAAA,CAAO,kBAAkB,EAAC,CAAA;AAAA,KAC5B;AAAA,GACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,OAAc,WAAA,CAAe,OAA4B,GAAA,EAAkB,EAAA;AACzE,IAAA,IAAI,CAAC,UAAA,CAAU,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CACT,IAAA,EACA,IACG,KAAA;AACH,UAAM,MAAA,YAAA,GAAe,SAAU,CAAA,SAAA,CAAU,IAAM,EAAA;AAAA,YAC7C,IAAA,EAAM,CAAC,QAAa,KAAA;AAClB,cAAA,UAAA,CAAW,OAAO,MAAM;AACtB,gBAAA,IAAA,CAAK,QAAQ,CAAA,CAAA;AAAA,eACd,CAAA,CAAA;AAAA,aACH;AAAA,WACD,CAAA,CAAA;AACD,UAAO,OAAA,MAAM,aAAa,WAAY,EAAA,CAAA;AAAA,SACxC;AAAA,OACF,CAAA;AAAA,KACF;AAAA,GACD,CAAA,CAAA;AACH;;ACfM,MAAA,YAAA,GAAe,CAAI,WAA+B,KAAA;AACtD,EAAA,MAAM,YAAY,eAAmB,EAAA,CAAA;AACrC,EAAM,MAAA,CAAC,OAAO,QAAQ,CAAA,GAAIC,eAAS,SAAU,CAAA,QAAA,CAAS,WAAW,CAAC,CAAA,CAAA;AAElE,EAAAC,eAAA,CAAU,MAAM;AACd,IAAM,MAAA,YAAA,GAAe,SAAU,CAAA,SAAA,CAAU,WAAa,EAAA;AAAA,MACpD,IAAA,EAAM,CAAC,QAAa,KAAA;AAClB,QAAA,QAAA,CAAS,QAAQ,CAAA,CAAA;AAAA,OACnB;AAAA,KACD,CAAA,CAAA;AAED,IAAA,OAAO,MAAM;AACX,MAAA,YAAA,CAAa,WAAY,EAAA,CAAA;AAAA,KAC3B,CAAA;AAAA,GACF,EAAG,CAAC,WAAW,CAAC,CAAA,CAAA;AAOhB,EAAM,MAAA,WAAA,GAAc,CAAC,QAA+B,KAAA;AAClD,IAAU,SAAA,CAAA,aAAA,CAAc,aAAa,QAAQ,CAAA,CAAA;AAAA,GAC/C,CAAA;AAEA,EAAO,OAAA,CAAC,OAAO,WAAW,CAAA,CAAA;AAC5B;;;;;;"} \ No newline at end of file diff --git a/dist/index.mjs.map b/dist/index.mjs.map index c68e854..ec05944 100644 --- a/dist/index.mjs.map +++ b/dist/index.mjs.map @@ -1 +1 @@ -{"version":3,"file":"index.mjs","sources":["../src/AppBridge.ts","../src/AppBridgeService.ts","../src/useAppBridge.ts"],"sourcesContent":["import { BehaviorSubject, Subscription } from 'rxjs';\n\n/**\n * Interface representing a collection of BehaviorSubject instances.\n *\n * @template T - The type of the values managed by the BehaviorSubjects.\n */\nexport type SubjectEntries = {\n [K in SubjectKey]: BehaviorSubject;\n};\n\n/**\n * Interface extending the Window object to include a subject manager.\n *\n * @template T - The type of the values managed by the subject manager.\n */\nexport interface WindowWithSubjectManager extends Window {\n _subjectManager: SubjectEntries;\n}\n\n/**\n * Options for configuring the AppBridge instance.\n */\nexport interface AppBridgeOptions {\n /**\n * If true, resets the AppBridge instance.\n */\n reset?: boolean;\n}\n\ndeclare const window: WindowWithSubjectManager;\n\n/**\n * Type alias for the keys of a given type T.\n */\nexport type SubjectKey = keyof T & string;\n\n/**\n * AppBridge class provides a bridge for state management using RxJS BehaviorSubjects.\n * It manages a collection of BehaviorSubject instances, allowing for state updates,\n * retrievals, and subscriptions.\n *\n * @template T - The type of the subjects managed by AppBridge.\n */\nexport class AppBridge {\n private static instance: AppBridge;\n\n /**\n * Private constructor to enforce the singleton pattern.\n */\n private constructor() {\n if (!window._subjectManager) {\n window._subjectManager = {} as SubjectEntries;\n }\n }\n\n /**\n * Retrieves the singleton instance of the AppBridge.\n * If the instance does not exist or the reset option is provided, a new instance is created.\n *\n * @template T - The type of the subjects managed by AppBridge.\n * @param options - Options to configure the instance.\n * @returns The singleton instance of AppBridge.\n */\n public static getInstance(options: AppBridgeOptions = {}): AppBridge {\n if (!AppBridge.instance || options.reset) {\n AppBridge.instance = new AppBridge();\n }\n return AppBridge.instance;\n }\n\n /**\n * Clears all BehaviorSubjects from the subject manager, completing and removing them.\n *\n * @example\n * ```typescript\n * const appBridge = AppBridge.getInstance();\n * appBridge.clearAllSubjects();\n * ```\n */\n public clearAllSubjects(): void {\n for (const subjectKey in window._subjectManager) {\n if (window._subjectManager.hasOwnProperty(subjectKey)) {\n window._subjectManager[subjectKey].complete();\n delete window._subjectManager[subjectKey];\n }\n }\n }\n\n /**\n * Retrieves a BehaviorSubject by its name, creating it if it does not exist.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @returns The BehaviorSubject instance.\n *\n * @example\n * ```typescript\n * const subject = appBridge.getSubject('mySubject');\n * subject.subscribe(value => console.log(value));\n * ```\n */\n public getSubject>(\n name: K,\n ): BehaviorSubject {\n if (!window._subjectManager[name]) {\n window._subjectManager[name] = new BehaviorSubject(null);\n }\n return window._subjectManager[name];\n }\n\n /**\n * Updates the value of a BehaviorSubject by its name, creating it if it does not exist.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @param newState - The new state to update.\n *\n * @example\n * ```typescript\n * appBridge.updateSubject('mySubject', { key: 'value' });\n * ```\n */\n public updateSubject>(name: K, newState: T[K]): void {\n this.getSubject(name).next(newState);\n }\n\n /**\n * Emits an error in the BehaviorSubject by its name.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @param error - The error to emit.\n *\n * @example\n * ```typescript\n * appBridge.errorSubject('mySubject', new Error('Something went wrong'));\n * ```\n */\n public errorSubject>(name: K, error: any): void {\n this.getSubject(name).error(error);\n }\n\n /**\n * Retrieves the current value of a BehaviorSubject by its name.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @returns The current value of the subject.\n *\n * @example\n * ```typescript\n * const currentValue = appBridge.getValue('mySubject');\n * console.log(currentValue);\n * ```\n */\n public getValue>(name: K): T[K] | null {\n return this.getSubject(name).getValue();\n }\n\n /**\n * Subscribes to a BehaviorSubject by its name with an observer.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @param observer - The observer object with next, error, and complete callbacks.\n * @returns The subscription to the subject.\n *\n * @example\n * ```typescript\n * const subscription = appBridge.subscribe('mySubject', {\n * next: value => console.log(value),\n * error: error => console.error(error),\n * complete: () => console.log('Completed')\n * });\n * ```\n */\n public subscribe>(\n name: K,\n observer: {\n next?: (value: T[K] | null) => void;\n error?: (error: any) => void;\n complete?: () => void;\n },\n ): Subscription {\n return this.getSubject(name).subscribe(observer);\n }\n}\n\n/**\n * Factory function for creating or retrieving the singleton instance of the AppBridge class.\n *\n * @template T - The type of the subjects managed by AppBridge.\n * @param options - Options to configure the instance.\n * @returns The singleton instance of AppBridge.\n *\n * @example\n * ```typescript\n * const appBridge = createAppBridge({ reset: true });\n * ```\n */\nexport function createAppBridge(options: AppBridgeOptions = {}) {\n return AppBridge.getInstance(options);\n}\n","import angular from 'angular';\nimport { createAppBridge, SubjectKey } from './AppBridge.ts';\n\n/**\n * Function to create a generic AppBridgeService for Angular.\n *\n * @returns The Angular module with the AppBridgeService factory.\n */\nexport function createAppBridgeService(applicationName: string) {\n return angular.module(applicationName, []).factory('AppBridgeService', [\n '$rootScope',\n function ($rootScope: angular.IRootScopeService) {\n const appBridge = createAppBridge();\n return {\n getSubject: >(name: K) =>\n appBridge.getSubject(name),\n getValue: >(name: K) =>\n appBridge.getValue(name),\n updateSubject: >(name: K, newState: T[K]) =>\n appBridge.updateSubject(name, newState),\n subscribe: >(\n name: K,\n next: (newState: T[K] | null) => void,\n ) => {\n const subscription = appBridge.subscribe(name, {\n next: (newState) => {\n $rootScope.$apply(() => {\n next(newState);\n });\n },\n });\n return () => subscription.unsubscribe();\n },\n };\n },\n ]);\n}\n\nexport default createAppBridgeService;\n","import { useEffect, useState } from 'react';\nimport { createAppBridge } from './AppBridge.ts';\nimport type { SubjectKey } from './AppBridge.ts';\n\n/**\n * Custom hook that subscribes to a subject from the AppBridge and provides state management.\n *\n * @template T - The type of the subjects managed by AppBridge.\n * @param subjectName - The name of the subject to subscribe to.\n * @returns A tuple containing the current state of the subject and a function to update the state.\n *\n * @example\n * ```typescript\n * const [state, updateState] = useAppBridge('mySubject');\n *\n * // Use the state\n * console.log(state);\n *\n * // Update the state\n * updateState({ key: 'value' });\n * ```\n */\nconst useAppBridge = (subjectName: SubjectKey) => {\n const appBridge = createAppBridge();\n const [state, setState] = useState(appBridge.getValue(subjectName));\n\n useEffect(() => {\n const subscription = appBridge.subscribe(subjectName, {\n next: (newState) => {\n setState(newState);\n },\n });\n\n return () => {\n subscription.unsubscribe();\n };\n }, [subjectName]);\n\n /**\n * Updates the state of the subject.\n *\n * @param newState - The new state to set.\n */\n const updateState = (newState: T[SubjectKey]) => {\n appBridge.updateSubject(subjectName, newState);\n };\n\n return [state, updateState] as const;\n};\n\nexport default useAppBridge;\n"],"names":[],"mappings":";;;;;;;AA4CO,MAAM,UAAA,GAAN,MAAM,UAAa,CAAA;AAAA;AAAA;AAAA;AAAA,EAMhB,WAAc,GAAA;AACpB,IAAI,IAAA,CAAC,OAAO,eAAiB,EAAA;AAC3B,MAAA,MAAA,CAAO,kBAAkB,EAAC,CAAA;AAAA,KAC5B;AAAA,GACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,OAAc,WAAA,CAAe,OAA4B,GAAA,EAAkB,EAAA;AACzE,IAAA,IAAI,CAAC,UAAA,CAAU,QAAY,IAAA,OAAA,CAAQ,KAAO,EAAA;AACxC,MAAU,UAAA,CAAA,QAAA,GAAW,IAAI,UAAa,EAAA,CAAA;AAAA,KACxC;AACA,IAAA,OAAO,UAAU,CAAA,QAAA,CAAA;AAAA,GACnB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWO,gBAAyB,GAAA;AAC9B,IAAW,KAAA,MAAA,UAAA,IAAc,OAAO,eAAiB,EAAA;AAC/C,MAAA,IAAI,MAAO,CAAA,eAAA,CAAgB,cAAe,CAAA,UAAU,CAAG,EAAA;AACrD,QAAO,MAAA,CAAA,eAAA,CAAgB,UAAU,CAAA,CAAE,QAAS,EAAA,CAAA;AAC5C,QAAO,OAAA,MAAA,CAAO,gBAAgB,UAAU,CAAA,CAAA;AAAA,OAC1C;AAAA,KACF;AAAA,GACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAA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A,SAAA,CAAU,MAAM;AACd,IAAM,MAAA,YAAA,GAAe,SAAU,CAAA,SAAA,CAAU,WAAa,EAAA;AAAA,MACpD,IAAA,EAAM,CAAC,QAAa,KAAA;AAClB,QAAA,QAAA,CAAS,QAAQ,CAAA,CAAA;AAAA,OACnB;AAAA,KACD,CAAA,CAAA;AAED,IAAA,OAAO,MAAM;AACX,MAAA,YAAA,CAAa,WAAY,EAAA,CAAA;AAAA,KAC3B,CAAA;AAAA,GACF,EAAG,CAAC,WAAW,CAAC,CAAA,CAAA;AAOhB,EAAM,MAAA,WAAA,GAAc,CAAC,QAA+B,KAAA;AAClD,IAAU,SAAA,CAAA,aAAA,CAAc,aAAa,QAAQ,CAAA,CAAA;AAAA,GAC/C,CAAA;AAEA,EAAO,OAAA,CAAC,OAAO,WAAW,CAAA,CAAA;AAC5B;;;;"} \ No newline at end of file +{"version":3,"file":"index.mjs","sources":["../src/AppBridge.ts","../src/AppBridgeService.ts","../src/useAppBridge.ts"],"sourcesContent":["import { BehaviorSubject, Subscription } from 'rxjs';\nimport {\n WindowWithSubjectManager,\n SubjectEntries,\n AppBridgeOptions,\n SubjectKey,\n} from './types.ts';\n\ndeclare const window: WindowWithSubjectManager;\n\n/**\n * AppBridge class provides a bridge for state management using RxJS BehaviorSubjects.\n * It manages a collection of BehaviorSubject instances, allowing for state updates,\n * retrievals, and subscriptions.\n *\n * @template T - The type of the subjects managed by AppBridge.\n */\nexport class AppBridge {\n private static instance: AppBridge;\n\n /**\n * Private constructor to enforce the singleton pattern.\n */\n private constructor() {\n if (!window._subjectManager) {\n window._subjectManager = {} as SubjectEntries;\n }\n }\n\n /**\n * Retrieves the singleton instance of the AppBridge.\n * If the instance does not exist or the reset option is provided, a new instance is created.\n *\n * @template T - The type of the subjects managed by AppBridge.\n * @param options - Options to configure the instance.\n * @returns The singleton instance of AppBridge.\n */\n public static getInstance(options: AppBridgeOptions = {}): AppBridge {\n if (!AppBridge.instance || options.reset) {\n AppBridge.instance = new AppBridge();\n }\n return AppBridge.instance;\n }\n\n /**\n * Clears all BehaviorSubjects from the subject manager, completing and removing them.\n *\n * @example\n * ```typescript\n * const appBridge = AppBridge.getInstance();\n * appBridge.clearAllSubjects();\n * ```\n */\n public clearAllSubjects(): void {\n for (const subjectKey in window._subjectManager) {\n if (window._subjectManager.hasOwnProperty(subjectKey)) {\n window._subjectManager[subjectKey].complete();\n delete window._subjectManager[subjectKey];\n }\n }\n }\n\n /**\n * Retrieves a BehaviorSubject by its name, creating it if it does not exist.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @returns The BehaviorSubject instance.\n *\n * @example\n * ```typescript\n * const subject = appBridge.getSubject('mySubject');\n * subject.subscribe(value => console.log(value));\n * ```\n */\n public getSubject>(\n name: K,\n ): BehaviorSubject {\n if (!window._subjectManager[name]) {\n window._subjectManager[name] = new BehaviorSubject(null);\n }\n return window._subjectManager[name];\n }\n\n /**\n * Updates the value of a BehaviorSubject by its name, creating it if it does not exist.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @param newState - The new state to update.\n *\n * @example\n * ```typescript\n * appBridge.updateSubject('mySubject', { key: 'value' });\n * ```\n */\n public updateSubject>(name: K, newState: T[K]): void {\n this.getSubject(name).next(newState);\n }\n\n /**\n * Emits an error in the BehaviorSubject by its name.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @param error - The error to emit.\n *\n * @example\n * ```typescript\n * appBridge.errorSubject('mySubject', new Error('Something went wrong'));\n * ```\n */\n public errorSubject>(name: K, error: any): void {\n this.getSubject(name).error(error);\n }\n\n /**\n * Retrieves the current value of a BehaviorSubject by its name.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @returns The current value of the subject.\n *\n * @example\n * ```typescript\n * const currentValue = appBridge.getValue('mySubject');\n * console.log(currentValue);\n * ```\n */\n public getValue>(name: K): T[K] | null {\n return this.getSubject(name).getValue();\n }\n\n /**\n * Subscribes to a BehaviorSubject by its name with an observer.\n *\n * @template K - The key type of the subject.\n * @param name - The name of the subject.\n * @param observer - The observer object with next, error, and complete callbacks.\n * @returns The subscription to the subject.\n *\n * @example\n * ```typescript\n * const subscription = appBridge.subscribe('mySubject', {\n * next: value => console.log(value),\n * error: error => console.error(error),\n * complete: () => console.log('Completed')\n * });\n * ```\n */\n public subscribe>(\n name: K,\n observer: {\n next?: (value: T[K] | null) => void;\n error?: (error: any) => void;\n complete?: () => void;\n },\n ): Subscription {\n return this.getSubject(name).subscribe(observer);\n }\n}\n\n/**\n * Factory function for creating or retrieving the singleton instance of the AppBridge class.\n *\n * @template T - The type of the subjects managed by AppBridge.\n * @param options - Options to configure the instance.\n * @returns The singleton instance of AppBridge.\n *\n * @example\n * ```typescript\n * const appBridge = createAppBridge({ reset: true });\n * ```\n */\nexport function createAppBridge(options: AppBridgeOptions = {}) {\n return AppBridge.getInstance(options);\n}\n","import angular from 'angular';\nimport { createAppBridge } from './AppBridge.ts';\nimport { SubjectKey } from './types.ts';\n\n/**\n * Function to create a generic AppBridgeService for Angular.\n *\n * @returns The Angular module with the AppBridgeService factory.\n */\nexport function createAppBridgeService(applicationName: string) {\n return angular.module(applicationName, []).factory('AppBridgeService', [\n '$rootScope',\n function ($rootScope: angular.IRootScopeService) {\n const appBridge = createAppBridge();\n return {\n getSubject: >(name: K) =>\n appBridge.getSubject(name),\n getValue: >(name: K) =>\n appBridge.getValue(name),\n updateSubject: >(name: K, newState: T[K]) =>\n appBridge.updateSubject(name, newState),\n subscribe: >(\n name: K,\n next: (newState: T[K] | null) => void,\n ) => {\n const subscription = appBridge.subscribe(name, {\n next: (newState) => {\n $rootScope.$apply(() => {\n next(newState);\n });\n },\n });\n return () => subscription.unsubscribe();\n },\n };\n },\n ]);\n}\n\nexport default createAppBridgeService;\n","import { useEffect, useState } from 'react';\nimport { createAppBridge } from './AppBridge.ts';\nimport type { SubjectKey } from './types.ts';\n\n/**\n * Custom hook that subscribes to a subject from the AppBridge and provides state management.\n *\n * @template T - The type of the subjects managed by AppBridge.\n * @param subjectName - The name of the subject to subscribe to.\n * @returns A tuple containing the current state of the subject and a function to update the state.\n *\n * @example\n * ```typescript\n * const [state, updateState] = useAppBridge('mySubject');\n *\n * // Use the state\n * console.log(state);\n *\n * // Update the state\n * updateState({ key: 'value' });\n * ```\n */\nconst useAppBridge = (subjectName: SubjectKey) => {\n const appBridge = createAppBridge();\n const [state, setState] = useState(appBridge.getValue(subjectName));\n\n useEffect(() => {\n const subscription = appBridge.subscribe(subjectName, {\n next: (newState) => {\n setState(newState);\n },\n });\n\n return () => {\n subscription.unsubscribe();\n };\n }, [subjectName]);\n\n /**\n * Updates the state of the subject.\n *\n * @param newState - The new state to set.\n */\n const updateState = (newState: T[SubjectKey]) => {\n appBridge.updateSubject(subjectName, newState);\n };\n\n return [state, updateState] as const;\n};\n\nexport default useAppBridge;\n"],"names":[],"mappings":";;;;;;;AAiBO,MAAM,UAAA,GAAN,MAAM,UAAa,CAAA;AAAA;AAAA;AAAA;AAAA,EAMhB,WAAc,GAAA;AACpB,IAAI,IAAA,CAAC,OAAO,eAAiB,EAAA;AAC3B,MAAA,MAAA,CAAO,kBAAkB,EAAC,CAAA;AAAA,KAC5B;AAAA,GACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,OAAc,WAAA,CAAe,OAA4B,GAAA,EAAkB,EAAA;AACzE,IAAA,IAAI,CAAC,UAAA,CAAU,QAAY,IAAA,OAAA,CAAQ,KAAO,EAAA;AACxC,MAAU,UAAA,CAAA,QAAA,GAAW,IAAI,UAAa,EAAA,CAAA;AAAA,KACxC;AACA,IAAA,OAAO,UAAU,CAAA,QAAA,CAAA;AAAA,GACnB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWO,gBAAyB,GAAA;AAC9B,IAAW,KAAA,MAAA,UAAA,IAAc,OAAO,eAAiB,EAAA;AAC/C,MAAA,IAAI,MAAO,CAAA,eAAA,CAAgB,cAAe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AA,KACF;AAAA,GACD,CAAA,CAAA;AACH;;ACfM,MAAA,YAAA,GAAe,CAAI,WAA+B,KAAA;AACtD,EAAA,MAAM,YAAY,eAAmB,EAAA,CAAA;AACrC,EAAM,MAAA,CAAC,OAAO,QAAQ,CAAA,GAAI,SAAS,SAAU,CAAA,QAAA,CAAS,WAAW,CAAC,CAAA,CAAA;AAElE,EAAA,SAAA,CAAU,MAAM;AACd,IAAM,MAAA,YAAA,GAAe,SAAU,CAAA,SAAA,CAAU,WAAa,EAAA;AAAA,MACpD,IAAA,EAAM,CAAC,QAAa,KAAA;AAClB,QAAA,QAAA,CAAS,QAAQ,CAAA,CAAA;AAAA,OACnB;AAAA,KACD,CAAA,CAAA;AAED,IAAA,OAAO,MAAM;AACX,MAAA,YAAA,CAAa,WAAY,EAAA,CAAA;AAAA,KAC3B,CAAA;AAAA,GACF,EAAG,CAAC,WAAW,CAAC,CAAA,CAAA;AAOhB,EAAM,MAAA,WAAA,GAAc,CAAC,QAA+B,KAAA;AAClD,IAAU,SAAA,CAAA,aAAA,CAAc,aAAa,QAAQ,CAAA,CAAA;AAAA,GAC/C,CAAA;AAEA,EAAO,OAAA,CAAC,OAAO,WAAW,CAAA,CAAA;AAC5B;;;;"} \ No newline at end of file diff --git a/docs/assets/navigation.js b/docs/assets/navigation.js index dfb5e5d..e81ed6a 100644 --- a/docs/assets/navigation.js +++ b/docs/assets/navigation.js @@ -1 +1 @@ -window.navigationData = "data:application/octet-stream;base64,H4sIAAAAAAAAE43SsU7DMBAG4He5OQIa0QLZqMRUIYYOHaoOxr40B65j2WcgQn13JJRCnLhJZ//+/J/t7TcwfjEU8Gjt0pHaI2RgBVdQwKFWQaO//lu6qvigIYN3MgqKPANZkVYODRTbMUhq4X0EnSFn+f0xG0ovlqk2/h8kw+hKIdNmG++1nS869IaMqj83xNU6vL6h5GdhxB7dxBHnto0d1SafDDvCzgzc2MiOcz3x5uFuNs+H6gqbSXGFzZQmHQrGxMuVwcjfy+ywvXBsL26Pu9QTrtF9kBz7XG3ioj/WqzDAE7VPfHrr6BDBp67mNEB39aLyCksRNKfaRlabS1Tb/QCv+WqXtwMAAA==" \ No newline at end of file +window.navigationData = "data:application/octet-stream;base64,H4sIAAAAAAAAE5XSsU7DMBAG4He5OQIatQWygdSpQgwdOlQdjH1pDK5j2WcgQn13pCgNTuJaZY1/f7nz3e4HCL8JCngy5tlKcUDIwDCqoIBjLbxCd9sf3VR0VJDBh9QCijwDXkklLGoodimIK+bcALpAzvKHU9ZL3CIjjHil15xkrUNxFB66y/lpH8B9aoP2U/JUy13iqs5HJUzwSNlnPn412QQ1Bt208vbz/wb1atqq/iypCW3JeM+NoyN/sQzq2kot6q+tpGrj396R0wvT7IA2wV+6kvpNl1xpsjJ8h1bs3GFmpN093s8W+VRcY5PU1tjEpXA43sX29jyj8PSqUQksmVcUW6WB1eUie7P/BTUnnbvqAwAA" \ No newline at end of file diff --git a/docs/assets/search.js b/docs/assets/search.js index c177f08..d0035dd 100644 --- a/docs/assets/search.js +++ b/docs/assets/search.js @@ -1 +1 @@ -window.searchData = "data:application/octet-stream;base64,H4sIAAAAAAAAE61XTW+jMBD9L9OrlWICSeDWlfZQVas9VOoeULQiMEnZEkC2SbaK8t9XEMB2audzT1XKe/Nm5nn8sQNWbjmE0Q4+siKF0CVQxGuEEJ6q6hvL0hUCgZrlEMK6TOsc+ePwZfQu1jkQSPKYc+QQAuxJH2niDaEShrHArwGXdZGIrCzUkEdYgwSBKmZYCC1Hqes6wZT6so7XevEHE/G9ECxDPmiLz0orRYfdIOtPBslfWZGW21+ZeO+i/oiLeIVsEM8KgWwZJ1oGNtZ9uQzff1Ztq0/ncIy+Xps6rjSeIUdxg+JDTzyj+4V9fh284Oe5NfCCnzfU7c5OjE4Xx1T3LSvck1orFM8FF3GRXK72oJMubrIthyTHmD3ledc/fnkiBubd2axQdOGuaojk3J1BXaWxwKuTOKbdnQcyVrKr0zhi/Q8/3uK8vm559oy71Xm94AnLFlfIq5Sb9L9uBK/INlly4ijtADeeqMfhDQdrL2BmXrgJ9TqmWmtuOOH7OtWPl9aY4jKuc2EoSovWwU5XoCW3nxPIihT/QriDDTKelQWE4I7GowAILDPM0+ZOdEiEQFKu102YefftDRNRsgZxgDw6QCKHuM5oGnjzOYl6Rvuh/UcLo0AiSlx/FDhjDUY1mAskck0wV4ONgURjE2yswTwgkWeCeRrMBxL5JpivwSZAookJNtFgU1tDphpsBiSamqLNNFgAJJqZYIHe3qbbgbG/Rz60RjhGpG4FbVpOzZ7pbtCm69RoG9UNoU3jqdE5qntCm95To3lUt4U27adG/6juDG0coEYLqW4ObUygRnvowZ92jDbIBKbPh3GKIoiratHtATv43Y2Z0w/1DhwId3sC0+bPXs5W86uRGOhlf12VUTwZxTtH5/2OKOlU4VPfEqC9kMR5zocLiQwwk/zARm93V2MPqKJ+GdtUg69EmVjCDBunwpsqvJmF1x78vD/4FbKaumshr1Bkw5VSchVdm+wKhUE0UDQdO3NzuCcoyboKcWwhds8LyVLaalsZXY7YvyMlWVG0dacjfzSPD0mcSKLNS+UuotQ4Vmq0jcLhNmmyU5lGaluKNbesYiVnahvibfuS3WbivZNf9+9fGUgpwejSnECVVZhnBUIYzff7f8vdQnerEAAA"; \ No newline at end of file +window.searchData = "data:application/octet-stream;base64,H4sIAAAAAAAAE61XS2/iMBD+L9OrRTPhnVtX2kNVrfZQqXuI0CokA802JJHtwFaI/75KSGKb2hTKnhDke4zns51hD7zYCQjCPbyleQKBzyCPNgQBPJTlN54mawIGFc8ggE2RVBmJ+/7J4FVuMmAQZ5EQJCAAOLBOaTLqpWJOkaSPgqsqj2Va5LrkCdZiwaCMOOXSqFH5oj87s4ZWRzO8wcr3RsprTfIxFzLK48vd7kzSJ86DT2uIM4r4Q5Y9V8s/FEtxeSEW5s3VrEm2clc1RHFurqAqk0jS1UWc0m6ugzgv+NVlnLD+Rx4vUVZdtz07xs3uolqKmKfLK+x1ypf8P14Ez8S3aXzmTmsBX7zaTuUtN1xnYGdeeAl1Pra1yveSxIcFNr+eXZXvzac4VjrtzvueS55qgo1QK2dCztd+LEuzG096q19pnhS7X6l8bRV/RHm0Jt6bprkkvori3tnF+HoNfW9/lk1ibu9T5HWe6Plq+3ASJK90uutIZ/w+MD/P+Ynez2X8RO9X9lbdwsLy7u/2pf7w0kOX0CqqMmk5ZYZaCztftlHcYcEgzRP6C8EetsRFWuQQgD8YDubAYJVSltTT0rEQBnGx2dQyi/bZC8Wy4DXiCLn3gIUe83Hgz/zFgoUdo3nQ/NDAEFiIzJ8MhnM0YGjAfJeab8CGwELfpjY0YCNg4dAGGxmwMbBwZIONDdgEWDi2wSYGbAosnNhgUwM2AxZObbCZAZsDC2c22Nxsb93tubW/Jzk0QXhWpBkF1i1He2ZmGlh3Ha15oBkI1o1HayRoZoJ179GaCpqxYN1+tAaDZjJYJ4DWbNAMB+sQ0BoPmvlgnQNaE8JjRM2B2xKXlDweD14YQlSWy/a22MPv9kB63fHfgwfB/sDArz8O6hTW32qLnl50N7lSwZGSwfFnAqJ7nSuBucb3HPxmmI6yTPTDtOIPFX/kojeTgbUJqLlfxrYsAbVWokumv2M13kzjzR28ZmgV3dCqyBPFnTqoa5Jp/2dIMX3FHLqZFkstalfSa5Lb44CraFNFmzlo7RtYa81Ya83EwWpLpG6g0ujaGtG1yJb+Vr+nNarWWHR1VhukFVNL0xVmO0pqbvoGdJ2/498nSyBak1w9qoRj42upoCuWXTMU7lL52ppvujFSE9IOIFpP4IJBmZaUpTlBEC4Oh39urmq7JREAAA=="; \ No newline at end of file diff --git a/docs/classes/AppBridge.AppBridge.html b/docs/classes/AppBridge.AppBridge.html index e42f5a8..6ff0190 100644 --- a/docs/classes/AppBridge.AppBridge.html +++ b/docs/classes/AppBridge.AppBridge.html @@ -1,8 +1,8 @@ -AppBridge | App Bridge Documentation - v0.0.4

AppBridge class provides a bridge for state management using RxJS BehaviorSubjects. +AppBridge | App Bridge Documentation - v1.0.0

AppBridge class provides a bridge for state management using RxJS BehaviorSubjects. It manages a collection of BehaviorSubject instances, allowing for state updates, retrievals, and subscriptions.

Type Parameters

  • T

    The type of the subjects managed by AppBridge.

    -

Methods

Methods

Methods

  • Clears all BehaviorSubjects from the subject manager, completing and removing them.

    Returns void

    Example

    const appBridge = AppBridge.getInstance<MyType>();
    appBridge.clearAllSubjects();
    -
  • Emits an error in the BehaviorSubject by its name.

    +
  • Emits an error in the BehaviorSubject by its name.

    Type Parameters

    • K extends string

      The key type of the subject.

    Parameters

    • name: K

      The name of the subject.

    • error: any

      The error to emit.

    Returns void

    Example

    appBridge.errorSubject('mySubject', new Error('Something went wrong'));
     
    -
  • Retrieves a BehaviorSubject by its name, creating it if it does not exist.

    +
  • Retrieves a BehaviorSubject by its name, creating it if it does not exist.

    Type Parameters

    • K extends string

      The key type of the subject.

    Parameters

    • name: K

      The name of the subject.

    Returns BehaviorSubject<null | T[K]>

    The BehaviorSubject instance.

    Example

    const subject = appBridge.getSubject('mySubject');
    subject.subscribe(value => console.log(value));
    -
  • Retrieves the current value of a BehaviorSubject by its name.

    +
  • Retrieves the current value of a BehaviorSubject by its name.

    Type Parameters

    • K extends string

      The key type of the subject.

    Parameters

    • name: K

      The name of the subject.

    Returns null | T[K]

    The current value of the subject.

    Example

    const currentValue = appBridge.getValue('mySubject');
    console.log(currentValue);
    -
  • Subscribes to a BehaviorSubject by its name with an observer.

    +
  • Subscribes to a BehaviorSubject by its name with an observer.

    Type Parameters

    • K extends string

      The key type of the subject.

    Parameters

    • name: K

      The name of the subject.

    • observer: {
          complete?: (() => void);
          error?: ((error) => void);
          next?: ((value) => void);
      }

      The observer object with next, error, and complete callbacks.

      • Optional complete?: (() => void)
          • (): void
          • Returns void

      • Optional error?: ((error) => void)
          • (error): void
          • Parameters

            • error: any

            Returns void

      • Optional next?: ((value) => void)
          • (value): void
          • Parameters

            • value: null | T[K]

            Returns void

    Returns Subscription

    The subscription to the subject.

    Example

    const subscription = appBridge.subscribe('mySubject', {
    next: value => console.log(value),
    error: error => console.error(error),
    complete: () => console.log('Completed')
    });
    -
  • Updates the value of a BehaviorSubject by its name, creating it if it does not exist.

    +
  • Updates the value of a BehaviorSubject by its name, creating it if it does not exist.

    Type Parameters

    • K extends string

      The key type of the subject.

    Parameters

    • name: K

      The name of the subject.

    • newState: T[K]

      The new state to update.

    Returns void

    Example

    appBridge.updateSubject('mySubject', { key: 'value' });
     
    -
  • Retrieves the singleton instance of the AppBridge. If the instance does not exist or the reset option is provided, a new instance is created.

    Type Parameters

    • T

      The type of the subjects managed by AppBridge.

      -

    Parameters

Parameters

Returns AppBridge<T>

The singleton instance of AppBridge.

-
\ No newline at end of file +
\ No newline at end of file diff --git a/docs/functions/AppBridge.createAppBridge.html b/docs/functions/AppBridge.createAppBridge.html index fadbe60..a778986 100644 --- a/docs/functions/AppBridge.createAppBridge.html +++ b/docs/functions/AppBridge.createAppBridge.html @@ -1,7 +1,7 @@ -createAppBridge | App Bridge Documentation - v0.0.4
  • Factory function for creating or retrieving the singleton instance of the AppBridge class.

    +createAppBridge | App Bridge Documentation - v1.0.0
    • Factory function for creating or retrieving the singleton instance of the AppBridge class.

      Type Parameters

      • T

        The type of the subjects managed by AppBridge.

        -

      Parameters

    Parameters

    Returns AppBridge<T>

    The singleton instance of AppBridge.

    Example

    const appBridge = createAppBridge<MyType>({ reset: true });
     
    -
\ No newline at end of file +
\ No newline at end of file diff --git a/docs/functions/AppBridgeService.createAppBridgeService.html b/docs/functions/AppBridgeService.createAppBridgeService.html index 5b9da01..a7399bb 100644 --- a/docs/functions/AppBridgeService.createAppBridgeService.html +++ b/docs/functions/AppBridgeService.createAppBridgeService.html @@ -1,3 +1,3 @@ -createAppBridgeService | App Bridge Documentation - v0.0.4
  • Function to create a generic AppBridgeService for Angular.

    +createAppBridgeService | App Bridge Documentation - v1.0.0
    • Function to create a generic AppBridgeService for Angular.

      Type Parameters

      • T

      Parameters

      • applicationName: string

      Returns IModule

      The Angular module with the AppBridgeService factory.

      -
    \ No newline at end of file +
\ No newline at end of file diff --git a/docs/functions/useAppBridge.default.html b/docs/functions/useAppBridge.default.html index ffc89b6..e05cfcf 100644 --- a/docs/functions/useAppBridge.default.html +++ b/docs/functions/useAppBridge.default.html @@ -1,7 +1,7 @@ -default | App Bridge Documentation - v0.0.4
  • Custom hook that subscribes to a subject from the AppBridge and provides state management.

    +default | App Bridge Documentation - v1.0.0
    • Custom hook that subscribes to a subject from the AppBridge and provides state management.

      Type Parameters

      • T

        The type of the subjects managed by AppBridge.

        -

      Parameters

      • subjectName: SubjectKey<T>

        The name of the subject to subscribe to.

        -

      Returns readonly [null | T[SubjectKey<T>], ((newState) => void)]

      A tuple containing the current state of the subject and a function to update the state.

      +

Parameters

  • subjectName: SubjectKey<T>

    The name of the subject to subscribe to.

    +

Returns readonly [null | T[SubjectKey<T>], ((newState) => void)]

A tuple containing the current state of the subject and a function to update the state.

Example

const [state, updateState] = useAppBridge<MyType>('mySubject');

// Use the state
console.log(state);

// Update the state
updateState({ key: 'value' });
-
\ No newline at end of file +
\ No newline at end of file diff --git a/docs/index.html b/docs/index.html index 7ad8a1a..32dd4b4 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1,4 +1,4 @@ -App Bridge Documentation - v0.0.4

App Bridge Documentation - v0.0.4

@rob-long/app-bridge

Version: 0.0.4
Description: App bridge for Angular and React

+App Bridge Documentation - v1.0.0

App Bridge Documentation - v1.0.0

@rob-long/app-bridge

Version: 0.0.4
Description: App bridge for Angular and React

Overview

The @rob-long/app-bridge library provides a seamless bridge for state management between Angular and React applications using RxJS. It allows easy integration and communication between different frameworks, ensuring consistent state handling across your application.

Installation

npm install @rob-long/app-bridge
 
@@ -9,7 +9,7 @@

Usage

Angular

import { appBridgeService } from '@rob-long/app-bridge';

const appBridge = appBridgeService('myApp');
-

React

import useAppBridge from '@rob-long/app-bridge';

const [state, updateState] = useAppBridge('mySubject'); +

React

import { useAppBridge } from '@rob-long/app-bridge';

const [state, updateState] = useAppBridge('mySubject');

Scripts

  • test: jest --config jest.config.json
  • @@ -26,4 +26,4 @@
  • Repository

For more detailed documentation, visit the TypeDoc pages.

-
\ No newline at end of file +
\ No newline at end of file diff --git a/docs/interfaces/AppBridge.AppBridgeOptions.html b/docs/interfaces/AppBridge.AppBridgeOptions.html deleted file mode 100644 index d5db24c..0000000 --- a/docs/interfaces/AppBridge.AppBridgeOptions.html +++ /dev/null @@ -1,4 +0,0 @@ -AppBridgeOptions | App Bridge Documentation - v0.0.4

Options for configuring the AppBridge instance.

-
interface AppBridgeOptions {
    reset?: boolean;
}

Properties

Properties

reset?: boolean

If true, resets the AppBridge instance.

-
\ No newline at end of file diff --git a/docs/interfaces/AppBridge.WindowWithSubjectManager.html b/docs/interfaces/AppBridge.WindowWithSubjectManager.html deleted file mode 100644 index 82c7cb8..0000000 --- a/docs/interfaces/AppBridge.WindowWithSubjectManager.html +++ /dev/null @@ -1,3 +0,0 @@ -WindowWithSubjectManager | App Bridge Documentation - v0.0.4

Interface extending the Window object to include a subject manager.

-
interface WindowWithSubjectManager<T> {}

Type Parameters

  • T

    The type of the values managed by the subject manager.

    -

Hierarchy

  • Window
    • WindowWithSubjectManager
\ No newline at end of file diff --git a/docs/interfaces/types.AppBridgeOptions.html b/docs/interfaces/types.AppBridgeOptions.html new file mode 100644 index 0000000..74a69a9 --- /dev/null +++ b/docs/interfaces/types.AppBridgeOptions.html @@ -0,0 +1,4 @@ +AppBridgeOptions | App Bridge Documentation - v1.0.0

Options for configuring the AppBridge instance.

+
interface AppBridgeOptions {
    reset?: boolean;
}

Properties

Properties

reset?: boolean

If true, resets the AppBridge instance.

+
\ No newline at end of file diff --git a/docs/interfaces/types.WindowWithSubjectManager.html b/docs/interfaces/types.WindowWithSubjectManager.html new file mode 100644 index 0000000..4de9e15 --- /dev/null +++ b/docs/interfaces/types.WindowWithSubjectManager.html @@ -0,0 +1,3 @@ +WindowWithSubjectManager | App Bridge Documentation - v1.0.0

Interface WindowWithSubjectManager<T>

Interface extending the Window object to include a subject manager.

+
interface WindowWithSubjectManager<T> {}

Type Parameters

  • T

    The type of the values managed by the subject manager.

    +

Hierarchy

  • Window
    • WindowWithSubjectManager
\ No newline at end of file diff --git a/docs/modules/AppBridge.html b/docs/modules/AppBridge.html index a41460f..be5e396 100644 --- a/docs/modules/AppBridge.html +++ b/docs/modules/AppBridge.html @@ -1,7 +1,3 @@ -AppBridge | App Bridge Documentation - v0.0.4

Index

Classes

Interfaces

Type Aliases

SubjectEntries -SubjectKey +AppBridge | App Bridge Documentation - v1.0.0

Index

Classes

Functions

\ No newline at end of file +
\ No newline at end of file diff --git a/docs/modules/AppBridgeService.html b/docs/modules/AppBridgeService.html index f6b6452..7244734 100644 --- a/docs/modules/AppBridgeService.html +++ b/docs/modules/AppBridgeService.html @@ -1,2 +1,2 @@ -AppBridgeService | App Bridge Documentation - v0.0.4
\ No newline at end of file +AppBridgeService | App Bridge Documentation - v1.0.0
\ No newline at end of file diff --git a/docs/modules/types.html b/docs/modules/types.html new file mode 100644 index 0000000..1574678 --- /dev/null +++ b/docs/modules/types.html @@ -0,0 +1,5 @@ +types | App Bridge Documentation - v1.0.0
\ No newline at end of file diff --git a/docs/modules/useAppBridge.html b/docs/modules/useAppBridge.html index 261844a..9b53892 100644 --- a/docs/modules/useAppBridge.html +++ b/docs/modules/useAppBridge.html @@ -1,2 +1,2 @@ -useAppBridge | App Bridge Documentation - v0.0.4

Index

Functions

\ No newline at end of file +useAppBridge | App Bridge Documentation - v1.0.0

Index

Functions

\ No newline at end of file diff --git a/docs/types/AppBridge.SubjectEntries.html b/docs/types/AppBridge.SubjectEntries.html deleted file mode 100644 index 627c031..0000000 --- a/docs/types/AppBridge.SubjectEntries.html +++ /dev/null @@ -1,3 +0,0 @@ -SubjectEntries | App Bridge Documentation - v0.0.4
SubjectEntries<T>: {
    [K in SubjectKey<T>]: BehaviorSubject<T[K] | null>
}

Interface representing a collection of BehaviorSubject instances.

-

Type Parameters

  • T

    The type of the values managed by the BehaviorSubjects.

    -
\ No newline at end of file diff --git a/docs/types/AppBridge.SubjectKey.html b/docs/types/AppBridge.SubjectKey.html deleted file mode 100644 index 90a612f..0000000 --- a/docs/types/AppBridge.SubjectKey.html +++ /dev/null @@ -1,2 +0,0 @@ -SubjectKey | App Bridge Documentation - v0.0.4
SubjectKey<T>: keyof T & string

Type alias for the keys of a given type T.

-

Type Parameters

  • T
\ No newline at end of file diff --git a/docs/types/types.SubjectEntries.html b/docs/types/types.SubjectEntries.html new file mode 100644 index 0000000..871cebd --- /dev/null +++ b/docs/types/types.SubjectEntries.html @@ -0,0 +1,3 @@ +SubjectEntries | App Bridge Documentation - v1.0.0
SubjectEntries<T>: {
    [K in SubjectKey<T>]: BehaviorSubject<T[K] | null>
}

Interface representing a collection of BehaviorSubject instances.

+

Type Parameters

  • T

    The type of the values managed by the BehaviorSubjects.

    +
\ No newline at end of file diff --git a/docs/types/types.SubjectKey.html b/docs/types/types.SubjectKey.html new file mode 100644 index 0000000..1efe62a --- /dev/null +++ b/docs/types/types.SubjectKey.html @@ -0,0 +1,2 @@ +SubjectKey | App Bridge Documentation - v1.0.0
SubjectKey<T>: keyof T & string

Type alias for the keys of a given type T.

+

Type Parameters

  • T
\ No newline at end of file diff --git a/package.json b/package.json index c1597db..456de73 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "scripts": { "test": "jest --config jest.config.json", "build": "rollup -c", + "build:local": "npm run build && npm pack -pack-destination ~", "lint": "eslint . --ext .ts", "format": "prettier --write .", "docs": "typedoc" diff --git a/src/AppBridge.ts b/src/AppBridge.ts index fbc057f..122ee83 100644 --- a/src/AppBridge.ts +++ b/src/AppBridge.ts @@ -1,40 +1,13 @@ import { BehaviorSubject, Subscription } from 'rxjs'; - -/** - * Interface representing a collection of BehaviorSubject instances. - * - * @template T - The type of the values managed by the BehaviorSubjects. - */ -export type SubjectEntries = { - [K in SubjectKey]: BehaviorSubject; -}; - -/** - * Interface extending the Window object to include a subject manager. - * - * @template T - The type of the values managed by the subject manager. - */ -export interface WindowWithSubjectManager extends Window { - _subjectManager: SubjectEntries; -} - -/** - * Options for configuring the AppBridge instance. - */ -export interface AppBridgeOptions { - /** - * If true, resets the AppBridge instance. - */ - reset?: boolean; -} +import { + WindowWithSubjectManager, + SubjectEntries, + AppBridgeOptions, + SubjectKey, +} from './types.ts'; declare const window: WindowWithSubjectManager; -/** - * Type alias for the keys of a given type T. - */ -export type SubjectKey = keyof T & string; - /** * AppBridge class provides a bridge for state management using RxJS BehaviorSubjects. * It manages a collection of BehaviorSubject instances, allowing for state updates, diff --git a/src/AppBridgeService.ts b/src/AppBridgeService.ts index 5a2ed08..37f94b8 100644 --- a/src/AppBridgeService.ts +++ b/src/AppBridgeService.ts @@ -1,5 +1,6 @@ import angular from 'angular'; -import { createAppBridge, SubjectKey } from './AppBridge.ts'; +import { createAppBridge } from './AppBridge.ts'; +import { SubjectKey } from './types.ts'; /** * Function to create a generic AppBridgeService for Angular. diff --git a/src/index.ts b/src/index.ts index f9136d0..dfc105d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,3 +3,4 @@ import appBridgeService from './AppBridgeService.ts'; import useAppBridge from './useAppBridge.ts'; export { createAppBridge, appBridgeService, useAppBridge }; +export * from './types.ts'; diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..7b0a8e0 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,35 @@ +import { BehaviorSubject } from 'rxjs'; + +/** + * Interface representing a collection of BehaviorSubject instances. + * + * @template T - The type of the values managed by the BehaviorSubjects. + */ + +export type SubjectEntries = { + [K in SubjectKey]: BehaviorSubject; +}; +/** + * Interface extending the Window object to include a subject manager. + * + * @template T - The type of the values managed by the subject manager. + */ + +export interface WindowWithSubjectManager extends Window { + _subjectManager: SubjectEntries; +} +/** + * Options for configuring the AppBridge instance. + */ + +export interface AppBridgeOptions { + /** + * If true, resets the AppBridge instance. + */ + reset?: boolean; +} +/** + * Type alias for the keys of a given type T. + */ + +export type SubjectKey = keyof T & string; diff --git a/src/useAppBridge.ts b/src/useAppBridge.ts index 0ab71c1..f15c0d0 100644 --- a/src/useAppBridge.ts +++ b/src/useAppBridge.ts @@ -1,6 +1,6 @@ import { useEffect, useState } from 'react'; import { createAppBridge } from './AppBridge.ts'; -import type { SubjectKey } from './AppBridge.ts'; +import type { SubjectKey } from './types.ts'; /** * Custom hook that subscribes to a subject from the AppBridge and provides state management. diff --git a/tests/AppBridge.test.ts b/tests/AppBridge.test.ts index 90ab4cc..0c1b3a5 100644 --- a/tests/AppBridge.test.ts +++ b/tests/AppBridge.test.ts @@ -1,5 +1,6 @@ import { BehaviorSubject } from 'rxjs'; -import { createAppBridge, WindowWithSubjectManager } from '../src/AppBridge'; +import { createAppBridge } from '../src/AppBridge'; +import { WindowWithSubjectManager } from '../src/types'; // Define the specific subject names for testing interface TestSubjects {