@logux/vuex
Advanced tools
Comparing version 0.9.21 to 0.9.22
@@ -1,5 +0,2 @@ | ||
import { | ||
toRefs, | ||
defineComponent | ||
} from 'vue' | ||
import { toRefs, defineComponent } from 'vue' | ||
@@ -16,3 +13,3 @@ import { useSubscription } from '../composable/index.js' | ||
}, | ||
setup (props, { slots }) { | ||
setup(props, { slots }) { | ||
let { channels } = toRefs(props) | ||
@@ -19,0 +16,0 @@ |
@@ -1,6 +0,2 @@ | ||
import { | ||
Ref, | ||
ComputedGetter, | ||
ComputedRef | ||
} from '@vue/reactivity' | ||
import { Ref, ComputedGetter, ComputedRef } from '@vue/reactivity' | ||
@@ -69,5 +65,5 @@ import { LoguxVuexStore } from '../index.js' | ||
*/ | ||
export function useSubscription ( | ||
export function useSubscription( | ||
channels: Channels, | ||
options?: useSubscriptionOptions | ||
): Ref<boolean> |
import { useStore } from 'vuex' | ||
import { | ||
ref, | ||
isRef, | ||
watch, | ||
computed, | ||
onBeforeUnmount | ||
} from 'vue' | ||
import { ref, isRef, watch, computed, onBeforeUnmount } from 'vue' | ||
function useSubscription (channels, options = {}) { | ||
function useSubscription(channels, options = {}) { | ||
let store = options.store || useStore() | ||
@@ -26,33 +20,37 @@ let debounce = options.debounce || 0 | ||
watch(() => id.value, (newId, oldId, onInvalidate) => { | ||
let oldSubscriptions = subscriptions.value | ||
let ignoreResponse = false | ||
let timeout | ||
watch( | ||
() => id.value, | ||
(newId, oldId, onInvalidate) => { | ||
let oldSubscriptions = subscriptions.value | ||
let ignoreResponse = false | ||
let timeout | ||
function resetTimeout () { | ||
clearTimeout(timeout) | ||
timeout = null | ||
} | ||
function resetTimeout() { | ||
clearTimeout(timeout) | ||
timeout = null | ||
} | ||
if (debounce > 0) { | ||
timeout = setTimeout(() => { | ||
if (debounce > 0) { | ||
timeout = setTimeout(() => { | ||
isSubscribing.value = true | ||
}, debounce) | ||
} else { | ||
isSubscribing.value = true | ||
}, debounce) | ||
} else { | ||
isSubscribing.value = true | ||
} | ||
subscribe(store, subscriptions.value).then(() => { | ||
if (timeout) resetTimeout(timeout) | ||
if (!ignoreResponse) { | ||
isSubscribing.value = false | ||
} | ||
}) | ||
onInvalidate(() => { | ||
ignoreResponse = true | ||
unsubscribe(store, oldSubscriptions) | ||
if (timeout) resetTimeout(timeout) | ||
}) | ||
}, { immediate: true }) | ||
subscribe(store, subscriptions.value).then(() => { | ||
if (timeout) resetTimeout(timeout) | ||
if (!ignoreResponse) { | ||
isSubscribing.value = false | ||
} | ||
}) | ||
onInvalidate(() => { | ||
ignoreResponse = true | ||
unsubscribe(store, oldSubscriptions) | ||
if (timeout) resetTimeout(timeout) | ||
}) | ||
}, | ||
{ immediate: true } | ||
) | ||
} else { | ||
@@ -73,3 +71,3 @@ let subscriptions = unifyChannelsObject(channels) | ||
function unifyChannelsObject (channels) { | ||
function unifyChannelsObject(channels) { | ||
return channels.map(i => { | ||
@@ -81,24 +79,29 @@ let subscription = typeof i === 'string' ? { channel: i } : i | ||
function subscriptionsId (subscriptions) { | ||
return subscriptions.map(i => i[1]).sort().join(' ') | ||
function subscriptionsId(subscriptions) { | ||
return subscriptions | ||
.map(i => i[1]) | ||
.sort() | ||
.join(' ') | ||
} | ||
function subscribe (store, subscriptions) { | ||
function subscribe(store, subscriptions) { | ||
if (!store.subscriptions) store.subscriptions = {} | ||
if (!store.subscribers) store.subscribers = {} | ||
return Promise.all(subscriptions.map(i => { | ||
let subscription = i[0] | ||
let json = i[1] | ||
if (!store.subscribers[json]) store.subscribers[json] = 0 | ||
store.subscribers[json] += 1 | ||
if (store.subscribers[json] === 1) { | ||
let action = { ...subscription, type: 'logux/subscribe' } | ||
store.subscriptions[json] = store.commit.sync(action) | ||
} | ||
return store.subscriptions[json] | ||
})) | ||
return Promise.all( | ||
subscriptions.map(i => { | ||
let subscription = i[0] | ||
let json = i[1] | ||
if (!store.subscribers[json]) store.subscribers[json] = 0 | ||
store.subscribers[json] += 1 | ||
if (store.subscribers[json] === 1) { | ||
let action = { ...subscription, type: 'logux/subscribe' } | ||
store.subscriptions[json] = store.commit.sync(action) | ||
} | ||
return store.subscriptions[json] | ||
}) | ||
) | ||
} | ||
function unsubscribe (store, subscriptions) { | ||
function unsubscribe(store, subscriptions) { | ||
subscriptions.forEach(i => { | ||
@@ -105,0 +108,0 @@ let subscription = i[0] |
export { Client, CrossTabClient } from '@logux/client' | ||
export { Channel, Channels, useSubscription } from './composable/index.js' | ||
export { LoguxVuexStore, createStoreCreator } from './store/index.js' | ||
export { Subscribe } from './component/index.js' | ||
export { useStore } from './inject/index.js' | ||
export { | ||
Channel, | ||
Channels, | ||
useSubscription | ||
} from './composable/index.js' | ||
export { | ||
LoguxVuexStore, | ||
createStoreCreator | ||
} from './store/index.js' |
@@ -27,4 +27,4 @@ import { Log } from '@logux/core' | ||
C extends Client = Client<{}, L> | ||
> ( | ||
>( | ||
injectKey?: InjectionKey<LoguxVuexStore<S, L, C>> | string | ||
): LoguxVuexStore<S, L, C> |
{ | ||
"name": "@logux/vuex", | ||
"version": "0.9.21", | ||
"version": "0.9.22", | ||
"description": "Vuex compatible API for Logux", | ||
@@ -21,3 +21,3 @@ "keywords": [ | ||
"peerDependencies": { | ||
"vue": ">=3.0.7", | ||
"vue": ">=3.0.8", | ||
"vuex": ">=4.0.0" | ||
@@ -54,2 +54,8 @@ }, | ||
}, | ||
"./store/package.json": "./store/package.json", | ||
"./store": { | ||
"require": "./store/index.cjs", | ||
"import": "./store/index.js", | ||
"default": "./store/index.js" | ||
}, | ||
"./inject/package.json": "./inject/package.json", | ||
@@ -61,8 +67,2 @@ "./inject": { | ||
}, | ||
"./store/package.json": "./store/package.json", | ||
"./store": { | ||
"require": "./store/index.cjs", | ||
"import": "./store/index.js", | ||
"default": "./store/index.js" | ||
}, | ||
"./utils/package.json": "./utils/package.json", | ||
@@ -69,0 +69,0 @@ "./utils": { |
import { Unsubscribe } from 'nanoevents' | ||
import { Action, Log } from '@logux/core' | ||
import { Client, ClientMeta } from '@logux/client' | ||
import { | ||
Client, | ||
ClientMeta | ||
} from '@logux/client' | ||
import { | ||
CommitOptions, | ||
@@ -163,3 +160,3 @@ Store as VuexStore, | ||
> extends VuexStore<S> { | ||
constructor (options: LoguxVuexStoreOptions<S>) | ||
constructor(options: LoguxVuexStoreOptions<S>) | ||
@@ -188,3 +185,3 @@ dispatch: VuexDispatch | ||
*/ | ||
on (event: 'change', listener: StateListener<S>): Unsubscribe | ||
on(event: 'change', listener: StateListener<S>): Unsubscribe | ||
@@ -278,2 +275,2 @@ /** | ||
C extends Client = Client<{}, L> | ||
> (client: C, options?: LoguxVuexOptions): createStore<L, C> | ||
>(client: C, options?: LoguxVuexOptions): createStore<L, C> |
@@ -5,9 +5,5 @@ import { createStore as createVuexStore } from 'vuex' | ||
import { | ||
deepCopy, | ||
isPromise, | ||
forEachValue | ||
} from '../utils/index.js' | ||
import { deepCopy, isPromise, forEachValue } from '../utils/index.js' | ||
function createStoreCreator (client, options = {}) { | ||
function createStoreCreator(client, options = {}) { | ||
let reasonlessHistory = options.reasonlessHistory || 1000 | ||
@@ -20,3 +16,3 @@ let onMissedHistory = options.onMissedHistory | ||
function createStore (vuexConfig) { | ||
function createStore(vuexConfig) { | ||
let store = createVuexStore(deepCopy(vuexConfig)) | ||
@@ -35,3 +31,3 @@ | ||
let actionCount = 0 | ||
function saveHistory (meta) { | ||
function saveHistory(meta) { | ||
actionCount += 1 | ||
@@ -55,3 +51,3 @@ if (saveStateEvery === 1 || actionCount % saveStateEvery === 1) { | ||
function originCommit (action, opts) { | ||
function originCommit(action, opts) { | ||
if (action.type === 'logux/state') { | ||
@@ -71,3 +67,7 @@ store.replaceState(action.state) | ||
store.commit = (type, payload, _options) => { | ||
let { action, options: commitOpts } = unifyCommitArgs(type, payload, _options) | ||
let { action, options: commitOpts } = unifyCommitArgs( | ||
type, | ||
payload, | ||
_options | ||
) | ||
let meta = { | ||
@@ -85,3 +85,4 @@ id: log.generateId(), | ||
originCommit(action, commitOpts) | ||
emit && emitter.emit('change', deepCopy(store.state), prevState, action, meta) | ||
emit && | ||
emitter.emit('change', deepCopy(store.state), prevState, action, meta) | ||
saveHistory(meta) | ||
@@ -119,3 +120,3 @@ } | ||
function replaceState (state, actions, pushHistory) { | ||
function replaceState(state, actions, pushHistory) { | ||
let last = actions.length ? actions[actions.length - 1][1] : '' | ||
@@ -156,3 +157,3 @@ let newState = actions.reduceRight((prev, [action, id]) => { | ||
let replaying | ||
function replay (actionId) { | ||
function replay(actionId) { | ||
let ignore = {} | ||
@@ -165,56 +166,58 @@ let actions = [] | ||
replaying = new Promise(resolve => { | ||
log.each((action, meta) => { | ||
if (meta.tab && meta.tab !== client.tabId) return true | ||
log | ||
.each((action, meta) => { | ||
if (meta.tab && meta.tab !== client.tabId) return true | ||
if (collecting || !stateHistory[meta.id]) { | ||
if (action.type === 'logux/undo') { | ||
ignore[action.id] = true | ||
if (collecting || !stateHistory[meta.id]) { | ||
if (action.type === 'logux/undo') { | ||
ignore[action.id] = true | ||
return true | ||
} else if (action.type.startsWith('logux/')) { | ||
return true | ||
} | ||
if (!ignore[meta.id]) actions.push([action, meta.id]) | ||
if (meta.id === actionId) { | ||
newAction = action | ||
collecting = false | ||
} | ||
return true | ||
} else if (action.type.startsWith('logux/')) { | ||
return true | ||
} else { | ||
replayed = true | ||
let stateFromHistory = deepCopy(stateHistory[meta.id]) | ||
replaceState(stateFromHistory, actions) | ||
return false | ||
} | ||
if (!ignore[meta.id]) actions.push([action, meta.id]) | ||
if (meta.id === actionId) { | ||
newAction = action | ||
collecting = false | ||
} | ||
return true | ||
} else { | ||
replayed = true | ||
let stateFromHistory = deepCopy(stateHistory[meta.id]) | ||
replaceState(stateFromHistory, actions) | ||
return false | ||
} | ||
}).then(() => { | ||
if (!replayed) { | ||
if (historyCleaned) { | ||
if (onMissedHistory) { | ||
onMissedHistory(newAction) | ||
} | ||
for (let i = actions.length - 1; i >= 0; i--) { | ||
let id = actions[i][1] | ||
if (stateHistory[id]) { | ||
replayed = true | ||
let stateFromHistory = deepCopy(stateHistory[id]) | ||
replaceState( | ||
stateFromHistory, | ||
actions.slice(0, i).concat([[newAction, actionId]]), | ||
id | ||
) | ||
break | ||
}) | ||
.then(() => { | ||
if (!replayed) { | ||
if (historyCleaned) { | ||
if (onMissedHistory) { | ||
onMissedHistory(newAction) | ||
} | ||
for (let i = actions.length - 1; i >= 0; i--) { | ||
let id = actions[i][1] | ||
if (stateHistory[id]) { | ||
replayed = true | ||
let stateFromHistory = deepCopy(stateHistory[id]) | ||
replaceState( | ||
stateFromHistory, | ||
actions.slice(0, i).concat([[newAction, actionId]]), | ||
id | ||
) | ||
break | ||
} | ||
} | ||
} | ||
} | ||
if (!replayed) { | ||
let state = collectState(deepCopy(vuexConfig)) | ||
replaceState(state, actions) | ||
if (!replayed) { | ||
let state = collectState(deepCopy(vuexConfig)) | ||
replaceState(state, actions) | ||
} | ||
} | ||
} | ||
replaying = false | ||
resolve() | ||
}) | ||
replaying = false | ||
resolve() | ||
}) | ||
}) | ||
@@ -241,3 +244,3 @@ | ||
async function process (action, meta) { | ||
async function process(action, meta) { | ||
if (replaying) { | ||
@@ -330,17 +333,19 @@ wait[meta.id] = true | ||
let ignores = {} | ||
log.each((action, meta) => { | ||
if (!meta.tab) { | ||
if (action.type === 'logux/undo') { | ||
ignores[action.id] = true | ||
} else if (!ignores[meta.id]) { | ||
previous.push([action, meta]) | ||
log | ||
.each((action, meta) => { | ||
if (!meta.tab) { | ||
if (action.type === 'logux/undo') { | ||
ignores[action.id] = true | ||
} else if (!ignores[meta.id]) { | ||
previous.push([action, meta]) | ||
} | ||
} | ||
} | ||
}).then(() => { | ||
if (previous.length > 0) { | ||
Promise.all(previous.map(i => process(...i))).then(init) | ||
} else { | ||
init() | ||
} | ||
}) | ||
}) | ||
.then(() => { | ||
if (previous.length > 0) { | ||
Promise.all(previous.map(i => process(...i))).then(init) | ||
} else { | ||
init() | ||
} | ||
}) | ||
@@ -353,3 +358,3 @@ return store | ||
function installModule (store, rootState, path, module) { | ||
function installModule(store, rootState, path, module) { | ||
let namespace = store._modules.getNamespace(path) | ||
@@ -375,3 +380,3 @@ let local = modifyLocalContext(store, namespace, module.context) | ||
function modifyLocalContext (store, namespace, context) { | ||
function modifyLocalContext(store, namespace, context) { | ||
let noNamespace = namespace === '' | ||
@@ -410,13 +415,17 @@ | ||
function registerAction (store, type, handler, local) { | ||
function registerAction(store, type, handler, local) { | ||
let entry = store._actions[type] || (store._actions[type] = []) | ||
function wrappedActionHandler (payload) { | ||
let res = handler.call(store, { | ||
dispatch: local.dispatch, | ||
commit: local.commit, | ||
getters: local.getters, | ||
state: local.state, | ||
rootGetters: store.getters, | ||
rootState: store.state | ||
}, payload) | ||
function wrappedActionHandler(payload) { | ||
let res = handler.call( | ||
store, | ||
{ | ||
dispatch: local.dispatch, | ||
commit: local.commit, | ||
getters: local.getters, | ||
state: local.state, | ||
rootGetters: store.getters, | ||
rootState: store.state | ||
}, | ||
payload | ||
) | ||
if (!isPromise(res)) { | ||
@@ -437,7 +446,7 @@ res = Promise.resolve(res) | ||
function hasSimplePayload (action) { | ||
function hasSimplePayload(action) { | ||
return 'payload' in action && typeof action.payload !== 'object' | ||
} | ||
function unifyCommitArgs (type, payload = {}, options = {}) { | ||
function unifyCommitArgs(type, payload = {}, options = {}) { | ||
let action | ||
@@ -463,5 +472,5 @@ let meta | ||
function collectState (store) { | ||
function collectState(store) { | ||
let state = store.state || {} | ||
function collectModuleState (module, moduleName, moduleState) { | ||
function collectModuleState(module, moduleName, moduleState) { | ||
if (moduleName) { | ||
@@ -472,4 +481,5 @@ moduleState[moduleName] = module.state | ||
forEachValue(module.modules, (childModule, childModuleName) => { | ||
let childModuleState = | ||
moduleName ? moduleState[moduleName] : moduleState | ||
let childModuleState = moduleName | ||
? moduleState[moduleName] | ||
: moduleState | ||
collectModuleState(childModule, childModuleName, childModuleState) | ||
@@ -476,0 +486,0 @@ }) |
@@ -1,6 +0,6 @@ | ||
function find (list, f) { | ||
function find(list, f) { | ||
return list.filter(f)[0] | ||
} | ||
function deepCopy (obj, cache = []) { | ||
function deepCopy(obj, cache = []) { | ||
if (obj === null || typeof obj !== 'object') { | ||
@@ -28,7 +28,7 @@ return obj | ||
function forEachValue (obj, fn) { | ||
function forEachValue(obj, fn) { | ||
Object.keys(obj).forEach(key => fn(obj[key], key)) | ||
} | ||
function isPromise (val) { | ||
function isPromise(val) { | ||
return val && typeof val.then === 'function' | ||
@@ -35,0 +35,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
53938
1554