Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/mobile-sdk-alpha/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type {
RegistrationInput,
RegistrationStatus,
SDKEvent,
SDKEventMap,
ScanMode,
ScanOpts,
ScanResult,
Expand Down
19 changes: 14 additions & 5 deletions packages/mobile-sdk-alpha/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
ScanOpts,
ScanResult,
SDKEvent,
SDKEventMap,
SelfClient,
Unsubscribe,
ValidationInput,
Expand Down Expand Up @@ -43,14 +44,21 @@ export function createSelfClient({ config, adapters }: { config: Config; adapter
}

const _adapters = { ...optionalDefaults, ...adapters } as Adapters;
const _cfg = { ...defaultConfig, ...config };
const listeners = new Map<SDKEvent, Set<(p: any) => void>>();

function on(event: SDKEvent, cb: (payload: any) => void): Unsubscribe {
function on<E extends SDKEvent>(event: E, cb: (payload: SDKEventMap[E]) => void): Unsubscribe {
const set = listeners.get(event) ?? new Set();
set.add(cb);
set.add(cb as any);
listeners.set(event, set);
return () => set.delete(cb);
return () => set.delete(cb as any);
}

function emit<E extends SDKEvent>(event: E, payload: SDKEventMap[E]): void {
const set = listeners.get(event);
if (!set) return;
for (const cb of set) {
(cb as (p: SDKEventMap[E]) => void)(payload);
}
}

async function scanDocument(opts: ScanOpts & { signal?: AbortSignal }): Promise<ScanResult> {
Expand All @@ -76,7 +84,7 @@ export function createSelfClient({ config, adapters }: { config: Config; adapter
if (!adapters.network) throw notImplemented('network');
if (!adapters.crypto) throw notImplemented('crypto');
const timeoutMs = opts.timeoutMs ?? cfg.timeouts?.proofMs ?? defaultConfig.timeouts.proofMs;
void timeoutMs;
void _adapters.clock.sleep(timeoutMs!, opts.signal).then(() => emit('error', new Error('timeout')));
return {
id: 'stub',
status: 'pending',
Expand All @@ -91,5 +99,6 @@ export function createSelfClient({ config, adapters }: { config: Config; adapter
checkRegistration,
generateProof,
on,
emit,
};
}
1 change: 1 addition & 0 deletions packages/mobile-sdk-alpha/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type {
RegistrationInput,
RegistrationStatus,
SDKEvent,
SDKEventMap,
ScanMode,
ScanOpts,
ScanResult,
Expand Down
10 changes: 8 additions & 2 deletions packages/mobile-sdk-alpha/src/types/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ export interface RegistrationStatus {
reason?: string;
}

export type SDKEvent = 'progress' | 'state' | 'error';
export interface SDKEventMap {
progress: Progress;
state: string;
error: Error;
}
export type SDKEvent = keyof SDKEventMap;

export type ScanMode = 'mrz' | 'nfc' | 'qr';
export interface ScanOpts {
Expand Down Expand Up @@ -121,7 +126,8 @@ export interface SelfClient {
timeoutMs?: number;
},
): Promise<ProofHandle>;
on(event: SDKEvent, cb: (payload: any) => void): Unsubscribe;
on<E extends SDKEvent>(event: E, cb: (payload: SDKEventMap[E]) => void): Unsubscribe;
emit<E extends SDKEvent>(event: E, payload: SDKEventMap[E]): void;
}
export type Unsubscribe = () => void;
export interface StorageAdapter {
Expand Down
Loading