Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/sdk init #192

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion apps/docs/packages/typescript/tma-js-sdk-solid.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,12 @@ import {
Match,
type ParentProps,
} from 'solid-js';
import { SDKProvider, useSDKContext } from '@tma.js/sdk-solid';
import {
SDKProvider,
useSDKContext,
useBackButton,
useMiniApp,
} from '@tma.js/sdk-solid';

/**
* Part of the application which doesn't know anything about SDK
Expand Down
171 changes: 89 additions & 82 deletions packages/sdk/src/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,96 +30,103 @@ export function init<O extends InitOptions>(options: O): ComputedInitResult<O> {
acceptCustomStyles = false,
} = options;

// Retrieve launch data.
const {
launchParams: {
initData,
initDataRaw,
version,
platform,
themeParams,
botInline = false,
},
isPageReload,
} = retrieveLaunchData();
try {
// Retrieve launch data.
const {
launchParams: {
initData,
initDataRaw,
version,
platform,
themeParams,
botInline = false,
},
isPageReload,
} = retrieveLaunchData();

const createRequestId = createRequestIdGenerator();
const postEvent = createPostEvent(version);
const createRequestId = createRequestIdGenerator();
const postEvent = createPostEvent(version);

// In Telegram web version we should listen to special event sent from the Telegram application
// to know, when we should reload the Mini App.
if (isIframe()) {
if (acceptCustomStyles) {
catchCustomStyles();
}
// In Telegram web version we should listen to special event sent from the Telegram application
// to know, when we should reload the Mini App.
if (isIframe()) {
if (acceptCustomStyles) {
catchCustomStyles();
}

// Notify Telegram, iframe is ready. This will result in sending style tag html from native
// application which is used in catchCustomStyles function. We should call this method also
// to start receiving "reload_iframe" events from the Telegram application.
postEvent('iframe_ready', { reload_supported: true });
on('reload_iframe', () => window.location.reload());
}
// Notify Telegram, iframe is ready. This will result in sending style tag html from native
// application which is used in catchCustomStyles function. We should call this method also
// to start receiving "reload_iframe" events from the Telegram application.
postEvent('iframe_ready', { reload_supported: true });
on('reload_iframe', () => window.location.reload());
}

const result: Omit<InitResult, 'viewport'> = {
backButton: createBackButton(isPageReload, version, postEvent),
closingBehavior: createClosingBehavior(isPageReload, postEvent),
cloudStorage: new CloudStorage(version, createRequestId, postEvent),
createRequestId,
hapticFeedback: new HapticFeedback(version, postEvent),
invoice: new Invoice(version, postEvent),
mainButton: createMainButton(
isPageReload,
themeParams.buttonColor || '#000000',
themeParams.buttonTextColor || '#ffffff',
const result: Omit<InitResult, 'viewport'> = {
backButton: createBackButton(isPageReload, version, postEvent),
closingBehavior: createClosingBehavior(isPageReload, postEvent),
cloudStorage: new CloudStorage(version, createRequestId, postEvent),
createRequestId,
hapticFeedback: new HapticFeedback(version, postEvent),
invoice: new Invoice(version, postEvent),
mainButton: createMainButton(
isPageReload,
themeParams.buttonColor || '#000000',
themeParams.buttonTextColor || '#ffffff',
postEvent,
),
miniApp: createMiniApp(
isPageReload,
themeParams.backgroundColor || '#ffffff',
version,
botInline,
postEvent,
),
popup: new Popup(version, postEvent),
postEvent,
),
miniApp: createMiniApp(
isPageReload,
themeParams.backgroundColor || '#ffffff',
version,
botInline,
postEvent,
),
popup: new Popup(version, postEvent),
postEvent,
qrScanner: new QRScanner(version, postEvent),
themeParams: createThemeParams(themeParams),
utils: new Utils(version, createRequestId, postEvent),
...(initData
// Init data could be missing in case, application was launched via InlineKeyboardButton.
? {
initData: new InitData(initData),
initDataRaw,
}
: {}),
};
qrScanner: new QRScanner(version, postEvent),
themeParams: createThemeParams(themeParams),
utils: new Utils(version, createRequestId, postEvent),
...(initData
// Init data could be missing in case, application was launched via InlineKeyboardButton.
? {
initData: new InitData(initData),
initDataRaw,
}
: {}),
};

const viewport = async
? createViewportAsync(isPageReload, platform, postEvent)
: createViewportSync(isPageReload, platform, postEvent);
const viewport = async
? createViewportAsync(isPageReload, platform, postEvent)
: createViewportSync(isPageReload, platform, postEvent);

if (viewport instanceof Promise) {
return viewport.then((vp) => {
processCSSVars(
cssVars,
result.miniApp,
result.themeParams,
vp,
);
if (viewport instanceof Promise) {
return viewport.then((vp) => {
processCSSVars(
cssVars,
result.miniApp,
result.themeParams,
vp,
);

return {
...result,
viewport: vp,
};
}) as ComputedInitResult<O>;
}
return {
...result,
viewport: vp,
};
}) as ComputedInitResult<O>;
}

processCSSVars(
cssVars,
result.miniApp,
result.themeParams,
viewport,
);
processCSSVars(
cssVars,
result.miniApp,
result.themeParams,
viewport,
);

return { ...result, viewport } as ComputedInitResult<O>;
return { ...result, viewport } as ComputedInitResult<O>;
} catch (e) {
if (async) {
return Promise.reject(e) as unknown as ComputedInitResult<O>;
}
throw e;
}
}