Skip to content

Commit

Permalink
feat(vue): added refAsync helper
Browse files Browse the repository at this point in the history
# Conflicts:
#	packages/vue/src/index.ts
  • Loading branch information
kedrzu committed Jul 11, 2023
1 parent c3d5e69 commit 09e516b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export * from './localStorageRef.js';
export * from './prop.js';
export * from './makeRef.js';
export * from './context.js';
export * from './useAsync.js';
export * from './useDataSource.js';
export * from './useElement.js';
export * from './useEmitAsync.js';
Expand All @@ -23,6 +22,7 @@ export * from './onHistoryBack.js';
export * from './onWindowEvent.js';
export * from './onWindowResize.js';
export * from './onWindowScroll.js';
export * from './refAsync.js';

export * from './modal/ModalHost.js';
export * from './modal/ModalService.js';
Expand Down
25 changes: 25 additions & 0 deletions packages/vue/src/refAsync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Ref, ref } from 'vue';

/**
* Creates a ref that is populated with promise result once it's resolved.
* @param promise Ref will be populated with this promise's result.
*/
export function refAsync<T>(promise: Promise<T>): Ref<T | undefined>;
/**
* Creates a ref that is populated with promise result once it's resolved.
* @param fcn Function that returns a promise. Ref will be populated with this promise's result.
*/
export function refAsync<T>(fcn: () => Promise<T>): Ref<T | undefined>;
export function refAsync<T>(promise: Promise<T> | (() => Promise<T>)): Ref<T | undefined> {
const reference = ref<T>();

if (typeof promise === 'function') {
promise = promise();
}

void promise.then(result => {
reference.value = result;
});

return reference;
}
11 changes: 0 additions & 11 deletions packages/vue/src/useAsync.ts

This file was deleted.

0 comments on commit 09e516b

Please sign in to comment.