Skip to content

Commit

Permalink
Merge pull request #642 from Telegram-Mini-Apps/639-bug-viewportbindc…
Browse files Browse the repository at this point in the history
…ssvars-adds-0px-variables

fix(sdk): fix problems related to signals' tuples and batched updates
  • Loading branch information
heyqbnk authored Jan 30, 2025
2 parents 2f762a3 + 7825425 commit f3fca69
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-jokes-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@telegram-apps/sdk": patch
---

Fix problems related to signals' tuples and batched updates
6 changes: 3 additions & 3 deletions packages/sdk/src/scopes/components/back-button/back-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const [_isMounted, isMounted] = createSignalsTuple(false);
*/
export const isSupported = createIsSupported(SETUP_METHOD_NAME);

const wrapComplete = createWrapComplete(COMPONENT_NAME, isMounted, SETUP_METHOD_NAME);
const wrapComplete = createWrapComplete(COMPONENT_NAME, _isMounted, SETUP_METHOD_NAME);
const wrapSupported = createWrapSupported(COMPONENT_NAME, SETUP_METHOD_NAME);

/**
Expand Down Expand Up @@ -62,14 +62,14 @@ export const hide = wrapComplete('hide', (): void => {
* }
*/
export const mount = wrapSupported('mount', (): void => {
if (!isMounted()) {
if (!_isMounted()) {
setVisibility(isPageReload() && getStorageValue<StorageValue>(COMPONENT_NAME) || false);
_isMounted.set(true);
}
});

function setVisibility(value: boolean): void {
if (value !== isVisible()) {
if (value !== _isVisible()) {
postEvent(SETUP_METHOD_NAME, { is_visible: value });
setStorageValue<StorageValue>(COMPONENT_NAME, value);
_isVisible.set(value);
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/scopes/components/biometry/signals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ export const [_state, state] = createSignalsTuple<State>({
/**
* Signal indicating biometry is available.
*/
export const isAvailable = createComputed(() => state().available);
export const isAvailable = createComputed(() => _state().available);
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const enableConfirmation = wrapMounted('enableConfirmation', (): void =>
* }
*/
export const mount = wrapBasic('mount', (): void => {
if (!isMounted()) {
if (!_isMounted()) {
setClosingConfirmation(
isPageReload() && getStorageValue<StorageValue>(COMPONENT_NAME) || false,
);
Expand All @@ -71,7 +71,7 @@ export const mount = wrapBasic('mount', (): void => {
});

function setClosingConfirmation(value: boolean): void {
if (value !== isConfirmationEnabled()) {
if (value !== _isConfirmationEnabled()) {
postEvent('web_app_setup_closing_behavior', { need_confirmation: value });
setStorageValue<StorageValue>(COMPONENT_NAME, value);
_isConfirmationEnabled.set(value);
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/scopes/components/init-data/init-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const [_state, state] =

function fromState<K extends keyof InitData>(key: K): Computed<InitData[K] | undefined> {
return createComputed(() => {
const s = state();
const s = _state();
return s ? s[key] : undefined;
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/scopes/components/main-button/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const wrapMounted = createWrapMounted(COMPONENT_NAME, isMounted);
* }
*/
export const mount = wrapBasic('mount', (): void => {
if (!isMounted()) {
if (!_isMounted()) {
const prev = isPageReload() && getStorageValue<StorageValue>(COMPONENT_NAME);
prev && internalState.set(prev);
_isMounted.set(true);
Expand Down
14 changes: 7 additions & 7 deletions packages/sdk/src/scopes/components/mini-app/signals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const [_backgroundColor, backgroundColor] =
* This value requires the Theme Params component to be mounted to extract a valid RGB value
* of the color key.
*/
export const backgroundColorRGB = rgbBasedOn(backgroundColor);
export const backgroundColorRGB = rgbBasedOn(_backgroundColor);


/**
Expand All @@ -52,7 +52,7 @@ export const [_bottomBarColor, bottomBarColor] =
* of the color key.
*/
export const bottomBarColorRGB = createComputed<RGB | undefined>(() => {
const color = bottomBarColor();
const color = _bottomBarColor();
return isRGB(color)
? color
: color === 'bottom_bar_bg_color'
Expand All @@ -75,7 +75,7 @@ export const [_headerColor, headerColor] = createSignalsTuple<HeaderColor>('bg_c
* This value requires the Theme Params component to be mounted to extract a valid RGB value
* of the color key.
*/
export const headerColorRGB = rgbBasedOn(headerColor);
export const headerColorRGB = rgbBasedOn(_headerColor);

/**
* True if CSS variables are currently bound.
Expand All @@ -99,8 +99,8 @@ export const [_isActive, isActive] = createSignalsTuple(true);
* Complete component state.
*/
export const state = createComputed<State>(() => ({
backgroundColor: backgroundColor(),
bottomBarColor: bottomBarColor(),
headerColor: headerColor(),
isActive: isActive(),
backgroundColor: _backgroundColor(),
bottomBarColor: _bottomBarColor(),
headerColor: _headerColor(),
isActive: _isActive(),
}));
10 changes: 3 additions & 7 deletions packages/sdk/src/scopes/components/secondary-button/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { createWrapSupported } from '@/scopes/wrappers/createWrapSupported.js';

import { internalState, isMounted, _isMounted, state } from './signals.js';
import type { State } from './types.js';
import { removeUndefined } from '@/utils/removeUndefined.js';

type StorageValue = State;

Expand All @@ -39,7 +40,7 @@ export const isSupported = createIsSupported(SETUP_METHOD_NAME);
* }
*/
export const mount = wrapSupported('mount', (): void => {
if (!isMounted()) {
if (!_isMounted()) {
const prev = isPageReload() && getStorageValue<StorageValue>(COMPONENT_NAME);
prev && internalState.set(prev);
_isMounted.set(true);
Expand Down Expand Up @@ -108,12 +109,7 @@ export const offClick = wrapSupported(
export const setParams = wrapComplete(
'setParams',
(updates: Partial<State>): void => {
internalState.set({
...internalState(),
...Object.fromEntries(
Object.entries(updates).filter(([, v]) => v !== undefined),
),
});
internalState.set({ ...internalState(), ...removeUndefined(updates) });
setStorageValue<StorageValue>(COMPONENT_NAME, internalState());

// We should not commit changes until the payload is correct. Some version of Telegram will
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const [_isMounted, isMounted] = createSignalsTuple(false);
export const isSupported = createIsSupported(SETUP_METHOD_NAME);

const wrapSupported = createWrapSupported(COMPONENT_NAME, SETUP_METHOD_NAME);
const wrapComplete = createWrapComplete(COMPONENT_NAME, isMounted, SETUP_METHOD_NAME);
const wrapComplete = createWrapComplete(COMPONENT_NAME, _isMounted, SETUP_METHOD_NAME);

/**
* Hides the Settings Button.
Expand Down Expand Up @@ -62,14 +62,14 @@ export const hide = wrapComplete('hide', (): void => {
* }
*/
export const mount = wrapSupported('mount', (): void => {
if (!isMounted()) {
if (!_isMounted()) {
setVisibility(isPageReload() && getStorageValue<StorageValue>(COMPONENT_NAME) || false);
_isMounted.set(true);
}
});

function setVisibility(value: boolean): void {
if (value !== isVisible()) {
if (value !== _isVisible()) {
postEvent(SETUP_METHOD_NAME, { is_visible: value });
setStorageValue<StorageValue>(COMPONENT_NAME, value);
_isVisible.set(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const isSupported = createIsSupported(SETUP_METHOD_NAME);
export const [_isVerticalEnabled, isVerticalEnabled] = createSignalsTuple(true);

const wrapSupported = createWrapSupported(COMPONENT_NAME, SETUP_METHOD_NAME);
const wrapComplete = createWrapComplete(COMPONENT_NAME, isMounted, SETUP_METHOD_NAME);
const wrapComplete = createWrapComplete(COMPONENT_NAME, _isMounted, SETUP_METHOD_NAME);

/**
* Disables vertical swipes.
Expand Down Expand Up @@ -76,7 +76,7 @@ export const enableVertical = wrapComplete('enableVertical', (): void => {
* }
*/
export const mount = wrapSupported('mount', (): void => {
if (!isMounted()) {
if (!_isMounted()) {
setVerticalEnabled(
isPageReload() && getStorageValue<StorageValue>(COMPONENT_NAME) || false,
true,
Expand All @@ -86,7 +86,7 @@ export const mount = wrapSupported('mount', (): void => {
});

function setVerticalEnabled(value: boolean, force?: boolean): void {
if (value !== isVerticalEnabled() || force) {
if (value !== _isVerticalEnabled() || force) {
postEvent(SETUP_METHOD_NAME, { allow_vertical_swipe: value });
setStorageValue<StorageValue>(COMPONENT_NAME, value);
_isVerticalEnabled.set(value);
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/scopes/components/theme-params/signals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const [_isCssVarsBound, isCssVarsBound] = createSignalsTuple(false);
export const [_state, state] = createSignalsTuple<ThemeParams>({});

function fromState<K extends keyof ThemeParams>(key: K): Computed<ThemeParams[K] | undefined> {
return createComputed(() => state()[key]);
return createComputed(() => _state()[key]);
}

/**
Expand Down Expand Up @@ -48,7 +48,7 @@ export const hintColor = fromState('hintColor');
* This value is calculated based on the current theme's background color.
*/
export const isDark = createComputed(() => {
const { bgColor } = state();
const { bgColor } = _state();
return !bgColor || isColorDark(bgColor);
});

Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/scopes/components/viewport/css-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CSSVarsBoundError } from '@/errors.js';
import { createSignalsTuple } from '@/signals-registry.js';
import { createWrapMounted } from '@/scopes/wrappers/createWrapMounted.js';
import { COMPONENT_NAME } from '@/scopes/components/viewport/const.js';
import { isMounted } from '@/scopes/components/viewport/mounting.js';
import { _isMounted } from '@/scopes/components/viewport/mounting.js';

import {
safeAreaInsetBottom,
Expand All @@ -22,7 +22,7 @@ import {
} from './signals.js';
import type { GetCSSVarNameFn } from './types.js';

const wrapMounted = createWrapMounted(COMPONENT_NAME, isMounted);
const wrapMounted = createWrapMounted(COMPONENT_NAME, _isMounted);

/**
* True if CSS variables are currently bound.
Expand Down Expand Up @@ -69,7 +69,7 @@ export const [_isCssVarsBound, isCssVarsBound] = createSignalsTuple(false);
export const bindCssVars = wrapMounted(
'bindCssVars',
(getCSSVarName?: GetCSSVarNameFn): VoidFunction => {
if (isCssVarsBound()) {
if (_isCssVarsBound()) {
throw new CSSVarsBoundError();
}

Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/scopes/components/viewport/fullscreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { defineNonConcurrentFn } from '@/scopes/defineNonConcurrentFn.js';

import { COMPONENT_NAME, FS_CHANGED_EVENT } from './const.js';
import { setState, signalFromState } from './signals.js';
import { isMounted } from './mounting.js';
import { _isMounted } from './mounting.js';
import { createSignalsTuple } from '@/signals-registry.js';

const REQUEST_METHOD_NAME = 'web_app_request_fullscreen';
const wrapComplete = createWrapComplete(COMPONENT_NAME, isMounted, REQUEST_METHOD_NAME);
const wrapComplete = createWrapComplete(COMPONENT_NAME, _isMounted, REQUEST_METHOD_NAME);

/**
* Signal indicating if the viewport is currently in fullscreen mode.
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/scopes/components/viewport/signals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ export function setState(s: Partial<State>): void {
const { height, stableHeight, width } = s;

_state.set({
...state(),
..._state(),
...removeUndefined({
...s,
height: height ? nonNegative(height) : undefined,
width: width ? nonNegative(width) : undefined,
stableHeight: stableHeight ? nonNegative(stableHeight) : undefined,
}),
});
setStorageValue<State>(COMPONENT_NAME, state());
setStorageValue<State>(COMPONENT_NAME, _state());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/scopes/defineMountFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function defineMountFn<Fn extends (...args: any) => AbortablePromise<any>
const [_isMounted, isMounted] = createSignalsTuple(false);

return [
(...args) => isMounted()
(...args) => _isMounted()
? AbortablePromise.resolve()
: fn(...args).then(data => {
batch(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/scopes/defineNonConcurrentFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function defineNonConcurrentFn<Fn extends (...args: any) => AbortableProm
});
});
}, fn),
[_promise, promise, createComputed(() => !!promise())],
[_promise, promise, createComputed(() => !!_promise())],
[_error, error],
];
}

0 comments on commit f3fca69

Please sign in to comment.