@lightningjs/blits 1.3.0 → 1.4.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.
@@ -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
+ })
@@ -11,10 +11,10 @@ export default {
11
11
  init: Symbol('init'),
12
12
  inputEvents: Symbol('inputEvents'),
13
13
  intervals: Symbol('intervals'),
14
+ launched: Symbol('launched'),
14
15
  level: Symbol('level'),
15
16
  methodKeys: Symbol('methodKeys'),
16
17
  originalState: Symbol('originalState'),
17
- pluginsRegistered: Symbol('pluginsRegistered'),
18
18
  propKeys: Symbol('propKeys'),
19
19
  raw: Symbol('raw'),
20
20
  ready: Symbol('ready'),
package/src/plugin.js CHANGED
@@ -1,22 +1,26 @@
1
- export const plugins = {}
1
+ import log from './plugins/log'
2
+
3
+ export const plugins = {
4
+ // log plugin added by default
5
+ log,
6
+ }
2
7
 
3
8
  const registerPlugin = (plugin, nameOrOptions = '', options = {}) => {
4
9
  // map name and options depending on the arguments provided
5
- let name
10
+ let name = undefined
6
11
  if (typeof nameOrOptions === 'object') {
7
12
  options = nameOrOptions
8
- name = ''
9
13
  } else {
10
14
  name = nameOrOptions
11
15
  }
12
16
 
13
17
  if (typeof plugin === 'function') {
14
- if (name === '') {
18
+ if (name === undefined) {
15
19
  throw Error('Error registering plugin: name is required for plugin')
16
20
  }
17
21
  plugins[name] = { plugin, options }
18
22
  } else if (plugin.plugin) {
19
- registerPlugin(plugin.plugin, plugin.name, options)
23
+ registerPlugin(plugin.plugin, name || plugin.name, options)
20
24
  }
21
25
  }
22
26
 
@@ -0,0 +1,25 @@
1
+ /*
2
+ * Copyright 2024 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 { reactive } from '../lib/reactivity/reactive'
19
+
20
+ export default {
21
+ name: 'appState',
22
+ plugin(state = {}) {
23
+ return reactive(state)
24
+ },
25
+ }
@@ -16,3 +16,5 @@
16
16
  */
17
17
 
18
18
  export { default as language } from './language.js'
19
+ export { default as theme } from './theme.js'
20
+ export { default as appState } from './appstate.js'
@@ -15,13 +15,11 @@
15
15
  * SPDX-License-Identifier: Apache-2.0
16
16
  */
17
17
 
18
- import log from '../../lib/log.js'
18
+ import { default as log } from '../lib/log'
19
19
 
20
20
  export default {
21
- $log: {
22
- value: log('App'),
23
- writable: false,
24
- enumerable: true,
25
- configurable: false,
21
+ name: 'log',
22
+ plugin() {
23
+ return log('App')
26
24
  },
27
25
  }
@@ -0,0 +1,88 @@
1
+ /*
2
+ * Copyright 2024 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 { Log } from '../lib/log'
19
+ import { reactive } from '../lib/reactivity/reactive.js'
20
+
21
+ const getValueByDotNotation = (obj, base, path) => {
22
+ const keys = path.split('.')
23
+
24
+ for (let i = 0; i < keys.length; i++) {
25
+ const key = keys[i]
26
+
27
+ if (obj !== null && key in obj === true) {
28
+ obj = obj[key]
29
+ base = base !== null && key in base === true ? base[key] : null
30
+ }
31
+ // see if the key is present in the base object
32
+ else if (base !== null && key in base === true) {
33
+ base = base[key]
34
+ obj = base
35
+ } else {
36
+ obj = undefined
37
+ break
38
+ }
39
+ }
40
+
41
+ return obj
42
+ }
43
+
44
+ export default {
45
+ name: 'theme',
46
+ plugin(config = {}) {
47
+ let themeMap = {}
48
+
49
+ const state = reactive({
50
+ current: 'default',
51
+ })
52
+
53
+ let themes = {}
54
+ let base = null
55
+ if ('themes' in config === true) {
56
+ themes = config.themes
57
+ state.current = config.current || 'default'
58
+ base = config.base || 'default'
59
+ } else {
60
+ themes = { default: config }
61
+ }
62
+
63
+ return {
64
+ get(key, fallback) {
65
+ // reference state.current, to trigger reactive effects for
66
+ // theme get function when current theme is modified
67
+ state.current
68
+ if (themeMap[key] !== undefined) return themeMap[key]
69
+ const value = getValueByDotNotation(themes[state.current], themes[base], key)
70
+ if (value !== undefined) {
71
+ // store the value for next time
72
+ themeMap[key] = value
73
+ return value
74
+ }
75
+ return fallback
76
+ },
77
+
78
+ set(theme) {
79
+ if (theme in themes) {
80
+ themeMap = {}
81
+ state.current = theme
82
+ } else {
83
+ Log.warn(`Theme ${theme} not found`)
84
+ }
85
+ },
86
+ }
87
+ },
88
+ }
@@ -1,3 +0,0 @@
1
-
2
- # Utilities
3
-