Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional observe callback not called #298

Open
hamptonsmith opened this issue Aug 7, 2022 · 1 comment
Open

Optional observe callback not called #298

hamptonsmith opened this issue Aug 7, 2022 · 1 comment

Comments

@hamptonsmith
Copy link

Hey folks! Thanks for an awesome library.

Am I misreading the observe() documentation regarding the optional callback? Documentation says "When changes are detected, the optional callback is called", but I'm not seeing that behavior:

Welcome to Node.js v16.14.2.
Type ".help" for more information.
> var fastJsonPatch = require('fast-json-patch')
undefined
> var doc = {};
undefined
> fastJsonPatch.observe(doc, function(p) { console.log(p); })
{ /* snip */ }
> doc.foo = 'bar';
'bar'
>

I'm expecting to see the patch array console.log()'d via the callback, but the callback is never called.

Calling generate() flushes the changes and I then see them printed via the callback.

@hamptonsmith hamptonsmith changed the title Observe callback not working Optional observe callback not called Aug 7, 2022
@Ghirigoro
Copy link

Ghirigoro commented Sep 24, 2022

I can confirm that the callback is not called:

import * as JSONPatch from "fast-json-patch";

const obj = {
  foo: "bar",
  baz: "qux",
  quux: 1,
};

let numCalls = 0;
const o = JSONPatch.observe(obj, (patches) => {
  numCalls++;
});


obj.foo = "baz";
obj.baz = "quux";
obj.quux = 2;
console.log(numCalls); // <- expected 0 got 0
// The observe callback is driven by a 0 duration timeout so we have to wait for another run through the event loop...
setTimeout(()=>{
  console.log(numCalls); // <- expected 1 got 0  
  JSONPatch.generate(o);
  console.log(numCalls); // <- expected 1 got 1
}, 100);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants