Skip to content

Commit fcd1225

Browse files
Fix accessing functions on multiple chaynsapi instances
(Fix for modules)
1 parent ae32f58 commit fcd1225

12 files changed

Lines changed: 39 additions & 5 deletions

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
"prettier": {
4545
"proseWrap": "always",
4646
"singleQuote": true,
47-
"tabWidth": 4
47+
"tabWidth": 4,
48+
"printWidth": 2000
4849
},
4950
"eslintConfig": {
5051
"extends": "@chayns-toolkit"

src/components/ChaynsProvider.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { FrameWrapper } from '../wrapper/FrameWrapper';
88
import { ModuleFederationWrapper } from '../wrapper/ModuleFederationWrapper';
99
import { SsrWrapper } from '../wrapper/SsrWrapper';
1010
import { ChaynsContext } from './ChaynsContext';
11-
import { addModuleWrapper, moduleWrapper, removeModuleWrapper } from './moduleWrapper';
11+
import { addModuleWrapper, chaynsApis, moduleWrapper, removeModuleWrapper } from './moduleWrapper';
1212

1313
const isServer = typeof window === 'undefined';
1414

@@ -28,6 +28,7 @@ export type ChaynsProviderProps = {
2828
renderedByServer?: boolean,
2929
isModule?: boolean,
3030
children?: ReactNode,
31+
chaynsApiId?: string
3132
}
3233

3334
const ChaynsProvider: React.FC<ChaynsProviderProps> = ({
@@ -37,8 +38,10 @@ const ChaynsProvider: React.FC<ChaynsProviderProps> = ({
3738
customFunctions,
3839
renderedByServer,
3940
isModule,
41+
chaynsApiId
4042
}) => {
4143
const customWrapper = useRef<IChaynsReact>(null!);
44+
const idRef = useRef(chaynsApiId ?? crypto?.randomUUID() ?? Math.random().toString());
4245

4346
if (!customWrapper.current) {
4447
if (isModule) {
@@ -63,6 +66,8 @@ const ChaynsProvider: React.FC<ChaynsProviderProps> = ({
6366
}
6467
}
6568
moduleWrapper.current = customWrapper.current;
69+
chaynsApis[idRef.current] = customWrapper.current.functions;
70+
customWrapper.current.chaynsApiId = idRef.current;
6671
}
6772

6873
const [isInitialized, setIsInitialized] = useState<boolean>(!!customWrapper.current?.values);

src/components/moduleWrapper.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IChaynsReact } from '../types/IChaynsReact';
1+
import { ChaynsReactFunctions, IChaynsReact } from '../types/IChaynsReact';
22

33
const moduleWrapperStack: IChaynsReact[] = [];
44
let current: IChaynsReact = undefined!;
@@ -13,7 +13,13 @@ export const moduleWrapper: { current: IChaynsReact } = {
1313
set current(chayns: IChaynsReact) {
1414
current = chayns;
1515
}
16-
}
16+
}
17+
18+
export const chaynsApis: Record<string, ChaynsReactFunctions> = {};
19+
20+
export const getChaynsApi = (id: string) => {
21+
return chaynsApis[id];
22+
}
1723

1824
export const addModuleWrapper = (chayns: IChaynsReact) => {
1925
moduleWrapperStack.push(chayns);

src/hooks/context.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useSyncExternalStore } from 'use-sync-external-store/shim';
33
import { ChaynsContext } from '../components/ChaynsContext';
44
import { IChaynsReact } from '../types/IChaynsReact';
55

6-
const createChaynsSelector = <T extends 'values' | 'functions' | 'customFunctions'>(key: T) => <Result>(selector: (value: IChaynsReact[T]) => Result): Result => {
6+
const createChaynsSelector = <T extends 'values' | 'functions' | 'customFunctions' | 'chaynsApiId'>(key: T) => <Result>(selector: (value: IChaynsReact[T]) => Result): Result => {
77
const store = useContext(ChaynsContext);
88

99
if (!store) {
@@ -18,3 +18,4 @@ const createChaynsSelector = <T extends 'values' | 'functions' | 'customFunction
1818
export const useValuesSelector = createChaynsSelector('values');
1919
export const useFunctionsSelector = createChaynsSelector('functions');
2020
export const useCustomFunctionsSelector = createChaynsSelector('customFunctions');
21+
export const useChaynsApiIdSelector = createChaynsSelector('chaynsApiId');

src/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ export { useDialogState, useDialogData } from './useDialogState';
2323
export { useCustomCallbackFunction } from './useCustomCallbackFunction';
2424
export { useCustomFunction } from './useCustomFunction';
2525
export { useStyleSettings } from './useStyleSettings';
26+
export { useChaynsApiId } from './useChaynsApiId';

src/hooks/useChaynsApiId.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { useChaynsApiIdSelector } from './context';
2+
3+
/**
4+
* @category Hooks
5+
*/
6+
export const useChaynsApiId = (): string => {
7+
return useChaynsApiIdSelector((chaynsApiId) => chaynsApiId);
8+
};

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ export * from './util/bindChaynsApi';
2828
export * from './util/appStorage';
2929

3030
export * from './util/collectCssChunks';
31+
32+
export { getChaynsApi } from './components/moduleWrapper';

src/types/IChaynsReact.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ export interface ChaynsReactValues {
352352
customData: any,
353353
dialog: { dialogInput: any, isClosingRequested: boolean },
354354
styleSettings?: ChaynsStyleSettings,
355+
chaynsApiId: string
355356
}
356357

357358
export interface DialogResultFile {
@@ -698,6 +699,7 @@ export interface IChaynsReact {
698699
getInitialData: () => ChaynsReactValues;
699700
subscribe: (listener: () => void) => () => void;
700701
emitChange: () => void;
702+
chaynsApiId: string;
701703
}
702704

703705
export interface OpenUrl {

src/wrapper/AppWrapper.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ export class AppWrapper implements IChaynsReact {
188188
constructor() {
189189
}
190190

191+
chaynsApiId: string;
192+
191193
notImplemented(call: string) {
192194
console.warn(`call ${call} not implement in app`);
193195
}

src/wrapper/FrameWrapper.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export class FrameWrapper implements IChaynsReact {
3232

3333
values: ChaynsReactValues = null!;
3434

35+
chaynsApiId: string = null!;
36+
3537
listeners: (() => void)[] = [];
3638

3739
functions: ChaynsReactFunctions = {

0 commit comments

Comments
 (0)