Coder Social home page Coder Social logo

bowser's People

Contributors

ajjahn avatar empiricalanalysis avatar jgaskins avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

bowser's Issues

require 'bowser/http'

Requiring browser/http is not enough, you need to require also bowser/event_target witch can be fixed quick.

support #to_n

Hey, just tried to use this lib instead of opal-browser. But I miss one thing. I can't convert Bowser::Element to native element. Maybe you should add include Native to classes like Element, Document etc. What do you think ?

Add extension DSL for elements

Element objects currently have a pretty limited number of methods and fall back to using some method_missing magic to delegate to native properties. This helps keep application JS payloads small by not defining every possible type of element and wrapping all of their methods/properties. This is fine for anything that returns primitive values (numbers, strings, or booleans), but I've begun working on apps that require a bit more functionality from the elements we're using, such as <video>, which have more complex properties.

The easiest solution I can think of is to add other files to require to extend the functionality. For example, if you want to handle some of the more advanced <video> functionality, such as HTMLMediaElement.prototype.buffered (find out which segments of an audio or video have been loaded from the server), you could add this to your app (hypothetically, that is; it does not yet exist):

require 'bowser/element/media'

This could add methods to support the buffered property to the Element class. We already added support for some <input /> element properties, but that's not ideal. It makes them available on every element. This can become problematic if there are multiple types of elements that have the same property names that need to do different things.

This hasn't been a problem because there probably aren't many different types of elements that have files or checked properties. But if there are (are there any? I honestly don't know), we'll need to be able to handle them.

I'm proposing some type of DSL to adding methods for specific element types. For the <video> example:

module Bowser
  class Element
    # Add for all media elements
    tag :video, :audio do
      def buffered
        TimeRanges.new(`#@native.buffered`)
      end
    end
  end
end

This way, it would invoke buffered only for Bowser::Element instances that wrap video and audio elements. For instances wrapping other element types, you'd get a NoMethodError as expected. As far as implementation goes, this could define an element subclass, and then Element.new could check these subclasses on instantiation. Something like this:

class Element 
  @types = Hash.new { |h, k| h[k] = Class.new(self) }
  def self.tag(*types, &block)
    types.each do |type|
      @types[type].class_exec(&block)
    end
  end

  # Return the subclass specified for this tag type
  def self.new(native)
    type = @types.fetch(`native.tagName`) { return super }
    type.new(native)
  end
end

the promise implementations do not provide Promise.when

it seems that Bowser::ServiceWorker::Promise does not implement Promise.when as shown in http://opalrb.org/docs/guides/master/promises.html

Composing Promises

Promise.when can be used to wait for more than 1 promise to resolve (or reject). Lets assume we wanted to get 2 different users:

first = get_json '/users/1.json'
second = get_json '/users/2.json'

Promise.when(first, second).then do |user1, user2|
  puts "got users: #{user1}, #{user2}"
end.fail do
  alert "Something bad happened"
end

Add WebWorker support

This will likely have a few challenges associated with it. Specifically, self in a WebWorker points to the global context (like window in the UI thread), but Opal overrides it in all compiled Ruby code. This may mean it will need to be written completely in JS (not even contained within a Ruby file).

What I'd like is to be able to do something like this:

Main app

require 'opal'
require 'bowser/worker'

worker = Bowser::Worker.new('worker.rb')
worker.on :message do |event|
  puts event.data # prints "lol" to the console
end

worker.post_message "LOL"

worker.rb

require 'opal' # Yeah, it'll most likely need its own Opal runtime. :-\
require 'bowser/worker/process' # Should not be the same file the main app loads, so we'll need to figure out where to put this.

Bowser::Worker.on :message do |event|
  post_message event.data.downcase # Multithreaded downcasing FTW
end

Main things:

  • Proxy JS MessageEvent with a Bowser::Event on both sides. It's possible a lot of the message-passing code will be identical on both sides, now that I think of it.
  • A WebWorker is more like how we think of processes rather than threads. No memory is shared between the two. This will need to be documented.
  • Only JSON primitives can go between threads.
    • We may need to JSON.dump whatever gets passed in (or to_n, whichever is more reliable and/or consumes less CPU/RAM). Maybe allow native objects through unmodified. Cross-browser benchmarks needed before any optimization happens.
    • Worker#post_message will need to warn if anything other than a Hash, Array, String, or Number is passed to it. Raising an exception might be a good idea, too, but all Ruby objects can come through, but it will have its constructor/prototype properties mangled.

If we can solve this, supporting ServiceWorker will be a nearly identical process.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.