Skip to content

Commit

Permalink
Merge branch 'develop' into feature/#2773
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal-Dziedzinski committed Jul 15, 2019
2 parents 1500ffb + aa4a054 commit e1266c6
Show file tree
Hide file tree
Showing 53 changed files with 637 additions and 346 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
core/build/*.js
node_modules
packages/module/*.js
12 changes: 10 additions & 2 deletions core/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ import store from '@vue-storefront/core/store'

import { enabledModules } from './modules-entry'

// Will be deprecated in 1.8
// Will be deprecated in 2.0
import { registerExtensions } from '@vue-storefront/core/compatibility/lib/extensions'
import { registerExtensions as extensions } from 'src/extensions'
import globalConfig from 'config'

import { injectReferences } from '@vue-storefront/module'
import { coreHooksExecutors } from '@vue-storefront/core/hooks'
import { registerNewModules } from 'src/modules';

function createRouter (): VueRouter {
return new VueRouter({
mode: 'history',
Expand Down Expand Up @@ -75,7 +79,7 @@ const createApp = async (ssrContext, config, storeCode = null): Promise<{app: Vu
store.state.storeView = storeView
// store.state.shipping.methods = shippingMethods

// to depreciate in near future
// @deprecated from 2.0
once('__VUE_EXTEND__', () => {
Vue.use(Vuelidate)
Vue.use(VueLazyload, {attempt: 2, preLoad: 1.5})
Expand Down Expand Up @@ -112,10 +116,14 @@ const createApp = async (ssrContext, config, storeCode = null): Promise<{app: Vu
ssrContext
}

injectReferences(app, store, router, globalConfig)
registerNewModules()
registerModules(enabledModules, appContext)
registerExtensions(extensions, app, router, store, config, ssrContext)
registerTheme(globalConfig.theme, app, router, store, globalConfig, ssrContext)

coreHooksExecutors.afterAppInit()
// @deprecated from 2.0
EventBus.$emit('application-after-init', app)

return { app, router, store }
Expand Down
41 changes: 41 additions & 0 deletions core/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { createListenerHook, createMutatorHook } from '@vue-storefront/module/hooks'

const { hook: beforeStoreViewChangeHook, executor: beforeStoreViewChangeExecutor }: {
hook: (storeViewMutator: (storeView: any) => any) => void,
executor: any
} = createMutatorHook()

const { hook: afterStoreViewChangeHook, executor: afterStoreViewChangeExecutor }: {
hook: (storeViewListener: (storeView?: any) => void) => void,
executor: any
} = createListenerHook()

const { hook: afterAppInitHook, executor: afterAppInitExecutor }: {
hook: (appInitListener: () => void) => void,
executor: any
} = createListenerHook()

/** Only for internal usage in core */
const coreHooksExecutors = {
afterAppInit: afterAppInitExecutor,
beforeStoreViewChange: beforeStoreViewChangeExecutor,
afterStoreViewChange: afterStoreViewChangeExecutor
}

const coreHooks = {
/** Hook is fired right after whole application is initialized. Modules are registered and theme setted up */
afterAppInit: afterAppInitHook,
/** Hook is fired directly before changing current storeView (multistrore)
* @param storeView Inside this function you have access to order object that you can access and modify. It should return order object.
*/
beforeStoreViewChange: beforeStoreViewChangeHook,
/** Hook is fired right after storeView (multistore) is changed
* @param storeView current storeView
*/
afterStoreViewChange: afterStoreViewChangeHook
}

