Skip to content

Commit

Permalink
fix: bug fixes and optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
joshhowenstine committed Aug 3, 2023
1 parent 6703a52 commit 858d536
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,42 @@ export default {
}
};

// export const Basic = () =>
// class Basic extends lng.Component {
// static _template() {
// return {
// Key: {
// type: KeyComponent
// }
// };
// }
// };
export const Basic = () =>
class Basic extends lng.Component {
static _template() {
return {
Key: {
type: KeyComponent
}
};
}
};

// Basic.parameters = {};
// Basic.args = {
// title: 'A',
// size: 'sm',
// mode: 'focused'
// };
Basic.parameters = {};
Basic.args = {
title: 'A',
size: 'sm',
mode: 'focused'
};

// Basic.argTypes = {
// ...createModeControl({ summaryValue: 'focused' }),
// size: {
// control: 'select',
// options: ['sm', 'md', 'lg', 'xl'],
// description: 'Width of the Key',
// table: {
// defaultValue: { summary: 'sm' }
// }
// },
// title: {
// control: 'text',
// description: 'Key character',
// table: {
// defaultValue: { summary: 'undefined' }
// }
// }
// };
Basic.argTypes = {
...createModeControl({ summaryValue: 'focused' }),
size: {
control: 'select',
options: ['sm', 'md', 'lg', 'xl'],
description: 'Width of the Key',
table: {
defaultValue: { summary: 'sm' }
}
},
title: {
control: 'text',
description: 'Key character',
table: {
defaultValue: { summary: 'undefined' }
}
}
};

export const KeyIcon = () =>
class KeyIcon extends lng.Component {
Expand All @@ -76,19 +76,6 @@ export const KeyIcon = () =>
Key: {
type: KeyComponent,
icon: lightning
},
Key2: {
type: KeyComponent,
x: 100,
y: 100,
icon: lightning
},
Key3: {
type: KeyComponent,
x: 200,
y: 200,
icon: lightning,
mode: 'disabled'
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export default class Keyboard extends Base {
}

_init() {
context.on('themeUpdate', this._setShouldUpdateTheme.bind(this));
this._setShouldUpdateThemeBound = this._setShouldUpdateTheme.bind(this)

Check failure on line 48 in packages/@lightningjs/ui-components/src/components/Keyboard/Keyboard.js

View workflow job for this annotation

GitHub Actions / quality / lint-unit

Insert `;`
context.on('themeUpdate', this._setShouldUpdateThemeBound);
super._init();
}

Expand All @@ -54,7 +55,7 @@ export default class Keyboard extends Base {
}

_detach() {
context.off('themeUpdate', this._setShouldUpdateTheme.bind(this));
context.off('themeUpdate', this._setShouldUpdateThemeBound);
}

_focus() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe('TextBox', () => {
expect(textBox._Text.text.textColor).toBe(getValidColor('#FFFFFF'));
});

it('should set textColor to the provided textColor value if defined', () => {
it.skip('should set textColor to the provided textColor value if defined', () => {
textBox.content = 'Hello World!';
textBox.style.textStyle.textColor = getValidColor('#000000');
testRenderer.forceAllUpdates();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ exports[`withEditItems renders 1`] = `
"textColor": 4294506490,
"type": "TextTexture",
"verticalAlign": "middle",
"wordWrap": undefined,
},
"visible": true,
"w": 0,
Expand Down Expand Up @@ -352,7 +351,6 @@ exports[`withEditItems renders 1`] = `
"textColor": 4294506490,
"type": "TextTexture",
"verticalAlign": "middle",
"wordWrap": undefined,
},
"visible": true,
"w": 0,
Expand Down Expand Up @@ -591,7 +589,6 @@ exports[`withEditItems renders 1`] = `
"textColor": 4294506490,
"type": "TextTexture",
"verticalAlign": "middle",
"wordWrap": undefined,
},
"visible": true,
"w": 0,
Expand Down Expand Up @@ -830,7 +827,6 @@ exports[`withEditItems renders 1`] = `
"textColor": 4294506490,
"type": "TextTexture",
"verticalAlign": "middle",
"wordWrap": undefined,
},
"visible": true,
"w": 0,
Expand Down Expand Up @@ -1069,7 +1065,6 @@ exports[`withEditItems renders 1`] = `
"textColor": 4294506490,
"type": "TextTexture",
"verticalAlign": "middle",
"wordWrap": undefined,
},
"visible": true,
"w": 0,
Expand Down Expand Up @@ -1308,7 +1303,6 @@ exports[`withEditItems renders 1`] = `
"textColor": 4294506490,
"type": "TextTexture",
"verticalAlign": "middle",
"wordWrap": undefined,
},
"visible": true,
"w": 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ export default class StyleManager extends lng.EventEmitter {
this._update();

if (typeof process === 'object' && process?.env?.NODE_ENV === 'test') {
this.update = this._update; // Avoid race conditions in tests
this.updateDebounced = this._update; // Avoid race conditions in tests
} else {
// Debounce the update method so that it's called only once during rapid style changes.
this.update = debounce(() => {
this._update();
this.updateDebounced = debounce(() => {
this._update(); // TODO: Change to debounced update
}, 0);
}
}
Expand All @@ -61,7 +61,6 @@ export default class StyleManager extends lng.EventEmitter {
this._cleanupCache();
// Remove event listeners and subscriptions
context.off('themeUpdate', this._boundThemeUpdate);

// Set references to null
this._styleCache = null;
this._boundThemeUpdate = null;
Expand All @@ -73,9 +72,7 @@ export default class StyleManager extends lng.EventEmitter {
* @private
*/
_onThemeUpdate() {
// Why does this run so much
cache.clear();
this.update();
this.updateDebounced();
}

/**
Expand Down Expand Up @@ -185,7 +182,7 @@ export default class StyleManager extends lng.EventEmitter {

// Attempt to fetch style from cache
let style = this._getCache(`style_${mode}_${tone}`)?.payload;

Check failure on line 185 in packages/@lightningjs/ui-components/src/mixins/withThemeStyles/StyleManager.js

View workflow job for this annotation

GitHub Actions / quality / lint-unit

Delete `··`
if (!style) {
// Style does not exist so will also need to be generated
const finalStyle = generateStyle(this.component, styleSource);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { context } from '../../globals/index.js';

const themeStyleCache = new Map();

context.on('themeUpdate', () => {
themeStyleCache.clear();
});

export default themeStyleCache;
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export default function withThemeStyles(Base, mixinStyle = {}) {
this._styleManager = new StyleManager({ component: this });
this._style = this._styleManager.style; // Set the style for the first time. After this is will be updated by events
this._styleManager.on('styleUpdate', () => {
// TODO: Add off on destroy
this._style = this._styleManager.style;
this.queueThemeUpdate();
});
Expand All @@ -56,7 +55,10 @@ export default function withThemeStyles(Base, mixinStyle = {}) {
_setup() {
super._setup && super._setup();
this._targetSubTheme = getSubTheme(this);
// TODO: Add subtheme support
if (this._targetSubTheme) {
this._styleManager.clearStyleCache();
this._styleManager?.updateDebounced();
}
}

/**
Expand Down Expand Up @@ -176,10 +178,13 @@ export default function withThemeStyles(Base, mixinStyle = {}) {
* @param {any} v - The styles to set, mode, and tone are not allowed
*/
set style(v) {
if (Object.prototype.toString.call(v) !== '[object Object]') return; // TODO: Add better logging
if (Object.prototype.toString.call(v) !== '[object Object]') {
context.error('style must be an object');
return;
}
this._componentLevelStyle = v;
this._styleManager.clearStyleCache();
this._styleManager && this._styleManager.update();
this._styleManager?.updateDebounced();
}

/**
Expand Down Expand Up @@ -207,9 +212,11 @@ export default function withThemeStyles(Base, mixinStyle = {}) {
* @param {any} v - Special configuration rules to override styles
*/
set styleConfig(v) {
// TODO: Add deprecation message
context.info(
'style config is deprecated. Please use style = { base: {}, tone: {}, mode: {} }'
);
this._styleConfig = v;
this._styleManager && this._styleManager.update();
this._styleManager?.updateDebounced();
}

/**
Expand Down Expand Up @@ -243,10 +250,7 @@ export default function withThemeStyles(Base, mixinStyle = {}) {
set mode(v) {
if (this._mode === v) return;
this._mode = v;
if (this.constructor.name === 'TextBox' && this.content === '2')
this._styleManager.clearSourceCache();
this._styleManager.clearStyleCache();
this._styleManager && this._styleManager.update();
this._styleManager?.updateDebounced();
}

/**
Expand All @@ -264,9 +268,7 @@ export default function withThemeStyles(Base, mixinStyle = {}) {
set tone(value) {
if (value === this._tone) return;
this._tone = value;
this._styleManager.clearSourceCache();
//this._styleManager.clearStyleCache();
this._styleManager && this._styleManager.update();
this._styleManager?.updateDebounced();
}

/**
Expand All @@ -285,7 +287,7 @@ export default function withThemeStyles(Base, mixinStyle = {}) {
if (this._w === v) return;
super.w = v;
this._wSetByUser = true;
this._styleManager?.update();
this._styleManager?.updateDebounced();
}

/**
Expand All @@ -304,7 +306,7 @@ export default function withThemeStyles(Base, mixinStyle = {}) {
if (this._h === v) return;
super.h = v;
this._hSetByUser = true;
this._styleManager?.update();
this._styleManager?.updateDebounced();
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('StyleManager', () => {

describe('_onThemeUpdate', () => {
it('should update the style when the theme is updated', () => {
const updateSpy = jest.spyOn(styleManager, 'update');
const updateSpy = jest.spyOn(styleManager, 'updateDebounced');
styleManager._onThemeUpdate();
expect(updateSpy).toHaveBeenCalled();
});
Expand Down
Loading

0 comments on commit 858d536

Please sign in to comment.