Skip to content

Commit

Permalink
fix set not working before activated load
Browse files Browse the repository at this point in the history
  • Loading branch information
jmeistrich committed Nov 13, 2023
1 parent cdc0e62 commit 5f9aa3b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/ObservableObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,7 @@ const activateNodeBase = (globalState.activateNode = function activateNodeBase(
if (onSet) {
let allChanges: Change[] = [];
let latestValue: any = undefined;
let runNumber = 0;
const runChanges = (listenerParams?: ListenerParams) => {
// Don't call the set if this is the first value coming in
if (allChanges.length > 0) {
Expand All @@ -1082,8 +1083,15 @@ const activateNodeBase = (globalState.activateNode = function activateNodeBase(
latestValue = undefined;
globalState.pendingNodes.delete(node);

runNumber++;
const thisRunNumber = runNumber;

const attemptNum = { current: 0 };
const run = () => {
if (thisRunNumber !== runNumber) {
// set may get called multiple times before it loads so ignore any previous runs
return;
}
let onError: () => void;
if (retry) {
if (timeoutRetry?.current) {
Expand All @@ -1093,6 +1101,7 @@ const activateNodeBase = (globalState.activateNode = function activateNodeBase(
onError = handleError;
timeoutRetry = timeout;
}

isSetting = true;
batch(
() =>
Expand All @@ -1118,16 +1127,13 @@ const activateNodeBase = (globalState.activateNode = function activateNodeBase(
},
);
};
run();
whenReady(node.state!.isLoaded, run);
}
};

const onChangeImmediate = ({ value, changes }: ListenerParams) => {
if (!isSetting || isSettingFromSubscribe) {
if (
get(getNode(node.state!.isLoaded)) &&
(changes.length > 1 || !isFunction(changes[0].prevAtPath))
) {
if (changes.length > 1 || !isFunction(changes[0].prevAtPath)) {
latestValue = value;
allChanges.push(...changes);
globalState.pendingNodes.set(node, runChanges);
Expand Down
45 changes: 45 additions & 0 deletions tests/computedtests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,28 @@ export const run = (isPersist: boolean) => {

expect(obs.test.get()).toEqual(true);
});
test('Computed set works before loaded', async () => {
let setValue: number | undefined = undefined;
let getValue: number | undefined = undefined;
const comp = observable(
activated({
onSet: ({ value }) => {
setValue = value;
},
get: () =>
new Promise<number>((resolve) =>
setTimeout(() => {
getValue = 1;
resolve(1);
}, 0),
),
}),
);
comp.set(2);
await promiseTimeout(0);
expect(getValue).toEqual(1);
expect(setValue).toEqual(2);
});
test('Computed handler is batched', () => {
const obs = observable(
activated({
Expand Down Expand Up @@ -1233,5 +1255,28 @@ export const run = (isPersist: boolean) => {

expect(didSet).toEqual(true);
});
test('get returning twice', async () => {
const num$ = observable(0);
const obs = observable(
activated({
get: (): Promise<string> | string =>
num$.get() > 0
? new Promise<string>((resolve) => {
setTimeout(() => {
resolve('hi');
}, 0);
})
: '',
}),
);
const handler = expectChangeHandler(obs);
expect(obs.get()).toEqual('');
num$.set(1);
await promiseTimeout(0);
expect(obs.get()).toEqual('hi');
expect(handler).toHaveBeenCalledWith('hi', '', [
{ path: [], pathTypes: [], prevAtPath: '', valueAtPath: 'hi' },
]);
});
});
};

0 comments on commit 5f9aa3b

Please sign in to comment.