export {
coreHooks,
coreHooksExecutors
}
2 changes: 2 additions & 0 deletions core/lib/module/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @deprecated from 2.0
import { Module } from 'vuex'
import { RouteConfig, NavigationGuard } from 'vue-router'
import Vue from 'vue'
Expand Down Expand Up @@ -77,6 +78,7 @@ class VueStorefrontModule {

public register (): VueStorefrontModuleConfig | void {
if (!this._isRegistered) {
Logger.warn('The module you are registering is using outdated API that will soon be depreciated. Please check https://docs.vuestorefront.io to learn more.', 'module', this._c.key)()
let areStoresUnique = true
const VSF: VSF = {
Vue,
Expand Down
4 changes: 3 additions & 1 deletion core/lib/multistore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import merge from 'lodash-es/merge'
import { RouterManager } from '@vue-storefront/core/lib/router-manager'
import VueRouter, { RouteConfig, RawLocation } from 'vue-router'
import config from 'config'
import { coreHooksExecutors } from '@vue-storefront/core/hooks'
import { StorageManager } from '@vue-storefront/core/store/lib/storage-manager'

export interface LocalizedRoute {
Expand Down Expand Up @@ -97,13 +98,14 @@ export function prepareStoreView (storeCode: string): StoreView {
loadLanguageAsync(storeView.i18n.defaultLocale)

if (storeViewHasChanged) {
storeView = coreHooksExecutors.beforeStoreViewChange(storeView)
rootStore.state.storeView = storeView
}
if (storeViewHasChanged || StorageManager.currentStoreCode !== storeCode) {
initializeSyncTaskStorage()
StorageManager.currentStoreCode = storeView.storeCode
}

coreHooksExecutors.afterStoreViewChange(storeView)
return storeView
}

Expand Down
4 changes: 4 additions & 0 deletions core/lib/test/unit/multistore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ jest.mock('@vue-storefront/core/app', () => ({ createApp: jest.fn() }))
jest.mock('../../../store', () => ({}))
jest.mock('@vue-storefront/i18n', () => ({loadLanguageAsync: jest.fn()}))
jest.mock('../../sync/task', () => ({initializeSyncTaskStorage: jest.fn()}))
jest.mock('@vue-storefront/core/hooks', () => ({ coreHooksExecutors: {
beforeStoreViewChange: jest.fn(),
afterStoreViewChange: jest.fn()
}}))
jest.mock('query-string', () => jest.fn())
jest.mock('@vue-storefront/core/lib/router-manager', () => ({
RouterManager: {}
Expand Down
2 changes: 1 addition & 1 deletion core/mixins/composite.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import EventBus from '@vue-storefront/core/compatibility/plugins/event-bus'
import { Logger } from '@vue-storefront/core/lib/logger'

// to be deprecated
// @deprecated from 2.0
export default {
beforeCreated () {
const eventName = this.$options.name.toLowerCase() + '-before-created'
Expand Down
2 changes: 2 additions & 0 deletions core/modules-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Order } from './modules/order'
import { User } from './modules/user'
import { registerModules } from 'src/modules'
import { Breadcrumbs } from './modules/breadcrumbs'

// @deprecated from 2.0, use registerModule instead
export const enabledModules: VueStorefrontModule[] = [
Breadcrumbs,
Cms,
Expand Down
7 changes: 0 additions & 7 deletions core/modules/cart/hooks/afterRegistration.ts

This file was deleted.

15 changes: 0 additions & 15 deletions core/modules/cart/hooks/beforeRegistration.ts

This file was deleted.

25 changes: 14 additions & 11 deletions core/modules/cart/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { module } from './store'
import { createModule } from '@vue-storefront/core/lib/module'
import { beforeRegistration } from './hooks/beforeRegistration'
import { afterRegistration } from './hooks/afterRegistration'
import { StorefrontModule } from '@vue-storefront/module'
import { cartStore } from './store'
import { cartCacheHandlerFactory } from './helpers/cartCacheHandler';
import { isServer } from '@vue-storefront/core/helpers'
import Vue from 'vue'
import { initCacheStorage } from '@vue-storefront/core/helpers/initCacheStorage'

export const KEY = 'cart'
export const Cart = createModule({
key: KEY,
store: { modules: [{ key: KEY, module }] },
beforeRegistration,
afterRegistration
})
export const CartModule: StorefrontModule = function (app, store, router, moduleConfig, appConfig) {
initCacheStorage('carts')

store.registerModule('cart', cartStore)

if (!isServer) store.dispatch('cart/load')
store.subscribe(cartCacheHandlerFactory(Vue))
}
2 changes: 1 addition & 1 deletion core/modules/cart/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import getters from './getters'
import mutations from './mutations'
import CartState from '../types/CartState'

export const module: Module<CartState, any> = {
export const cartStore: Module<CartState, any> = {
namespaced: true,
state: {
isMicrocartOpen: false,
Expand Down
54 changes: 0 additions & 54 deletions core/modules/cart/test/unit/hooks/afterRegistration.spec.ts

This file was deleted.

67 changes: 0 additions & 67 deletions core/modules/cart/test/unit/hooks/beforeRegistration.spec.ts

This file was deleted.

11 changes: 6 additions & 5 deletions core/modules/cart/test/unit/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Cart } from '../../index'
import { CartModule } from '../../index'

jest.mock('../../store', () => ({}));
jest.mock('@vue-storefront/core/lib/module', () => ({ createModule: jest.fn(() => ({ module: 'cart' })) }));
jest.mock('../../hooks/beforeRegistration', () => jest.fn());
jest.mock('../../hooks/afterRegistration', () => jest.fn());
jest.mock('@vue-storefront/module', () => ({ createModule: jest.fn(() => ({ module: 'cart' })) }));
jest.mock('../../helpers/cartCacheHandler', () => ({ cartCacheHandlerFactory: jest.fn() }))
jest.mock('@vue-storefront/core/helpers', () => ({ isServer: false }))
jest.mock('@vue-storefront/core/helpers/initCacheStorage', () => ({ initCacheStorage: jest.fn() }));

describe('Cart Module', () => {
it('can be initialized', () => {
expect(Cart).toBeTruthy()
expect(CartModule).toBeTruthy()
})
});
2 changes: 1 addition & 1 deletion core/modules/cart/test/unit/store/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { module } from '../../../store'
import { cartStore } from '../../../store'

jest.mock('../../../store/actions', () => ({}));
jest.mock('../../../store/getters', () => ({}));
Expand Down
27 changes: 0 additions & 27 deletions core/modules/catalog/hooks/beforeRegistration.ts

This file was deleted.

Loading

0 comments on commit e1266c6

Please sign in to comment.