Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit 0367052

Browse files
authored
Recreate DestinationFilters whenever we get new Settings (#3)
* Allow the filters plugin to be updated with newer Settings after the initial update.
1 parent eec7fbb commit 0367052

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

Examples/DestinationFiltersExample/DestinationFiltersExample.xcodeproj/project.pbxproj

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
4631BE162B602F920063334A /* AnalyticsFilters in Frameworks */ = {isa = PBXBuildFile; productRef = 4631BE152B602F920063334A /* AnalyticsFilters */; };
1011
75EE630E28DBC60500A0B99E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EE630D28DBC60500A0B99E /* AppDelegate.swift */; };
1112
75EE631028DBC60500A0B99E /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EE630F28DBC60500A0B99E /* SceneDelegate.swift */; };
1213
75EE631228DBC60500A0B99E /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EE631128DBC60500A0B99E /* ViewController.swift */; };
@@ -16,7 +17,6 @@
1617
75EE632528DBC60800A0B99E /* DestinationFiltersExampleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EE632428DBC60800A0B99E /* DestinationFiltersExampleTests.swift */; };
1718
75EE632F28DBC60800A0B99E /* DestinationFiltersExampleUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EE632E28DBC60800A0B99E /* DestinationFiltersExampleUITests.swift */; };
1819
75EE633128DBC60800A0B99E /* DestinationFiltersExampleUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EE633028DBC60800A0B99E /* DestinationFiltersExampleUITestsLaunchTests.swift */; };
19-
75EE634128DBCDD800A0B99E /* DestinationFilters-Swift in Frameworks */ = {isa = PBXBuildFile; productRef = 75EE634028DBCDD800A0B99E /* DestinationFilters-Swift */; };
2020
75EE634328DBD4CA00A0B99E /* WebhookDestination.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EE634228DBD4CA00A0B99E /* WebhookDestination.swift */; };
2121
/* End PBXBuildFile section */
2222

@@ -60,7 +60,7 @@
6060
isa = PBXFrameworksBuildPhase;
6161
buildActionMask = 2147483647;
6262
files = (
63-
75EE634128DBCDD800A0B99E /* DestinationFilters-Swift in Frameworks */,
63+
4631BE162B602F920063334A /* AnalyticsFilters in Frameworks */,
6464
);
6565
runOnlyForDeploymentPostprocessing = 0;
6666
};
@@ -164,10 +164,11 @@
164164
buildRules = (
165165
);
166166
dependencies = (
167+
4631BE142B602F8B0063334A /* PBXTargetDependency */,
167168
);
168169
name = DestinationFiltersExample;
169170
packageProductDependencies = (
170-
75EE634028DBCDD800A0B99E /* DestinationFilters-Swift */,
171+
4631BE152B602F920063334A /* AnalyticsFilters */,
171172
);
172173
productName = DestinationFiltersExample;
173174
productReference = 75EE630A28DBC60500A0B99E /* DestinationFiltersExample.app */;
@@ -311,6 +312,10 @@
311312
/* End PBXSourcesBuildPhase section */
312313

