Skip to content

Commit

Permalink
Merge pull request #138 from leapfrogtechnology/fix/return-from-callback
Browse files Browse the repository at this point in the history
Fix: return response from callback
  • Loading branch information
mesaugat authored Aug 21, 2024
2 parents 39e49b7 + cd44aec commit d0e5da9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/AsyncStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import AsyncStoreParams from './AsyncStoreParams';
* Async Store implementation contract.
*/
interface AsyncStore {
initialize: (callback: (err?: any) => void, params?: AsyncStoreParams) => void;
initialize: (callback: (err?: any) => void, params?: AsyncStoreParams) => any;
set: (properties: any) => void;
get: (key: string) => any;
getAll: () => any;
Expand Down
2 changes: 1 addition & 1 deletion src/impl/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function initialize(callback: (err?: any) => void, params?: AsyncStorePar
d[STORE_KEY] = Object.create(null);
d[ID_KEY] = randomUUID();

d.run(callback);
return d.run(callback);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function initializeFastifyPlugin(adapter: AsyncStoreAdapter = AsyncStoreA
* Initialize the async store based on the adapter provided.
*
* @param {AsyncStoreAdapter} [adapter=AsyncStoreAdapter.DOMAIN]
* @returns {(params: AsyncStoreParams) => void}
* @returns {(callback: (err?: any) => void, params?: AsyncStoreParams) => any }
*/
export function initialize(adapter: AsyncStoreAdapter = AsyncStoreAdapter.DOMAIN) {
if (isInitialized()) {
Expand All @@ -111,7 +111,7 @@ export function initialize(adapter: AsyncStoreAdapter = AsyncStoreAdapter.DOMAIN
return (callback: (err?: any) => void, params?: AsyncStoreParams) => {
initializedAdapter = adapter;

instance.initialize(callback, params);
return instance.initialize(callback, params);
};
}

Expand Down
83 changes: 83 additions & 0 deletions test/domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,5 +839,88 @@ describe('store: [adapter=DOMAIN]', () => {
globalStore.initialize(adapter)(callback);
});
});

it('should return the response from callback function.', (done) => {
const callback = () => {
globalStore.set({ foo: 'foo' });

return functionAccessingStore();
};

const functionAccessingStore = () => {
return globalStore.get('foo');
};

const response = globalStore.initialize(adapter)(callback);
expect(response).to.equal('foo');

done();
});

it('should return the response from async callback function.', async () => {
const callback = async () => {
globalStore.set({ foo: 'foo' });

functionAccessingStore();
const response = await asyncTask();

return response;
};

const functionAccessingStore = () => {
expect(globalStore.get('foo')).to.equal('foo');
};

const asyncTask = () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(globalStore.get('foo'));
}, 1);
});
};

const response = await globalStore.initialize(adapter)(callback);
expect(response).to.equal('foo');
});
});

describe('Error Handling:', () => {
it('should bubble up the promise rejection from the callback.', async () => {
const callback = () => {
globalStore.set({ foo: 'foo' });

return new Promise((resolve, reject) => {
setTimeout(() => {
reject('Hello world');
}, 1);
});
};

try {
await globalStore.initialize(adapter)(callback);
expect.fail('Should not reach here.');
} catch (e) {
expect(e).to.equal('Hello world');
}
});

it('should bubble up the error thrown from the callback.', (done) => {
const callback = () => {
globalStore.set({ foo: 'foo' });

throw new Error('Hello world');
};

try {
globalStore.initialize(adapter)(callback);
expect.fail('Should not reach here.');
} catch (e) {
if (e instanceof Error) {
expect(e.message).to.equal('Hello world');
}
}

done();
});
});
});

0 comments on commit d0e5da9

Please sign in to comment.