Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/sdk/server-node/contract-tests/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ app.get('/', (req: Request, res: Response) => {
'client-prereq-events',
'event-gzip',
'optional-event-gzip',
'flag-change-listeners',
],
});
});
Expand Down
52 changes: 52 additions & 0 deletions packages/sdk/server-node/contract-tests/src/sdkClientEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ interface CommandParams {
newEndpoint: string;
oldEndpoint: string;
};
registerFlagChangeListener?: {
listenerId: string;
callbackUri: string;
};
unregisterListener?: {
listenerId: string;
};
}

export function makeSdkConfig(options: SdkConfigOptions, tag: string): LDOptions {
Expand Down Expand Up @@ -316,9 +323,15 @@ export interface SdkClientEntity {
doCommand: (params: CommandParams) => Promise<any>;
}

interface ListenerEntry {
eventName: string;
handler: (...args: any[]) => void;
}

export async function newSdkClientEntity(options: any): Promise<SdkClientEntity> {
const c: any = {};
const log = Log(options.tag);
const listeners = new Map<string, ListenerEntry>();

log.info(`Creating client with configuration: ${JSON.stringify(options.configuration)}`);
const timeout =
Expand All @@ -341,6 +354,11 @@ export async function newSdkClientEntity(options: any): Promise<SdkClientEntity>
}

c.close = () => {
// Unregister all listeners before closing to avoid firing callbacks after shutdown.
listeners.forEach((entry) => {
client.off(entry.eventName, entry.handler);
});
listeners.clear();
client.close();
log.info('Test ended');
};
Expand Down Expand Up @@ -514,6 +532,40 @@ export async function newSdkClientEntity(options: any): Promise<SdkClientEntity>
}
}

case 'registerFlagChangeListener': {
const p = params.registerFlagChangeListener!;
const eventName = 'update';

const handler = (eventParams: { key: string }) => {
got
.post(p.callbackUri, {
json: {
listenerId: p.listenerId,
flagKey: eventParams.key,
},
})
.catch(() => {});
};

const existing = listeners.get(p.listenerId);
if (existing) {
client.off(existing.eventName, existing.handler);
}
listeners.set(p.listenerId, { eventName, handler });
client.on(eventName, handler);
return undefined;
}

case 'unregisterListener': {
const p = params.unregisterListener!;
const entry = listeners.get(p.listenerId);
if (entry) {
client.off(entry.eventName, entry.handler);
listeners.delete(p.listenerId);
}
return undefined;
}

default:
throw badCommandError;
}
Expand Down