@dazn/kopytko-framework 1.3.1 → 1.3.3

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,3 +1,17 @@
1
+ ## [1.3.3](https://github.com/getndazn/kopytko-framework/compare/v1.3.2...v1.3.3) (2022-12-09)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * subscriptions remove own entries during iteration ([#36](https://github.com/getndazn/kopytko-framework/issues/36)) ([f67b6bd](https://github.com/getndazn/kopytko-framework/commit/f67b6bd5b209bb4ef2aa830b830162ee308a41ce))
7
+
8
+ ## [1.3.2](https://github.com/getndazn/kopytko-framework/compare/v1.3.1...v1.3.2) (2022-09-01)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * Ignoring Invalid elements when calculating elements' index in children array ([#31](https://github.com/getndazn/kopytko-framework/issues/31)) ([73aed6e](https://github.com/getndazn/kopytko-framework/commit/73aed6e2e7ba38b946c81fe18ceecf414f7bbcf2))
14
+
1
15
  ## [1.3.1](https://github.com/getndazn/kopytko-framework/compare/v1.3.0...v1.3.1) (2022-08-05)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dazn/kopytko-framework",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "description": "A modern Roku's Brightscript framework",
5
5
  "keywords": [
6
6
  "brightscript",
@@ -69,21 +69,6 @@ function KopytkoDiffUtility() as Object
69
69
  m._diffElementChildren(currentElement.children, newElement.children, newElement.props.id)
70
70
  end sub
71
71
 
72
- ' @private
73
- prototype._markElementToBeRemoved = sub (element as Object)
74
- m._diffResult.elementsToRemove.push(element.props.id)
75
-
76
- if (element.children = Invalid OR element.children.count() = 0)
77
- return
78
- end if
79
-
80
- for each child in element.children
81
- if (child <> Invalid)
82
- m._markElementToBeRemoved(child)
83
- end if
84
- end for
85
- end sub
86
-
87
72
  ' @private
88
73
  prototype._diffElementProps = sub (elementId as String, currentProps as Object, newProps as Object)
89
74
  for each newProp in newProps
@@ -98,34 +83,49 @@ function KopytkoDiffUtility() as Object
98
83
  end for
99
84
  end sub
100
85
 
101
- ' @todo Make it not rerender the whole children tree if they were only reordered
86
+ ' @todo Support elements reordering
102
87
  ' @private
103
88
  prototype._diffElementChildren = sub (currentChildren as Object, newChildren as Object, parentElementId = Invalid as Dynamic)
104
- if (currentChildren = Invalid)
105
- currentChildren = []
106
- end if
89
+ if (newChildren = Invalid) then newChildren = []
107
90
 
108
- if (newChildren = Invalid)
109
- newChildren = []
91
+ currentChildrenMapped = {}
92
+ if (currentChildren <> Invalid)
93
+ for each currentChild in currentChildren
94
+ if (currentChild <> Invalid)
95
+ currentChildrenMapped[currentChild.props.id] = currentChild
96
+ end if
97
+ end for
110
98
  end if
111
99
 
112
- if (currentChildren.count() > newChildren.count())
113
- biggestArray = currentChildren
114
- else
115
- biggestArray = newChildren
116
- end if
100
+ nonInvalidNewChildIndex = 0
101
+ for each newChild in newChildren
102
+ if (newChild <> Invalid)
103
+ newChild.index = nonInvalidNewChildIndex
104
+ nonInvalidNewChildIndex++
105
+ newChild.parentId = parentElementId
117
106
 
118
- for i = 0 to biggestArray.count() - 1
119
- if (newChildren[i] <> Invalid)
120
- newChildren[i].index = i
121
- newChildren[i].parentId = parentElementId
107
+ m._diffElement(currentChildrenMapped[newChild.props.id], newChild)
108
+ currentChildrenMapped.delete(newChild.props.id)
122
109
  end if
110
+ end for
123
111
 
124
- if (currentChildren[i] <> Invalid)
125
- currentChildren[i].index = i
126
- end if
112
+ for each currentChildIdToRemove in currentChildrenMapped
113
+ m._markElementToBeRemoved(currentChildrenMapped[currentChildIdToRemove])
114
+ end for
115
+ end sub
116
+
117
+ ' @private
118
+ prototype._markElementToBeRemoved = sub (element as Object)
119
+ m._diffResult.elementsToRemove.push(element.props.id)
127
120
 
128
- m._diffElement(currentChildren[i], newChildren[i])
121
+ if (element.children = Invalid OR element.children.count() = 0)
122
+ return
123
+ end if
124
+
125
+ for each child in element.children
126
+ if (child <> Invalid)
127
+ m._markElementToBeRemoved(child)
128
+ end if
129
129
  end for
130
130
  end sub
131
131
 
@@ -1,4 +1,5 @@
1
1
  ' @import /components/getType.brs from @dazn/kopytko-utils
2
+ ' @import /components/uuid.brs from @dazn/kopytko-utils
2
3
  ' @import /components/utils/KopytkoGlobalNode.brs
3
4
  function StoreFacade() as Object
4
5
  if (m._store <> Invalid)
@@ -15,7 +16,7 @@ function StoreFacade() as Object
15
16
  prototype = {}
16
17
 
17
18
  prototype._store = _global.store
18
- prototype._subscriptions = []
19
+ prototype._subscriptions = {}
19
20
 
20
21
  prototype.updateNode = sub (key as String, value as Dynamic)
21
22
  data = m.get(key)
@@ -94,20 +95,20 @@ function StoreFacade() as Object
94
95
 
95
96
  prototype.subscribeOnce = sub (key as String, callback as Function)
96
97
  m._handleSubscriber(key)
97
- m._subscriptions.push({ key: key, callback: [callback], once: true })
98
+ m._subscriptions[m._getRandomizedKey(key)] = { key: key, callback: [callback], once: true }
98
99
  end sub
99
100
 
100
101
  prototype.subscribe = sub (key as String, callback as Function)
101
102
  m._handleSubscriber(key)
102
- m._subscriptions.push({ key: key, callback: [callback], once: false })
103
+ m._subscriptions[m._getRandomizedKey(key)] = { key: key, callback: [callback], once: false }
103
104
  end sub
104
105
 
105
106
  prototype.unsubscribe = sub (key as String, callback as Function)
106
- for i = 0 to m._subscriptions.count() - 1
107
- listener = m._subscriptions[i]
107
+ for each subscriptionKey in m._subscriptions
108
+ listener = m._subscriptions[subscriptionKey]
108
109
 
109
110
  if (listener <> Invalid AND listener.key = key AND listener.callback[0] = callback)
110
- m._subscriptions.delete(i)
111
+ m._subscriptions.delete(subscriptionKey)
111
112
  end if
112
113
  end for
113
114
  end sub
@@ -139,14 +140,14 @@ function StoreFacade() as Object
139
140
  prototype._notify = sub (key as String, data as Object)
140
141
  value = data.value
141
142
 
142
- for i = 0 to m._subscriptions.count() - 1
143
- listener = m._subscriptions[i]
143
+ for each subscriptionKey in m._subscriptions
144
+ listener = m._subscriptions[subscriptionKey]
144
145
 
145
- if (listener.key = key)
146
+ if (listener <> Invalid AND listener.key = key)
146
147
  listener.callback[0](value)
147
148
 
148
149
  if (listener.once)
149
- m._subscriptions.delete(i)
150
+ m._subscriptions.delete(subscriptionKey)
150
151
  end if
151
152
  end if
152
153
  end for
@@ -164,6 +165,10 @@ function StoreFacade() as Object
164
165
  end if
165
166
  end sub
166
167
 
168
+ prototype._getRandomizedKey = function (key as String) as String
169
+ return key + "_" + uuid()
170
+ end function
171
+
167
172
  m._store = prototype
168
173
 
169
174
  return m._store