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

feat: extract public api to the composition class #676

Merged
merged 12 commits into from
Jul 2, 2024
Merged
7 changes: 6 additions & 1 deletion abstract/ActivityBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const ACTIVE_ATTR = 'active';
const ACTIVE_PROP = '___ACTIVITY_IS_ACTIVE___';

export class ActivityBlock extends Block {
/** @protected */
historyTracked = false;

init$ = activityBlockCtx(this);

_debouncedHistoryFlush = debounce(this._historyFlush.bind(this), 10);
Expand Down Expand Up @@ -37,6 +39,7 @@ export class ActivityBlock extends Block {
});
}

/** @protected */
initCallback() {
super.initCallback();
if (this.hasAttribute('current-activity')) {
Expand Down Expand Up @@ -131,6 +134,7 @@ export class ActivityBlock extends Block {
ActivityBlock._activityCallbacks.delete(this);
}

/** @protected */
destroyCallback() {
super.destroyCallback();
this._isActivityRegistered() && this.unregisterActivity();
Expand All @@ -144,6 +148,7 @@ export class ActivityBlock extends Block {

if (!hasCurrentActivityInCtx) {
this.$['*currentActivity'] = null;
this.setOrAddState('*modalActive', false);
}
}

Expand Down Expand Up @@ -199,4 +204,4 @@ ActivityBlock.activities = Object.freeze({
DETAILS: 'details',
});

/** @typedef {(typeof ActivityBlock)['activities'][keyof (typeof ActivityBlock)['activities']] | null} ActivityType */
/** @typedef {(typeof ActivityBlock)['activities'][keyof (typeof ActivityBlock)['activities']] | (string & {}) | null} ActivityType */
53 changes: 20 additions & 33 deletions abstract/Block.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ export class Block extends BaseComponent {

/** @type {string[]} */
static styleAttrs = [];

/** @protected */
requireCtxName = false;
allowCustomTemplate = true;

/** @type {import('./ActivityBlock.js').ActivityType} */
activityType = null;

Expand Down Expand Up @@ -52,6 +54,7 @@ export class Block extends BaseComponent {
}

/**
* @private
* @param {string} key
* @param {number} count
* @returns {string}
Expand All @@ -65,6 +68,7 @@ export class Block extends BaseComponent {
/**
* @param {string} key
* @param {() => void} resolver
* @protected
*/
bindL10n(key, resolver) {
this.localeManager?.bindL10n(this, key, resolver);
Expand Down Expand Up @@ -118,15 +122,7 @@ export class Block extends BaseComponent {
);
}

/** @param {import('./ActivityBlock.js').ActivityType} activityType */
setActivity(activityType) {
if (this.hasBlockInCtx((b) => b.activityType === activityType)) {
this.$['*currentActivity'] = activityType;
return;
}
console.warn(`Activity type "${activityType}" not found in the context`);
}

/** @protected */
connectedCallback() {
const styleAttrs = /** @type {typeof Block} */ (this.constructor).styleAttrs;
styleAttrs.forEach((attr) => {
Expand Down Expand Up @@ -158,11 +154,13 @@ export class Block extends BaseComponent {
WindowHeightTracker.registerClient(this);
}

/** @protected */
disconnectedCallback() {
super.disconnectedCallback();
WindowHeightTracker.unregisterClient(this);
}

/** @protected */
initCallback() {
if (!this.has('*blocksRegistry')) {
this.add('*blocksRegistry', new Set());
Expand All @@ -188,12 +186,18 @@ export class Block extends BaseComponent {
});
}

/** @returns {LocaleManager | null} */
/**
* @private
* @returns {LocaleManager | null}
*/
get localeManager() {
return this.has('*localeManager') ? this.$['*localeManager'] : null;
}

/** @returns {A11y | null} */
/**
* @returns {A11y | null}
* @protected
*/
get a11y() {
return this.has('*a11y') ? this.$['*a11y'] : null;
}
Expand All @@ -203,9 +207,10 @@ export class Block extends BaseComponent {
return this.$['*blocksRegistry'];
}

/** @protected */
destroyCallback() {
let blocksRegistry = this.blocksRegistry;
blocksRegistry.delete(this);
blocksRegistry?.delete(this);

this.localeManager?.destroyL10nBindings(this);
this.l10nProcessorSubs = new Map();
Expand All @@ -214,7 +219,7 @@ export class Block extends BaseComponent {
// TODO: this should be done inside symbiote
Data.deleteCtx(this);

if (blocksRegistry.size === 0) {
if (blocksRegistry?.size === 0) {
setTimeout(() => {
// Destroy global context after all blocks are destroyed and all callbacks are run
this.destroyCtxCallback();
Expand All @@ -234,28 +239,10 @@ export class Block extends BaseComponent {
this.a11y?.destroy();
}

/**
* @param {Number} bytes
* @param {Number} [decimals]
*/
fileSizeFmt(bytes, decimals = 2) {
let units = ['B', 'KB', 'MB', 'GB', 'TB'];
/**
* @param {String} str
* @returns {String}
*/
if (bytes === 0) {
return `0 ${units[0]}`;
}
let k = 1024;
let dm = decimals < 0 ? 0 : decimals;
let i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / k ** i).toFixed(dm)) + ' ' + units[i];
}

/**
* @param {String} url
* @returns {String}
* @protected
*/
proxyUrl(url) {
if (this.cfg.secureDeliveryProxy && this.cfg.secureDeliveryProxyUrlResolver) {
Expand Down
5 changes: 2 additions & 3 deletions abstract/CTX.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ export const blockCtx = () => ({});
/** @param {import('./Block').Block} fnCtx */
export const activityBlockCtx = (fnCtx) => ({
...blockCtx(),
'*currentActivity': '',
'*currentActivity': null,
'*currentActivityParams': {},
'*history': [],
'*historyBack': null,
'*closeModal': () => {
fnCtx.set$({
'*currentActivity': null,
'*modalActive': false,
'*currentActivity': '',
});
},
});
Expand All @@ -25,7 +25,6 @@ export const uploaderBlockCtx = (fnCtx) => ({
'*uploadList': [],
'*focusedEntry': null,
'*uploadQueue': new Queue(1),
'*uploadCollection': null,
/** @type {ReturnType<import('../types').OutputErrorCollection>[]} */
'*collectionErrors': [],
/** @type {import('../types').OutputCollectionState | null} */
Expand Down
Loading
Loading