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

Object.observe is obsolete with ES6. Try to find a solution with Proxy #3

Open
franckOL opened this issue Aug 8, 2016 · 16 comments · May be fixed by #7
Open

Object.observe is obsolete with ES6. Try to find a solution with Proxy #3

franckOL opened this issue Aug 8, 2016 · 16 comments · May be fixed by #7

Comments

@franckOL
Copy link

franckOL commented Aug 8, 2016

Object.observe(resources.links.actions.resources[actionId].data, function (changes) {
       ^

TypeError: Object.observe is not a function
at C:_Utilisateur\Project\ObjectIn\webofthings.js\plugins\corePlugin.js:70:
12
at arrayEach (C:_Utilisateur\Project\ObjectIn\webofthings.js\node_modules\l
odash\internal\arrayEach.js:15:9)
at Object. (C:Utilisateur\Project\ObjectIn\webofthings.js\node
modules\lodash\internal\createForEach.js:15:9)
at CorePlugin.observeActions (C:_Utilisateur\Project\ObjectIn\webofthings.j
s\plugins\corePlugin.js:69:5)
at CorePlugin.start (C:_Utilisateur\Project\ObjectIn\webofthings.js\plugins
\corePlugin.js:30:26)
at initPlugins (C:_Utilisateur\Project\ObjectIn\webofthings.js\wot-server.j
s:50:14)
at createServer (C:_Utilisateur\Project\ObjectIn\webofthings.js\wot-server.
js:12:3)
at Object. (C:_Utilisateur\Project\ObjectIn\webofthings.js\wot.j
s:2:1)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
at run (node.js:348:7)
at startup (node.js:140:9)

@franckOL
Copy link
Author

franckOL commented Aug 8, 2016

Found that https://github.com/anywhichway/proxy-observe , but seem too heavy

@domguinard
Copy link
Member

Thanks franckOL. We actually did see that this was deprecated and started experimenting with alternatives. Will report on our findings and hopefully provide a patch soon. For now, the ideal is to stick with the current Node LTS: 4.X which still supports Object.observe().

@franckOL
Copy link
Author

franckOL commented Sep 6, 2016

Hi,
What about the pull request, it works for all version of node js, and it's really minor change.
Franck

@domguinard
Copy link
Member

Sorry about that Franck, we missed the PR. This is really simple and great. I just accepted the PR, thanks a lot!

domguinard added a commit that referenced this issue Sep 18, 2016
domguinard added a commit that referenced this issue Sep 18, 2016
@domguinard domguinard reopened this Nov 19, 2016
@domguinard
Copy link
Member

Reopening as the new Node LTS is now 6 we'll be looking into this.

@ngmgithub
Copy link

ngmgithub commented Apr 16, 2017

Running the latest commit in master as of today. Still seeing the Object.observe is not a function issue.

webofthings/wot-book@9ab17ed

Running Node v6.10.0 over nvm

@developer-tarun
Copy link

Hi Guys ! Any solution to this problem ? I am going through chapter 7 of webofthings, and I am stuck because of this Object.observe problem when i try to execute the wot.js

@domguinard
Copy link
Member

Hi guys, we have tested a number of solutions but haven't found a totally nice and simple one yet, we are still working on it but meanwhile I would stick to Node 4.X which supports Object.observe(). (you can install several version of node with NVM)

@logivity
Copy link

logivity commented Jul 1, 2017

Any update on this?

@ngmgithub
Copy link

ngmgithub commented Jul 1, 2017 via email

@domguinard
Copy link
Member

Thanks for the ping and sorry for being so incredibly slow on that one. We had a fix (see PR) but there were a few weird behaviours. I'll get back to it asap but feel free to help. I'll start with a look at the link thanks for that.

@franckOL
Copy link
Author

franckOL commented Dec 4, 2017

Hello, @domguinard, i see some evolution on W3C draft on WOT specification, also what is your advice . may be we can use ==> https://github.com/thingweb/node-wot ?

@ngmgithub
Copy link

ngmgithub commented Feb 9, 2018

@domguinard We had some previous discussion about the use of ES6 proxies. Another alternative would be the use of event emitters of which I am attempting to become more familiar. Any thoughts on this approach?

@germain171
Copy link

germain171 commented Feb 15, 2019

Hi all, very new to this dev stuff. so dont mind my silly question.
i get this, when 'possibly' using the newest version of Chrome to debug a Mobile device. which i think is on an older version of chrome wrapped in an Industrial Browser.
Uncaught TypeError: Object.observe is not a function at WebInspector.Main._createSettings (inspector.js:10452) at WebInspector.Main._gotPreferences (inspector.js:10444) at DevToolsAPIImpl.embedderMessageAck (devtools_compatibility.js:33) at <anonymous>:1:13
When using command navigator.appVersion.match(/.*Chrome\/([0-9\.]+)/)[1] in the dev console with the remote browser connected, i get the dev console version of 72.0.3626.109.
so at this point i cannot even interrogate what the version of chrome/ind browser. To see if it is a version compatibility issue.
My current plan, and its not a great one. Is to download and install Chrome from a random guess age in the past and keep downloading older ones until it works. ( i dont like this fix)

Just tried it in straight Chrome with out ind Browser wrapper. still the same. so it not the Ind Browser part that is an issue. and i cannot update the Browser on the Device. (Lollipop)

Any ideas ?

@aolite
Copy link

aolite commented Mar 26, 2020

Hi all, thank you very much for the ideas exposed here. I personally have tries to implement the examples of chapter 7 using nest.js as a server. During the implementation, we also found the deprecated method Object.observer().

To overcome this problem, I created a proxy variable to watch for changes in the object. Inside the proxy, I created an event emitter to send an event to the socket connection to perform the send operation to the WebSocket client:

const myEvent = new EventEmitter();
const proxyResource = new Proxy(resourcesJson, {
    get: function(target, prop) {
      console.log({ type: 'get', target, prop });
      myEvent.emit('ws-wot');
      return Reflect.get(target, prop);
    },
    set: function(target, prop, value) {
      console.log({ type: 'set', target, prop, value });
      
      return Reflect.set(target, prop, value);
    }
  });

One more change to be performed is on the plugins that require to change the proxy variable directly in order to reflect the change over the object:

private simulate(): void {
        this.interval = setInterval(() => {
            proxyResource.pi.sensors.pir.value = !this.model.value;
            this.showValue();
        }, this.localParams.frequency);
        this.logger.log(`Simulated sensor ${this.pluginName} sensor Started!`);
    }

Last step os to establish the WebSocket gateway is formed by a function in charge of accepting connections. Once the WS client connected is able to get data streams from the server once the data is generated.

@WebSocketGateway( { transports: ['websocket'] })
export class EventsGateway /*implements OnGatewayConnection, OnGatewayDisconnect*/{

  @WebSocketServer() server: Server;
  private logger = new Logger(EventsGateway.name);

  handleConnection(client: any, ...args: any[]) {
    this.logger.log(`New Connection:${args [0]}`);
    //this.logger.log(inspect(client))
    this.logger.log(inspect(args[0].url))
    const url = args[0].url;
    this.logger.log(inspect(url));

    client.send('Listening data...');

    myEvent.on('ws-wot', ()=>{
      this.logger.log('Event acquired');
      client.send(JSON.stringify(this.selectedResource(url)));
    });
    
  }
...

For your consideration, the complete example is here

@DavidGomez-dev
Copy link

Hi there,
Maybe not the best solution, but some idea for a simple one.
I used the own resources.json as a simple database.
In the plugin I used readFile and a setInterval to check the value with some frequency.
Then on the actuator router, I used readFile and writeFile to update the value on the json according to the PUT value of the call.
Just another idea.

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

Successfully merging a pull request may close this issue.

8 participants