Skip to content

Commit

Permalink
fix: skip initial fetch with context (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew authored Jan 17, 2025
1 parent 40397e2 commit 7261dc1
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.9.2

* Fix: Skip initial toggles fetch when using updateContext or setContextFields

## 1.9.1

* Chore: Upgrade event_emitter version
Expand Down
7 changes: 6 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:unleash_proxy_client_flutter/unleash_proxy_client_flutter.dart';
import 'package:unleash_proxy_client_flutter/unleash_context.dart';

void main() {
WidgetsFlutterBinding.ensureInitialized();
Expand Down Expand Up @@ -41,8 +42,12 @@ class _MyHomePageState extends State<MyHomePage> {
var unleash = UnleashClient(
url: Uri.parse('https://sandbox.getunleash.io/enterprise/api/frontend'),
clientKey: 'SDKIntegration:development.f0474f4a37e60794ee8fb00a4c112de58befde962af6d5055b383ea3',
refreshInterval: 30,
refreshInterval: 10,
experimental: const ExperimentalConfig(togglesStorageTTL: 60),
appName: 'example-flutter-app');
unleash.updateContext(UnleashContext(
userId: '123',
));
void updateCounterEnabled(_) {
final counterEnabled = unleash.isEnabled('counter');
setState(() {
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.9.0"
version: "1.9.2"
uuid:
dependency: transitive
description:
Expand Down
13 changes: 9 additions & 4 deletions lib/unleash_proxy_client_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class UnleashClient extends EventEmitter {
this.customHeaders = const {},
this.impressionDataAll = false,
// bump on each release, overwrite in tests, do not change in client code
this.sdkName = '[email protected].1',
this.sdkName = '[email protected].2',
this.experimental}) {
_init();
metrics = Metrics(
Expand Down Expand Up @@ -204,8 +204,6 @@ class UnleashClient extends EventEmitter {

if (bootstrap != null) {
await _storeLastRefreshTimestamp();
} else {
lastRefreshTimestamp = await _getLastRefreshTimestamp();
}

clientState = ClientState.initialized;
Expand Down Expand Up @@ -355,7 +353,12 @@ class UnleashClient extends EventEmitter {

Future<void> setContextFields(Map<String, String> fields) async {
if (!_anyFieldHasChanged(fields)) return;
if (clientState == ClientState.ready) {
if (started == false) {
await _waitForEvent('initialized');
fields.forEach((field, value) {
_updateContextField(field, value);
});
} else if (clientState == ClientState.ready) {
fields.forEach((field, value) {
_updateContextField(field, value);
});
Expand Down Expand Up @@ -456,6 +459,8 @@ class UnleashClient extends EventEmitter {

metrics.start();

lastRefreshTimestamp = await _getLastRefreshTimestamp();

if (!_isUpToDate()) {
await _fetchToggles();
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: A Flutter/Dart client that can be used together with the unleash-pr
homepage: https://github.com/Unleash/unleash_proxy_client_flutter
repository: https://github.com/Unleash/unleash_proxy_client_flutter
issue_tracker: https://github.com/Unleash/unleash_proxy_client_flutter
version: 1.9.1
version: 1.9.2

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
62 changes: 62 additions & 0 deletions test/unleash_proxy_client_flutter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,68 @@ void main() {
expect(anotherGetMock.calledTimes, 1);
});

test('skip initial fetch when context is identical with updateContext',
() async {
final getMock = GetMock();
final sharedStorageProvider = InMemoryStorageProvider();
final unleash = UnleashClient(
url: url,
clientKey: 'proxy-123',
appName: 'flutter-test',
storageProvider: sharedStorageProvider,
fetcher: getMock,
experimental: const ExperimentalConfig(togglesStorageTTL: 10));
unleash.updateContext(UnleashContext(userId: '123'));

await unleash.start();

final anotherGetMock = GetMock();
final anotherUnleash = UnleashClient(
url: url,
clientKey: 'proxy-123',
appName: 'flutter-test',
storageProvider: sharedStorageProvider,
fetcher: anotherGetMock,
experimental: const ExperimentalConfig(togglesStorageTTL: 10),
);
anotherUnleash.updateContext(UnleashContext(userId: '123'));

await anotherUnleash.start();

expect(anotherGetMock.calledTimes, 0);
});

test('skip initial fetch when context is identical with setContextFields',
() async {
final getMock = GetMock();
final sharedStorageProvider = InMemoryStorageProvider();
final unleash = UnleashClient(
url: url,
clientKey: 'proxy-123',
appName: 'flutter-test',
storageProvider: sharedStorageProvider,
fetcher: getMock,
experimental: const ExperimentalConfig(togglesStorageTTL: 10));
unleash.setContextFields({'userId': '123'});

await unleash.start();

final anotherGetMock = GetMock();
final anotherUnleash = UnleashClient(
url: url,
clientKey: 'proxy-123',
appName: 'flutter-test',
storageProvider: sharedStorageProvider,
fetcher: anotherGetMock,
experimental: const ExperimentalConfig(togglesStorageTTL: 10),
);
anotherUnleash.setContextFields({'userId': '123'});

await anotherUnleash.start();

expect(anotherGetMock.calledTimes, 0);
});

test('do not skip initial fetch when TTL exceeded', () async {
final getMock = GetMock();
final sharedStorageProvider = InMemoryStorageProvider();
Expand Down

0 comments on commit 7261dc1

Please sign in to comment.