Skip to content

Commit

Permalink
Closes #8. Add debouncing for rendering the preview.
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavin001 committed Jul 17, 2014
1 parent bcd3c42 commit 458ae83
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,25 @@ Or Settings/Preferences ➔ Packages ➔ Search for `coffeescript-preview`
- [x] [Updates on Tab Change](https://github.com/Glavin001/atom-coffeescript-preview/issues/3)
- [x] [Highlights using active Atom theme](https://github.com/Glavin001/atom-coffeescript-preview/issues/5)

## Package Settings

- `Refresh Debounce Period` (milliseconds) -
Set the debounce rate for preview refreshing.
For instance, if you type or switch tabs,
how long of a pause before the preview refreshes.
- `Update On Tab Change` (boolean) -
Should the preview update to the currently active tab?

## Screenshots

### Preview CoffeeScript as JavaScript

![screenshot](https://raw.githubusercontent.com/Glavin001/atom-coffeescript-preview/master/screenshot.png)

### Syncing with Tab Changes

![screencapture](https://cloud.githubusercontent.com/assets/1885333/3576573/99212e10-0b93-11e4-8cd5-9da29e9230dd.gif)


[npm]: https://www.npmjs.org/package/generator-atom-package
[atom-doc]: https://atom.io/docs/latest/creating-a-package "Official documentation"
12 changes: 9 additions & 3 deletions lib/coffeescript-preview-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class CoffeePreviewView extends ScrollView
atom.config.observe 'editor.fontSize', () =>
@changeHandler()

# Setup debounced renderer
atom.config.observe 'coffeescript-preview.refreshDebouncePeriod', \
(wait) =>
@debouncedRenderHTMLCode = _.debounce @renderHTMLCode.bind(@), wait

serialize: ->
deserializer: 'CoffeePreviewView'
filePath: @getPath()
Expand Down Expand Up @@ -87,7 +92,6 @@ class CoffeePreviewView extends ScrollView
null

handleTabChanges: =>
console.log 'changed tab'
updateOnTabChange =
atom.config.get 'coffeescript-preview.updateOnTabChange'
if updateOnTabChange
Expand Down Expand Up @@ -120,9 +124,11 @@ class CoffeePreviewView extends ScrollView
renderHTML: ->
@showLoading()
if @editor?
@renderHTMLCode(@editor.getText())
@debouncedRenderHTMLCode()

renderHTMLCode: (coffeeText) =>
renderHTMLCode: () =>
# console.log 'renderHTMLCode'
coffeeText = @editor.getText()
try
# Compile CoffeeScript into JavaScript
text = coffeescript.compile coffeeText
Expand Down
2 changes: 1 addition & 1 deletion lib/coffeescript-preview.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CoffeePreviewView = require './coffeescript-preview-view'
module.exports =
configDefaults:
updateOnTabChange: true
refreshDebouncePeriod: 100

coffeePreviewView: null

Expand Down Expand Up @@ -63,7 +64,6 @@ module.exports =
return unless editor?
uri = "coffeescript-preview://editor"
previewPane = atom.workspace.paneForUri(uri)
console.log previewPane
if previewPane
previewPane.destroyItem(previewPane.itemForUri(uri))
return
Expand Down

0 comments on commit 458ae83

Please sign in to comment.