Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update context await #7

Merged
merged 3 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/unleash_proxy_client_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ class UnleashClient extends EventEmitter {
/// The flag used commonly for "disabled" feature toggles that are not visible to frontend SDKs.
bool impressionDataAll;

/// Internal indicator if the client has been started
var started = false;

UnleashClient(
{required this.url,
required this.clientKey,
Expand Down Expand Up @@ -240,7 +243,10 @@ class UnleashClient extends EventEmitter {
}

Future<void> updateContext(UnleashContext unleashContext) async {
if (clientState == ClientState.ready) {
if (started == false) {
await _waitForEvent('initialized');
_updateContextFields(unleashContext);
} else if (clientState == ClientState.ready) {
_updateContextFields(unleashContext);
await _fetchToggles();
} else {
Expand Down Expand Up @@ -295,6 +301,7 @@ class UnleashClient extends EventEmitter {
}

Future<void> start() async {
started = true;
if (clientState == ClientState.initializing) {
await _waitForEvent('initialized');
}
Expand Down
48 changes: 48 additions & 0 deletions test/unleash_proxy_client_flutter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,54 @@ void main() {
]);
});

test('update context with await', () async {
final getMock = GetMock();
final unleash = UnleashClient(
url: url,
clientKey: 'proxy-123',
appName: 'flutter-test',
sessionIdGenerator: generateSessionId,
storageProvider: InMemoryStorageProvider(),
fetcher: getMock);

await unleash.updateContext(UnleashContext(
userId: '123',
remoteAddress: 'address',
sessionId: 'session',
properties: {'customKey': 'customValue'}));
await unleash.start();

expect(getMock.calledTimes, 1);
expect(getMock.calledWithUrls, [
Uri.parse(
'https://app.unleash-hosted.com/demo/api/proxy?userId=123&remoteAddress=address&sessionId=session&customKey=customValue')
]);
});

test('update context without await', () async {
final getMock = GetMock();
final unleash = UnleashClient(
url: url,
clientKey: 'proxy-123',
appName: 'flutter-test',
sessionIdGenerator: generateSessionId,
storageProvider: InMemoryStorageProvider(),
fetcher: getMock);

unleash.updateContext(UnleashContext(
userId: '123',
remoteAddress: 'address',
sessionId: 'session',
properties: {'customKey': 'customValue'}));
await unleash.start();

expect(getMock.calledTimes, 1);
expect(getMock.calledWithUrls, [
Uri.parse(
'https://app.unleash-hosted.com/demo/api/proxy?userId=123&remoteAddress=address&sessionId=session&customKey=customValue')
]);
});

test('set context field should wait on asynchronous start', () async {
final getMock = GetMock();
final unleash = UnleashClient(
Expand Down