Permalink
Browse files

Using native promises

  • Loading branch information...
nvh committed Sep 7, 2017
1 parent 865771b commit 48e7b780111dd6d3762204cd05c76724397d4a0c
Showing with 28 additions and 50 deletions.
  1. +26 −50 framer/Utils.coffee
  2. +1 −0 package.json
  3. +1 −0 test/tests/UtilsTest.coffee
View
@@ -524,64 +524,40 @@ Utils.loadWebFontConfig = (config) ->
if allLoadedResult?
return allLoadedResult
promise =
resolved: false
error: null
resolve: null
reject: null
'then': (cb) ->
@resolve = cb
@resolve?() if @resolved
return @
'catch': (cb) ->
@reject = cb
@reject?(@error) if @error?
return @
customActive = config.active
customInactive = config.inactive
customFontactive = config.fontactive
customFontinactive = config.fontinactive
resolvePromise = ->
promise.resolve?()
promise.resolved = true
rejectPromise = (error) ->
promise.reject?(error)
promise.error = error
config.fontactive = (font) ->
_isFontLoadedResults[font] = true
customFontactive?(font)
if fonts.length is 1
f = resolvePromise
resolvePromise = null
f()
config.fontinactive = (font) ->
console.warn("Tried to load unavailable font: '#{font}'")
_isFontLoadedResults[font] = false
customFontinactive?(font)
if fonts.length is 1
f = rejectPromise
rejectPromise = null
error = new Error("#{font} failed to load")
f(error)
config.active = ->
customActive?()
resolvePromise?()
config.inactive = ->
customInactive?()
error = new Error("#{fonts.join(', ')} failed to load")
rejectPromise?(error)
WebFont.load config
promise = new Promise (resolve, reject) ->
config.fontactive = (font) ->
_isFontLoadedResults[font] = true
customFontactive?(font)
if fonts.length is 1
resolve()
config.fontinactive = (font) ->
console.warn("Tried to load unavailable font: '#{font}'")
_isFontLoadedResults[font] = false
customFontinactive?(font)
if fonts.length is 1
error = new Error("#{font} failed to load")
reject(error)
config.active = ->
customActive?()
resolve()
config.inactive = ->
customInactive?()
error = new Error("#{fonts.join(', ')} failed to load")
reject(error)
WebFont.load config
return promise
# Load fonts from Google Web Fonts
Utils.loadWebFont = (font, weight, source = "google") ->
if not _isFontLoadedResults[font]? or _isFontLoadedResults[font] is false
delete _isFontLoadedResults[font]
View
@@ -21,6 +21,7 @@
"coffee-coverage": "^2.0.1",
"coffee-loader": "^0.7.2",
"coffee-script": "^1.9.1",
"es6-promise": "^4.1.1",
"eventemitter3": "^1.1.1",
"gulp": "^3.8.11",
"gulp-coffeelint": "^0.6.0",
@@ -1,3 +1,4 @@
require('es6-promise').polyfill()
assert = require "assert"
describe "Utils", ->

0 comments on commit 48e7b78

Please sign in to comment.