@lightningjs/blits 0.3.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 (79) hide show
  1. package/.editorconfig +7 -0
  2. package/.eslintignore +1 -0
  3. package/.eslintrc.cjs +60 -0
  4. package/.vscode/extensions.json +6 -0
  5. package/.vscode/settings.json +9 -0
  6. package/LICENSE +202 -0
  7. package/NOTICE +3 -0
  8. package/README.md +11 -0
  9. package/bin/index.cjs +25 -0
  10. package/boilerplate/.editorconfig +7 -0
  11. package/boilerplate/.eslintignore +3 -0
  12. package/boilerplate/.eslintrc.js +38 -0
  13. package/boilerplate/.vscode/extensions.json +7 -0
  14. package/boilerplate/.vscode/launch.json +12 -0
  15. package/boilerplate/.vscode/settings.json +9 -0
  16. package/boilerplate/index.html +11 -0
  17. package/boilerplate/package.json +36 -0
  18. package/boilerplate/public/assets/logo.png +0 -0
  19. package/boilerplate/src/App.js +12 -0
  20. package/boilerplate/src/components/Loader.js +23 -0
  21. package/boilerplate/src/index.js +11 -0
  22. package/boilerplate/src/pages/Home.js +15 -0
  23. package/boilerplate/src/renderer.js +3 -0
  24. package/boilerplate/vite.config.js +23 -0
  25. package/docs/.nojekyll +0 -0
  26. package/docs/README.md +30 -0
  27. package/docs/_sidebar.md +9 -0
  28. package/docs/component_state.md +21 -0
  29. package/docs/components.md +27 -0
  30. package/docs/computed_properties.md +18 -0
  31. package/docs/getting_started.md +40 -0
  32. package/docs/index.html +47 -0
  33. package/docs/lifecycle_events.md +46 -0
  34. package/docs/props.md +44 -0
  35. package/docs/template_syntax.md +31 -0
  36. package/docs/user_input.md +64 -0
  37. package/docs/watchers.md +38 -0
  38. package/index.js +26 -0
  39. package/package.json +44 -0
  40. package/src/application.js +55 -0
  41. package/src/component.js +192 -0
  42. package/src/component.test.js +51 -0
  43. package/src/components/Circle.js +31 -0
  44. package/src/components/Image.js +33 -0
  45. package/src/components/RouterView.js +38 -0
  46. package/src/components/Sprite.js +56 -0
  47. package/src/components/Text.js +61 -0
  48. package/src/element.js +236 -0
  49. package/src/focus.js +65 -0
  50. package/src/helpers/sequence.js +26 -0
  51. package/src/launch.js +61 -0
  52. package/src/lib/codegenerator/generator.js +276 -0
  53. package/src/lib/codegenerator/generator.test.js +970 -0
  54. package/src/lib/colors/colors.js +70 -0
  55. package/src/lib/colors/colors.test.js +190 -0
  56. package/src/lib/colors/htmlColors.js +168 -0
  57. package/src/lib/componentId.js +27 -0
  58. package/src/lib/componentId.test.js +83 -0
  59. package/src/lib/eventListeners.js +55 -0
  60. package/src/lib/hooks.js +29 -0
  61. package/src/lib/log.js +106 -0
  62. package/src/lib/reactivity/effect.js +54 -0
  63. package/src/lib/reactivity/effect.test.js +35 -0
  64. package/src/lib/reactivity/reactive.js +113 -0
  65. package/src/lib/reactivity/reactive.test.js +27 -0
  66. package/src/lib/setup/base.js +218 -0
  67. package/src/lib/setup/computed.js +43 -0
  68. package/src/lib/setup/input.js +28 -0
  69. package/src/lib/setup/methods.js +35 -0
  70. package/src/lib/setup/props.js +53 -0
  71. package/src/lib/setup/props.test.js +205 -0
  72. package/src/lib/setup/routes.js +24 -0
  73. package/src/lib/setup/state.js +45 -0
  74. package/src/lib/setup/watch.js +33 -0
  75. package/src/lib/templateparser/parser.js +103 -0
  76. package/src/lib/templateparser/parser.test.js +769 -0
  77. package/src/router.js +67 -0
  78. package/src/settings.js +34 -0
  79. package/vite/index.js +1 -0
