@dazn/kopytko-framework 1.4.0 → 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/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
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
|
|
6
|
+
' WARNING: it pollutes scope (m["$$eventBus"])
|
|
6
7
|
' @class
|
|
7
8
|
function EventBusFacade() as Object
|
|
8
9
|
_global = KopytkoGlobalNode()
|
|
@@ -12,14 +13,13 @@ function EventBusFacade() as Object
|
|
|
12
13
|
})
|
|
13
14
|
end if
|
|
14
15
|
|
|
15
|
-
if (m
|
|
16
|
-
return m
|
|
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 =
|
|
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.
|
|
39
|
-
m.
|
|
38
|
+
m._eventBus.unobserveFieldScoped(eventName)
|
|
39
|
+
m._eventBus.observeFieldScoped(eventName, "EventBus_onEventFired")
|
|
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.
|
|
50
|
+
m._eventBus.unobserveFieldScoped(eventName)
|
|
51
51
|
|
|
52
52
|
return
|
|
53
53
|
end if
|
|
@@ -57,24 +57,31 @@ 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.
|
|
72
|
+
m._eventBus[eventName] = payload
|
|
66
73
|
end sub
|
|
67
74
|
|
|
68
75
|
' @private
|
|
69
76
|
prototype._ensureEventExistence = sub (eventName as String)
|
|
70
|
-
if (NOT m.
|
|
77
|
+
if (NOT m._eventBus.hasField(eventName))
|
|
71
78
|
fields = {}
|
|
72
79
|
fields[eventName] = {}
|
|
73
|
-
m.
|
|
80
|
+
m._eventBus.addFields(fields)
|
|
74
81
|
end if
|
|
75
82
|
end sub
|
|
76
83
|
|
|
77
|
-
m
|
|
84
|
+
m["$$eventBus"] = prototype
|
|
78
85
|
|
|
79
86
|
return prototype
|
|
80
87
|
end function
|
|
@@ -82,7 +89,7 @@ end function
|
|
|
82
89
|
' @private
|
|
83
90
|
sub EventBus_onEventFired(event as Object)
|
|
84
91
|
eventName = event.getField()
|
|
85
|
-
callbacks = m.
|
|
92
|
+
callbacks = m["$$eventBus"]._eventsMap[eventName]
|
|
86
93
|
|
|
87
94
|
if (callbacks = Invalid)
|
|
88
95
|
return
|
|
@@ -91,13 +98,6 @@ sub EventBus_onEventFired(event as Object)
|
|
|
91
98
|
payload = event.getData()
|
|
92
99
|
|
|
93
100
|
for each callback in callbacks
|
|
94
|
-
|
|
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
|
|
101
|
+
functionCall(callback.handler, [payload], callback.context)
|
|
102
102
|
end for
|
|
103
103
|
end sub
|
|
@@ -34,10 +34,8 @@ sub destroyKopytko(data = {} as Object)
|
|
|
34
34
|
|
|
35
35
|
componentWillUnmount()
|
|
36
36
|
|
|
37
|
-
if (m
|
|
38
|
-
|
|
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 = {}
|