@lightningjs/blits 0.9.0 → 0.9.2

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,21 @@
1
1
  # Changelog
2
2
 
3
+ # v0.9.2
4
+
5
+ _21 mar 2024_
6
+
7
+ - Fixed issue with updates not triggering during array operation
8
+
9
+ # v0.9.1
10
+
11
+ _21 mar 2024_
12
+
13
+ - Improved support for reactive advanced array operations (i.e. `splice()`, `concat()`, `sort()`)
14
+ - Upgraded Lightning renderer to v0.7.6
15
+ - Reverted removal of destroy of child nodes (added in v0.8.1)
16
+ - Introduced `viewportMargin` setting
17
+ - Added `undefined`-check for props sent to renderer
18
+
3
19
  # v0.9.0
4
20
 
5
21
  _18 mar 2024_
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lightningjs/blits",
3
- "version": "0.9.0",
3
+ "version": "0.9.2",
4
4
  "description": "Blits: The Lightning 3 App Development Framework",
5
5
  "bin": "bin/index.js",
6
6
  "exports": {
@@ -45,7 +45,7 @@
45
45
  "tape": "^5.5.0"
46
46
  },
47
47
  "dependencies": {
48
- "@lightningjs/renderer": "^0.7.5",
48
+ "@lightningjs/renderer": "^0.7.6",
49
49
  "@lightningjs/vite-plugin-import-chunk-url": "^0.3.0",
50
50
  "execa": "^8.0.1",
51
51
  "kolorist": "^1.8.0",
package/src/element.js CHANGED
@@ -194,7 +194,7 @@ const Props = {
194
194
  this._set.add('alpha')
195
195
  },
196
196
  set text(v) {
197
- this._props.text = v !== undefined ? v.toString() : ''
197
+ this._props.text = v.toString()
198
198
  },
199
199
  set effects(v) {
200
200
  this._props.shader = renderer.createShader('DynamicShader', {
@@ -229,7 +229,7 @@ const Props = {
229
229
  this._props.overflowSuffix = v === false ? ' ' : v === true ? undefined : v
230
230
  },
231
231
  set letterSpacing(v) {
232
- this._props.letterSpacing = v
232
+ this._props.letterSpacing = v || 1
233
233
  },
234
234
  set lineHeight(v) {
235
235
  this._props.lineHeight = v
@@ -259,12 +259,14 @@ const Element = {
259
259
  for (const [prop, value] of Object.entries(props)) {
260
260
  const descriptor = Object.getOwnPropertyDescriptor(Props, prop)
261
261
  if (descriptor && descriptor.set) {
262
- this.props[prop] = unpackTransition(value)
262
+ if (value !== undefined) {
263
+ this.props[prop] = unpackTransition(value)
264
+ }
263
265
  }
264
266
  }
265
267
 
266
- // correct for default white nodes
267
- if (!this.props._set.has('color')) {
268
+ // correct for default white nodes (but not for text nodes)
269
+ if (!props.__textnode && !this.props._set.has('color')) {
268
270
  this.props._props.color =
269
271
  this.props._set.has('src') || this.props._set.has('texture') ? 0xffffffff : 0
270
272
  }
@@ -286,6 +288,7 @@ const Element = {
286
288
  }
287
289
  },
288
290
  set(prop, value) {
291
+ if (value === undefined) return
289
292
  const propsSet = new Set(this.props._set)
290
293
 
291
294
  this.props._props = {}
package/src/launch.d.ts CHANGED
@@ -190,7 +190,23 @@ export interface Settings {
190
190
  *
191
191
  * Defaults to `false`
192
192
  */
193
- inspector?: boolean
193
+ inspector?: boolean,
194
+ /**
195
+ * Add an extra margin to the viewport for earlier pre-loading of elements and components
196
+ *
197
+ * By default the Lightning renderer, only renders elements that are inside the defined viewport.
198
+ * Everything outside of these bounds is removed from the render tree.
199
+ *
200
+ * With the viewportMargin you have the option to _virtually_ increase the viewport area,
201
+ * to expedite the pre-loading of elements and / or delay the unloading of elements depending
202
+ * on their position in the (virtual) viewport
203
+ *
204
+ * The margin can be specified in 4 directions by defining an array [top, right, bottom, left],
205
+ * or as a single number which is then applied to all 4 directions equally.
206
+ *
207
+ * Defaults to `0`
208
+ */
209
+ viewportMargin?: number | [number, number, number, number]
194
210
  }
195
211
 
196
212
  /**
package/src/launch.js CHANGED
@@ -63,6 +63,7 @@ You can remove the option from your \`src/index.js\`-file. And you can safely re
63
63
  : window.navigator.hardwareConcurrency || 2,
64
64
  clearColor: (settings.canvasColor && colors.normalize(settings.canvasColor)) || 0x00000000,
65
65
  enableInspector: settings.inspector || false,
66
+ boundsMargin: settings.viewportMargin || 0,
66
67
  },
67
68
  target,
68
69
  driver
@@ -216,8 +216,10 @@ const generateForLoopCode = function (templateObject, parent) {
216
216
  if (parent) {
217
217
  ctx.renderCode.push(`parent = ${parent}`)
218
218
  }
219
+
220
+ const forCounter = counter
219
221
  ctx.renderCode.push(`
220
- const collection = ${cast(result[2], ':for')}
222
+ const collection = ${cast(result[2], ':for')} || []
221
223
  const keys = []
222
224
  for(let __index = 0; __index < collection.length; __index++) {
223
225
  parent = ${parent}
@@ -269,12 +271,11 @@ const generateForLoopCode = function (templateObject, parent) {
269
271
  ctx.renderCode.push('}')
270
272
 
271
273
  ctx.renderCode.push(`
272
- if(elms[${counter}]) {
273
- Object.keys(elms[${counter}]).forEach(key => {
274
+ if(elms[${forCounter}]) {
275
+ Object.keys(elms[${forCounter}]).forEach(key => {
274
276
  if(keys.indexOf(key) === -1) {
275
- elms[${counter}][key].delete && elms[${counter}][key].delete()
276
- elms[${counter}][key].destroy && elms[${counter}][key].destroy()
277
- delete elms[${counter}][key]
277
+ elms[${forCounter}][key].destroy && elms[${forCounter}][key].destroy()
278
+ delete elms[1][key]
278
279
  }
279
280
  })
280
281
  }
@@ -2145,7 +2145,6 @@ test('Generate code for a template with a simple for-loop on an Element', (asser
2145
2145
  if(elms[1]) {
2146
2146
  Object.keys(elms[1]).forEach(key => {
2147
2147
  if(keys.indexOf(key) === -1) {
2148
- elms[1][key].delete && elms[1][key].delete()
2149
2148
  elms[1][key].destroy && elms[1][key].destroy()
2150
2149
  delete elms[1][key]
2151
2150
  }
@@ -2237,7 +2236,6 @@ test('Generate code for a template with a simple for-loop on an Element with a c
2237
2236
  if(elms[1]) {
2238
2237
  Object.keys(elms[1]).forEach(key => {
2239
2238
  if(keys.indexOf(key) === -1) {
2240
- elms[1][key].delete && elms[1][key].delete()
2241
2239
  elms[1][key].destroy && elms[1][key].destroy()
2242
2240
  delete elms[1][key]
2243
2241
  }
@@ -2314,25 +2312,24 @@ test('Generate code for a template with a simple for-loop on an Element with a k
2314
2312
  index: __index,
2315
2313
  item: collection[__index],
2316
2314
  })
2317
- keys.push(item.id)
2315
+ keys.push(scope.item.id)
2318
2316
 
2319
2317
  parent = elms[0]
2320
2318
  elms[1] = elms[1] || {}
2321
- if(!elms[1][item.id]) {
2322
- elms[1][item.id] = this.element({parent: parent || 'root'}, component)
2319
+ if(!elms[1][scope.item.id]) {
2320
+ elms[1][scope.item.id] = this.element({parent: parent || 'root'}, component)
2323
2321
  }
2324
2322
 
2325
2323
  const elementConfig1 = {}
2326
- elementConfig1['key'] = "item.id"
2324
+ elementConfig1['key'] = scope.item.id
2327
2325
 
2328
- if(!elms[1][item.id].nodeId) {
2329
- elms[1][item.id].populate(elementConfig1)
2326
+ if(!elms[1][scope.item.id].nodeId) {
2327
+ elms[1][scope.item.id].populate(elementConfig1)
2330
2328
  }
2331
2329
  }
2332
2330
  if(elms[1]) {
2333
2331
  Object.keys(elms[1]).forEach(key => {
2334
2332
  if(keys.indexOf(key) === -1) {
2335
- elms[1][key].delete && elms[1][key].delete()
2336
2333
  elms[1][key].destroy && elms[1][key].destroy()
2337
2334
  delete elms[1][key]
2338
2335
  }
@@ -2424,7 +2421,7 @@ test('Generate code for a template with a simple for-loop on a Component with a
2424
2421
 
2425
2422
  if(typeof cmp1 !== 'undefined') {
2426
2423
  for(let key in cmp1.config.props) {
2427
- delete elementConfig1[cmp1.config.props[key]]
2424
+ delete elementConfig1[cmp1.config.props[key]]
2428
2425
  }
2429
2426
  }
2430
2427
 
@@ -2449,12 +2446,11 @@ test('Generate code for a template with a simple for-loop on a Component with a
2449
2446
  }
2450
2447
  }
2451
2448
  }
2452
- if(elms[2]) {
2453
- Object.keys(elms[2]).forEach(key => {
2449
+ if(elms[1]) {
2450
+ Object.keys(elms[1]).forEach(key => {
2454
2451
  if(keys.indexOf(key) === -1) {
2455
- elms[2][key].delete && elms[2][key].delete()
2456
- elms[2][key].destroy && elms[2][key].destroy()
2457
- delete elms[2][key]
2452
+ elms[1][key].destroy && elms[1][key].destroy()
2453
+ delete elms[1][key]
2458
2454
  }
2459
2455
  })
2460
2456
  }
@@ -2550,7 +2546,6 @@ test('Generate code for a template with a simple for-loop on an Element with an
2550
2546
  if(elms[1]) {
2551
2547
  Object.keys(elms[1]).forEach(key => {
2552
2548
  if(keys.indexOf(key) === -1) {
2553
- elms[1][key].delete && elms[1][key].delete()
2554
2549
  elms[1][key].destroy && elms[1][key].destroy()
2555
2550
  delete elms[1][key]
2556
2551
  }
@@ -16,10 +16,33 @@
16
16
  */
17
17
 
18
18
  let currentEffect = null
19
+
20
+ let paused = false
21
+
22
+ export const pauseTracking = () => {
23
+ paused = true
24
+ }
25
+
26
+ export const resumeTracking = () => {
27
+ paused = false
28
+ for (let i = 0; i < trackQueue.length; i++) {
29
+ const item = trackQueue[i]
30
+ currentEffect = item.currentEffect
31
+ track(item.target, item.key)
32
+ }
33
+ trackQueue.length = 0
34
+ }
35
+
19
36
  const objectMap = new WeakMap()
20
37
 
38
+ const trackQueue = []
39
+
21
40
  export const track = (target, key) => {
22
41
  if (currentEffect) {
42
+ if (paused) {
43
+ trackQueue.push({ target, key, currentEffect })
44
+ return
45
+ }
23
46
  let effectsMap = objectMap.get(target)
24
47
  if (!effectsMap) {
25
48
  effectsMap = new Map()
@@ -15,76 +15,89 @@
15
15
  * SPDX-License-Identifier: Apache-2.0
16
16
  */
17
17
 
18
- import { track, trigger } from './effect.js'
19
-
20
- const arrayMethods = [
21
- 'constructor',
22
- 'includes',
23
- 'indexOf',
24
- 'lastIndexOf',
25
- 'push',
26
- 'pop',
27
- 'shift',
28
- 'splice',
29
- 'unshift',
30
- 'sort',
31
- 'reverse',
32
- ]
33
-
34
- const arrayPatchMethods = ['push', 'pop', 'shift', 'unshift', 'splice', 'sort', 'reverse']
18
+ import { track, trigger, pauseTracking, resumeTracking } from './effect.js'
19
+ import symbols from '../symbols.js'
20
+
21
+ const arrayPatchMethods = ['push', 'pop', 'shift', 'unshift', 'splice']
35
22
 
36
23
  const proxyMap = new WeakMap()
37
24
 
38
- const reactiveProxy = (target) => {
39
- const isProxy = proxyMap.get(target)
40
- if (isProxy) {
41
- return isProxy
25
+ const getProxy = (obj) => {
26
+ return proxyMap.get(obj)
27
+ }
28
+
29
+ const getRaw = (value) => {
30
+ const raw = value && value[symbols.raw]
31
+ return raw ? getRaw(raw) : value
32
+ }
33
+
34
+ const reactiveProxy = (original, _parent = null, _key) => {
35
+ // if original object is already a proxy, don't create a new one but return the existing one instead
36
+ const existingProxy = getProxy(original)
37
+ if (existingProxy) {
38
+ return existingProxy
42
39
  }
43
40
 
44
41
  const handler = {
45
42
  get(target, key, receiver) {
46
- if (Array.isArray(target) && arrayMethods.includes(key)) {
47
- return Reflect.get(target, key, receiver)
43
+ // return the original object instead of the proxied
44
+ if (key === symbols.raw) {
45
+ return original
48
46
  }
49
47
 
50
- if (target[key] !== null && typeof target[key] === 'object') {
51
- return reactiveProxy(target[key])
48
+ // handling arrays
49
+ if (Array.isArray(target)) {
50
+ // augment array path methods (that change the length of the array)
51
+ if (arrayPatchMethods.indexOf(key) > -1) {
52
+ return function (...args) {
53
+ pauseTracking()
54
+ const result = target[key].apply(this, args)
55
+ // trigger a change on the parent object and the key
56
+ // i.e. when pushing a new item to `obj.data`, _parent will equal `obj`
57
+ // and _key will equal `data`
58
+ resumeTracking()
59
+ trigger(_parent, _key)
60
+ return result
61
+ }
62
+ } else {
63
+ return Reflect.get(target, key, receiver)
64
+ }
52
65
  }
53
66
 
67
+ // handling objects (but not null values, which have object type in JS)
68
+ if (typeof target[key] === 'object' && target[key] !== null) {
69
+ if (Array.isArray(target[key])) {
70
+ track(target, key)
71
+ }
72
+ // create a new reactive proxy
73
+ return reactiveProxy(target[key], target, key)
74
+ }
75
+
76
+ // handling all other types
77
+ // track the key on the target
54
78
  track(target, key)
79
+ // return the reflected value
55
80
  return Reflect.get(target, key, receiver)
56
81
  },
57
82
  set(target, key, value, receiver) {
58
- const oldValue = target[key]
83
+ // get the raw values (without proxy wrapper) so we can compare them
84
+ const oldRawValue = getRaw(target[key])
85
+ const rawValue = getRaw(value)
59
86
 
60
- let result
61
- if (typeof value === 'object' && Array.isArray(value) && proxyMap.get(target[key])) {
62
- if (oldValue !== value) {
63
- result = oldValue.splice(0, target[key].length, ...value)
64
- } else {
65
- result = true
66
- }
67
- } else {
87
+ let result = true
88
+ if (oldRawValue !== rawValue) {
68
89
  result = Reflect.set(target, key, value, receiver)
69
90
  }
70
91
 
71
- if (typeof value === 'object') {
72
- reactiveProxy(target[key])
73
- }
74
-
75
- if (key === 'length') {
76
- trigger(target, key)
77
- } else {
78
- if (result && oldValue !== value) {
79
- trigger(target, key)
80
- }
92
+ if (result && oldRawValue !== value) {
93
+ trigger(target, key, true)
81
94
  }
82
95
  return result
83
96
  },
84
97
  }
85
98
 
86
- const proxy = new Proxy(target, handler)
87
- proxyMap.set(target, proxy)
99
+ const proxy = new Proxy(original, handler)
100
+ proxyMap.set(original, proxy)
88
101
  return proxy
89
102
  }
90
103
 
@@ -270,15 +270,15 @@ export default (component, name, identifier) => {
270
270
  }
271
271
 
272
272
  const deleteChildren = function (children) {
273
- // for (let i = 0; i < children.length; i++) {
274
- // if (!children[i]) return
275
- // if (Array.isArray(children[i])) {
276
- // deleteChildren(children[i])
277
- // } else if (children[i].destroy) {
278
- // children[i].destroy()
279
- // }
280
- // children[i] = null
281
- // }
273
+ for (let i = 0; i < children.length; i++) {
274
+ if (!children[i]) return
275
+ if (Array.isArray(children[i])) {
276
+ deleteChildren(children[i])
277
+ } else if (children[i].destroy) {
278
+ children[i].destroy()
279
+ }
280
+ children[i] = null
281
+ }
282
282
 
283
283
  children.length = 0
284
284
  }
@@ -13,6 +13,7 @@ export default {
13
13
  methodKeys: Symbol('methodKeys'),
14
14
  originalState: Symbol('originalState'),
15
15
  propKeys: Symbol('propKeys'),
16
+ raw: Symbol('raw'),
16
17
  ready: Symbol('ready'),
17
18
  renderer: Symbol('renderer'),
18
19
  routes: Symbol('routes'),
@@ -236,12 +236,12 @@ const removeView = async (route, view, transition) => {
236
236
  cacheMap.set(route, { view: view, focus: previousFocus })
237
237
  } else {
238
238
  // remove and cleanup
239
- // for (let i = 0; i < view[symbols.children].length; i++) {
240
- // if (view[symbols.children][i] && view[symbols.children][i].destroy) {
241
- // view[symbols.children][i].destroy()
242
- // view[symbols.children][i] = null
243
- // }
244
- // }
239
+ for (let i = 0; i < view[symbols.children].length; i++) {
240
+ // if (view[symbols.children][i] && view[symbols.children][i].destroy) {
241
+ // view[symbols.children][i].destroy()
242
+ view[symbols.children][i] = null
243
+ // }
244
+ }
245
245
  view.destroy()
246
246
  view = null
247
247
  }