Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

Commit

Permalink
packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirek Simek committed Dec 1, 2021
1 parent 6617afd commit 2ba2654
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oarepo/vue-query-synchronizer",
"version": "3.0.0a5",
"version": "3.0.0a10",
"private": false,
"license": "MIT",
"description": "Browser vue-router query synchronization library",
Expand Down
42 changes: 35 additions & 7 deletions src/library/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
SpaceArrayDatatype,
StringDatatype
} from './datatypes'
import {LocationQuery, RouteLocationNormalizedLoaded, Router} from "vue-router";
import {LocationQuery, RouteLocationNormalizedLoaded, Router} from 'vue-router';
import {
DataType,
DataTypes,
Expand All @@ -18,8 +18,8 @@ import {
QueryParameterDefinitions,
QuerySettings,
TypedParsedQuery
} from "./types";
import {WatchStopHandle} from "@vue/runtime-core";
} from './types';
import {WatchStopHandle} from '@vue/runtime-core';

export * from './datatypes'
export * from './types'
Expand All @@ -40,6 +40,11 @@ function parseParamDefinition<T>(
splitted = ['string', splitted[0]]
}
const datatype = datatypes[splitted[0]]

if (!datatype) {
throw new Error(`No query datatype "${splitted[0]}"`);
}

def = {
datatype: datatype,
defaultValue: datatype.parseDefault(splitted[1])
Expand All @@ -56,7 +61,7 @@ function parseParamDefinition<T>(
const _query = reactive({
query: {} as ParsedQuery,
rawQuery: {} as LocationQuery,
enabled: true,
enabled: false,
serializedId: 0
})

Expand Down Expand Up @@ -122,6 +127,7 @@ function serializeChangedValue(key: string, value: any) {
}

function saveValueToRawQuery(key: string, value: string | string[]) {
dlog('save value to raw query', key, value);
if (value === undefined) {
if (key in _query.rawQuery) {
delete _query.rawQuery[key]
Expand All @@ -142,6 +148,7 @@ function saveValueToRawQuery(key: string, value: string | string[]) {
}

if (!(key in _query.rawQuery)) {
dlog('value not in rawQuery, added');
_query.rawQuery[key] = value
_query.serializedId++
} else {
Expand All @@ -161,8 +168,11 @@ function saveValueToRawQuery(key: string, value: string | string[]) {
modified = true
}
if (modified) {
dlog('value in rawQuery, modified');
_query.rawQuery[key] = value
_query.serializedId++
} else {
dlog('value in rawQuery, not modified', _query.rawQuery[key]);
}
}
dlog('Set to raw', key, value)
Expand All @@ -187,16 +197,18 @@ function clearWatchers() {
function handleRouteChange(to: RouteLocationNormalizedLoaded) {
fingerprint = null
detailedFingerprint = {}
Object.keys(_query.query).forEach(function (key) {
Object.keys(_query.query).forEach(function(key) {
delete _query.query[key]
})
queryDefinition = {}
querySettings = {}
clearWatchers()

if (!prepareQuery(to)) {
dlog('No query meta on this route');
_query.enabled = false
} else {
dlog('Got query meta on this route');
_query.enabled = true
}
}
Expand Down Expand Up @@ -245,6 +257,7 @@ function setup(_router: Router, _datatypes: DataTypes, _debug: boolean, _navigat
}

router.beforeEach((to, from) => {
dlog('beforeEach called', to.name, from.name);
if (to.name !== from.name) {
handleRouteChange(to)
}
Expand Down Expand Up @@ -330,14 +343,14 @@ function removeValue<T>(key: string, value: T, datatype: DataType<T[]>) {
}

const handler = {
set: function (target: ParsedQuery, prop: string, value: any) {
set: function(target: ParsedQuery, prop: string, value: any) {
if (!(prop in target)) {
define(prop, StringDatatype, '')
}
target[prop] = value
return true
},
get: function (target: ParsedQuery, prop: string) {
get: function(target: ParsedQuery, prop: string) {
if (prop === 'define') {
return define
}
Expand Down Expand Up @@ -373,6 +386,19 @@ export function useRawQuery() {
return _query
}

export function watchQuery(fn: (...x: any[])=>any) {
watch(function() {
return _query.serializedId;
}, function() {
return fn;
});
}

export function queryWatchSource() {
return _query.serializedId;
}


const QuerySynchronizer = {
install(app: any, {
router,
Expand Down Expand Up @@ -415,3 +441,5 @@ const QuerySynchronizer = {
* @param debug if set to true, print library's debug messages
*/
export default QuerySynchronizer

export { proxiedQuery as query }

0 comments on commit 2ba2654

Please sign in to comment.