Skip to content

Commit f69296d

Browse files
committed
chore: remove sub to all config changes
1 parent d258c58 commit f69296d

7 files changed

Lines changed: 62 additions & 354 deletions

File tree

packages/sdk/README.md

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -178,31 +178,14 @@ const maxConnections = replane.get("max-connections", { default: 10 });
178178
replane.disconnect();
179179
```
180180

181-
### `replane.subscribe(callback)` or `replane.subscribe(configName, callback)`
181+
### `replane.subscribe(configName, callback)`
182182

183-
Subscribe to config changes and receive real-time updates when configs are modified.
184-
185-
**Two overloads:**
186-
187-
1. **Subscribe to all config changes:**
188-
189-
```ts
190-
const unsubscribe = replane.subscribe((config) => {
191-
console.log(`Config ${config.name} changed to:`, config.value);
192-
});
193-
```
194-
195-
2. **Subscribe to a specific config:**
196-
```ts
197-
const unsubscribe = replane.subscribe("billing-enabled", (config) => {
198-
console.log(`billing-enabled changed to:`, config.value);
199-
});
200-
```
183+
Subscribe to a specific config's changes and receive real-time updates when it is modified.
201184

202185
Parameters:
203186

204-
- `callback` (function) – Function called when any config changes. Receives an object with `{ name, value }`.
205-
- `configName` (K extends keyof T) – Optional. If provided, only changes to this specific config will trigger the callback.
187+
- `configName` (K extends keyof T) – The config to watch for changes.
188+
- `callback` (function) – Function called when the config changes. Receives an object with `{ name, value }`.
206189

207190
Returns a function to unsubscribe from the config changes.
208191

@@ -220,19 +203,13 @@ await replane.connect({
220203
baseUrl: "https://cloud.replane.dev", // or your self-hosted URL
221204
});
222205

223-
// Subscribe to all config changes
224-
const unsubscribeAll = replane.subscribe((config) => {
225-
console.log(`Config ${config.name} updated:`, config.value);
226-
});
227-
228206
// Subscribe to a specific config
229207
const unsubscribeFeature = replane.subscribe("feature-flag", (config) => {
230208
console.log("Feature flag changed:", config.value);
231209
// config.value is typed as boolean
232210
});
233211

234212
// Later: unsubscribe when done
235-
unsubscribeAll();
236213
unsubscribeFeature();
237214

238215
// Clean up when done
@@ -450,30 +427,18 @@ await replane.connect({
450427
baseUrl: "https://replane.my-host.com",
451428
});
452429

453-
// Subscribe to all config changes
454-
const unsubscribeAll = replane.subscribe((config) => {
455-
console.log(`Config ${config.name} changed:`, config.value);
456-
457-
// React to specific config changes
458-
if (config.name === "feature-flag") {
459-
console.log("Feature flag updated:", config.value);
460-
}
461-
});
462-
463-
// Subscribe to a specific config only
430+
// Subscribe to specific configs
464431
const unsubscribeFeature = replane.subscribe("feature-flag", (config) => {
465432
console.log("Feature flag changed:", config.value);
466433
// config.value is automatically typed as boolean
467434
});
468435

469-
// Subscribe to multiple specific configs
470436
const unsubscribeMaxUsers = replane.subscribe("max-users", (config) => {
471437
console.log("Max users changed:", config.value);
472438
// config.value is automatically typed as number
473439
});
474440

475441
// Cleanup
476-
unsubscribeAll();
477442
unsubscribeFeature();
478443
unsubscribeMaxUsers();
479444
replane.disconnect();

packages/sdk/examples/browser/src/main.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,20 @@ async function main() {
143143
// Render initial config
144144
renderConfig(replane);
145145

146-
// Subscribe to all config changes
147-
replane.subscribe((config) => {
146+
// Subscribe to specific configs
147+
replane.subscribe("feature-flags", (config) => {
148148
log(`Config "${config.name}" updated`);
149149
renderConfig(replane);
150150
});
151151

152-
// Subscribe to specific config
153-
replane.subscribe("feature-flags", () => {
154-
log("Feature flags changed!");
152+
replane.subscribe("rate-limits", (config) => {
153+
log(`Config "${config.name}" updated`);
154+
renderConfig(replane);
155+
});
156+
157+
replane.subscribe("maintenance-mode", (config) => {
158+
log(`Config "${config.name}" updated`);
159+
renderConfig(replane);
155160
});
156161

157162
log("Subscribed to config updates");

packages/sdk/examples/bun/src/index.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,31 +84,37 @@ async function main() {
8484
// Subscribe to config changes (real-time updates via SSE)
8585
console.log("\nSubscribing to config changes...");
8686

87-
const unsubscribeAll = replane.subscribe((config) => {
88-
console.log(`[Update] Config "${config.name}" changed:`, config.value);
89-
});
90-
91-
// Subscribe to a specific config
87+
// Subscribe to specific configs
9288
const unsubscribeFeatures = replane.subscribe("feature-flags", (config) => {
9389
console.log("[Update] Feature flags changed:", config.value);
9490
});
9591

92+
const unsubscribeRateLimits = replane.subscribe("rate-limits", (config) => {
93+
console.log("[Update] Rate limits changed:", config.value);
94+
});
95+
96+
const unsubscribeMaintenance = replane.subscribe("maintenance-mode", (config) => {
97+
console.log("[Update] Maintenance mode changed:", config.value);
98+
});
99+
96100
// Keep the process running to receive updates
97101
console.log("Listening for config updates. Press Ctrl+C to exit.\n");
98102

99103
// Handle graceful shutdown
100104
process.on("SIGINT", () => {
101105
console.log("\nShutting down...");
102-
unsubscribeAll();
103106
unsubscribeFeatures();
107+
unsubscribeRateLimits();
108+
unsubscribeMaintenance();
104109
replane.disconnect();
105110
process.exit(0);
106111
});
107112

108113
process.on("SIGTERM", () => {
109114
console.log("\nShutting down...");
110-
unsubscribeAll();
111115
unsubscribeFeatures();
116+
unsubscribeRateLimits();
117+
unsubscribeMaintenance();
112118
replane.disconnect();
113119
process.exit(0);
114120
});

packages/sdk/examples/deno/src/index.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,28 @@ async function main() {
8484
// Subscribe to config changes (real-time updates via SSE)
8585
console.log("\nSubscribing to config changes...");
8686

87-
const unsubscribeAll = replane.subscribe((config) => {
88-
console.log(`[Update] Config "${config.name}" changed:`, config.value);
89-
});
90-
91-
// Subscribe to a specific config
87+
// Subscribe to specific configs
9288
const unsubscribeFeatures = replane.subscribe("feature-flags", (config) => {
9389
console.log("[Update] Feature flags changed:", config.value);
9490
});
9591

92+
const unsubscribeRateLimits = replane.subscribe("rate-limits", (config) => {
93+
console.log("[Update] Rate limits changed:", config.value);
94+
});
95+
96+
const unsubscribeMaintenance = replane.subscribe("maintenance-mode", (config) => {
97+
console.log("[Update] Maintenance mode changed:", config.value);
98+
});
99+
96100
// Keep the process running to receive updates
97101
console.log("Listening for config updates. Press Ctrl+C to exit.\n");
98102

99103
// Handle graceful shutdown
100104
Deno.addSignalListener("SIGINT", () => {
101105
console.log("\nShutting down...");
102-
unsubscribeAll();
103106
unsubscribeFeatures();
107+
unsubscribeRateLimits();
108+
unsubscribeMaintenance();
104109
replane.disconnect();
105110
Deno.exit(0);
106111
});

packages/sdk/examples/node/src/index.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,31 +84,37 @@ async function main() {
8484
// Subscribe to config changes (real-time updates via SSE)
8585
console.log("\nSubscribing to config changes...");
8686

87-
const unsubscribeAll = replane.subscribe((config) => {
88-
console.log(`[Update] Config "${config.name}" changed:`, config.value);
89-
});
90-
91-
// Subscribe to a specific config
87+
// Subscribe to specific configs
9288
const unsubscribeFeatures = replane.subscribe("feature-flags", (config) => {
9389
console.log("[Update] Feature flags changed:", config.value);
9490
});
9591

92+
const unsubscribeRateLimits = replane.subscribe("rate-limits", (config) => {
93+
console.log("[Update] Rate limits changed:", config.value);
94+
});
95+
96+
const unsubscribeMaintenance = replane.subscribe("maintenance-mode", (config) => {
97+
console.log("[Update] Maintenance mode changed:", config.value);
98+
});
99+
96100
// Keep the process running to receive updates
97101
console.log("Listening for config updates. Press Ctrl+C to exit.\n");
98102

99103
// Handle graceful shutdown
100104
process.on("SIGINT", () => {
101105
console.log("\nShutting down...");
102-
unsubscribeAll();
103106
unsubscribeFeatures();
107+
unsubscribeRateLimits();
108+
unsubscribeMaintenance();
104109
replane.disconnect();
105110
process.exit(0);
106111
});
107112

108113
process.on("SIGTERM", () => {
109114
console.log("\nShutting down...");
110-
unsubscribeAll();
111115
unsubscribeFeatures();
116+
unsubscribeRateLimits();
117+
unsubscribeMaintenance();
112118
replane.disconnect();
113119
process.exit(0);
114120
});

packages/sdk/src/client.ts

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ export class Replane<T extends object = Record<string, unknown>> {
5959
private logger: ReplaneLogger;
6060
private storage: ReplaneRemoteStorage | null = null;
6161
private configSubscriptions = new Map<keyof T, Set<(config: MapConfig<T>) => void>>();
62-
private clientSubscriptions = new Set<(config: MapConfig<T>) => void>();
6362

6463
/**
6564
* Create a new Replane client.
@@ -215,24 +214,6 @@ export class Replane<T extends object = Record<string, unknown>> {
215214
}
216215
}
217216

218-
/**
219-
* Subscribe to config changes.
220-
*
221-
* @param callback - Function called when any config changes
222-
* @returns Unsubscribe function
223-
*
224-
* @example
225-
* ```typescript
226-
* const unsubscribe = client.subscribe((change) => {
227-
* console.log(`Config ${change.name} changed to ${change.value}`);
228-
* });
229-
*
230-
* // Later: stop listening
231-
* unsubscribe();
232-
* ```
233-
*/
234-
subscribe(callback: (config: MapConfig<T>) => void): () => void;
235-
236217
/**
237218
* Subscribe to a specific config's changes.
238219
*
@@ -250,46 +231,20 @@ export class Replane<T extends object = Record<string, unknown>> {
250231
subscribe<K extends keyof T>(
251232
configName: K,
252233
callback: (config: { name: K; value: T[K] }) => void
253-
): () => void;
254-
255-
subscribe<K extends keyof T>(
256-
callbackOrConfigName: K | ((config: MapConfig<T>) => void),
257-
callbackOrUndefined?: (config: { name: K; value: T[K] }) => void
258234
): () => void {
259-
let configName: keyof T | undefined = undefined;
260-
let callback: (config: MapConfig<T>) => void;
261-
262-
if (typeof callbackOrConfigName === "function") {
263-
callback = callbackOrConfigName;
264-
} else {
265-
configName = callbackOrConfigName as keyof T;
266-
if (callbackOrUndefined === undefined) {
267-
throw new Error("callback is required when config name is provided");
268-
}
269-
// Type assertion is safe: MapConfig<T> is a union that includes { name: K, value: T[K] }
270-
callback = callbackOrUndefined as (config: MapConfig<T>) => void;
271-
}
272-
273235
// Wrap the callback to ensure that we have a unique reference
274-
const originalCallback = callback;
275-
callback = (...args: Parameters<typeof callback>) => {
276-
originalCallback(...args);
236+
// Type assertion is safe: MapConfig<T> is a union that includes { name: K, value: T[K] }
237+
const wrappedCallback = (config: MapConfig<T>) => {
238+
callback(config as { name: K; value: T[K] });
277239
};
278240

279-
if (configName === undefined) {
280-
this.clientSubscriptions.add(callback);
281-
return () => {
282-
this.clientSubscriptions.delete(callback);
283-
};
284-
}
285-
286241
if (!this.configSubscriptions.has(configName)) {
287242
this.configSubscriptions.set(configName, new Set());
288243
}
289-
this.configSubscriptions.get(configName)!.add(callback);
244+
this.configSubscriptions.get(configName)!.add(wrappedCallback);
290245

291246
return () => {
292-
this.configSubscriptions.get(configName)?.delete(callback);
247+
this.configSubscriptions.get(configName)?.delete(wrappedCallback);
293248
if (this.configSubscriptions.get(configName)?.size === 0) {
294249
this.configSubscriptions.delete(configName);
295250
}
@@ -378,9 +333,6 @@ export class Replane<T extends object = Record<string, unknown>> {
378333

379334
const change = { name: config.name as keyof T, value: config.value as T[keyof T] };
380335

381-
for (const callback of this.clientSubscriptions) {
382-
callback(change);
383-
}
384336
for (const callback of this.configSubscriptions.get(config.name as keyof T) ?? []) {
385337
callback(change);
386338
}

0 commit comments

Comments
 (0)