Jump To …
README lib / psd / blend_mode.coffee lib / psd / channel_image.coffee lib / psd / color.coffee lib / psd / descriptor.coffee lib / psd / file.coffee lib / psd / header.coffee lib / psd / image.coffee lib / psd / image_export.coffee lib / psd / image_exports / png.coffee lib / psd / image_format.coffee lib / psd / image_formats / layer_raw.coffee lib / psd / image_formats / layer_rle.coffee lib / psd / image_formats / raw.coffee lib / psd / image_formats / rle.coffee lib / psd / image_mode.coffee lib / psd / image_modes / cmyk.coffee lib / psd / image_modes / greyscale.coffee lib / psd / image_modes / rgb.coffee lib / psd / init.coffee lib / psd / layer / blend_modes.coffee lib / psd / layer / blending_ranges.coffee lib / psd / layer / channel_image.coffee lib / psd / layer / helpers.coffee lib / psd / layer / info.coffee lib / psd / layer / mask.coffee lib / psd / layer / name.coffee lib / psd / layer / position_channels.coffee lib / psd / layer.coffee lib / psd / layer_info / blend_clipping_elements.coffee lib / psd / layer_info / blend_interior_elements.coffee lib / psd / layer_info / fill_opacity.coffee lib / psd / layer_info / gradient_fill.coffee lib / psd / layer_info / layer_id.coffee lib / psd / layer_info / layer_name_source.coffee lib / psd / layer_info / legacy_typetool.coffee lib / psd / layer_info / locked.coffee lib / psd / layer_info / metadata.coffee lib / psd / layer_info / nested_section_divider.coffee lib / psd / layer_info / object_effects.coffee lib / psd / layer_info / section_divider.coffee lib / psd / layer_info / solid_color.coffee lib / psd / layer_info / typetool.coffee lib / psd / layer_info / unicode_name.coffee lib / psd / layer_info / vector_mask.coffee lib / psd / layer_info / vector_origination.coffee lib / psd / layer_info / vector_stroke.coffee lib / psd / layer_info / vector_stroke_content.coffee lib / psd / layer_info.coffee lib / psd / layer_mask.coffee lib / psd / lazy_execute.coffee lib / psd / mask.coffee lib / psd / node.coffee lib / psd / nodes / ancestry.coffee lib / psd / nodes / build_preview.coffee lib / psd / nodes / group.coffee lib / psd / nodes / layer.coffee lib / psd / nodes / root.coffee lib / psd / nodes / search.coffee lib / psd / path_record.coffee lib / psd / resource.coffee lib / psd / resource_section.coffee lib / psd / resources / layer_comps.coffee lib / psd / resources.coffee lib / psd / util.coffee lib / psd.coffee shims / init.coffee shims / png.coffee

# lazy_execute.coffee lib/psd/ |
--- | ---
¶ LazyExecute is very important when it comes to speed. Because some PSD documents can be extremely large and hold a LOT of data, we can significantly speed up parsing by temporarily skipping over chunks of the PSD. Chances are that you aren't going to use every piece of data in the document. This means, when you do request some data that's proxied through LazyExecute, we can parse it on the fly. This overhead should be incredibly minimal. While not as elegant as the PSD.rb counterpart, it gets the job done. We look at the object we need to proxy, and define proxies for every item on its prototype on this one. The proxy checks to see if the object has been loaded first before passing on the call to the object. If the object has not been loaded yet, we record our current position in the file, jump to the known start position of the data, parse it by calling a set method on the object, jump back to the original position in the file, and then call the proxied property. ## Example
    obj = new SomeObject()
    data = new LazyExecute(obj, file)
      .now('skip')
      .later('parse')
      .ignore('foo', 'bar')
      .get()
    module.exports = class LazyExecute
      constructor: (@obj, @file) ->
        @startPos = @file.tell()
        @loaded = false
        @loadMethod = null
        @loadArgs = []
        @passthru = []
¶ This describes the method that we want to run at object instantiation. Typically this will skip over the data that we will parse on-demand later. We can pass any arguments we want to the method as well. |
      now: (method, args...) ->
        @obj[method].apply(@obj, args)
        return @
¶ Here we describe the method we want to run when the first method/property on the object is accessed. We can also define any arguments that need to be passed to the function. |
      later: (method, args...) ->
        @loadMethod = method
        @loadArgs = args
        return @
¶ Sometimes we don't have to parse the data in order to get some important information. For example, we can get the widht/height from the full preview image without parsing the image itself, since that data comes from the header. Purely convenience, but helps to optimize usage. The arguments are a list of method/property names we don't want to trigger on-demand parsing. |
      ignore: (args...) ->
        @passthru.concat args
        return @
¶ This is called once all of the paramters of the proxy have been set up, i.e. now, later, and skip. This defines all items on the proxied objects prototype on this object, and checks to make sure the proxied object has been loaded before passing on the call. |
      get: ->
        for key, val of @obj then do (key, val) =>
          return if @[key]?
          Object.defineProperty @, key,
            get: ->
              @load() if not @loaded and not (key in @passthru)
              @obj[key]

        @
¶ If we are accessing a property for the first time, then this will call the load method, which was defined during setup with `later()`. The steps this performs are: Records the current file position. Jumps to the recorded start position for the proxied data. Calls the load method, which was defined with `later()`. Jumps back to the original file position. Sets the `@loaded` flag to true so we know this object has been parsed. |
      load: ->
        origPos = @file.tell()
        @file.seek @startPos

        @obj[@loadMethod].apply(@obj, @loadArgs)

        @file.seek origPos
        @loaded = true

generated Tue May 12 2015 10:38:19 GMT-0400 (EDT)