Skip to content

Commit

Permalink
cleanup debugging log calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Akolyte01 committed Aug 15, 2023
1 parent 87179fe commit 071b9ef
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 35 deletions.
28 changes: 3 additions & 25 deletions src/async-stores/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ export const asyncWritable = <S extends Stores, T>(
let thisStore: Writable<T>;

flagStoreCreated();
const { reloadable, initial, debug, rebounceDelay } = options;

const debuggy = debug ? (...args) => console.log(debug, ...args) : undefined;
const { reloadable, initial, rebounceDelay } = options;

const rebouncedSelfLoad = rebounce(selfLoadFunction, rebounceDelay);

Expand All @@ -91,16 +89,13 @@ export const asyncWritable = <S extends Stores, T>(
let resolveCurrentLoad: (value: T | PromiseLike<T> | Error) => void;

const setCurrentLoadPromise = () => {
debuggy?.('setCurrentLoadPromise -> new load promise generated');
currentLoadPromise = new Promise((resolve) => {
resolveCurrentLoad = resolve;
});
};

const getLoadedValueOrThrow = async (callback?: () => void) => {
debuggy?.('getLoadedValue -> starting await current load');
const result = await currentLoadPromise;
debuggy?.('getLoadedValue -> got loaded result', result);
callback?.();
if (result instanceof Error) {
throw result;
Expand All @@ -118,16 +113,13 @@ export const asyncWritable = <S extends Stores, T>(
try {
// parentValues
const finalValue = (await rebouncedSelfLoad(parentValues)) as T;
debuggy?.('setting value');
thisStore.set(finalValue);

if (!get(loadState).isWriting) {
debuggy?.('setting LOADED');
setState('LOADED');
}
resolveCurrentLoad(finalValue);
} catch (error) {
debuggy?.('caught error', error);
if (error.name === 'AbortError') {
if (thisLoadTracker === mostRecentLoadTracker) {
// Normally when a load is aborted we want to leave the state as is.
Expand All @@ -139,7 +131,7 @@ export const asyncWritable = <S extends Stores, T>(
} else {
logError(error);
setState('ERROR');
debuggy?.('resolving current load with error', error);

// Resolve with an Error rather than rejecting so that unhandled rejections
// are not created by the store's internal processes. These errors are
// converted back to promise rejections via the load or reload functions,
Expand All @@ -155,16 +147,13 @@ export const asyncWritable = <S extends Stores, T>(

// called when store receives its first subscriber
const onFirstSubscription: StartStopNotifier<T> = () => {
debuggy?.('onFirstSubscription');
setCurrentLoadPromise();
parentValues = getAll(stores);
setState('LOADING');

const initialLoad = async () => {
debuggy?.('initial load called');
try {
parentValues = await loadAll(stores);
debuggy?.('setting ready');
ready = true;
changeReceived = false;
selfLoadThenSet();
Expand All @@ -181,7 +170,6 @@ export const asyncWritable = <S extends Stores, T>(
if (ready) {
if (get(loadState).isSettled) {
setCurrentLoadPromise();
debuggy?.('setting RELOADING');
setState('RELOADING');
}
ready = false;
Expand All @@ -197,17 +185,13 @@ export const asyncWritable = <S extends Stores, T>(
);

cleanupSubscriptions = () => {
debuggy?.('cleaning up subscriptions');
parentUnsubscribers.map((unsubscriber) => unsubscriber());
ready = false;
changeReceived = false;
};

// called on losing last subscriber
return () => {
debuggy?.('stopping store');
cleanupSubscriptions();
};
return cleanupSubscriptions;
};

thisStore = writable(initial, onFirstSubscription);
Expand Down Expand Up @@ -242,7 +226,6 @@ export const asyncWritable = <S extends Stores, T>(
}
} catch (error) {
logError(error);
debuggy?.('setting ERROR');
setState('ERROR');
resolveCurrentLoad(newValue);
throw error;
Expand Down Expand Up @@ -270,27 +253,22 @@ export const asyncWritable = <S extends Stores, T>(
ready = false;
changeReceived = false;
if (get(loadState).isSettled) {
debuggy?.('new load promise');
setCurrentLoadPromise();
}
debuggy?.('setting RELOADING from reload');
const wasErrored = get(loadState).isError;
setState('RELOADING');

const visitMap = visitedMap ?? new WeakMap();
try {
parentValues = await reloadAll(stores, visitMap);
debuggy?.('parentValues', parentValues);
ready = true;
debuggy?.(changeReceived, reloadable, wasErrored);
if (changeReceived || reloadable || wasErrored) {
selfLoadThenSet();
} else {
resolveCurrentLoad(get(thisStore));
setState('LOADED');
}
} catch (error) {
debuggy?.('caught error during reload');
setState('ERROR');
resolveCurrentLoad(error);
}
Expand Down
1 change: 0 additions & 1 deletion src/async-stores/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export type WritableLoadable<T> = AsyncLoadable<T> & AsyncWritable<T>;
export interface AsyncStoreOptions<T> {
reloadable?: true;
trackState?: true;
debug?: string;
initial?: T;
rebounceDelay?: number;
}
Expand Down
12 changes: 3 additions & 9 deletions test/async-stores/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {
derived,
readable,
writable,
isReloadable,
rebounce,
safeLoad,
} from '../../src';

Expand Down Expand Up @@ -398,15 +396,14 @@ describe('asyncWritable', () => {

const load = () => {
const valueToReturn = getFinalValue();
console.log('valueToReturn', valueToReturn);

return new Promise<string>((resolve) =>
setTimeout(() => resolve(valueToReturn), 100)
);
};

const myLoadable = asyncReadable('initial', load, {
reloadable: true,
debug: 'my thing:',
});

expect(myLoadable.load()).resolves.toBe('second');
Expand Down Expand Up @@ -534,9 +531,7 @@ describe('asyncWritable', () => {
setTimeout(() => resolve(valueA + valueB), 100)
)
);
const myLoadable = asyncDerived([parentA, parentB], load, {
debug: 'myLoadable',
});
const myLoadable = asyncDerived([parentA, parentB], load);
myLoadable.subscribe(vi.fn());

let result = await myLoadable.load();
Expand Down Expand Up @@ -1263,7 +1258,7 @@ describe('trackState', () => {
const { store: myStore, state: myState } = asyncReadable(
'initial',
load,
{ trackState: true, reloadable: true, debug: 'thing' }
{ trackState: true, reloadable: true }
);

myStore.subscribe(vitest.fn());
Expand All @@ -1286,7 +1281,6 @@ describe('trackState', () => {
.mockRejectedValueOnce('failure');
const myParent = asyncReadable('initial', parentLoad, {
reloadable: true,
debug: 'parent:',
});
const { store: myStore, state: myState } = asyncDerived(
myParent,
Expand Down

0 comments on commit 071b9ef

Please sign in to comment.