Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: return response from callback #138

Merged
merged 4 commits into from
Aug 21, 2024
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
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');
});
HiteshShrestha marked this conversation as resolved.
Show resolved Hide resolved
});

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');
HiteshShrestha marked this conversation as resolved.
Show resolved Hide resolved
}
});

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');
HiteshShrestha marked this conversation as resolved.
Show resolved Hide resolved
}
}

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