Skip to content

Commit

Permalink
progress-4
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Dec 9, 2024
1 parent 775ef65 commit 4bd28b1
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 57 deletions.
2 changes: 1 addition & 1 deletion imports/api/makeFindOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const makeFindOne =
loading: loadingResult,
found,
result: upToDate,
} = useItem(collection, selector, options, [loadingSubscription, ...deps]);
} = useItem(collection, selector, options, deps);

const fields = {...init, ...upToDate};

Expand Down
38 changes: 19 additions & 19 deletions imports/api/publication/stopSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import assert from 'assert';
import {defer, type Deferred} from '../../lib/async/defer';

import type SubscriptionHandle from './SubscriptionHandle';
import { debugMeteorSubscriptions } from './subscriptionInternals';
import {debugMeteorSubscriptions} from './subscriptionInternals';
import {get, set} from './subscriptionRegistry';

const _gcQueue = new Map<string, Deferred>();
Expand All @@ -14,10 +14,10 @@ const stopSubscription = (
) => {
const entry = get(key);
if (entry === undefined) {
console.debug({
what: 'stopSubscription',
msg: `subscription ${key} already stopped`,
});
// console.debug({
// what: 'stopSubscription',
// msg: `subscription ${key} already stopped`,
// });
return;
}

Expand All @@ -37,26 +37,26 @@ const stopSubscription = (
if (entry.refCount === 0) {
set(key, undefined);
const sub = entry.internals;
console.debug({
what: 'stopSubscription',
msg: 'refCount === 0',
id: sub.id,
name: sub.name,
params: sub.params,
});
// console.debug({
// what: 'stopSubscription',
// msg: 'refCount === 0',
// id: sub.id,
// name: sub.name,
// params: sub.params,
// });
sub.inactive = true;
const prev = _gcQueue.get(sub.id);
if (prev !== undefined) prev.cancel();

const next = defer(() => {
if (sub.inactive) {
console.debug({
what: 'stopSubscription',
msg: 'unsub',
id: sub.id,
name: sub.name,
params: sub.params,
});
// console.debug({
// what: 'stopSubscription',
// msg: 'unsub',
// id: sub.id,
// name: sub.name,
// params: sub.params,
// });
handle.stop();
debugMeteorSubscriptions();
}
Expand Down
14 changes: 9 additions & 5 deletions imports/api/publication/subscriptionInternals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ let prev = '';
export const debugMeteorSubscriptions = () => {
const subscriptions = Meteor.connection._subscriptions;
const subs = Object.values(subscriptions).filter(Boolean);
const next = JSON.stringify({
subCount: subs.length,
subs: subs.map(({name, params}) => ({name, params})),
}, undefined, 2);
const next = JSON.stringify(
{
subCount: subs.length,
subs: subs.map(({name, params}) => ({name, params})),
},
undefined,
2,
);

if (next !== prev) {
console.debug(next);
// console.debug(next);
prev = next;
}
};
Expand Down
48 changes: 30 additions & 18 deletions imports/api/query/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ const _noFullDocumentPipeline = (): Pipeline => {
const _optionsToPipeline = (options: Options) =>
options.project === undefined ? [] : [{$project: options.project}];

let watchCount = 0;
let _watchStreamCount = 0;

export const getWatchStreamCount = () => _watchStreamCount;

const _watchStream = <T extends Document, U = T>(
collection: Collection<T, U>,
Expand All @@ -133,16 +135,25 @@ const _watchStream = <T extends Document, U = T>(
];

const rawCollection = collection.rawCollection();
const {collectionName} = rawCollection;

console.debug({collection: collectionName, watchCount: ++watchCount});
const stream = rawCollection.watch(pipeline, {
startAtOperationTime,
fullDocument: 'whenAvailable',
fullDocumentBeforeChange: 'whenAvailable',
...changeStreamOptions,
});

let open = true;
++_watchStreamCount;
console.debug({_watchStreamCount});
stream.on('close', () => {
if (open) {
open = false;
--_watchStreamCount;
console.debug({_watchStreamCount});
}
});

return _groupFragments(stream);
};

Expand Down Expand Up @@ -232,28 +243,28 @@ const _watchSetup = async <T extends Document, U = T>(
return {init, stream: filteredStream, filterIsSuperset};
};

const _makeQueue = async () => {
let queued = 0;
let queue = new Promise((resolve) => {
class Queue {
#queued = 0;
#queue = new Promise<void>((resolve) => {
resolve(undefined);
});
await queue;

const enqueue = (task: () => Promise<void> | void) => {
if (queued !== 0) return;
++queued;
queue = queue
public get length() {
return this.#queued;
}

public enqueue(task: () => Promise<void> | void) {
++this.#queued;
this.#queue = this.#queue
.then(async () => {
--queued;
--this.#queued;
return task();
})
.catch((error) => {
console.error({error});
});
};

return enqueue;
};
}
}

type Fragment = {
_id: {
Expand Down Expand Up @@ -315,10 +326,11 @@ const _pipe = async <T extends Document, U = T>(
emitter: FilteredOplogHandle,
w: Watch<T, U>,
) => {
const enqueue = await _makeQueue();
const queue = new Queue();

const onEntry = debounce(() => {
enqueue(async () => {
if (queue.length > 0) return;
queue.enqueue(async () => {
const {init} = await _watchInit(
w.collection,
w.filter,
Expand Down
24 changes: 12 additions & 12 deletions imports/ui/appointments/AppointmentCancellationDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useState} from 'react';
import React, {useState, useMemo} from 'react';

import Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
Expand Down Expand Up @@ -49,6 +49,15 @@ const AppointmentCancellationDialog = ({open, onClose, appointment}: Props) => {
}
};

const renderValue = useMemo(
() =>
loading
? (value) => value || 'Loading options ...'
: (value) =>
value || 'Please choose a reason for the cancellation (required)',
[loading],
);

return (
<Dialog open={open} onClose={onClose}>
{loading && <LinearProgress />}
Expand All @@ -68,18 +77,9 @@ const AppointmentCancellationDialog = ({open, onClose, appointment}: Props) => {
<InputLabel shrink>Cancellation reason</InputLabel>
<Select
displayEmpty
renderValue={(value) => {
return (
value ||
(loading
? 'Loading options ...'
: 'Please choose a reason for the cancellation (required)')
);
}}
renderValue={renderValue}
value={reason}
inputProps={{
readOnly: loading,
}}
readOnly={loading}
onChange={(e) => {
setReason(e.target.value);
}}
Expand Down
Empty file.
Empty file.
13 changes: 12 additions & 1 deletion imports/ui/settings/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,24 @@ export const useSetting = <K extends SettingKey>(
Settings,
{key},
undefined,
[loading, key],
[key],
);

const value = useMemo(
() => withDefaultFn(loading, userId, key, setting),
[withDefaultFn, loading, userId, key, setting],
);

// console.debug({
// key,
// value,
// loading,
// loggingIn,
// loggingOut,
// loadingSettingSubscription,
// loadingSettingResult,
// });

const setValue = useCallback(
async (newValue: UserSettings[K]) => setSetting<K>(key, newValue),
[key],
Expand Down
2 changes: 1 addition & 1 deletion test/app/client/change-password.app-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ client(__filename, () => {
/Timed out/,
);
await app.findByText('Incorrect password');
}).timeout(10_000);
}).timeout(15_000);
});

0 comments on commit 4bd28b1

Please sign in to comment.