@lightningjs/blits 1.3.1 → 1.4.1
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 +21 -1
- package/docs/_sidebar.md +3 -0
- package/docs/components/utility_methods.md +280 -0
- package/docs/plugins/global_app_state.md +77 -0
- package/docs/plugins/theme.md +176 -0
- package/docs/sidebar.json +12 -0
- package/package.json +2 -2
- package/src/component/base/events.js +4 -3
- package/src/component/base/methods.js +31 -4
- package/src/component.d.ts +15 -1
- package/src/component.js +17 -4
- package/src/components/Layout.js +1 -1
- package/src/components/Layout.orig.js +1 -1
- package/src/components/RouterView.js +1 -1
- package/src/components/RouterView.orig.js +1 -1
- package/src/engines/L3/element.js +27 -0
- package/src/engines/L3/element.test.js +112 -0
- package/src/engines/L3/launch.js +13 -11
- package/src/lib/codegenerator/generator.js +9 -0
- package/src/lib/eventListeners.js +42 -16
- package/src/lib/eventListeners.test.js +185 -0
- package/src/plugin.js +3 -4
- package/src/plugins/appstate.js +25 -0
- package/src/plugins/index.js +2 -0
- package/src/plugins/theme.js +88 -0
- package/docs/components/utilities.md +0 -3
|
@@ -23,6 +23,15 @@ import { Log } from '../../lib/log.js'
|
|
|
23
23
|
|
|
24
24
|
export default {
|
|
25
25
|
focus: {
|
|
26
|
+
value: function (e) {
|
|
27
|
+
Log.warn('this.focus is deprecated, use this.$focus instead')
|
|
28
|
+
return this.$focus(e)
|
|
29
|
+
},
|
|
30
|
+
writable: false,
|
|
31
|
+
enumerable: true,
|
|
32
|
+
configurable: false,
|
|
33
|
+
},
|
|
34
|
+
$focus: {
|
|
26
35
|
value: function (e) {
|
|
27
36
|
this[symbols.state].hasFocus = true
|
|
28
37
|
Focus.set(this, e)
|
|
@@ -54,6 +63,15 @@ export default {
|
|
|
54
63
|
configurable: false,
|
|
55
64
|
},
|
|
56
65
|
select: {
|
|
66
|
+
value: function (ref) {
|
|
67
|
+
Log.warn('this.select is deprecated, use this.$select instead')
|
|
68
|
+
return this.$select(ref)
|
|
69
|
+
},
|
|
70
|
+
writable: false,
|
|
71
|
+
enumerable: true,
|
|
72
|
+
configurable: false,
|
|
73
|
+
},
|
|
74
|
+
$select: {
|
|
57
75
|
value: function (ref) {
|
|
58
76
|
let selected = null
|
|
59
77
|
this[symbols.children].forEach((child) => {
|
|
@@ -77,8 +95,8 @@ export default {
|
|
|
77
95
|
},
|
|
78
96
|
trigger: {
|
|
79
97
|
value: function (key) {
|
|
80
|
-
|
|
81
|
-
trigger(
|
|
98
|
+
Log.warn('this.trigger is deprecated, use this.$trigger instead')
|
|
99
|
+
return this.$trigger(key)
|
|
82
100
|
},
|
|
83
101
|
writable: false,
|
|
84
102
|
enumerable: true,
|
|
@@ -86,8 +104,17 @@ export default {
|
|
|
86
104
|
},
|
|
87
105
|
$trigger: {
|
|
88
106
|
value: function (key) {
|
|
89
|
-
|
|
90
|
-
|
|
107
|
+
let target = this[symbols.originalState]
|
|
108
|
+
// when dot notation used, find the nested target
|
|
109
|
+
if (key.indexOf('.') > -1) {
|
|
110
|
+
const keys = key.split('.')
|
|
111
|
+
key = keys.pop(keys)
|
|
112
|
+
for (let i = 0; i < keys.length; i++) {
|
|
113
|
+
target = target[keys[i]]
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
// trigger with force set to true
|
|
117
|
+
trigger(target, key, true)
|
|
91
118
|
},
|
|
92
119
|
writable: false,
|
|
93
120
|
enumerable: true,
|
package/src/component.d.ts
CHANGED
|
@@ -265,7 +265,9 @@ declare namespace Component {
|
|
|
265
265
|
/**
|
|
266
266
|
* Listen to events emitted by other components
|
|
267
267
|
*/
|
|
268
|
-
$listen:
|
|
268
|
+
$listen: {
|
|
269
|
+
(event: string, callback: (args: any) => void, priority?: number): void;
|
|
270
|
+
}
|
|
269
271
|
|
|
270
272
|
/**
|
|
271
273
|
* Emit events that other components can listen to
|
|
@@ -300,6 +302,11 @@ declare namespace Component {
|
|
|
300
302
|
/**
|
|
301
303
|
* Set focus to the Component, optionally pass a KeyboardEvent for instant event bubbling
|
|
302
304
|
*/
|
|
305
|
+
$focus: (event?: KeyboardEvent) => void
|
|
306
|
+
/**
|
|
307
|
+
* @deprecated
|
|
308
|
+
* Deprecated: use `this.$focus()` instead
|
|
309
|
+
*/
|
|
303
310
|
focus: (event?: KeyboardEvent) => void
|
|
304
311
|
|
|
305
312
|
/**
|
|
@@ -317,6 +324,12 @@ declare namespace Component {
|
|
|
317
324
|
* }
|
|
318
325
|
* ```
|
|
319
326
|
*/
|
|
327
|
+
$select: (ref: string) => ComponentInstance | ElementInstance
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* @deprecated
|
|
331
|
+
* Deprecated: use `this.$select()` instead
|
|
332
|
+
*/
|
|
320
333
|
select: (ref: string) => ComponentInstance | ElementInstance
|
|
321
334
|
|
|
322
335
|
/**
|
|
@@ -345,6 +358,7 @@ declare namespace Component {
|
|
|
345
358
|
|
|
346
359
|
export interface ElementInstance {
|
|
347
360
|
focus?: () => void
|
|
361
|
+
$focus?: () => void
|
|
348
362
|
}
|
|
349
363
|
|
|
350
364
|
export interface ComponentConfig<Props extends string, S, M> {
|
package/src/component.js
CHANGED
|
@@ -171,11 +171,24 @@ const Component = (name = required('name'), config = required('config')) => {
|
|
|
171
171
|
// setup watchers if the components has watchers specified
|
|
172
172
|
if (this[symbols.watchers]) {
|
|
173
173
|
Object.keys(this[symbols.watchers]).forEach((watchKey) => {
|
|
174
|
-
let
|
|
174
|
+
let target = this
|
|
175
|
+
let key = watchKey
|
|
176
|
+
// when dot notation used, find the nested target
|
|
177
|
+
if (watchKey.indexOf('.') > -1) {
|
|
178
|
+
const keys = watchKey.split('.')
|
|
179
|
+
key = keys.pop()
|
|
180
|
+
for (let i = 0; i < keys.length; i++) {
|
|
181
|
+
target = target[keys[i]]
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
let old = this[key]
|
|
186
|
+
|
|
175
187
|
effect((force = false) => {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
188
|
+
const newValue = target[key]
|
|
189
|
+
if (old !== newValue || force === true) {
|
|
190
|
+
this[symbols.watchers][watchKey].apply(this, [newValue, old])
|
|
191
|
+
old = newValue
|
|
179
192
|
}
|
|
180
193
|
})
|
|
181
194
|
})
|
package/src/components/Layout.js
CHANGED
|
@@ -37,7 +37,7 @@ export default () => {
|
|
|
37
37
|
___layout() {
|
|
38
38
|
let offset = 0
|
|
39
39
|
|
|
40
|
-
this
|
|
40
|
+
this.$select('slot').children.forEach((el) => {
|
|
41
41
|
el.set(this.direction === 'vertical' ? 'y' : 'x', offset)
|
|
42
42
|
// todo: grab width from interface, not directly from node
|
|
43
43
|
offset +=
|
|
@@ -22,7 +22,7 @@ export default () => {
|
|
|
22
22
|
___layout() {
|
|
23
23
|
let offset = 0
|
|
24
24
|
|
|
25
|
-
this
|
|
25
|
+
this.$select('slot').children.forEach((el) => {
|
|
26
26
|
el.set(this.direction === 'vertical' ? 'y' : 'x', offset)
|
|
27
27
|
// todo: grab width from interface, not directly from node
|
|
28
28
|
offset +=
|
|
@@ -125,6 +125,28 @@ const propsTransformer = {
|
|
|
125
125
|
this.props['color'] = 0xffffffff
|
|
126
126
|
}
|
|
127
127
|
},
|
|
128
|
+
set fit(v) {
|
|
129
|
+
const resizeMode = {}
|
|
130
|
+
|
|
131
|
+
if (v === 'cover' || v === 'contain') {
|
|
132
|
+
this.props['textureOptions'] = { resizeMode: { type: v } }
|
|
133
|
+
return
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (typeof v === 'object' || (isObjectString(v) === true && (v = parseToObject(v)))) {
|
|
137
|
+
resizeMode['type'] = v.type || 'cover'
|
|
138
|
+
|
|
139
|
+
if (typeof v.position === 'number') {
|
|
140
|
+
resizeMode['clipY'] = resizeMode['clipX'] = v.position
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (typeof v.position === 'object') {
|
|
144
|
+
resizeMode['clipX'] = 'x' in v.position === true ? v.position.x : null
|
|
145
|
+
resizeMode['clipY'] = 'y' in v.position === true ? v.position.y : null
|
|
146
|
+
}
|
|
147
|
+
this.props['textureOptions'] = { resizeMode }
|
|
148
|
+
}
|
|
149
|
+
},
|
|
128
150
|
set rtt(v) {
|
|
129
151
|
this.props['rtt'] = v
|
|
130
152
|
if (v === true && this.raw['color'] === undefined) {
|
|
@@ -183,6 +205,11 @@ const propsTransformer = {
|
|
|
183
205
|
}
|
|
184
206
|
},
|
|
185
207
|
set effects(v) {
|
|
208
|
+
for (let i = 0; i < v.length; i++) {
|
|
209
|
+
if (v[i].props && v[i].props.color) {
|
|
210
|
+
v[i].props.color = colors.normalize(v[i].props.color)
|
|
211
|
+
}
|
|
212
|
+
}
|
|
186
213
|
this.props['shader'] = renderer.createShader('DynamicShader', {
|
|
187
214
|
effects: v,
|
|
188
215
|
})
|
|
@@ -159,6 +159,118 @@ test('Element - Set `texture` property', (assert) => {
|
|
|
159
159
|
assert.end()
|
|
160
160
|
})
|
|
161
161
|
|
|
162
|
+
test('Element - Set `fit` property with a string type value', (assert) => {
|
|
163
|
+
assert.capture(renderer, 'createNode', () => new EventEmitter())
|
|
164
|
+
const el = createElement()
|
|
165
|
+
|
|
166
|
+
const fitVal = 'contain'
|
|
167
|
+
el.set('fit', fitVal)
|
|
168
|
+
|
|
169
|
+
assert.ok(el.node['textureOptions'] instanceof Object, 'textureOptions should be an object')
|
|
170
|
+
assert.ok(
|
|
171
|
+
el.node['textureOptions']['resizeMode'] instanceof Object,
|
|
172
|
+
'resizeMode should be an object'
|
|
173
|
+
)
|
|
174
|
+
assert.equal(
|
|
175
|
+
el.node['textureOptions']['resizeMode']['type'],
|
|
176
|
+
fitVal,
|
|
177
|
+
'Node resizeMode "type" parameter should be set'
|
|
178
|
+
)
|
|
179
|
+
assert.equal(
|
|
180
|
+
el.props.props['textureOptions']['resizeMode']['type'],
|
|
181
|
+
fitVal,
|
|
182
|
+
'Props textureOptions parameter should be set'
|
|
183
|
+
)
|
|
184
|
+
assert.equal(el.props.raw.get('fit'), fitVal, "Props' raw map entry should be added")
|
|
185
|
+
assert.end()
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
test('Element - Set `fit` property with an object type value', (assert) => {
|
|
189
|
+
assert.capture(renderer, 'createNode', () => new EventEmitter())
|
|
190
|
+
const el = createElement()
|
|
191
|
+
|
|
192
|
+
const fitVal = "{type: 'cover', position: {x: 0, y:1}}"
|
|
193
|
+
el.set('fit', fitVal)
|
|
194
|
+
|
|
195
|
+
assert.equal(
|
|
196
|
+
el.node['textureOptions']['resizeMode']['type'],
|
|
197
|
+
'cover',
|
|
198
|
+
'Node resizeMode "type" parameter should be set'
|
|
199
|
+
)
|
|
200
|
+
assert.equal(
|
|
201
|
+
el.props.props['textureOptions']['resizeMode']['type'],
|
|
202
|
+
'cover',
|
|
203
|
+
'Props textureOptions parameter should be set'
|
|
204
|
+
)
|
|
205
|
+
assert.equal(
|
|
206
|
+
el.node['textureOptions']['resizeMode']['clipX'],
|
|
207
|
+
0,
|
|
208
|
+
'Node resizeMode "clipX" parameter should be set to correct value'
|
|
209
|
+
)
|
|
210
|
+
assert.equal(
|
|
211
|
+
el.node['textureOptions']['resizeMode']['clipY'],
|
|
212
|
+
1,
|
|
213
|
+
'Node resizeMode "clipY" parameter should be set to correct value'
|
|
214
|
+
)
|
|
215
|
+
assert.equal(el.props.raw.get('fit'), fitVal, "Props' raw map entry should be added")
|
|
216
|
+
assert.end()
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
test('Element - Set `fit` property with position key as a string type value', (assert) => {
|
|
220
|
+
assert.capture(renderer, 'createNode', () => new EventEmitter())
|
|
221
|
+
const el = createElement()
|
|
222
|
+
|
|
223
|
+
const fitVal = "{type: 'cover', position: 1}"
|
|
224
|
+
el.set('fit', fitVal)
|
|
225
|
+
|
|
226
|
+
assert.equal(
|
|
227
|
+
el.node['textureOptions']['resizeMode']['type'],
|
|
228
|
+
'cover',
|
|
229
|
+
'Node resizeMode "type" parameter should be set'
|
|
230
|
+
)
|
|
231
|
+
assert.equal(
|
|
232
|
+
el.props.props['textureOptions']['resizeMode']['type'],
|
|
233
|
+
'cover',
|
|
234
|
+
'Props textureOptions parameter should be set'
|
|
235
|
+
)
|
|
236
|
+
assert.equal(
|
|
237
|
+
el.node['textureOptions']['resizeMode']['clipX'],
|
|
238
|
+
1,
|
|
239
|
+
'Node resizeMode "clipX" parameter should be set to correct value'
|
|
240
|
+
)
|
|
241
|
+
assert.equal(
|
|
242
|
+
el.node['textureOptions']['resizeMode']['clipY'],
|
|
243
|
+
1,
|
|
244
|
+
'Node resizeMode "clipY" parameter should be set to correct value'
|
|
245
|
+
)
|
|
246
|
+
assert.equal(el.props.raw.get('fit'), fitVal, "Props' raw map entry should be added")
|
|
247
|
+
assert.end()
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
test('Element - Set `fit` property should not set not required keys', (assert) => {
|
|
251
|
+
assert.capture(renderer, 'createNode', () => new EventEmitter())
|
|
252
|
+
const el = createElement()
|
|
253
|
+
|
|
254
|
+
const fitVal = "{type: 'cover', dummy1: 0, dummy2: 0}"
|
|
255
|
+
el.set('fit', fitVal)
|
|
256
|
+
|
|
257
|
+
assert.equal(
|
|
258
|
+
el.node['textureOptions']['resizeMode']['type'],
|
|
259
|
+
'cover',
|
|
260
|
+
'Node resizeMode "type" parameter should be set'
|
|
261
|
+
)
|
|
262
|
+
assert.equal(
|
|
263
|
+
el.props.props['textureOptions']['resizeMode']['dummy1'],
|
|
264
|
+
undefined,
|
|
265
|
+
'Props resizeMode "dummy1" parameter should not be set'
|
|
266
|
+
)
|
|
267
|
+
assert.equal(
|
|
268
|
+
el.props.props['textureOptions']['resizeMode']['dummy2'],
|
|
269
|
+
undefined,
|
|
270
|
+
'Props resizeMode "dummy2" parameter should not be set'
|
|
271
|
+
)
|
|
272
|
+
assert.end()
|
|
273
|
+
})
|
|
162
274
|
function createElement() {
|
|
163
275
|
const el = element({}, {})
|
|
164
276
|
el.populate({
|
package/src/engines/L3/launch.js
CHANGED
|
@@ -16,6 +16,10 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
import { RendererMain } from '@lightningjs/renderer'
|
|
19
|
+
import { WebGlCoreRenderer, SdfTextRenderer } from '@lightningjs/renderer/webgl'
|
|
20
|
+
import { CanvasCoreRenderer, CanvasTextRenderer } from '@lightningjs/renderer/canvas'
|
|
21
|
+
import { Inspector } from '@lightningjs/renderer/inspector'
|
|
22
|
+
|
|
19
23
|
import { Log } from '../../lib/log.js'
|
|
20
24
|
import { SCREEN_RESOLUTIONS } from '../../constants.js'
|
|
21
25
|
import colors from '../../lib/colors/colors.js'
|
|
@@ -24,17 +28,14 @@ import shaderLoader from './shaderLoader.js'
|
|
|
24
28
|
|
|
25
29
|
export let renderer = {}
|
|
26
30
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
Log.warn(
|
|
30
|
-
`
|
|
31
|
+
const renderEngine = (settings) => {
|
|
32
|
+
const renderMode = 'renderMode' in settings ? settings.renderMode : 'webgl'
|
|
31
33
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
)
|
|
36
|
-
}
|
|
34
|
+
if (renderMode === 'webgl') return WebGlCoreRenderer
|
|
35
|
+
if (renderMode === 'canvas') return CanvasCoreRenderer
|
|
36
|
+
}
|
|
37
37
|
|
|
38
|
+
export default (App, target, settings = {}) => {
|
|
38
39
|
renderer = new RendererMain(
|
|
39
40
|
{
|
|
40
41
|
appWidth: settings.w || 1920,
|
|
@@ -50,12 +51,13 @@ You can remove the option from your \`src/index.js\`-file. And you can safely re
|
|
|
50
51
|
? settings.webWorkersLimit
|
|
51
52
|
: window.navigator.hardwareConcurrency || 2,
|
|
52
53
|
clearColor: (settings.canvasColor && colors.normalize(settings.canvasColor)) || 0x00000000,
|
|
53
|
-
|
|
54
|
+
inspector: settings.inspector === true ? Inspector : undefined,
|
|
54
55
|
boundsMargin: settings.viewportMargin || 0,
|
|
55
56
|
// gpu memory limit, converted from mb to bytes - defaults to 200mb
|
|
56
57
|
txMemByteThreshold:
|
|
57
58
|
'gpuMemoryLimit' in settings ? settings.gpuMemoryLimit * 1024 * 1024 : 200 * 1024 * 1024,
|
|
58
|
-
|
|
59
|
+
renderEngine: renderEngine(settings),
|
|
60
|
+
fontEngines: [SdfTextRenderer, CanvasTextRenderer],
|
|
59
61
|
},
|
|
60
62
|
target
|
|
61
63
|
)
|
|
@@ -300,6 +300,14 @@ const generateForLoopCode = function (templateObject, parent) {
|
|
|
300
300
|
ctx.renderCode.push(`parent = ${parent}`)
|
|
301
301
|
}
|
|
302
302
|
|
|
303
|
+
const indexRegex = new RegExp(`\\$${index}(?!['\\w])`)
|
|
304
|
+
const indexResult = indexRegex.exec(key)
|
|
305
|
+
if (Array.isArray(indexResult)) {
|
|
306
|
+
ctx.renderCode.push(
|
|
307
|
+
`console.warn(" Using '${index}' in the key, like key=${key}, is not recommended")`
|
|
308
|
+
)
|
|
309
|
+
}
|
|
310
|
+
|
|
303
311
|
const forStartCounter = counter
|
|
304
312
|
|
|
305
313
|
ctx.renderCode.push(`
|
|
@@ -310,6 +318,7 @@ const generateForLoopCode = function (templateObject, parent) {
|
|
|
310
318
|
let l = rawCollection.length
|
|
311
319
|
while(l--) {
|
|
312
320
|
const ${item} = rawCollection[l]
|
|
321
|
+
const ${index} = l
|
|
313
322
|
keys.add('' + ${interpolate(key, '') || 'l'})
|
|
314
323
|
}
|
|
315
324
|
`)
|
|
@@ -16,40 +16,66 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
const eventsMap = new Map()
|
|
19
|
+
const callbackCache = new Map()
|
|
19
20
|
|
|
20
21
|
export default {
|
|
21
|
-
registerListener(component, event, cb) {
|
|
22
|
+
registerListener(component, event, cb, priority = 0) {
|
|
22
23
|
let componentsMap = eventsMap.get(event)
|
|
23
|
-
if (
|
|
24
|
+
if (componentsMap === undefined) {
|
|
24
25
|
componentsMap = new Map()
|
|
25
26
|
eventsMap.set(event, componentsMap)
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
let components = componentsMap.get(component)
|
|
29
|
-
if (
|
|
30
|
+
if (components === undefined) {
|
|
30
31
|
components = new Set()
|
|
31
32
|
componentsMap.set(component, components)
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
components.add(cb)
|
|
35
|
+
components.add({ cb, priority })
|
|
36
|
+
callbackCache.delete(event) // Invalidate the callbackCache when a new callback is added
|
|
35
37
|
},
|
|
38
|
+
|
|
36
39
|
executeListeners(event, params) {
|
|
37
40
|
const componentsMap = eventsMap.get(event)
|
|
38
|
-
if (componentsMap) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
if (componentsMap === undefined || componentsMap.size === 0) {
|
|
42
|
+
return true // No listeners, so execution can be considered successful
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (callbackCache.has(event) === false) {
|
|
46
|
+
const allCallbacks = []
|
|
47
|
+
for (const [component, components] of componentsMap) {
|
|
48
|
+
for (const callbackObj of components) {
|
|
49
|
+
allCallbacks.push({ ...callbackObj, component })
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
allCallbacks.sort((a, b) => b.priority - a.priority)
|
|
53
|
+
callbackCache.set(event, allCallbacks) // callbackCache the sorted callbacks with component context
|
|
44
54
|
}
|
|
55
|
+
|
|
56
|
+
const callbacks = callbackCache.get(event)
|
|
57
|
+
for (let i = 0; i < callbacks.length; i++) {
|
|
58
|
+
const { cb, component } = callbacks[i]
|
|
59
|
+
const result = cb.call(component, params)
|
|
60
|
+
if (result === false) {
|
|
61
|
+
return false // Stop propagation if any listener returns false
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return true // All listeners executed without stopping propagation
|
|
45
66
|
},
|
|
67
|
+
|
|
46
68
|
removeListeners(component) {
|
|
47
|
-
eventsMap
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
69
|
+
for (const [event, componentsMap] of eventsMap) {
|
|
70
|
+
if (componentsMap.has(component)) {
|
|
71
|
+
componentsMap.delete(component)
|
|
72
|
+
callbackCache.delete(event) // Invalidate the callbackCache for this event
|
|
73
|
+
|
|
74
|
+
// If no more components for this event, remove the event entirely
|
|
75
|
+
if (componentsMap.size === 0) {
|
|
76
|
+
eventsMap.delete(event)
|
|
77
|
+
}
|
|
52
78
|
}
|
|
53
|
-
}
|
|
79
|
+
}
|
|
54
80
|
},
|
|
55
81
|
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 Comcast Cable Communications Management, LLC
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*
|
|
15
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import test from 'tape'
|
|
19
|
+
import eventListener from './eventListeners.js'
|
|
20
|
+
|
|
21
|
+
const resetModule = () => {
|
|
22
|
+
eventListener.removeListeners('testComponent')
|
|
23
|
+
eventListener.removeListeners('anotherComponent')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
test('registerListener', (t) => {
|
|
27
|
+
resetModule()
|
|
28
|
+
|
|
29
|
+
const component = 'testComponent'
|
|
30
|
+
const event = 'testEvent'
|
|
31
|
+
const cb = () => {}
|
|
32
|
+
|
|
33
|
+
t.doesNotThrow(
|
|
34
|
+
() => eventListener.registerListener(component, event, cb),
|
|
35
|
+
'Should not throw exception when registering a listener'
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
t.doesNotThrow(
|
|
39
|
+
() => eventListener.registerListener(component, event, cb, 1),
|
|
40
|
+
'Should not throw exception when registering a listener with priority'
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
t.doesNotThrow(
|
|
44
|
+
() => eventListener.registerListener('anotherComponent', event, cb),
|
|
45
|
+
'Should not throw exception when registering multiple components for the same event'
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
t.end()
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('executeListeners - no listeners', (t) => {
|
|
52
|
+
resetModule()
|
|
53
|
+
|
|
54
|
+
const result = eventListener.executeListeners('nonExistentEvent')
|
|
55
|
+
t.equal(result, true, 'Should return true when no listeners are registered')
|
|
56
|
+
|
|
57
|
+
t.end()
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
test('executeListeners - with listeners', (t) => {
|
|
61
|
+
resetModule()
|
|
62
|
+
|
|
63
|
+
const component = 'testComponent'
|
|
64
|
+
const event = 'testEvent'
|
|
65
|
+
let callCount = 0
|
|
66
|
+
const cb1 = () => {
|
|
67
|
+
callCount++
|
|
68
|
+
}
|
|
69
|
+
const cb2 = () => {
|
|
70
|
+
callCount++
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
eventListener.registerListener(component, event, cb1)
|
|
74
|
+
eventListener.registerListener(component, event, cb2)
|
|
75
|
+
|
|
76
|
+
const result = eventListener.executeListeners(event)
|
|
77
|
+
t.equal(result, true, 'Should return true when all listeners execute successfully')
|
|
78
|
+
t.equal(callCount, 2, 'Should call all registered callbacks')
|
|
79
|
+
|
|
80
|
+
t.end()
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test('executeListeners - priority order', (t) => {
|
|
84
|
+
resetModule()
|
|
85
|
+
|
|
86
|
+
const component = 'testComponent'
|
|
87
|
+
const event = 'testEvent'
|
|
88
|
+
const order = []
|
|
89
|
+
const cb1 = () => {
|
|
90
|
+
order.push(1)
|
|
91
|
+
}
|
|
92
|
+
const cb2 = () => {
|
|
93
|
+
order.push(2)
|
|
94
|
+
}
|
|
95
|
+
const cb3 = () => {
|
|
96
|
+
order.push(3)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
eventListener.registerListener(component, event, cb1, 1)
|
|
100
|
+
eventListener.registerListener(component, event, cb2, 3)
|
|
101
|
+
eventListener.registerListener(component, event, cb3, 2)
|
|
102
|
+
|
|
103
|
+
eventListener.executeListeners(event)
|
|
104
|
+
t.deepEqual(order, [2, 3, 1], 'Should execute callbacks in priority order')
|
|
105
|
+
|
|
106
|
+
t.end()
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test('executeListeners - stop propagation', (t) => {
|
|
110
|
+
resetModule()
|
|
111
|
+
|
|
112
|
+
const component = 'testComponent'
|
|
113
|
+
const event = 'testEvent'
|
|
114
|
+
let callCount = 0
|
|
115
|
+
const cb1 = () => {
|
|
116
|
+
callCount++
|
|
117
|
+
return false
|
|
118
|
+
}
|
|
119
|
+
const cb2 = () => {
|
|
120
|
+
callCount++
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
eventListener.registerListener(component, event, cb1)
|
|
124
|
+
eventListener.registerListener(component, event, cb2)
|
|
125
|
+
|
|
126
|
+
const result = eventListener.executeListeners(event)
|
|
127
|
+
t.equal(result, false, 'Should return false when propagation is stopped')
|
|
128
|
+
t.equal(callCount, 1, 'Should stop calling callbacks after one returns false')
|
|
129
|
+
|
|
130
|
+
t.end()
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
test('removeListeners', (t) => {
|
|
134
|
+
resetModule()
|
|
135
|
+
|
|
136
|
+
const component = 'testComponent'
|
|
137
|
+
const event1 = 'testEvent1'
|
|
138
|
+
const event2 = 'testEvent2'
|
|
139
|
+
const cb = () => {}
|
|
140
|
+
|
|
141
|
+
eventListener.registerListener(component, event1, cb)
|
|
142
|
+
eventListener.registerListener(component, event2, cb)
|
|
143
|
+
eventListener.registerListener('anotherComponent', event1, cb)
|
|
144
|
+
|
|
145
|
+
eventListener.removeListeners(component)
|
|
146
|
+
|
|
147
|
+
const result1 = eventListener.executeListeners(event1)
|
|
148
|
+
t.equal(result1, true, 'Should still return true for event1 as another component is registered')
|
|
149
|
+
|
|
150
|
+
const result2 = eventListener.executeListeners(event2)
|
|
151
|
+
t.equal(result2, true, 'Should return true for event2 as no listeners remain')
|
|
152
|
+
|
|
153
|
+
t.end()
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
test('cache invalidation', (t) => {
|
|
157
|
+
resetModule()
|
|
158
|
+
|
|
159
|
+
const component = 'testComponent'
|
|
160
|
+
const event = 'testEvent'
|
|
161
|
+
let callOrder = []
|
|
162
|
+
const cb1 = () => {
|
|
163
|
+
callOrder.push(1)
|
|
164
|
+
}
|
|
165
|
+
const cb2 = () => {
|
|
166
|
+
callOrder.push(2)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
eventListener.registerListener(component, event, cb1)
|
|
170
|
+
eventListener.executeListeners(event)
|
|
171
|
+
|
|
172
|
+
eventListener.registerListener(component, event, cb2, 1)
|
|
173
|
+
eventListener.executeListeners(event)
|
|
174
|
+
|
|
175
|
+
t.deepEqual(callOrder, [1, 2, 1], 'Should invalidate cache when new listener is added')
|
|
176
|
+
|
|
177
|
+
callOrder = []
|
|
178
|
+
eventListener.removeListeners(component)
|
|
179
|
+
eventListener.registerListener(component, event, cb1)
|
|
180
|
+
eventListener.executeListeners(event)
|
|
181
|
+
|
|
182
|
+
t.deepEqual(callOrder, [1], 'Should invalidate cache when listeners are removed')
|
|
183
|
+
|
|
184
|
+
t.end()
|
|
185
|
+
})
|