Skip to content

Commit

Permalink
change extracting functions to not delete it from the object until it…
Browse files Browse the repository at this point in the history
…'s activated
  • Loading branch information
jmeistrich committed Dec 2, 2023
1 parent ceb4ad9 commit 63f181b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/ObservableObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,6 @@ export function extractFunctionOrComputed(node: NodeValue, obj: Record<string, a
} else if (typeof v === 'function') {
const childNode = node.children?.get(k);
extractFunction(node, k, v);
delete obj[k];
// If child was previously activated, then peek the new linked observable to make sure it's activated
if (childNode && !childNode.lazy) {
if (isObservable(v)) {
Expand All @@ -843,7 +842,6 @@ export function extractFunctionOrComputed(node: NodeValue, obj: Record<string, a
const childNode = getNode(v);
if (childNode?.isComputed) {
extractFunction(node, k, v, childNode);
delete obj[k];
} else {
return true;
}
Expand Down Expand Up @@ -871,6 +869,13 @@ export function peek(node: NodeValue) {
if (lazy) {
delete node.lazy;
if (isFunction(node) || isFunction(lazy)) {
if (node.parent) {
const parentValue = getNodeValue(node.parent);
if (parentValue) {
delete parentValue[node.key];
}
}

value = activateNodeFunction(node as any, lazy as () => void);
}

Expand Down
5 changes: 3 additions & 2 deletions tests/computed-old.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,8 +803,9 @@ describe('proxy', () => {
return obs.items[key].text;
}),
});
expect(obs.get()).toEqual({
items: { test1: { text: 'hi' }, test2: { text: 'hello' } },
expect(obs.get().items).toEqual({
test1: { text: 'hi' },
test2: { text: 'hello' },
});

obs.itemText['test1'].set('hi!');
Expand Down
5 changes: 3 additions & 2 deletions tests/computedtests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1208,8 +1208,9 @@ export const run = (isPersist: boolean) => {
lookup: (key): Observable<string> => obs.items[key].text,
}),
});
expect(obs.get()).toEqual({
items: { test1: { text: 'hi' }, test2: { text: 'hello' } },
expect(obs.get().items).toEqual({
test1: { text: 'hi' },
test2: { text: 'hello' },
});

obs.itemText['test1'].set('hi!');
Expand Down
13 changes: 12 additions & 1 deletion tests/tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1858,7 +1858,8 @@ describe('Deep changes keep listeners', () => {
],
);
expect(handler2).toHaveBeenCalledWith(
{ arr: [{ _id: 1 }, { _id: 2 }, { _id: 3 }] },
{ arr: [{ _id: 1 }, { _id: 2 }, { _id: 3 }], arr_keyExtractor },
// Note: Cloning to create previous loses functions
{ arr: [{ _id: 0 }, { _id: 1 }, { _id: 2 }] },
[
{
Expand Down Expand Up @@ -2879,6 +2880,16 @@ describe('Functions', () => {

expect(obs.test() === 'hi!');
});
test('Functions still exist on object', () => {
const obs = observable({ text: 'hi' } as { text: any; test: () => string });
obs.assign({
test: () => 'hi!',
});

const raw = obs.get();

expect(raw.test() === 'hi!');
});
});

describe('Extend observableFunctions', () => {
Expand Down

0 comments on commit 63f181b

Please sign in to comment.