@lightningjs/blits 0.10.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.
Files changed (48) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/boilerplate/js/default/src/index.js +1 -0
  3. package/docs/essentials/displaying_text.md +1 -1
  4. package/package.json +5 -5
  5. package/src/application.js +2 -1
  6. package/src/component/base/events.js +1 -1
  7. package/src/component/base/utils.js +34 -1
  8. package/src/component/setup/props.js +3 -1
  9. package/src/component/setup/props.test.js +51 -19
  10. package/src/component.d.ts +2 -1
  11. package/src/component.js +20 -13
  12. package/src/component.test.js +340 -4
  13. package/src/components/Circle.js +13 -12
  14. package/src/components/FPScounter.js +388 -536
  15. package/src/components/Image.js +7 -9
  16. package/src/components/Layout.js +49 -0
  17. package/src/components/Layout.orig.js +34 -0
  18. package/src/components/RouterView.js +6 -10
  19. package/src/components/Sprite.js +10 -12
  20. package/src/components/index.js +2 -2
  21. package/src/components/index.orig.js +2 -2
  22. package/src/constants.js +14 -0
  23. package/src/engine.js +1 -1
  24. package/src/engines/L3/element.js +193 -180
  25. package/src/engines/L3/element.test.js +170 -0
  26. package/src/engines/L3/fontLoader.js +4 -5
  27. package/src/engines/L3/index.js +2 -2
  28. package/src/engines/L3/launch.js +10 -16
  29. package/src/engines/L3/shaderLoader.js +12 -0
  30. package/src/engines/index.js +1 -1
  31. package/src/focus.js +3 -2
  32. package/src/launch.d.ts +31 -3
  33. package/src/launch.js +1 -1
  34. package/src/lib/codegenerator/generator.js +212 -84
  35. package/src/lib/codegenerator/generator.test.js +657 -942
  36. package/src/lib/colors/colors.js +59 -46
  37. package/src/lib/componentId.js +9 -0
  38. package/src/lib/componentId.test.js +2 -1
  39. package/src/lib/precompiler/precompiler.js +3 -4
  40. package/src/lib/reactivity/effect.js +10 -13
  41. package/src/lib/reactivity/reactive.js +24 -14
  42. package/src/lib/symbols.js +1 -0
  43. package/src/router/router.js +17 -3
  44. package/vite/index.js +1 -4
  45. package/vite/preCompiler.js +3 -1
  46. package/boilerplate/js/default/src/renderer.js +0 -4
  47. package/src/components/Text.js +0 -119
  48. package/src/components/Text.orig.js +0 -81
@@ -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
+ })
@@ -21,31 +21,32 @@ export default () =>
21
21
  Component('Circle', {
22
22
  /* eslint-disable no-unused-vars */
23
23
  code: {
24
- render: function anonymous(parent, component, context, components) {
24
+ render: function anonymous(parent, component, context, components, effect, getRaw) {
25
25
  const elms = []
26
-
27
- if (!elms[0]) {
28
- elms[0] = this.element({ parent: parent || 'root' }, component)
29
- }
26
+ let componentType
27
+ const rootComponent = component
30
28
  const elementConfig0 = {}
31
29
 
32
- if (!elms[0].nodeId) {
33
- elms[0].populate(elementConfig0)
34
- }
30
+ elms[0] = this.element({ parent: parent || 'root' }, component)
35
31
 
32
+ elementConfig0['color'] = component.color
33
+ elementConfig0['w'] = component.size
34
+ elementConfig0['h'] = component.size
35
+ elementConfig0['effects'] = [component.shader('radius', { radius: component.radius })]
36
+ elms[0].populate(elementConfig0)
36
37
  return elms
37
38
  },
38
39
  effects: [
39
- function anonymous(component, elms, context, components) {
40
+ function anonymous(component, elms, context, components, rootComponent, effect) {
40
41
  elms[0].set('color', component.color)
41
42
  },
42
- function anonymous(component, elms, context, components) {
43
+ function anonymous(component, elms, context, components, rootComponent, effect) {
43
44
  elms[0].set('w', component.size)
44
45
  },
45
- function anonymous(component, elms, context, components) {
46
+ function anonymous(component, elms, context, components, rootComponent, effect) {
46
47
  elms[0].set('h', component.size)
47
48
  },
48
- function anonymous(component, elms, context, components) {
49
+ function anonymous(component, elms, context, components, rootComponent, effect) {
49
50
  elms[0].set('effects', [component.shader('radius', { radius: component.radius })])
50
51
  },
51
52
  ],