Skip to content

Commit 9121478

Browse files
feat: change some things (#35)
1 parent 85ad66a commit 9121478

File tree

4 files changed

+53
-16
lines changed

4 files changed

+53
-16
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import gleam/dict
2727
import gleam/dynamic
2828
import gleam/io
2929
import gleam/option.{None}
30-
import openfeature/api as openfeature
30+
import openfeature
3131
import openfeature/client
3232
import openfeature/evaluation_context
3333
import openfeature/providers/in_memory
@@ -82,7 +82,7 @@ Further documentation can be found at <https://hexdocs.pm/openfeature>.
8282
Once you've added a provider as a dependency, it can be registered with OpenFeature like this:
8383

8484
```gleam
85-
import openfeature/api as openfeature
85+
import openfeature
8686
8787
openfeature.set_provider(my_provider())
8888
```
@@ -96,7 +96,7 @@ Sometimes, the value of a flag must consider some dynamic criteria about the app
9696
```gleam
9797
import gleam/dynamic
9898
import gleam/option.{None}
99-
import openfeature/api as openfeature
99+
import openfeature
100100
import openfeature/client
101101
import openfeature/evaluation_context.{EvaluationContext}
102102
@@ -141,7 +141,7 @@ TODO
141141
Clients can be assigned to a domain. A domain is a logical identifier which can be used to associate clients with a particular provider. If a domain has no associated provider, the default provider is used.
142142

143143
```gleam
144-
import openfeature/api as openfeature
144+
import openfeature
145145
146146
// registering the default provider
147147
openfeature.set_provider(local_provider())
@@ -163,7 +163,7 @@ TODO
163163
The OpenFeature API provides a close function to perform a cleanup of all registered providers. This should only be called when your application is in the process of shutting down.
164164

165165
```gleam
166-
import openfeature/api as openfeature
166+
import openfeature
167167
168168
openfeature.shutdown()
169169
```

gleam.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name = "openfeature"
2-
version = "0.5.0"
2+
version = "0.6.0"
33
description = "The Gleam SDK for the OpenFeature specification."
44
licences = ["Apache-2.0"]
55
repository = { type = "github", user = "horvathandris", repo = "openfeature-gleam-sdk" }

src/openfeature/api.gleam src/openfeature.gleam

+45-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import gleam/result
66
import openfeature/client.{type Client, Client, ClientMetadata}
77
import openfeature/domain.{type Domain}
88
import openfeature/evaluation_context.{type EvaluationContext, EvaluationContext}
9+
import openfeature/events.{type EventCallback}
910
import openfeature/provider.{type FeatureProvider, type Metadata}
1011
import openfeature/providers/no_op
1112
import worm
@@ -21,12 +22,15 @@ type APIMessage {
2122
)
2223
GetProvider(reply_with: Subject(FeatureProvider), domain: Domain)
2324
SetContext(evaluation_context: EvaluationContext)
25+
AddHandler(domain: Domain, callback: EventCallback)
26+
RemoveHandler(domain: Domain, callback: EventCallback)
2427
}
2528

2629
type API {
2730
API(
2831
provider_registry: Dict(Domain, FeatureProvider),
2932
global_context: EvaluationContext,
33+
event_callbacks: Dict(Domain, List(EventCallback)),
3034
)
3135
}
3236

@@ -35,7 +39,7 @@ fn get_api_subject() -> Subject(APIMessage) {
3539
}
3640

3741
fn init_api() {
38-
let initial_state = API(dict.new(), evaluation_context.empty())
42+
let initial_state = API(dict.new(), evaluation_context.empty(), dict.new())
3943
let assert Ok(subject) =
4044
actor.start(initial_state, fn(message: APIMessage, state: API) {
4145
case message {
@@ -55,6 +59,14 @@ fn init_api() {
5559
SetContext(evaluation_context) ->
5660
set_context_internal(state, evaluation_context)
5761
|> actor.continue
62+
63+
AddHandler(domain, callback) ->
64+
add_handler_internal(state, domain, callback)
65+
|> actor.continue
66+
67+
RemoveHandler(domain, callback) ->
68+
remove_handler_internal(state, domain, callback)
69+
|> actor.continue
5870
}
5971
})
6072
subject
@@ -76,14 +88,14 @@ fn set_provider_internal(
7688
domain: Domain,
7789
provider: FeatureProvider,
7890
) -> API {
79-
provider.initialize(state.global_context)
80-
|> actor.send(reply_with, _)
91+
let init_result = provider.initialize(state.global_context)
92+
actor.send(reply_with, init_result)
8193

8294
let provider_registry =
8395
state.provider_registry
8496
|> dict.insert(domain, provider)
8597

86-
API(provider_registry, state.global_context)
98+
API(provider_registry, state.global_context, state.event_callbacks)
8799
}
88100

89101
fn get_provider() -> FeatureProvider {
@@ -147,7 +159,7 @@ fn set_context_internal(
147159
state: API,
148160
evaluation_context: EvaluationContext,
149161
) -> API {
150-
API(state.provider_registry, evaluation_context)
162+
API(state.provider_registry, evaluation_context, state.event_callbacks)
151163
}
152164

153165
pub fn shutdown() {
@@ -159,3 +171,31 @@ fn shutdown_internal(state: API) -> Nil {
159171
|> dict.values
160172
|> list.each(fn(provider) { provider.shutdown() })
161173
}
174+
175+
fn add_handler_internal(
176+
state: API,
177+
domain: Domain,
178+
callback: EventCallback,
179+
) -> API {
180+
let domain_callbacks =
181+
state.event_callbacks
182+
|> dict.get(domain)
183+
|> result.unwrap([])
184+
|> list.prepend(callback)
185+
186+
state.event_callbacks
187+
|> dict.insert(domain, domain_callbacks)
188+
|> API(state.provider_registry, state.global_context, _)
189+
}
190+
191+
fn remove_handler_internal(state: API, domain: Domain, callback: EventCallback) {
192+
let domain_callbacks =
193+
state.event_callbacks
194+
|> dict.get(domain)
195+
|> result.unwrap([])
196+
|> list.drop_while(fn(x) { x == callback })
197+
198+
state.event_callbacks
199+
|> dict.insert(domain, domain_callbacks)
200+
|> API(state.provider_registry, state.global_context, _)
201+
}

src/openfeature/eventing.gleam src/openfeature/events.gleam

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import gleam/dynamic.{type Dynamic}
33
import gleam/option.{type Option}
44
import openfeature/error.{type ErrorCode}
55

6-
type EventCallback =
6+
pub type EventCallback =
77
fn(EventDetails) -> Nil
88

99
type EventType {
@@ -23,8 +23,5 @@ type ProviderEventDetails {
2323
}
2424

2525
type EventDetails {
26-
EventDetails(
27-
provider_name: String,
28-
provider_event_details: ProviderEventDetails,
29-
)
26+
EventDetails(provider_name: String, event_details: ProviderEventDetails)
3027
}

0 commit comments

Comments
 (0)