Easy javascript question - calling a property of type function by name? #357
Replies: 12 comments
-
Posted at 2015-04-14 by alexanderbrevig What about
Not tested. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-04-14 by @gfwilliams Also |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-04-15 by @allObjects @drazzy, cool thing your are up to... #webserver you can go even further... as you know, require() can load dynamically - at runtime - module from the card and there is also a way to unload it afterwards. Depending on the extension, you first check for a statically loaded function, if it is not there, go for a module, and if there is no module loaded, throw the error... 404. What about below code (used long names help with documentation):
Are you also thinking of a file type / extension that can include references to other files and The handler as module could also have ways to go after 'load code from EPROM'. Btw, if managed code is eval-ed, it is not THAT a bad thing to do... PS: I'm currently in a time/resource bind, otherwise the code above would be tested... ;-) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-04-15 by @gfwilliams Just to overcomplicate matters, you could actually serve up a kind of poor-mans JSP, by loading the file and searching for |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-04-15 by @allObjects
@drazzy, is that the requirement? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-04-15 by @joakim I like the One could even create a dynamic lightweight routing table in memory by loading each of the "route modules" one by one, storing its parameters + a reference to its file (not the callback itself) in the routing table and then unloading the module from memory. On lookup, the server would load the matching route's module, run its callback, and unload it. The route modules could live in e.g. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-04-15 by @allObjects @joakim, it all depends on the requirements... but thanks for your kind words. @drazzy's current implementation can be seen at the very beginning of this post - ESP8266 server only serves page once, sometimes twice. His needs are obviously more than satisfied with a simple dedicated implementation using switch-case to handle just 3 options. Currently I'm deep into node.js / express / ... (and other heavy things - as whole frameworks solutions compared to (small) modules) where things are not that resource constraint... and that leads me (sometimes to go astray) to more generic than just pragmatic solutions. It is an adjustment from client and server side JS to micro controller JS. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-04-15 by DrAzzy Thanks all for the suggestions and thoughts. I've obviously gotten the original question sorted out. I have a bunch of Pico's coming, and I figure many of them will be connected to the internet doing stuff. The natural choice for internet interfacing is HTTP - as it's standard, simple, the tools for it are well developed, and basic client software is ubiquitous. I figure if it needs to react to conditions communicated over WiFi, that's an http server. Since I'm going to need something like this on multiple projects, I figured I should make something generic. So I thought about requirements:
@gordon - I absolutely do not want to go down the path of creating my own markup system for pages - that makes the Espruino do a whole lot more work, when you can just serve a static page with some JS that loads the status page and puts the values into the right places - which also has the benefit of the big pages only having to be served once. Not only is it more work, I think parsing files like that is the Wrong Way. @allObjects - Definitely right call on the "handlers" object to contain json/etc handlers - that also lets me genericize the code that calls the handler. I'm not sure what the memory use ramifications of using require() to load the handlers. I'd hope that the handlers would be small enough that that's not a problem, but I could easily have it call code stored on eeprom/SD card if I had to, and add or remove handlers at will (for example, we could dynamically add handlers in response to other events...). However, at this point, it doesn't seem necessary or useful.
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-04-15 by @allObjects @drazzy, is above code working to your expectations? I try to understand and get a bit confused about the sequence in which things get called. Last but not least the 15s timeout on line 16 throws me off. (The double-require of http - once in line 5 and the second time in 25 are probably just a casual thing...). To capture the ready state, Promise help. When challenged with that, I though chose a more lightweight approach and has retry option built in - and called it Tasker - Task synchronizer - kind of a 'purposed' Promise. It helped me to detangle the callbacks, and also get rid of timeouts that most of the time take annoying long but are never long enough when it comes to hick0-ups. As you posted in a different conversation, the callback-mania can become a throw-off and a challenge. May be it is a bit here too... So many things have to get ready - including dependencies have to be considered - before being able to 'moving on' - and you know only within (nested) callbacks when that is the case (for example, you need the ip address, which you can get when successfully connected, and then you can create the server...). I also wonder why you put all into one single object: wifi. Having the things separated would allow to exchange the 'http connection' with what ever is available C33K, ESP8266, WIZ550S2E,... On the other hand - as pointed out for your case - your gearing up for managing swarms of Picos that connect with ESP8266. Bottom line, the code you present, is straight forward. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-04-16 by @gfwilliams Looks good - just some ideas:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-04-16 by @joakim @gordon Why would Or were you thinking of latency, energy consumption or some other factor? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-04-16 by @gfwilliams Loading on demand is fine, but If you just load it with |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2015-04-14 by DrAzzy
Say I have an object with a number of properties of the function type.
Now, I want a function that takes a string and an argument. If the object has that property, call the function with the argument. If not, return an error. Is there any way to do this without eval, or making scratch variables that waste memory?
The context is that I am writing a webserver code for Espruino which will serve most file extensions off the SD card, but if it sees certain extensions (indicating that the page should be dynamically generated), it will see if there's a function to generate that page, and call it if so.
My thinking was that I'd have an object like this, so if I got a request for status.json, I'd call the jsonPage.status() function, if I got a request for asdf.json, I'd call jsonPage.asdf() - or if that didn't exist, I'd return a 404. But what I'm puzzled about is how to call the function without using eval(), which I've been criticized for overusing in the past - I have no idea how else to do it! Even using eval is really awkward, since I'd need to do var temp=eval("jsonPage."+funname); temp(argument); - this means making a copy, which is wasteful of memory...
There's got to be a simple way to do this, but I just don't know what it is, nor what to call this to get results from google.
Beta Was this translation helpful? Give feedback.
All reactions