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

Commit

Permalink
Recreate DestinationFilters whenever we get new Settings (#3)
Browse files Browse the repository at this point in the history
* Allow the filters plugin to be updated with newer Settings after the initial update.
  • Loading branch information
didiergarcia authored Feb 5, 2024
1 parent eec7fbb commit 0367052
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

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

Expand Down Expand Up @@ -60,7 +60,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
75EE634128DBCDD800A0B99E /* DestinationFilters-Swift in Frameworks */,
4631BE162B602F920063334A /* AnalyticsFilters in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -164,10 +164,11 @@
buildRules = (
);
dependencies = (
4631BE142B602F8B0063334A /* PBXTargetDependency */,
);
name = DestinationFiltersExample;
packageProductDependencies = (
75EE634028DBCDD800A0B99E /* DestinationFilters-Swift */,
4631BE152B602F920063334A /* AnalyticsFilters */,
);
productName = DestinationFiltersExample;
productReference = 75EE630A28DBC60500A0B99E /* DestinationFiltersExample.app */;
Expand Down Expand Up @@ -311,6 +312,10 @@
/* End PBXSourcesBuildPhase section */

/* Begin PBXTargetDependency section */
4631BE142B602F8B0063334A /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
productRef = 4631BE132B602F8B0063334A /* AnalyticsFilters */;
};
75EE632228DBC60800A0B99E /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = 75EE630928DBC60500A0B99E /* DestinationFiltersExample */;
Expand Down Expand Up @@ -462,8 +467,10 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = "";
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = DestinationFiltersExample/Info.plist;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
Expand All @@ -476,8 +483,9 @@
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = prayansh.DestinationFiltersExample;
PRODUCT_BUNDLE_IDENTIFIER = com.segment.destfiltersexample;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
Expand All @@ -489,8 +497,10 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = "";
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = DestinationFiltersExample/Info.plist;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
Expand All @@ -503,8 +513,9 @@
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = prayansh.DestinationFiltersExample;
PRODUCT_BUNDLE_IDENTIFIER = com.segment.destfiltersexample;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
Expand Down Expand Up @@ -625,9 +636,13 @@
/* End XCConfigurationList section */

/* Begin XCSwiftPackageProductDependency section */
75EE634028DBCDD800A0B99E /* DestinationFilters-Swift */ = {
4631BE132B602F8B0063334A /* AnalyticsFilters */ = {
isa = XCSwiftPackageProductDependency;
productName = AnalyticsFilters;
};
4631BE152B602F920063334A /* AnalyticsFilters */ = {
isa = XCSwiftPackageProductDependency;
productName = "DestinationFilters-Swift";
productName = AnalyticsFilters;
};
/* End XCSwiftPackageProductDependency section */
};
Expand Down
6 changes: 3 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
{
"identity" : "analytics-swift-live",
"kind" : "remoteSourceControl",
"location" : "git@github.com:segment-integrations/analytics-swift-live.git",
"location" : "https://github.com/segment-integrations/analytics-swift-live.git",
"state" : {
"revision" : "3b07681d498595aa8e06cd814003db53661abb88",
"version" : "0.2.3"
"branch" : "add-removeLivePlugins-function",
"revision" : "049479fe4e18fcdda96ae2578e1f5be3d34f2e41"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let package = Package(
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/segment-integrations/analytics-swift-live.git", .upToNextMajor(from: "0.2.3")),
.package(url: "https://github.com/segment-integrations/analytics-swift-live.git", .upToNextMajor(from: "1.0.0")),
.package(url: "https://github.com/segmentio/analytics-swift.git", from: "1.4.7"),
.package(url: "https://github.com/segmentio/substrata-swift.git", .upToNextMajor(from: "0.0.2")),
],
Expand Down
28 changes: 26 additions & 2 deletions Sources/AnalyticsFilters/DestinationFilters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public class DestinationFilters: UtilityPlugin {
var dest = new DestinationFilter(destination, rules);
return analytics.add(dest);
}
function removePreviousDestinationFilters() {
analytics.removeLivePlugins()
return true
}
"""
#else
var destinationFilterEdgeFunctionTypes = """
Expand All @@ -56,6 +61,11 @@ public class DestinationFilters: UtilityPlugin {
var dest = new DestinationFilter(destination, rules);
return analytics.add(dest);
}
function removePreviousDestinationFilters() {
analytics.removeLivePlugins()
return true
}
"""
#endif

Expand All @@ -79,11 +89,19 @@ public class DestinationFilters: UtilityPlugin {
}
}

var metricsPlugin: MetricsPlugin? = nil

public func update(settings: Settings, type: UpdateType) {
guard type == .initial else { return }
var setOfActiveDestinations: Set<String> = []
let middlewareSettings = settings.middlewareSettings
let rules = middlewareSettings?["routingRules"]?.arrayValue as? [[String: Any]] // This is an array

if let eng = engine {
// First remove any exisitng destination filters
eng.call(functionName: "removePreviousDestinationFilters")
setOfActiveDestinations = []
}

rules?.forEach {rule in
let destination = rule["destinationName"] as? String ?? ""
if !destination.isEmpty {
Expand All @@ -96,7 +114,13 @@ public class DestinationFilters: UtilityPlugin {
}
}

analytics?.add(plugin: MetricsPlugin(setOfActiveDestinations: setOfActiveDestinations))
// need to remove the previous Metrics Plugin and add the new one.
if let mp = metricsPlugin {
analytics?.remove(plugin: mp)
}

metricsPlugin = MetricsPlugin(setOfActiveDestinations: setOfActiveDestinations)
analytics?.add(plugin: metricsPlugin!)
}

}

0 comments on commit 0367052

Please sign in to comment.