313314
/* Begin PBXTargetDependency section */
315+
4631BE142B602F8B0063334A /* PBXTargetDependency */ = {
316+
isa = PBXTargetDependency;
317+
productRef = 4631BE132B602F8B0063334A /* AnalyticsFilters */;
318+
};
314319
75EE632228DBC60800A0B99E /* PBXTargetDependency */ = {
315320
isa = PBXTargetDependency;
316321
target = 75EE630928DBC60500A0B99E /* DestinationFiltersExample */;
@@ -462,8 +467,10 @@
462467
buildSettings = {
463468
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
464469
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
470+
CODE_SIGN_IDENTITY = "Apple Development";
465471
CODE_SIGN_STYLE = Automatic;
466472
CURRENT_PROJECT_VERSION = 1;
473+
DEVELOPMENT_TEAM = "";
467474
GENERATE_INFOPLIST_FILE = YES;
468475
INFOPLIST_FILE = DestinationFiltersExample/Info.plist;
469476
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
@@ -476,8 +483,9 @@
476483
"@executable_path/Frameworks",
477484
);
478485
MARKETING_VERSION = 1.0;
479-
PRODUCT_BUNDLE_IDENTIFIER = prayansh.DestinationFiltersExample;
486+
PRODUCT_BUNDLE_IDENTIFIER = com.segment.destfiltersexample;
480487
PRODUCT_NAME = "$(TARGET_NAME)";
488+
PROVISIONING_PROFILE_SPECIFIER = "";
481489
SWIFT_EMIT_LOC_STRINGS = YES;
482490
SWIFT_VERSION = 5.0;
483491
TARGETED_DEVICE_FAMILY = "1,2";
@@ -489,8 +497,10 @@
489497
buildSettings = {
490498
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
491499
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
500+
CODE_SIGN_IDENTITY = "Apple Development";
492501
CODE_SIGN_STYLE = Automatic;
493502
CURRENT_PROJECT_VERSION = 1;
503+
DEVELOPMENT_TEAM = "";
494504
GENERATE_INFOPLIST_FILE = YES;
495505
INFOPLIST_FILE = DestinationFiltersExample/Info.plist;
496506
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
@@ -503,8 +513,9 @@
503513
"@executable_path/Frameworks",
504514
);
505515
MARKETING_VERSION = 1.0;
506-
PRODUCT_BUNDLE_IDENTIFIER = prayansh.DestinationFiltersExample;
516+
PRODUCT_BUNDLE_IDENTIFIER = com.segment.destfiltersexample;
507517
PRODUCT_NAME = "$(TARGET_NAME)";
518+
PROVISIONING_PROFILE_SPECIFIER = "";
508519
SWIFT_EMIT_LOC_STRINGS = YES;
509520
SWIFT_VERSION = 5.0;
510521
TARGETED_DEVICE_FAMILY = "1,2";
@@ -625,9 +636,13 @@
625636
/* End XCConfigurationList section */
626637

627638
/* Begin XCSwiftPackageProductDependency section */
628-
75EE634028DBCDD800A0B99E /* DestinationFilters-Swift */ = {
639+
4631BE132B602F8B0063334A /* AnalyticsFilters */ = {
640+
isa = XCSwiftPackageProductDependency;
641+
productName = AnalyticsFilters;
642+
};
643+
4631BE152B602F920063334A /* AnalyticsFilters */ = {
629644
isa = XCSwiftPackageProductDependency;
630-
productName = "DestinationFilters-Swift";
645+
productName = AnalyticsFilters;
631646
};
632647
/* End XCSwiftPackageProductDependency section */
633648
};

Package.resolved

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let package = Package(
1717
],
1818
dependencies: [
1919
// Dependencies declare other packages that this package depends on.
20-
.package(url: "https://github.com/segment-integrations/analytics-swift-live.git", .upToNextMajor(from: "0.2.3")),
20+
.package(url: "https://github.com/segment-integrations/analytics-swift-live.git", .upToNextMajor(from: "1.0.0")),
2121
.package(url: "https://github.com/segmentio/analytics-swift.git", from: "1.4.7"),
2222
.package(url: "https://github.com/segmentio/substrata-swift.git", .upToNextMajor(from: "0.0.2")),
2323
],

Sources/AnalyticsFilters/DestinationFilters.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public class DestinationFilters: UtilityPlugin {
3737
var dest = new DestinationFilter(destination, rules);
3838
return analytics.add(dest);
3939
}
40+
41+
function removePreviousDestinationFilters() {
42+
analytics.removeLivePlugins()
43+
return true
44+
}
4045
"""
4146
#else
4247
var destinationFilterEdgeFunctionTypes = """
@@ -56,6 +61,11 @@ public class DestinationFilters: UtilityPlugin {
5661
var dest = new DestinationFilter(destination, rules);
5762
return analytics.add(dest);
5863
}
64+
65+
function removePreviousDestinationFilters() {
66+
analytics.removeLivePlugins()
67+
return true
68+
}
5969
"""
6070
#endif
6171

@@ -79,11 +89,19 @@ public class DestinationFilters: UtilityPlugin {
7989
}
8090
}
8191

92+
var metricsPlugin: MetricsPlugin? = nil
93+
8294
public func update(settings: Settings, type: UpdateType) {
83-
guard type == .initial else { return }
8495
var setOfActiveDestinations: Set<String> = []
8596
let middlewareSettings = settings.middlewareSettings
8697
let rules = middlewareSettings?["routingRules"]?.arrayValue as? [[String: Any]] // This is an array
98+
99+
if let eng = engine {
100+
// First remove any exisitng destination filters
101+
eng.call(functionName: "removePreviousDestinationFilters")
102+
setOfActiveDestinations = []
103+
}
104+
87105
rules?.forEach {rule in
88106
let destination = rule["destinationName"] as? String ?? ""
89107
if !destination.isEmpty {
@@ -96,7 +114,13 @@ public class DestinationFilters: UtilityPlugin {
96114
}
97115
}
98116

99-
analytics?.add(plugin: MetricsPlugin(setOfActiveDestinations: setOfActiveDestinations))
117+
// need to remove the previous Metrics Plugin and add the new one.
118+
if let mp = metricsPlugin {
119+
analytics?.remove(plugin: mp)
120+
}
121+
122+
metricsPlugin = MetricsPlugin(setOfActiveDestinations: setOfActiveDestinations)
123+
analytics?.add(plugin: metricsPlugin!)
100124
}
101125

102126
}

0 commit comments

Comments
 (0)