@dazn/kopytko-framework 1.4.0 → 1.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dazn/kopytko-framework",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "A modern Roku's Brightscript framework",
5
5
  "keywords": [
6
6
  "brightscript",
@@ -1,25 +1,25 @@
1
1
  ' @import /components/ArrayUtils.brs from @dazn/kopytko-utils
2
+ ' @import /components/functionCall.brs from @dazn/kopytko-utils
2
3
  ' @import /components/utils/KopytkoGlobalNode.brs
3
4
 
4
5
  ' Pub/Sub implementation
5
- ' WARNING: it pollutes scope (m._eventBus)
6
+ ' WARNING: it pollutes scope (m["$$eventBus"])
6
7
  ' @class
7
8
  function EventBusFacade() as Object
8
9
  _global = KopytkoGlobalNode()
9
10
  if (NOT _global.hasField("eventBus"))
10
11
  _global.addFields({
11
- eventBus: CreateObject("roSGNode", "Node"),
12
+ eventBus: CreateObject("roSGNode", "EventBus"),
12
13
  })
13
14
  end if
14
15
 
15
- if (m._eventBus <> Invalid)
16
- return m._eventBus
16
+ if (m["$$eventBus"] <> Invalid)
17
+ return m["$$eventBus"]
17
18
  end if
18
19
 
19
20
  prototype = {}
20
- prototype.global = _global
21
21
  prototype._arrayUtils = ArrayUtils()
22
- prototype._eventBus = m.global.eventBus
22
+ prototype._eventBus = _global.eventBus
23
23
  prototype._eventsMap = {}
24
24
 
25
25
  ' Attach subscriber for an event
@@ -35,8 +35,8 @@ function EventBusFacade() as Object
35
35
 
36
36
  m._eventsMap[eventName].push({ handler: handler, context: context })
37
37
 
38
- m.global.eventBus.unobserveFieldScoped(eventName)
39
- m.global.eventBus.observeFieldScoped(eventName, "EventBus_onEventFired")
38
+ m._eventBus.unobserveFieldScoped(eventName)
39
+ m._eventBus.observeFieldScoped(eventName, "EventBus_onEventFired", ["$$payload"])
40
40
  end sub
41
41
 
42
42
  ' Detach subscriber for an event
@@ -47,7 +47,7 @@ function EventBusFacade() as Object
47
47
 
48
48
  if (callbacks = Invalid OR callbacks.count() <= 1)
49
49
  m._eventsMap.delete(eventName)
50
- m.global.eventBus.unobserveFieldScoped(eventName)
50
+ m._eventBus.unobserveFieldScoped(eventName)
51
51
 
52
52
  return
53
53
  end if
@@ -57,24 +57,36 @@ function EventBusFacade() as Object
57
57
  end function, handlerToRemove)
58
58
  end sub
59
59
 
60
+ prototype.clear = sub()
61
+ for each eventName in m._eventsMap
62
+ m._eventsMap.delete(eventName)
63
+ m._eventBus.unobserveFieldScoped(eventName)
64
+ end for
65
+ end sub
66
+
60
67
  ' Trigger given event with given payload
61
68
  ' @param {String} eventName
62
69
  ' @param {Object} [payload={}]
63
70
  prototype.trigger = sub (eventName as String, payload = {} as Object)
64
71
  m._ensureEventExistence(eventName)
65
- m.global.eventBus[eventName] = payload
72
+
73
+ ' The event payload is stored in a separate field to avoid memory leaks.
74
+ ' Payload needs to be removed from the EventBus node, and if that would
75
+ ' be stored in the AA field of a specific event, then callbacks would be
76
+ ' triggered for another time after changing the value to invalid.
77
+ m._eventBus["$$payload"] = payload
78
+ m._eventBus[eventName] = true
79
+ m._eventBus["$$payload"] = Invalid
66
80
  end sub
67
81
 
68
82
  ' @private
69
83
  prototype._ensureEventExistence = sub (eventName as String)
70
- if (NOT m.global.eventBus.hasField(eventName))
71
- fields = {}
72
- fields[eventName] = {}
73
- m.global.eventBus.addFields(fields)
84
+ if (NOT m._eventBus.hasField(eventName))
85
+ m._eventBus.addField(eventName, "boolean", true)
74
86
  end if
75
87
  end sub
76
88
 
77
- m._eventBus = prototype
89
+ m["$$eventBus"] = prototype
78
90
 
79
91
  return prototype
80
92
  end function
@@ -82,22 +94,19 @@ end function
82
94
  ' @private
83
95
  sub EventBus_onEventFired(event as Object)
84
96
  eventName = event.getField()
85
- callbacks = m._eventBus._eventsMap[eventName]
97
+ callbacks = m["$$eventBus"]._eventsMap[eventName]
86
98
 
87
99
  if (callbacks = Invalid)
88
100
  return
89
101
  end if
90
102
 
91
- payload = event.getData()
103
+ payload = Invalid
104
+ context = event.getInfo()
105
+ if (context <> Invalid)
106
+ payload = context["$$payload"]
107
+ end if
92
108
 
93
109
  for each callback in callbacks
94
- if (callback.context = Invalid)
95
- handler = callback.handler
96
- handler(payload)
97
- else
98
- callback.context["_eventBus_callback_handler"] = callback.handler
99
- callback.context._eventBus_callback_handler(payload)
100
- callback.context.delete("_eventBus_callback_handler")
101
- end if
110
+ functionCall(callback.handler, [payload], callback.context)
102
111
  end for
103
112
  end sub
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="utf-8" ?>
2
+
3
+ <component name="EventBus" extends="Node">
4
+ <interface>
5
+ <field id="$$payload" type="assocarray" /> <!-- prefixed with $$ to avoid name collision with dynamically added events -->
6
+ </interface>
7
+ </component>
@@ -34,10 +34,8 @@ sub destroyKopytko(data = {} as Object)
34
34
 
35
35
  componentWillUnmount()
36
36
 
37
- if (m.global.eventBus <> Invalid)
38
- for each event in m.global.eventBus.getFields()
39
- m.global.eventBus.unobserveFieldScoped(event)
40
- end for
37
+ if (m["$$eventBus"] <> Invalid)
38
+ m["$$eventBus"].clear()
41
39
  end if
42
40
 
43
41
  m.state = {}
@@ -5,9 +5,12 @@
5
5
 
6
6
  ' @returns {Mock}
7
7
  function KopytkoGlobalNode() as Object
8
+ eventBusNode = createNode()
9
+ eventBusNode.addField("$$payload", "assocarray", false)
10
+
8
11
  fields = {
9
12
  cache: createNode(),
10
- eventBus: createNode(),
13
+ eventBus: eventBusNode,
11
14
  router: createNode(),
12
15
  store: createNode(),
13
16
  theme: createNode(),