Skip to content

Commit

Permalink
create provider util
Browse files Browse the repository at this point in the history
  • Loading branch information
hieu-w committed Sep 18, 2024
1 parent e48d4a6 commit 19d4cd7
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions demo/w3a-vue-plugin/src/lib/createProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { inject, provide } from "vue";

export default function createProvider(key, composable) {
const keySymbol = typeof key === "symbol" ? key : Symbol(key);

const useContext = () => {
const context = inject(keySymbol);

if (!context) {
throw new Error(`Attempted to access context outside of provider for ${keySymbol.toString()}`);
}

return context;
};

return [
(...args) => {
const context = composable(...args);
provide(keySymbol, context);
return context;
},
useContext,
];
}

0 comments on commit 19d4cd7

Please sign in to comment.