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

Adding new extension hooks #462

Merged
merged 4 commits into from
Apr 21, 2015

Conversation

LightGuard
Copy link
Member

There are three new extension hooks in place now:

  • before_extensions
  • All extensions added via this method, following the same extension rules,
    will be run before any other extensions loaded with extension.
  • after_extensions
  • All extensions added via this method, following the same extension rules,
    will be run after any other extensions loaded with extension.
  • after_generation
  • All extensions added via this method, following the same extension rules,
    will be run after the page generation is completed. This will only run on the
    first generation, not on reloads.

Extensions will now be notified if they are being reloaded. To be notified of
this an extension must have an on_reload(site) method, which will be called
if there is a change. An extension is then free to re-run an initialization
logic, clean-up the site variable, or create new state needed for a re-run of
the execute(site) method.

There are three new extension hooks in place now:

* before_extensions
+
All extensions added via this method, following the same extension rules, will be
run before any other extensions loaded with `extension`.
* after_extensions
+
All extensions added via this method, following the same extension rules, will be
run after any other extensions loaded with `extension`.
* after_generation
+
All extensions added via this method, following the same extension rules, will be
run after the page generation is completed. This will only run on the
first generation, not on reloads.

Extensions will now be notified if they are being reloaded. To be
notified of this an extension must have an `on_reload(site)` method,
which will be called if there is a change. An extension is then free to
re-run an initialization logic, clean-up the site variable, or create
new state needed for a re-run of the `execute(site)` method.
@LightGuard
Copy link
Member Author

@aslakknutsen Care to play around with this and see if it will fit your needs?

@aslakknutsen
Copy link
Member

@LightGuard Will do. before/after sounds nice for a 'load/store cache'

@aslakknutsen
Copy link
Member

on_reload(site, changed_file) would be useful. Then we know what was updated and can clear disk cache.

@LightGuard
Copy link
Member Author

Ah. I was thinking you'd want a notification of the extension being rerun.
What you're talking about is more like reload_page. Also useful I would
think.

On Saturday, April 11, 2015, Aslak Knutsen notifications@github.com wrote:

on_reload(site, changed_file) would be useful. Then we know what was
updated and can clear disk cache.


Reply to this email directly or view it on GitHub
#462 (comment).

Jason Porter
http://en.gravatar.com/lightguardjp

@aslakknutsen
Copy link
Member

@LightGuard technically yes, but.. currently you're just calling on_reload on all extensions. And you don't really know which extension to reload, unless the extension it self, which you don't track, is being reloaded.

Any way.. the changed file could be a class/file used by an extension. At that point, only the extension it self knows if it needs to redo something based on this change.

@aslakknutsen
Copy link
Member

The naming is a bit off to me; before_pipeline_extension and after_pipeline_extension

It's part of the pipeline so it's not really before/after pipeline . Maybe just before/after?

@aslakknutsen
Copy link
Member

perhaps add support for the extension to register these hooks as we do with transformers:
https://github.com/LightGuard/awestruct/blob/feature/add_more_hooks/lib/awestruct/pipeline.rb#L33

module TestExtension

   class MyBigExtension

      def transform(transformers)
         transformers << MyTrans.new
      end

      def before(befores)
         befores << MyBefore.new
      end

      def after(afters)
         afters << MyAfter.new
      end

      def execute(site)

         # do something

      end
   end

   class MyTrans
      def transform(site, page, rendered)
         # wrap_page_with_custom_x(rendered)
      end
   end

   class MyBefore
      def execute(site)
         # site.my_data = load_data()
      end
   end

   class MyAfter
      def execute(site)
         # store_data(my_data)
      end
   end

end


Awestruct::Extensions::Pipeline.new do

   extension TestExtension::MyBigExtension.new

end

@aslakknutsen
Copy link
Member

We do currently workaround this by passing pipeline as an argument, .. which might be just as clean and easier to be honest.. It just looks a bit different registering one of these extensions compared with the others. e.g.

module TestExtension

   class MyBigExtension

      def initialize(pipeline)
         pipline.transformer MyTrans.new
         pipline.before MyBefore.new
         pipline.after MyAfter.new
         pipline.extension self
      end

      def execute(site)

         # do something

      end
   end

   class MyTrans
      def transform(site, page, rendered)
         # wrap_page_with_custom_x(rendered)
      end
   end

   class MyBefore
      def execute(site)
         # site.my_data = load_data()
      end
   end

   class MyAfter
      def execute(site)
         # store_data(my_data)
      end
   end

end


Awestruct::Extensions::Pipeline.new do

   TestExtension::MyBigExtension.new self

end

@LightGuard
Copy link
Member Author

Well, it actually is run before/after all the extensions, but I couldn't find a good name for it. Do you have any suggestions?

The new hooks (before_extensions, after_extensions, after_generation)
may now be added by including those methods on extensions and simply
including the extension as normal (`extension MyExtension.new`).
LightGuard added a commit that referenced this pull request Apr 21, 2015
@LightGuard LightGuard merged commit 402581a into awestruct:master Apr 21, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants