@lightningjs/blits 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ ## v1.1.0
4
+
5
+ _25 july 2024_
6
+
7
+ - Made `holdTimeout` configurable and added to launch settings
8
+ - Improved test coverage
9
+ - Removed imporChunkUrl dependency as it’s no longer needed
10
+ - Abstracted path from pre-compiler, to make it more reusable (in playground for example)
11
+ - Added `defaultFont` launch setting to boilerplate example
12
+
3
13
  ## v1.0.0
4
14
 
5
15
  _15 july 2024_
@@ -6,6 +6,7 @@ Blits.Launch(App, 'app', {
6
6
  h: 1080,
7
7
  multithreaded: false,
8
8
  debugLevel: 1,
9
+ defaultFont: 'lato',
9
10
  fonts: [
10
11
  {
11
12
  family: 'lato',
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@lightningjs/blits",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Blits: The Lightning 3 App Development Framework",
5
5
  "bin": "bin/index.js",
6
6
  "exports": {
7
7
  ".": "./index.js",
8
8
  "./vite": "./vite/index.js",
9
- "./transitions": "./src/router/transitions/index.js"
9
+ "./transitions": "./src/router/transitions/index.js",
10
+ "./precompiler": "./src/lib/precompiler/precompiler.js"
10
11
  },
11
12
  "scripts": {
12
13
  "test": "c8 npm run test:run",
@@ -46,7 +47,6 @@
46
47
  "dependencies": {
47
48
  "@lightningjs/msdf-generator": "^1.0.2",
48
49
  "@lightningjs/renderer": "^1.0.0",
49
- "@lightningjs/vite-plugin-import-chunk-url": "^0.3.0",
50
50
  "execa": "^8.0.1",
51
51
  "kolorist": "^1.8.0",
52
52
  "ora": "^7.0.1",
@@ -20,6 +20,7 @@ import Focus from './focus.js'
20
20
  import Settings from './settings.js'
21
21
 
22
22
  import symbols from './lib/symbols.js'
23
+ import { DEFAULT_HOLD_TIMEOUT_MS } from './constants.js'
23
24
 
24
25
  const Application = (config) => {
25
26
  const defaultKeyMap = {
@@ -61,7 +62,7 @@ const Application = (config) => {
61
62
  clearTimeout(holdTimeout)
62
63
  holdTimeout = setTimeout(() => {
63
64
  Focus.hold = true
64
- }, 50)
65
+ }, Settings.get('holdTimeout', DEFAULT_HOLD_TIMEOUT_MS))
65
66
  }
66
67
 
67
68
  keyUpHandler = () => {
@@ -17,6 +17,12 @@
17
17
 
18
18
  import test from 'tape'
19
19
  import Component from './component.js'
20
+ import { initLog } from './lib/log.js'
21
+ import symbols from './lib/symbols.js'
22
+ import util from 'node:util'
23
+ import { renderer, stage } from './launch.js'
24
+
25
+ initLog()
20
26
 
21
27
  test('Type', (assert) => {
22
28
  const expected = 'function'
@@ -37,15 +43,345 @@ test('Component - Factory function', (assert) => {
37
43
  test('Component - Factory requires a name to be passed', (assert) => {
38
44
  assert.throws(() => {
39
45
  Component()
40
- }, 'Thorw an error when no name argument has been passed')
46
+ }, 'Throw an error when no name argument has been passed')
41
47
 
42
48
  assert.end()
43
49
  })
44
50
 
45
- test('Component - Factory requires a name to be passed', (assert) => {
51
+ test('Component - Factory requires a config to be passed', (assert) => {
46
52
  assert.throws(() => {
47
- Component()
48
- }, 'Throw an error when no name argument has been passed')
53
+ Component('Foo')
54
+ }, 'Throw an error when no config argument has been passed')
55
+
56
+ assert.end()
57
+ })
58
+
59
+ test('Component - Instance should create internal id', (assert) => {
60
+ const button = Component('Button', {})()
61
+ const heading = Component('Heading', {})()
62
+
63
+ assert.equal(button[symbols.id], 1, 'Button instance object should have correct internal id')
64
+ assert.equal(heading[symbols.id], 2, 'Heading instance object should have correct internal id')
65
+ assert.end()
66
+ })
67
+
68
+ test('Component - Instance should create component Id', (assert) => {
69
+ const foo = Component('Foo', {})
70
+ const bar = Component('Bar', {})
71
+ const fooInstance0 = foo()
72
+ const fooInstance1 = foo()
73
+ const barInstance0 = bar()
74
+
75
+ assert.equal(
76
+ fooInstance0.componentId,
77
+ 'BlitsComponent::Foo_1',
78
+ 'First Foo instance should have correct id'
79
+ )
80
+ assert.equal(
81
+ fooInstance1.componentId,
82
+ 'BlitsComponent::Foo_2',
83
+ 'Second Foo instance should have correct id'
84
+ )
85
+ assert.equal(
86
+ barInstance0.componentId,
87
+ 'BlitsComponent::Bar_1',
88
+ 'First Bar instance should have correct id'
89
+ )
90
+ assert.end()
91
+ })
92
+
93
+ test('Component - Instance should initiate lifecycle object', (assert) => {
94
+ const foo = Component('Foo', {})()
95
+
96
+ assert.ok(foo.lifecycle, 'Lifecycle object should be initialized')
97
+ assert.equal(
98
+ foo.lifecycle.component,
99
+ foo,
100
+ 'Lifecycle object should have a reference to foo instance'
101
+ )
102
+ assert.equal(
103
+ foo.lifecycle.previous,
104
+ null,
105
+ 'Lifecycle object should have previous state not initialized'
106
+ )
107
+ assert.equal(foo.lifecycle.current, 'init', 'Lifecycle object should have initial current state')
108
+ assert.end()
109
+ })
110
+
111
+ test('Component - Instance should set a parent reference', (assert) => {
112
+ const parentElement = {}
113
+ const parentComponent = {}
114
+
115
+ const foo = Component('Foo', {})({}, parentElement, parentComponent)
116
+
117
+ assert.equal(
118
+ foo.parent,
119
+ parentComponent,
120
+ 'Foo instance object should have parent component object reference'
121
+ )
122
+ assert.equal(
123
+ foo[symbols.holder],
124
+ parentElement,
125
+ 'Foo instance object should have parent element object reference'
126
+ )
127
+ assert.end()
128
+ })
129
+
130
+ test('Component - Instance should set a root reference', (assert) => {
131
+ const root = {}
132
+
133
+ const foo = Component('Foo', {})({}, {}, {}, root)
134
+
135
+ assert.equal(
136
+ foo.rootParent,
137
+ root,
138
+ 'Foo instance object should have root component object reference'
139
+ )
140
+ assert.end()
141
+ })
142
+
143
+ test('Component - Instance should initialize reactive props', (assert) => {
144
+ const foo = Component(
145
+ 'Foo',
146
+ {}
147
+ )({
148
+ props: { foo: 'bar' },
149
+ })
150
+
151
+ const props = foo[symbols.props]
152
+
153
+ assert.ok(props, 'Foo instance should have props defined')
154
+ assert.ok(util.types.isProxy(props), 'Foo instance props should be proxy')
155
+ assert.equal(props.foo, 'bar')
156
+ assert.end()
157
+ })
158
+
159
+ test('Component - Instance should initialize timeouts array', (assert) => {
160
+ const foo = Component('Foo', {})()
161
+
162
+ const timeouts = foo[symbols.timeouts]
163
+
164
+ assert.ok(Array.isArray(timeouts), 'Foo instance timeouts should be an array')
165
+ assert.end()
166
+ })
167
+
168
+ test('Component - Instance should initialize intervals array', (assert) => {
169
+ const foo = Component('Foo', {})()
170
+
171
+ const intervals = foo[symbols.intervals]
49
172
 
173
+ assert.ok(Array.isArray(intervals), 'Foo instance intervals should be an array')
50
174
  assert.end()
51
175
  })
176
+
177
+ test('Component - Instance should initialize originalState', (assert) => {
178
+ const config = {
179
+ state() {
180
+ return {
181
+ foo: 'bar',
182
+ }
183
+ },
184
+ }
185
+ const foo = Component('Foo', config)()
186
+
187
+ const state = foo[symbols.originalState]
188
+
189
+ assert.equal(state.foo, 'bar', 'Foo instance should store originalState properties')
190
+ assert.equal(state.hasFocus, false, 'Foo instance should store originalState hasFocus property')
191
+ assert.end()
192
+ })
193
+
194
+ test('Component - Instance should initialize reactive state', (assert) => {
195
+ const config = {
196
+ state() {
197
+ return {
198
+ foo: 'bar',
199
+ }
200
+ },
201
+ }
202
+ const foo = Component('Foo', config)()
203
+
204
+ const state = foo[symbols.state]
205
+
206
+ assert.ok(state, 'Foo instance should have state defined')
207
+ assert.ok(util.types.isProxy(state), 'Foo instance state should be proxy')
208
+ assert.equal(state.foo, 'bar')
209
+ assert.end()
210
+ })
211
+
212
+ test('Component - Instance should initialize children', (assert) => {
213
+ const parent = {}
214
+ const expected = []
215
+ const config = {
216
+ code: {
217
+ render: () => {},
218
+ effects: [],
219
+ },
220
+ state() {
221
+ return {
222
+ foo: 'bar',
223
+ }
224
+ },
225
+ }
226
+ const capture = assert.capture(config.code, 'render', () => expected)
227
+ const foo = Component('Foo', config)({}, parent)
228
+
229
+ const children = foo[symbols.children]
230
+
231
+ assert.ok(Array.isArray(children), 'Foo instance children should be an array')
232
+ assert.equal(children, expected, 'Children should be an expected array')
233
+ const args = capture()[0].args
234
+ assert.equal(args[0], parent, 'Render should be called with parent parameter')
235
+ assert.equal(args[1], foo, 'Render should be called with Foo component instance')
236
+ assert.equal(args[2], config, 'Render should be called with config parameter')
237
+ assert.ok(args[3].Layout, 'Render should be called with global components object')
238
+ assert.end()
239
+ })
240
+
241
+ test('Component - Instance should initialize wrapper', (assert) => {
242
+ const expected = {}
243
+ const config = {
244
+ code: {
245
+ render: () => [expected],
246
+ effects: [],
247
+ },
248
+ }
249
+ const foo = Component('Foo', config)()
250
+
251
+ const wrapper = foo[symbols.wrapper]
252
+
253
+ assert.equal(wrapper, expected, 'Wrapper should be a first element of the children')
254
+ assert.end()
255
+ })
256
+
257
+ test('Component - Instance should initialize slots', (assert) => {
258
+ const expected = {
259
+ [symbols.isSlot]: true,
260
+ }
261
+ const config = {
262
+ code: {
263
+ render: () => [{}, {}, expected, {}],
264
+ effects: [],
265
+ },
266
+ }
267
+ const foo = Component('Foo', config)()
268
+
269
+ const slots = foo[symbols.slots]
270
+
271
+ assert.ok(Array.isArray(slots), 'Slots should be an array')
272
+ assert.equal(slots[0], expected, 'Slot should be a expected element of the children')
273
+ assert.end()
274
+ })
275
+
276
+ test('Component - Instance should initialize hook events', (assert) => {
277
+ const wrapper = {
278
+ node: {
279
+ on() {},
280
+ },
281
+ }
282
+ const rendererCapture = assert.capture(renderer, 'on')
283
+ const nodeCapture = assert.capture(wrapper.node, 'on')
284
+ const config = {
285
+ code: {
286
+ render: () => [wrapper],
287
+ effects: [],
288
+ },
289
+ hooks: {
290
+ frameTick() {},
291
+ idle() {},
292
+ attach() {},
293
+ detach() {},
294
+ enter() {},
295
+ exit() {},
296
+ },
297
+ }
298
+ Component('Foo', config)()
299
+
300
+ const rendererCalls = rendererCapture()
301
+ assert.equal(
302
+ rendererCalls[0].args[0],
303
+ 'frameTick',
304
+ '`frameTick` event should be registered in Renderer'
305
+ )
306
+ assert.equal(rendererCalls[1].args[0], 'idle', '`idle` event should be registered in Renderer')
307
+ const nodeCalls = nodeCapture()
308
+ assert.equal(
309
+ nodeCalls[0].args[0],
310
+ 'inBounds',
311
+ '`inBounds` event should be registered in the wrapper'
312
+ )
313
+ assert.equal(
314
+ nodeCalls[1].args[0],
315
+ 'outOfBounds',
316
+ '`outOfBounds` event should be registered in the wrapper'
317
+ )
318
+ assert.equal(
319
+ nodeCalls[2].args[0],
320
+ 'inViewport',
321
+ '`inViewport` event should be registered in the wrapper'
322
+ )
323
+ assert.equal(
324
+ nodeCalls[3].args[0],
325
+ 'outOfViewport',
326
+ '`outOfViewport` event should be registered in the wrapper'
327
+ )
328
+ assert.end()
329
+ })
330
+
331
+ test('Component - Instance should execute all side effects', (assert) => {
332
+ const root = {}
333
+ const capture = assert.captureFn(() => {})
334
+ const children = []
335
+ const config = {
336
+ code: {
337
+ render: () => children,
338
+ effects: [capture],
339
+ },
340
+ }
341
+ const foo = Component('Foo', config)({}, {}, {}, root)
342
+
343
+ const calls = capture.calls[0]
344
+ assert.equals(calls.receiver, stage, 'Effect`s receiver should be the stage object')
345
+ assert.equals(calls.args[0], foo, 'Effect should be invoked with foo component instance')
346
+ assert.equals(calls.args[1], children, 'Effect should be invoked with component`s children')
347
+ assert.equals(calls.args[2], config, 'Effect should be invoked with config object')
348
+ assert.ok(calls.args[3].Layout, 'Effect should be invoked with global components object')
349
+ assert.equals(calls.args[4], root, 'Effect should be invoked with root component')
350
+ assert.ok(typeof calls.args[5] === 'function', 'Effect should be invoked with effect function')
351
+ assert.end()
352
+ })
353
+
354
+ test('Component - Instance should have all symbols configured', (assert) => {
355
+ const config = {
356
+ state() {
357
+ return {
358
+ foo: 'bar',
359
+ }
360
+ },
361
+ watch: {
362
+ foo(value, oldValue) {},
363
+ },
364
+ }
365
+ const foo = Component('Foo', config)()
366
+
367
+ Object.getOwnPropertySymbols(foo).forEach((symbol) => {
368
+ const descriptor = Object.getOwnPropertyDescriptor(foo, symbol)
369
+ assert.ok(descriptor.enumerable === false, `${symbol.description} should not be enumerable`)
370
+ assert.ok(descriptor.configurable === false, `${symbol.description} should not be configurable`)
371
+ })
372
+ assert.end()
373
+ })
374
+
375
+ test('Component - Instance should have ready state after the next process tick', (assert) => {
376
+ assert.plan(1)
377
+
378
+ const foo = Component('Foo', {})()
379
+
380
+ setTimeout(() => {
381
+ assert.equal(
382
+ foo.lifecycle.state,
383
+ 'ready',
384
+ 'Foo component lifecycle should be eventually in a ready state'
385
+ )
386
+ })
387
+ })
@@ -0,0 +1,14 @@
1
+ export const DEFAULT_HOLD_TIMEOUT_MS = 50
2
+
3
+ export const SCREEN_RESOLUTIONS = {
4
+ hd: 0.66666667,
5
+ '720p': 0.66666667,
6
+ 720: 0.66666667,
7
+ fhd: 1,
8
+ fullhd: 1,
9
+ '1080p': 1,
10
+ 1080: 1,
11
+ '4k': 2,
12
+ '2160p': 2,
13
+ 2160: 2,
14
+ }
@@ -17,7 +17,7 @@
17
17
 
18
18
  import { RendererMain } from '@lightningjs/renderer'
19
19
  import { Log } from '../../lib/log.js'
20
- import { screenResolutions } from '../../lib/utils.js'
20
+ import { SCREEN_RESOLUTIONS } from '../../constants.js'
21
21
  import colors from '../../lib/colors/colors.js'
22
22
  import fontLoader from './fontLoader.js'
23
23
  import shaderLoader from './shaderLoader.js'
@@ -42,8 +42,8 @@ You can remove the option from your \`src/index.js\`-file. And you can safely re
42
42
  fpsUpdateInterval: settings.fpsInterval || 1000,
43
43
  deviceLogicalPixelRatio:
44
44
  settings.pixelRatio ||
45
- screenResolutions[settings.screenResolution] ||
46
- screenResolutions[window.innerHeight] ||
45
+ SCREEN_RESOLUTIONS[settings.screenResolution] ||
46
+ SCREEN_RESOLUTIONS[window.innerHeight] ||
47
47
  1,
48
48
  numImageWorkers:
49
49
  'webWorkersLimit' in settings
package/src/focus.js CHANGED
@@ -17,6 +17,8 @@
17
17
 
18
18
  import symbols from './lib/symbols.js'
19
19
  import { navigating } from './router/router.js'
20
+ import Settings from './settings.js'
21
+ import { DEFAULT_HOLD_TIMEOUT_MS } from './constants.js'
20
22
 
21
23
  let focusedComponent = null
22
24
  let focusChain = []
@@ -48,8 +50,7 @@ export default {
48
50
  focusChain = []
49
51
  }
50
52
  },
51
- // todo: make the hold timeout configurable?
52
- this.hold ? 50 : 0
53
+ this.hold ? Settings.get('holdTimeout', DEFAULT_HOLD_TIMEOUT_MS) : 0
53
54
  )
54
55
  }
55
56
  },
package/src/launch.d.ts CHANGED
@@ -243,7 +243,18 @@ export interface Settings {
243
243
  *
244
244
  * Defaults to `webgl`
245
245
  */
246
- renderMode?: RenderModes
246
+ renderMode?: RenderModes,
247
+
248
+ /**
249
+ * The time, in milliseconds, after which Blits considers a key press a _hold_ key press
250
+ *
251
+ * During a hold key press the focus delegation behaviour is different: when scrolling
252
+ * through a long list, focus is not handed over to each individual list item, creating a
253
+ * smoother experience
254
+ *
255
+ * Defaults to `50` (ms)
256
+ */
257
+ holdTimeout?: number
247
258
  }
248
259
 
249
260
  /**
package/src/launch.js CHANGED
@@ -19,7 +19,7 @@ import Settings from './settings.js'
19
19
  import { initLog } from './lib/log.js'
20
20
  import engine from './engine.js'
21
21
 
22
- export let renderer
22
+ export let renderer = {}
23
23
  export const stage = {}
24
24
 
25
25
  export default (App, target, settings) => {
@@ -25,3 +25,12 @@ export const createHumanReadableId = (name) => {
25
25
  export const createInternalId = () => {
26
26
  return ++counter
27
27
  }
28
+
29
+ /**
30
+ * Resets the counter value back to 0.
31
+ * Used in automated tests to ensure consistent results, not intended
32
+ * to be used in the actual framework code!
33
+ */
34
+ export const resetCounter = () => {
35
+ counter = 0
36
+ }
@@ -16,7 +16,7 @@
16
16
  */
17
17
 
18
18
  import test from 'tape'
19
- import { createHumanReadableId, createInternalId } from './componentId.js'
19
+ import { createHumanReadableId, createInternalId, resetCounter } from './componentId.js'
20
20
 
21
21
  test('Type createHumandReadableId', (assert) => {
22
22
  const expected = 'function'
@@ -59,6 +59,7 @@ test('Type createInternalId', (assert) => {
59
59
  })
60
60
 
61
61
  test('Returns an internal id, disregarding the component name', (assert) => {
62
+ resetCounter()
62
63
  const expected = 1
63
64
  const actual = createInternalId('Spinner')
64
65
 
@@ -17,7 +17,6 @@
17
17
 
18
18
  import parser from '../templateparser/parser.js'
19
19
  import generator from '../codegenerator/generator.js'
20
- import path from 'path'
21
20
 
22
21
  export default (source, filePath) => {
23
22
  if (
@@ -46,8 +45,7 @@ export default (source, filePath) => {
46
45
  resourceName = source.match(/Blits\.Component\(['"](.*)['"]\s*,/)[1]
47
46
  }
48
47
 
49
- const componentPath = path.relative(process.cwd(), filePath)
50
- const parsed = parser(templateContent, resourceName, null, componentPath)
48
+ const parsed = parser(templateContent, resourceName, null, filePath)
51
49
 
52
50
  // Generate the code
53
51
  const code = generator.call({ components: {} }, parsed)
package/vite/index.js CHANGED
@@ -15,13 +15,10 @@
15
15
  * SPDX-License-Identifier: Apache-2.0
16
16
  */
17
17
 
18
- import { importChunkUrl } from '@lightningjs/vite-plugin-import-chunk-url'
19
-
20
18
  import preCompiler from './preCompiler.js'
21
19
  import msdfGenerator from './msdfGenerator.js'
22
20
 
23
- export { importChunkUrl as importChunkUrl } from '@lightningjs/vite-plugin-import-chunk-url'
24
21
  export { default as preCompiler } from './preCompiler.js'
25
22
  export { default as msdfGenerator } from './msdfGenerator.js'
26
23
 
27
- export default [importChunkUrl(), preCompiler(), msdfGenerator()]
24
+ export default [preCompiler(), msdfGenerator()]
@@ -16,6 +16,7 @@
16
16
  */
17
17
 
18
18
  import compiler from '../src/lib/precompiler/precompiler.js'
19
+ import path from 'path'
19
20
 
20
21
  export default function () {
21
22
  let config
@@ -26,7 +27,8 @@ export default function () {
26
27
  },
27
28
  transform(source, filePath) {
28
29
  if (config.blits && config.blits.precompile === false) return source
29
- return compiler(source, filePath)
30
+ const relativePath = path.relative(process.cwd(), filePath)
31
+ return compiler(source, relativePath)
30
32
  },
31
33
  }
32
34
  }