##// END OF EJS Templates
Fixed queryEx: query fresh results every time when no active observers are exist
cin -
r150:d9c99ae7dec8 v1.10.1 default
parent child
Show More
@@ -42,19 +42,32 export const query = <T, Q, O>(store: Qu
42 };
42 };
43 };
43 };
44
44
45 /**
46 * Wraps the query method of the store, the resulting method takes a query
47 * expression and returns two observable sequences. The first sequence represents
48 * the results of the query, the second sequence provides the updates to the
49 * query results.
50 *
51 * @param store The store used to query data
52 * @param includeUpdates The flag to include item updates not only additions and
53 * deletions. By default this flag is set to true.
54 * @returns Two observable sequences
55 */
45 export const queryEx = <T, Q, O>(store: Queryable<T, Q, O>, includeUpdates = true) =>
56 export const queryEx = <T, Q, O>(store: Queryable<T, Q, O>, includeUpdates = true) =>
46 (query?: Q, options?: O): [data: QueryResults<T>, updates: QueryResults<T>] => {
57 (query?: Q, options?: O): [data: QueryResults<T>, updates: QueryResults<T>] => {
47
58
48 const pending: T[] = [];
59 /** count active observers */
49
60 let listeners = 0;
50 let results: PromiseOrValue<T[]> = pending;
61 let results: PromiseOrValue<T[]> = [];
51
62
52 const data = observe<OrderedUpdate<T>>(({ next, complete, error }) => {
63 const data = observe<OrderedUpdate<T>>(({ next, complete, error }) => {
53 const processResults = (items: T[]) =>
64 const processResults = (items: T[]) =>
54 items.forEach((item, newIndex) => next({ item, newIndex, prevIndex: -1 }));
65 items.forEach((item, newIndex) => next({ item, newIndex, prevIndex: -1 }));
55
66
56 try {
67 try {
57 if (results === pending)
68 // is there are no active observers here, we need to query actual
69 // data from the store.
70 if (listeners === 0)
58 results = store.query(query, options);
71 results = store.query(query, options);
59
72
60 if (isPromise(results)) {
73 if (isPromise(results)) {
@@ -74,8 +87,14 export const queryEx = <T, Q, O>(store:
74 const updates = observe<OrderedUpdate<T>>(({ next, complete, error, isClosed }) => {
87 const updates = observe<OrderedUpdate<T>>(({ next, complete, error, isClosed }) => {
75 try {
88 try {
76 if (!isClosed() && isDjObservableResults<T>(results)) {
89 if (!isClosed() && isDjObservableResults<T>(results)) {
90 // subscribe fot the changes
91 listeners++;
77 const h = results.observe((item, prevIndex, newIndex) => next({ item, prevIndex, newIndex }), includeUpdates);
92 const h = results.observe((item, prevIndex, newIndex) => next({ item, prevIndex, newIndex }), includeUpdates);
78 return () => h.remove();
93 return () => {
94 // unsubscribe from changes
95 listeners--;
96 h.remove();
97 };
79 } else {
98 } else {
80 complete();
99 complete();
81 }
100 }
@@ -56,8 +56,8 tasks.matching{ it.name =~ /^configureTs
56 }
56 }
57 }
57 }
58
58
59 npmInstall {
59 task npmInstallLocalDeps {
60 //npmInstall.dependsOn it
60 npmInstall.dependsOn it
61 dependsOn configurations.npmLocal
61 dependsOn configurations.npmLocal
62
62
63 doFirst {
63 doFirst {
General Comments 0
You need to be logged in to leave comments. Login now