webofthings / webofthings.js

A gateway and server reference implementation of the Web of Things model
http://book.webofthings.io
Apache License 2.0
74 stars 26 forks source link

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

Open franckOL opened 8 years ago

franckOL commented 8 years ago
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 commented 8 years ago

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

domguinard commented 8 years ago

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 commented 8 years ago

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

domguinard commented 8 years ago

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

domguinard commented 7 years ago

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

ngmgithub commented 7 years ago

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

https://github.com/webofthings/wot-book/commit/9ab17edeaa4284290cd6563d81336503cea470e7

Running Node v6.10.0 over nvm

developer-tarun commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

Any update on this?

ngmgithub commented 7 years ago

Maybe we should work on some suggestions in the community to get them started. The following may be a useful tutorial to get started.

https://www.bitovi.com/blog/long-live-es6-proxies

On Sat, Jul 1, 2017 at 3:12 PM, logivity notifications@github.com wrote:

Any update on this?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/webofthings/webofthings.js/issues/3#issuecomment-312415991, or mute the thread https://github.com/notifications/unsubscribe-auth/ACxalq47R2T3BTiH2RWKw5cQDOMlLpW7ks5sJfFVgaJpZM4Je5gQ .

domguinard commented 7 years ago

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 commented 6 years ago

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 commented 6 years ago

@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 commented 5 years ago

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 commented 4 years ago

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 commented 3 years ago

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.