@@ -56,13 +56,19 @@ import {
5656 mapEnsure ,
5757 mapForEach ,
5858 mapGet ,
59- mapMatch ,
6059 mapNew ,
6160 mapSet ,
6261 mapToObj ,
6362 visitTree ,
6463} from '../common/map.ts' ;
65- import { objFreeze , objMap , objToMap } from '../common/obj.ts' ;
64+ import {
65+ objFreeze ,
66+ objGet ,
67+ objIds ,
68+ objIsEqual ,
69+ objMap ,
70+ objToMap ,
71+ } from '../common/obj.ts' ;
6672import {
6773 getUndefined ,
6874 ifNotUndefined ,
@@ -211,8 +217,11 @@ export const createQueries = getCreateFunction((store: Store): Queries => {
211217 build : Build ,
212218 paramValues : ParamValues = { } ,
213219 ) : Queries => {
220+ const oldParamValues = getParamValues ( queryId ) ;
221+
214222 setDefinition ( queryId , tableId ) ;
215223 setQueryArgs ( queryId , [ build , objToMap ( paramValues ) ] ) ;
224+ callParamListeners ( queryId , oldParamValues , paramValues ) ;
216225 resetPreStores ( queryId ) ;
217226
218227 const [ , paramsMap ] = getQueryArgs ( queryId ) ! ;
@@ -593,33 +602,42 @@ export const createQueries = getCreateFunction((store: Store): Queries => {
593602 return queries ;
594603 } ;
595604
605+ const callParamListeners = (
606+ queryId : Id ,
607+ oldParamValues : ParamValues ,
608+ newParamValues : ParamValues ,
609+ ) => {
610+ const allParamIds = setNew < Id > ( [
611+ ...objIds ( oldParamValues ) ,
612+ ...objIds ( newParamValues ) ,
613+ ] ) ;
614+
615+ let changed = 0 ;
616+ collForEach ( allParamIds , ( paramId ) => {
617+ const newParamValue = objGet ( newParamValues , paramId ) ;
618+ if ( ! arrayOrValueEqual ( objGet ( oldParamValues , paramId ) , newParamValue ) ) {
619+ changed = 1 ;
620+ callListeners ( paramValueListeners , [ queryId , paramId ] , newParamValue ) ;
621+ }
622+ } ) ;
623+ if ( changed ) {
624+ callListeners ( paramValuesListeners , [ queryId ] , newParamValues ) ;
625+ }
626+ } ;
627+
596628 const delQueryDefinition = ( queryId : Id ) : Queries => {
597- const oldParamValues = getQueryArgs ( queryId ) ?. [ 1 ] ;
629+ const oldParamValues = getParamValues ( queryId ) ;
598630 resetPreStores ( queryId ) ;
599631 delDefinition ( queryId ) ;
600- ifNotUndefined ( oldParamValues , ( paramValues ) => {
601- mapForEach ( paramValues , ( paramId ) =>
602- callListeners ( paramValueListeners , [ queryId , paramId ] , undefined ) ,
603- ) ;
604- callListeners ( paramValuesListeners , [ queryId ] , { } ) ;
605- } ) ;
632+ callParamListeners ( queryId , oldParamValues , { } ) ;
606633 return queries ;
607634 } ;
608635
609636 const setParamValues = ( queryId : Id , paramValues : ParamValues ) : Queries => {
610637 ifNotUndefined ( getQueryArgs ( queryId ) , ( [ definition , oldParamValues ] ) => {
611- const changedParamValues : IdMap < ParamValue > = mapNew ( ) ;
612- mapMatch (
613- oldParamValues ,
614- paramValues ,
615- ( _ , paramId , newValue ) => {
616- if ( ! arrayOrValueEqual ( newValue , mapGet ( oldParamValues , paramId ) ) ) {
617- mapSet ( changedParamValues , paramId , newValue ) ;
618- }
619- } ,
620- ( _ , paramId ) => mapSet ( changedParamValues , paramId , undefined ) ,
621- ) ;
622- if ( ! collIsEmpty ( changedParamValues ) ) {
638+ if (
639+ ! objIsEqual ( mapToObj ( oldParamValues ) , paramValues , arrayOrValueEqual )
640+ ) {
623641 resultStore . transaction ( ( ) =>
624642 setQueryDefinition (
625643 queryId ,
@@ -628,10 +646,6 @@ export const createQueries = getCreateFunction((store: Store): Queries => {
628646 paramValues ,
629647 ) ,
630648 ) ;
631- mapForEach ( changedParamValues , ( paramId , value ) =>
632- callListeners ( paramValueListeners , [ queryId , paramId ] , value ) ,
633- ) ;
634- callListeners ( paramValuesListeners , [ queryId ] , { ...paramValues } ) ;
635649 }
636650 } ) ;
637651 return queries ;
@@ -641,14 +655,15 @@ export const createQueries = getCreateFunction((store: Store): Queries => {
641655 queryId : Id ,
642656 paramId : Id ,
643657 value : ParamValue ,
644- ) : Queries =>
645- setParamValues ( queryId , {
646- ...mapToObj ( getQueryArgs ( queryId ) ?. [ 1 ] ) ,
647- [ paramId ] : value ,
648- } ) ;
658+ ) : Queries => {
659+ if ( ! arrayOrValueEqual ( getParamValue ( queryId , paramId ) , value ) ) {
660+ setParamValues ( queryId , { ...getParamValues ( queryId ) , [ paramId ] : value } ) ;
661+ }
662+ return queries ;
663+ } ;
649664
650665 const getParamValues = ( queryId : Id ) : ParamValues =>
651- ifNotUndefined ( getQueryArgs ( queryId ) ?. [ 1 ] , mapToObj ) ?? { } ;
666+ mapToObj ( getQueryArgs ( queryId ) ?. [ 1 ] ) ;
652667
653668 const getParamValue = ( queryId : Id , paramId : Id ) : ParamValue | undefined =>
654669 mapGet ( getQueryArgs ( queryId ) ?. [ 1 ] , paramId ) ;
0 commit comments