@@ -108,20 +108,44 @@ extension Settings: Equatable {
108
108
}
109
109
110
110
extension Analytics {
111
- internal func update( settings: Settings , type: UpdateType ) {
112
- apply { ( plugin) in
113
- // tell all top level plugins to update.
114
- update ( plugin: plugin, settings: settings, type: type)
111
+ internal func update( settings: Settings ) {
112
+ guard let system: System = store. currentState ( ) else { return }
113
+ apply { plugin in
114
+ plugin. update ( settings: settings, type: updateType ( for: plugin, in: system) )
115
+ if let destPlugin = plugin as? DestinationPlugin {
116
+ destPlugin. apply { subPlugin in
117
+ subPlugin. update ( settings: settings, type: updateType ( for: subPlugin, in: system) )
118
+ }
119
+ }
115
120
}
116
121
}
117
122
118
- internal func update( plugin: Plugin , settings: Settings , type: UpdateType ) {
119
- plugin. update ( settings: settings, type: type)
120
- // if it's a destination, tell it's plugins to update as well.
121
- if let dest = plugin as? DestinationPlugin {
122
- dest. apply { ( subPlugin) in
123
- subPlugin. update ( settings: settings, type: type)
123
+ internal func updateIfNecessary( plugin: Plugin ) {
124
+ guard let system: System = store. currentState ( ) else { return }
125
+ // if we're already running, update has already been called for existing plugins,
126
+ // so we just wanna call it on this one if it hasn't been done already.
127
+ if system. running, let settings = system. settings {
128
+ let alreadyInitialized = system. initializedPlugins. contains { p in
129
+ return plugin === p
124
130
}
131
+ if !alreadyInitialized {
132
+ store. dispatch ( action: System . AddPluginToInitialized ( plugin: plugin) )
133
+ plugin. update ( settings: settings, type: . initial)
134
+ } else {
135
+ plugin. update ( settings: settings, type: . refresh)
136
+ }
137
+ }
138
+ }
139
+
140
+ internal func updateType( for plugin: Plugin , in system: System ) -> UpdateType {
141
+ let alreadyInitialized = system. initializedPlugins. contains { p in
142
+ return plugin === p
143
+ }
144
+ if alreadyInitialized {
145
+ return . refresh
146
+ } else {
147
+ store. dispatch ( action: System . AddPluginToInitialized ( plugin: plugin) )
148
+ return . initial
125
149
}
126
150
}
127
151
@@ -135,6 +159,7 @@ extension Analytics {
135
159
operatingMode. run ( queue: DispatchQueue . main) {
136
160
if let state: System = self . store. currentState ( ) , let settings = state. settings {
137
161
self . store. dispatch ( action: System . UpdateSettingsAction ( settings: settings) )
162
+ self . update ( settings: settings)
138
163
}
139
164
self . store. dispatch ( action: System . ToggleRunningAction ( running: true ) )
140
165
}
@@ -145,9 +170,6 @@ extension Analytics {
145
170
146
171
let writeKey = self . configuration. values. writeKey
147
172
let httpClient = HTTPClient ( analytics: self )
148
- let systemState : System ? = store. currentState ( )
149
- let hasSettings = ( systemState? . settings? . integrations != nil && systemState? . settings? . plan != nil )
150
- let updateType = ( hasSettings ? UpdateType . refresh : UpdateType . initial)
151
173
152
174
// stop things; queue in case our settings have changed.
153
175
store. dispatch ( action: System . ToggleRunningAction ( running: false ) )
@@ -158,7 +180,7 @@ extension Analytics {
158
180
// this will cause them to be cached.
159
181
self . store. dispatch ( action: System . UpdateSettingsAction ( settings: s) )
160
182
// let plugins know we just received some settings..
161
- self . update ( settings: s, type : updateType )
183
+ self . update ( settings: s)
162
184
}
163
185
}
164
186
// we're good to go back to a running state.
0 commit comments