@@ -0,0 +1,70 @@
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 htmlColors from './htmlColors.js'
19
+ import { Log } from '../log.js'
20
+
21
+ export default {
22
+ normalize(color = '') {
23
+ color = color.toString()
24
+
25
+ if (color.startsWith('0x')) {
26
+ return color
27
+ }
28
+
29
+ const rgbaRegex = /rgba?\((.+)\)/gi
30
+ const hslaRegex = /hsla?\((.+)\)/gi
31
+
32
+ // RGB(A) color format
33
+ if (rgbaRegex.test(color)) {
34
+ const match = new RegExp(rgbaRegex).exec(color)
35
+ if (match[1]) {
36
+ color = match[1]
37
+ .split(',')
38
+ .map((c, i) => {
39
+ if (i == 3) {
40
+ c = Math.min(Math.max(Math.round(c * 255), 0), 255)
41
+ }
42
+ return parseInt(c).toString(16).padStart(2, '0')
43
+ })
44
+ .join('')
45
+ return color.padEnd(8, 'f').padStart(10, '0x')
46
+ }
47
+ }
48
+ // HSL(A) color format
49
+ else if (hslaRegex.test(color)) {
50
+ Log.warn('HSL(A) color format is not supported yet')
51
+ return 0xffffffff
52
+ }
53
+ // HTMl name color format
54
+ else if (color in htmlColors) {
55
+ return htmlColors[color]
56
+ }
57
+ // hex rgba format
58
+ else if (color.startsWith('#')) {
59
+ color = color.substring(1)
60
+ }
61
+ if (color.length === 3) {
62
+ color = color
63
+ .split('')
64
+ .map((c) => c + c)
65
+ .join('')
66
+ }
67
+
68
+ return color.padEnd(8, 'f').padStart(10, '0x')
69
+ },
70
+ }
@@ -0,0 +1,190 @@
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 colors from './colors.js'
20
+
21
+ test('Object', (assert) => {
22
+ const expected = 'object'
23
+ const actual = typeof colors
24
+
25
+ assert.equal(actual, expected, 'Colors should be an object')
26
+ assert.end()
27
+ })
28
+
29
+ test('6 character hex', (assert) => {
30
+ const input = ['#ffffff', '#000000', '#c0ffee', '#7dd3fc', '#0c4a6e']
31
+ const expected = ['0xffffffff', '0xff000000', '0xffeeffc0', '0xfffcd37d', '0xff6e4a0c']
32
+
33
+ const actual = input.map(colors.normalize)
34
+
35
+ assert.deepEqual(
36
+ actual,
37
+ expected,
38
+ 'A 6 character hex code should return the correct color in abgr format'
39
+ )
40
+ assert.end()
41
+ })
42
+
43
+ test('3 character hex', (assert) => {
44
+ const input = ['#fff', '#000', '#c8c']
45
+ const expected = ['0xffffffff', '0xff000000', '0xffcc88cc']
46
+
47
+ const actual = input.map(colors.normalize)
48
+
49
+ assert.deepEqual(
50
+ actual,
51
+ expected,
52
+ 'A 3 character hex code should return the correct color in abgr format'
53
+ )
54
+ assert.end()
55
+ })
56
+
57
+ test('8 character hex (rgba)', (assert) => {
58
+ const input = ['#ffffffff', '#000000ff', '#c0ffee80', '#7dd3fc10', '#0c4a6eff']
59
+ const expected = ['0xffffffff', '0xff000000', '0x80eeffc0', '0x10fcd37d', '0xff6e4a0c']
60
+
61
+ const actual = input.map(colors.normalize)
62
+
63
+ assert.deepEqual(
64
+ actual,
65
+ expected,
66
+ 'A 8 character hex (rgba) code should return the correct color in abgr format'
67
+ )
68
+ assert.end()
69
+ })
70
+
71
+ test('RGB colors', (assert) => {
72
+ const input = [
73
+ 'rgb(255,255,255)',
74
+ 'rgb(0,0,0)',
75
+ 'rgb(192, 255, 238)',
76
+ 'rgb(125, 211, 252)',
77
+ 'rgb(12, 74, 110)',
78
+ ]
79
+ const expected = ['0xffffffff', '0xff000000', '0xffeeffc0', '0xfffcd37d', '0xff6e4a0c']
80
+
81
+ const actual = input.map(colors.normalize)
82
+
83
+ assert.deepEqual(
84
+ actual,
85
+ expected,
86
+ 'RGB codes prefixed with # should return the correct color in abgr format'
87
+ )
88
+ assert.end()
89
+ })
90
+
91
+ test('RGBA colors', (assert) => {
92
+ const input = [
93
+ 'rgb(255,255,255,1)',
94
+ 'rgb(0,0,0,1)',
95
+ 'rgb(192, 255, 238, 0.5)',
96
+ 'rgb(125, 211, 252, 0.1)',
97
+ 'rgb(12, 74, 110, 0)',
98
+ 'rgb(12, 74, 110, -0.5)', // negative alpha should become 0
99
+ 'rgb(12, 74, 110, 1.2)', // highher than 1 alpha should become 1
100
+ ]
101
+ const expected = [
102
+ '0xffffffff',
103
+ '0xff000000',
104
+ '0x80eeffc0',
105
+ '0x1afcd37d',
106
+ '0x006e4a0c',
107
+ '0x006e4a0c',
108
+ '0xff6e4a0c',
109
+ ]
110
+
111
+ const actual = input.map(colors.normalize)
112
+
113
+ assert.deepEqual(
114
+ actual,
115
+ expected,
116
+ 'RGB codes prefixed with # should return the correct color in abgr format'
117
+ )
118
+ assert.end()
119
+ })
120
+
121
+ test('RGBA colors', (assert) => {
122
+ const input = [
123
+ 'rgb(255,255,255,1)',
124
+ 'rgb(0,0,0,1)',
125
+ 'rgb(192, 255, 238, 0.5)',
126
+ 'rgb(125, 211, 252, 0.1)',
127
+ 'rgb(12, 74, 110, 0)',
128
+ 'rgb(12, 74, 110, -0.5)', // negative alpha should become 0
129
+ 'rgb(12, 74, 110, 1.2)', // highher than 1 alpha should become 1
130
+ ]
131
+ const expected = [
132
+ '0xffffffff',
133
+ '0xff000000',
134
+ '0x80eeffc0',
135
+ '0x1afcd37d',
136
+ '0x006e4a0c',
137
+ '0x006e4a0c',
138
+ '0xff6e4a0c',
139
+ ]
140
+
141
+ const actual = input.map(colors.normalize)
142
+
143
+ assert.deepEqual(
144
+ actual,
145
+ expected,
146
+ 'RGB codes prefixed with # should return the correct color in abgr format'
147
+ )
148
+ assert.end()
149
+ })
150
+
151
+ test('HTML colors', (assert) => {
152
+ const input = ['blanchedalmond', 'aqua', 'tomato', 'saddlebrown', 'red']
153
+
154
+ const expected = [
155
+ parseInt('0xffcdebff', 16),
156
+ parseInt('0xffffff00', 16),
157
+ parseInt('0xff4763ff', 16),
158
+ parseInt('0xff13458b', 16),
159
+ parseInt('0xff0000ff', 16),
160
+ ]
161
+
162
+ const actual = input.map(colors.normalize)
163
+
164
+ assert.deepEqual(
165
+ actual,
166
+ expected,
167
+ 'RGB codes prefixed with # should return the correct color in abgr format'
168
+ )
169
+ assert.end()
170
+ })
171
+
172
+ // test('hsl', (assert) => {
173
+ // const input = [
174
+ // 'hsl(0,0,100)',
175
+ // 'hsl(0,0, 0)',
176
+ // 'hsl(164,100,88)',
177
+ // 'hsl(199,95,74)',
178
+ // 'hsl(202,80,24)',
179
+ // ]
180
+ // const expected = ['0xffffffff', '0xff000000', '0xffeeffc0', '0xfffcd37d', '0xff6e4a0c']
181
+
182
+ // const actual = input.map(colors.normalize)
183
+
184
+ // assert.deepEqual(actual, expected, 'A hsl code should return the correct color in abgr format')
185
+ // assert.end()
186
+ // })
187
+
188
+ // hls()
189
+ // hlsa()
190
+ // html color (red, blue, pink)
@@ -0,0 +1,168 @@
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
+ export default {
19
+ aliceblue: 0xf0f8ffff,
20
+ antiquewhite: 0xfaebd7ff,
21
+ aqua: 0xffffff00,
22
+ aquamarine: 0x7fffd4ff,
23
+ azure: 0xf0ffffff,
24
+ beige: 0xf5f5dcff,
25
+ bisque: 0xffe4c4ff,
26
+ black: 0x000000ff,
27
+ blanchedalmond: 0xffebcdff,
28
+ blue: 0x0000ffff,
29
+ blueviolet: 0x8a2be2ff,
30
+ brown: 0xa52a2aff,
31
+ burlywood: 0xdeb887ff,
32
+ cadetblue: 0x5f9ea0ff,
33
+ chartreuse: 0x7fff00ff,
34
+ chocolate: 0xd2691eff,
35
+ coral: 0xff7f50ff,
36
+ cornflowerblue: 0x6495edff,
37
+ cornsilk: 0xfff8dcff,
38
+ crimson: 0xdc143cff,
39
+ cyan: 0x00ffffff,
40
+ darkblue: 0x00008bff,
41
+ darkcyan: 0x008b8bff,
42
+ darkgoldenrod: 0xb8860bff,
43
+ darkgray: 0xa9a9a9ff,
44
+ darkgreen: 0x006400ff,
45
+ darkgrey: 0xa9a9a9ff,
46
+ darkkhaki: 0xbdb76bff,
47
+ darkmagenta: 0x8b008bff,
48
+ darkolivegreen: 0x556b2fff,
49
+ darkorange: 0xff8c00ff,
50
+ darkorchid: 0x9932ccff,
51
+ darkred: 0x8b0000ff,
52
+ darksalmon: 0xe9967aff,
53
+ darkseagreen: 0x8fbc8fff,
54
+ darkslateblue: 0x483d8bff,
55
+ darkslategray: 0x2f4f4fff,
56
+ darkslategrey: 0x2f4f4fff,
57
+ darkturquoise: 0x00ced1ff,
58
+ darkviolet: 0x9400d3ff,
59
+ deeppink: 0xff1493ff,
60
+ deepskyblue: 0x00bfffff,
61
+ dimgray: 0x696969ff,
62
+ dimgrey: 0x696969ff,
63
+ dodgerblue: 0x1e90ffff,
64
+ firebrick: 0xb22222ff,
65
+ floralwhite: 0xfffaf0ff,
66
+ forestgreen: 0x228b22ff,
67
+ fuchsia: 0xff00ffff,
68
+ gainsboro: 0xdcdcdcff,
69
+ ghostwhite: 0xf8f8ffff,
70
+ gold: 0xffd700ff,
71
+ goldenrod: 0xdaa520ff,
72
+ gray: 0x808080ff,
73
+ green: 0x008000ff,
74
+ greenyellow: 0xadff2fff,
75
+ grey: 0x808080ff,
76
+ honeydew: 0xf0fff0ff,
77
+ hotpink: 0xff69b4ff,
78
+ indianred: 0xcd5c5cff,
79
+ indigo: 0x4b0082ff,
80
+ ivory: 0xfffff0ff,
81
+ khaki: 0xf0e68cff,
82
+ lavender: 0xe6e6faff,
83
+ lavenderblush: 0xfff0f5ff,
84
+ lawngreen: 0x7cfc00ff,
85
+ lemonchiffon: 0xfffacdff,
86
+ lightblue: 0xadd8e6ff,
87
+ lightcoral: 0xf08080ff,
88
+ lightcyan: 0xe0ffffff,
89
+ lightgoldenrodyellow: 0xfafad2ff,
90
+ lightgray: 0xd3d3d3ff,
91
+ lightgreen: 0x90ee90ff,
92
+ lightgrey: 0xd3d3d3ff,
93
+ lightpink: 0xffb6c1ff,
94
+ lightsalmon: 0xffa07aff,
95
+ lightseagreen: 0x20b2aaff,
96
+ lightskyblue: 0x87cefaff,
97
+ lightslategray: 0x778899ff,
98
+ lightslategrey: 0x778899ff,
99
+ lightsteelblue: 0xb0c4deff,
100
+ lightyellow: 0xffffe0ff,
101
+ lime: 0x00ff00ff,
102
+ limegreen: 0x32cd32ff,
103
+ linen: 0xfaf0e6ff,
104
+ magenta: 0xff00ffff,
105
+ maroon: 0x800000ff,
106
+ mediumaquamarine: 0x66cdaaff,
107
+ mediumblue: 0x0000cdff,
108
+ mediumorchid: 0xba55d3ff,
109
+ mediumpurple: 0x9370dbff,
110
+ mediumseagreen: 0x3cb371ff,
111
+ mediumslateblue: 0x7b68eeff,
112
+ mediumspringgreen: 0x00fa9aff,
113
+ mediumturquoise: 0x48d1ccff,
114
+ mediumvioletred: 0xc71585ff,
115
+ midnightblue: 0x191970ff,
116
+ mintcream: 0xf5fffaff,
117
+ mistyrose: 0xffe4e1ff,
118
+ moccasin: 0xffe4b5ff,
119
+ navajowhite: 0xffdeadff,
120
+ navy: 0x000080ff,
121
+ oldlace: 0xfdf5e6ff,
122
+ olive: 0x808000ff,
123
+ olivedrab: 0x6b8e23ff,
124
+ orange: 0xffa500ff,
125
+ orangered: 0xff4500ff,
126
+ orchid: 0xda70d6ff,
127
+ palegoldenrod: 0xeee8aaff,
128
+ palegreen: 0x98fb98ff,
129
+ paleturquoise: 0xafeeeeff,
130
+ palevioletred: 0xdb7093ff,
131
+ papayawhip: 0xffefd5ff,
132
+ peachpuff: 0xffdab9ff,
133
+ peru: 0xcd853fff,
134
+ pink: 0xffc0cbff,
135
+ plum: 0xdda0ddff,
136
+ powderblue: 0xb0e0e6ff,
137
+ purple: 0x800080ff,
138
+ rebeccapurple: 0x663399ff,
139
+ red: 0xff0000ff,
140
+ rosybrown: 0xbc8f8fff,
141
+ royalblue: 0x4169e1ff,
142
+ saddlebrown: 0x8b4513ff,
143
+ salmon: 0xfa8072ff,
144
+ sandybrown: 0xf4a460ff,
145
+ seagreen: 0x2e8b57ff,
146
+ seashell: 0xfff5eeff,
147
+ sienna: 0xa0522dff,
148
+ silver: 0xc0c0c0ff,
149
+ skyblue: 0x87ceebff,
150
+ slateblue: 0x6a5acdff,
151
+ slategray: 0x708090ff,
152
+ slategrey: 0x708090ff,
153
+ snow: 0xfffafaff,
154
+ springgreen: 0x00ff7fff,
155
+ steelblue: 0x4682b4ff,
156
+ tan: 0xd2b48cff,
157
+ teal: 0x008080ff,
158
+ thistle: 0xd8bfd8ff,
159
+ tomato: 0xff6347ff,
160
+ transparent: 0x00000000,
161
+ turquoise: 0x40e0d0ff,
162
+ violet: 0xee82eeff,
163
+ wheat: 0xf5deb3ff,
164
+ white: 0xffffffff,
165
+ whitesmoke: 0xf5f5f5ff,
166
+ yellow: 0xffff00ff,
167
+ yellowgreen: 0x9acd32ff,
168
+ }
@@ -0,0 +1,27 @@
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
+ const counters = {}
19
+ let counter = 0
20
+
21
+ export const createHumanReadableId = (name) => {
22
+ return `BlitsComponent::${name}_${(counters[name] = (counters[name] || 0) + 1)}`
23
+ }
24
+
25
+ export const createInternalId = () => {
26
+ return ++counter
27
+ }
@@ -0,0 +1,83 @@
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 { createHumanReadableId, createInternalId } from './componentId.js'
20
+
21
+ test('Type createHumandReadableId', (assert) => {
22
+ const expected = 'function'
23
+ const actual = typeof createHumanReadableId
24
+
25
+ assert.equal(actual, expected, 'createHumanReadableId should be a function')
26
+ assert.end()
27
+ })
28
+
29
+ test('Returns a readable id, based on a counter and component name', (assert) => {
30
+ const expected = 'BlitsComponent::Spinner_1'
31
+ const actual = createHumanReadableId('Spinner')
32
+
33
+ assert.equal(actual, expected, 'createHumanReadableId should return correct ID')
34
+ assert.end()
35
+ })
36
+
37
+ test('Returns a readable id with incremented count', (assert) => {
38
+ const expected = 'BlitsComponent::Spinner_2'
39
+ const actual = createHumanReadableId('Spinner')
40
+
41
+ assert.equal(actual, expected, 'createHumanReadableId should return correct ID')
42
+ assert.end()
43
+ })
44
+
45
+ test('Has a seperate count per component name', (assert) => {
46
+ const expected = 'BlitsComponent::Card_1'
47
+ const actual = createHumanReadableId('Card')
48
+
49
+ assert.equal(actual, expected, 'createHumanReadableId should return correct ID')
50
+ assert.end()
51
+ })
52
+
53
+ test('Type createInternalId', (assert) => {
54
+ const expected = 'function'
55
+ const actual = typeof createInternalId
56
+
57
+ assert.equal(actual, expected, 'createInternalId should be a function')
58
+ assert.end()
59
+ })
60
+
61
+ test('Returns an internal id, disregarding the component name', (assert) => {
62
+ const expected = 1
63
+ const actual = createInternalId('Spinner')
64
+
65
+ assert.equal(actual, expected, 'createInternalId should return correct ID')
66
+ assert.end()
67
+ })
68
+
69
+ test('Returns an id with incremented counter', (assert) => {
70
+ const expected = 2
71
+ const actual = createInternalId('Spinner')
72
+
73
+ assert.equal(actual, expected, 'createInternalId should return correct ID')
74
+ assert.end()
75
+ })
76
+
77
+ test('Uses the same counter for all components', (assert) => {
78
+ const expected = 3
79
+ const actual = createInternalId('Card')
80
+
81
+ assert.equal(actual, expected, 'createInternalId should return correct ID')
82
+ assert.end()
83
+ })
@@ -0,0 +1,55 @@
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
+ const eventsMap = new Map()
19
+
20
+ export default {
21
+ registerListener(component, event, cb) {
22
+ let componentsMap = eventsMap.get(event)
23
+ if (!componentsMap) {
24
+ componentsMap = new Map()
25
+ eventsMap.set(event, componentsMap)
26
+ }
27
+
28
+ let components = componentsMap.get(component)
29
+ if (!components) {
30
+ components = new Set()
31
+ componentsMap.set(component, components)
32
+ }
33
+
34
+ components.add(cb)
35
+ },
36
+ executeListeners(event, params) {
37
+ const componentsMap = eventsMap.get(event)
38
+ if (componentsMap) {
39
+ componentsMap.forEach((component) => {
40
+ component.forEach((cb) => {
41
+ cb(params)
42
+ })
43
+ })
44
+ }
45
+ },
46
+ removeListeners(component) {
47
+ eventsMap.forEach((componentMap) => {
48
+ const cmp = componentMap.get(component)
49
+ if (cmp) {
50
+ cmp.clear()
51
+ componentMap.delete(cmp)
52
+ }
53
+ })
54
+ },
55
+ }
@@ -0,0 +1,29 @@
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
+ const cbs = {}
19
+
20
+ export const emit = (hook, name, scope) => {
21
+ cbs[name] && cbs[name][hook] && cbs[name][hook].apply(scope)
22
+ }
23
+
24
+ export const registerHooks = (hooks = {}, name) => {
25
+ cbs[name] = {}
26
+ Object.keys(hooks).forEach((hook) => {
27
+ if (typeof hooks[hook] === 'function') cbs[name][hook] = hooks[hook]
28
+ })
29
+ }