Repository navigation
fix: Vue Integration (#68) #235
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { describe, it, expect, vi } from 'vitest' | ||
| import { AbbyProvider, useAbby, useFeatureFlag, useRemoteConfig } from '../index' | ||
|
|
||
| describe('Vue Integration', () => { | ||
| it('should create AbbyProvider', () => { | ||
| const provider = AbbyProvider | ||
| expect(provider).toBeDefined() | ||
| expect(provider.name).toBe('AbbyProvider') | ||
| }) | ||
|
|
||
| describe('useAbby', () => { | ||
| it('should return variant and onAct', () => { | ||
| const { variant, onAct } = useAbby() | ||
| expect(variant).toBeDefined() | ||
| expect(onAct).toBeDefined() | ||
| expect(typeof onAct).toBe('function') | ||
| }) | ||
| }) | ||
|
|
||
| describe('useFeatureFlag', () => { | ||
| it('should return feature flag value', () => { | ||
| const flagValue = useFeatureFlag('test-flag') | ||
| expect(flagValue).toBeDefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe('useRemoteConfig', () => { | ||
| it('should return remote config value', () => { | ||
| const configValue = useRemoteConfig('test-config') | ||
| expect(configValue).toBeDefined() | ||
| }) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { defineComponent, h, ref, provide, inject, onMounted, onBeforeUnmount } from 'vue' | ||
| import { initAbby, AbbyClient, getVariant, getFeatureFlag, getRemoteConfig, trackAction } from '@tryabby/core' | ||
|
|
||
| interface AbbyProviderProps { | ||
| projectId: string | ||
| environmentId: string | ||
| apiKey: string | ||
| } | ||
|
|
||
| const ABBY_SYMBOL = Symbol('abbyClient') | ||
|
|
||
| const AbbyProvider = defineComponent({ | ||
| name: 'AbbyProvider', | ||
| props: { | ||
| projectId: { type: String, required: true }, | ||
| environmentId: { type: String, required: true }, | ||
| apiKey: { type: String, required: true } | ||
| }, | ||
| setup(props, { slots }) { | ||
| const abbyClient = ref<AbbyClient | null>(null) | ||
|
|
||
| onMounted(() => { | ||
| abbyClient.value = initAbby({ | ||
| projectId: props.projectId, | ||
| environmentId: props.environmentId, | ||
| apiKey: props.apiKey | ||
| }) | ||
| }) | ||
|
|
||
| onBeforeUnmount(() => { | ||
| if (abbyClient.value && typeof abbyClient.value.destroy === 'function') { | ||
| abbyClient.value.destroy() | ||
| } | ||
| }) | ||
|
|
||
| provide(ABBY_SYMBOL, abbyClient) | ||
|
|
||
| return () => slots.default ? slots.default() : h('div') | ||
| } | ||
| }) | ||
|
|
||
| const useAbby = (experimentName?: string) => { | ||
| const abbyClient = inject<any>(ABBY_SYMBOL, ref(null)) | ||
| const variant = ref('control') | ||
|
|
||
| onMounted(() => { | ||
| if (abbyClient.value && experimentName) { | ||
| const v = getVariant(abbyClient.value, experimentName) | ||
| if (v) variant.value = v | ||
| } | ||
| }) | ||
|
Comment on lines
+46
to
+51
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update all three composables when the client becomes available. For a synchronous descendant rendered through the provider's slot, Vue runs the descendant's mounted hook before the provider's mounted hook. Each lookup therefore sees Observe the injected client ref and read each value when the client becomes available. Also handle descendants mounted after client initialization. Also applies to: 68-72, 81-85
|
||
|
|
||
| const onAct = (action: string, data?: Record<string, any>) => { | ||
| if (abbyClient.value) { | ||
| trackAction(abbyClient.value, action, data) | ||
| } else { | ||
| console.log(`Tracking action: ${action}`, data) | ||
| } | ||
| } | ||
|
|
||
| return { variant, onAct } | ||
| } | ||
|
|
||
| const useFeatureFlag = (flagName: string) => { | ||
| const abbyClient = inject<any>(ABBY_SYMBOL, ref(null)) | ||
| const flagValue = ref(false) | ||
|
|
||
| onMounted(() => { | ||
| if (abbyClient.value) { | ||
| flagValue.value = getFeatureFlag(abbyClient.value, flagName) | ||
| } | ||
| }) | ||
|
|
||
| return flagValue | ||
| } | ||
|
|
||
| const useRemoteConfig = (configName: string) => { | ||
| const abbyClient = inject<any>(ABBY_SYMBOL, ref(null)) | ||
| const configValue = ref({}) | ||
|
|
||
| onMounted(() => { | ||
| if (abbyClient.value) { | ||
| configValue.value = getRemoteConfig(abbyClient.value, configName) | ||
| } | ||
| }) | ||
|
|
||
| return configValue | ||
| } | ||
|
|
||
| export { | ||
| AbbyProvider, | ||
| useAbby, | ||
| useFeatureFlag, | ||
| useRemoteConfig | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "name": "@tryabby/vue", | ||
| "version": "1.0.0", | ||
| "description": "Vue 3 integration for Abby", | ||
| "main": "index.ts", | ||
| "types": "index.ts", | ||
| "dependencies": { | ||
| "@tryabby/core": "^1.0.0", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The A fresh install resolves The module therefore fails at import or compile time. Consumers cannot load Set a core range that resolves to a release exporting the APIs the integration uses. Then update the imports and lifecycle calls to match that release's API. The choice of core release and API is a design decision, but the package must resolve and load under its declared range.
|
||
| "vue": "^3.2.0" | ||
| }, | ||
| "devDependencies": { | ||
| "vitest": "^0.10.0" | ||
| }, | ||
| "scripts": { | ||
| "test": "vitest" | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Return the selected variant from the shared client.
For a visitor assigned
treatment,useAbby()still returnscontrol. This function creates a separate null client ref and never reads the provider's client or an assignment.Connect the composable to the provider's client and return the actual assignment. Preserve
controlwhen it is the selected variant.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed the review feedback and verified local checks pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Analysis chain
Script executed:
Length of output: 15482
@rhys-morThe shared client is now injected, but the variant can still remaincontrol.In
packages/vue/index.ts, the descendant’s mounted hook runs before the provider’s mounted hook. The client is therefore still null whenuseAbby()checks it at Line 47, and the composable does not retry when the client becomes available.Please initialize the client earlier or watch the injected client for changes. A mounted provider test should assert both
treatmentandcontrolassignments. I’m not marking this finding as addressed yet.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
You are interacting with an AI system.