{"version":3,"file":"effector-vue.mjs","sources":["effector-vue/EffectorScopePlugin.ts","effector-vue/index.ts"],"sourcesContent":["import {Scope} from 'effector'\nimport {Plugin} from 'vue-next'\n\nexport function EffectorScopePlugin(options: {\n  scope: Scope\n  scopeName?: string\n}): Plugin {\n  return {\n    install(app) {\n      let scopeName = options.scopeName ?? 'root'\n\n      app.config.globalProperties.scopeName = scopeName\n      app.provide(app.config.globalProperties.scopeName, options.scope)\n    },\n  }\n}\n","import type Vue from 'vue'\nimport type {VueConstructor, ComponentOptions} from 'vue'\nimport * as VueModule from 'vue'\nimport {\n  createEvent,\n  restore,\n  is,\n  combine,\n  Store,\n  withRegion,\n  clearNode,\n  sample,\n  Unit,\n} from 'effector'\n\n/**\n * Vue 3 has no default export, so a default import of it makes this entry\n * unloadable from a Vue 3 app: bundlers fail with \"default is not exported by\n * vue\". Read the default off the module namespace instead; the extra binding\n * keeps bundlers from folding that read back into a default import. The Vue 2\n * API itself is only touched when the mixin or createComponent runs.\n */\nconst VueModuleRuntime: any = VueModule\nconst Vue2 = (VueModuleRuntime.default ?? VueModuleRuntime) as VueConstructor\n\nexport const VueEffector = (vue: VueConstructor, options: Object) => {\n  vue.mixin(effectorMixin)\n}\n\nconst effectorMixin: ComponentOptions<Vue> = {\n  beforeCreate() {\n    let shape = this.$options.effector\n\n    if (typeof shape === 'function') {\n      // @ts-ignore\n      shape = shape.call(this)\n    }\n    if (!shape) return\n    if (!this.$options.computed) this.$options.computed = {}\n\n    let obj: {[key: string]: Unit<any>} = {}\n\n    // normalize effector field data\n    if (is.store(shape)) {\n      obj = {state: shape}\n    } else if (typeof shape === 'object') {\n      obj = {...shape}\n    } else {\n      throw Error('property should be Store')\n    }\n\n    // @ts-ignore\n    this.__clear = createEvent()\n\n    // @ts-ignore\n    withRegion(this.__clear, () => {\n      const state: Record<string, Store<any>> = {}\n      let nextID = 0\n\n      for (const key in obj) {\n        const v = obj[key]\n\n        if (is.store(v)) {\n          state[key] = v\n        } else if (is.event(v) || is.effect(v)) {\n          state[key] = restore(\n            v.map(() => ++nextID),\n            null,\n          )\n        } else {\n          throw Error(\n            `Effector property ${key} should be Store or Unit (will be transform to Store<number>)`,\n          )\n        }\n      }\n\n      const store = combine(state)\n      for (const key in store.defaultState) {\n        // @ts-ignore\n        Vue2.util.defineReactive(this, key, store.defaultState[key])\n      }\n\n      store.watch(value => {\n        for (const key in value) {\n          // @ts-ignore\n          this[key] = value[key]\n        }\n      })\n\n      for (const key in state) {\n        const updated = createEvent()\n        sample({clock: updated, target: state[key]})\n\n        // @ts-ignore\n        this.$options.computed[key] = {\n          // @ts-ignore\n          get: () => this[key],\n          set: updated,\n        }\n      }\n    })\n  },\n\n  beforeDestroy() {\n    // @ts-ignore\n    if (this.__clear) {\n      // @ts-ignore\n      clearNode(this.__clear)\n    }\n  },\n}\n\nexport function createComponent<S>(options: any, store?: S) {\n  return Vue2.extend(\n    Object.assign(\n      {},\n      options,\n      store && {\n        effector: () => store,\n      },\n    ),\n  )\n}\n\nexport {EffectorScopePlugin} from './EffectorScopePlugin'\n"],"names":["EffectorScopePlugin","options","install","app","_options$scopeName","scopeName","config","globalProperties","provide","scope","createComponent","store","Vue2","extend","Object","assign","effector","_VueModuleRuntime$def","VueModule","default","VueEffector","vue","mixin","effectorMixin","beforeCreate","shape","this","$options","call","computed","obj","is","state","Error","__clear","createEvent","withRegion","nextID","key","v","event","effect","restore","map","combine","defaultState","util","defineReactive","watch","value","updated","sample","clock","target","get","set","beforeDestroy","clearNode"],"mappings":"AAGO,SAASA,EAAoBC,GAIlC,MAAO,CACLC,OAAAA,CAAQC,GAAK,IAAAC,EACX,IAAIC,GAASD,EAAGH,EAAQI,aAAS,MAAAD,WAAAA,EAAI,OAErCD,EAAIG,OAAOC,iBAAiBF,UAAYA,EACxCF,EAAIK,QAAQL,EAAIG,OAAOC,iBAAiBF,UAAWJ,EAAQQ,MAC7D,EAEJ,CCiGO,SAASC,EAAmBT,EAAcU,GAC/C,OAAOC,EAAKC,OAC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