Merge pull request #59 from hundredrabbits/async

refactor for async
This commit is contained in:
Лu Лinveгa 2019-07-17 10:31:38 +12:00 committed by GitHub
commit 6b77402544
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 74 additions and 98 deletions

View File

@ -1,7 +1,6 @@
function Library (ronin) { function Library (ronin) {
this.open = (path, callback) => { this.open = async (path) => {
ronin.surface.open(path, callback) return ronin.surface.open(path)
return path
} }
this.export = (path, type = 'image/png', quality = 1.0) => { this.export = (path, type = 'image/png', quality = 1.0) => {
@ -12,27 +11,24 @@ function Library (ronin) {
return path return path
} }
this.draw = (path, rect, callback) => { this.draw = async (path, rect) => {
const img = new Image() const img = new Image()
img.src = path img.src = path
ronin.surface.draw(img, rect, callback) return ronin.surface.draw(img, rect)
return rect
} }
this.resize = (w = 1, h = 1, callback) => { this.resize = async (w = 1, h = 1) => {
const rect = w <= 1 || h <= 1 ? { x: 0, y: 0, w: this.frame().w * w, h: this.frame().h * h } : { x: 0, y: 0, w, h } const rect = w <= 1 || h <= 1 ? { x: 0, y: 0, w: this.frame().w * w, h: this.frame().h * h } : { x: 0, y: 0, w, h }
const a = document.createElement('img') const a = document.createElement('img')
const b = document.createElement('img') const b = document.createElement('img')
a.src = ronin.surface.el.toDataURL() a.src = ronin.surface.el.toDataURL()
ronin.surface.resizeImage(a, b) ronin.surface.resizeImage(a, b)
ronin.surface.resize(rect, true) ronin.surface.resize(rect, true)
ronin.surface.draw(b, rect, callback) return ronin.surface.draw(b, rect)
return rect
} }
this.crop = (rect, callback) => { this.crop = async (rect) => {
ronin.surface.crop(rect, callback) return ronin.surface.crop(rect)
return rect
} }
this.folder = (path = ronin.source.path) => { this.folder = (path = ronin.source.path) => {
@ -79,13 +75,22 @@ function Library (ronin) {
// Arrays // Arrays
this.map = (fn, arr) => { this.map = async (fn, arr) => {
return arr.map(fn) return Promise.all(arr.map(fn))
} }
this.filter = (fn, arr) => { this._filter = (fn, arr) => {
return arr.filter(fn) return arr.filter(fn)
} }
this.filter= (fn, arr) => {
const list = Array.from(arr);
return Promise.all(list.map((element, index) => fn(element, index, list)))
.then(result => {
return list.filter((_, index) => {
return result[index];
});
});
}
this.reduce = (fn, arr, acc = 0) => { this.reduce = (fn, arr, acc = 0) => {
return arr.reduce(fn, acc) return arr.reduce(fn, acc)

View File

@ -50,7 +50,7 @@ function Lisp (input, lib) {
// docstring // docstring
console.log(input[2].value) console.log(input[2].value)
} }
context.scope[identifier] = function () { context.scope[identifier] = async function () {
const lambdaArguments = arguments const lambdaArguments = arguments
const lambdaScope = argumentNames.reduce(function (acc, x, i) { const lambdaScope = argumentNames.reduce(function (acc, x, i) {
acc[x.value] = lambdaArguments[i] acc[x.value] = lambdaArguments[i]
@ -60,7 +60,7 @@ function Lisp (input, lib) {
} }
}, },
lambda: function (input, context) { lambda: function (input, context) {
return function () { return async function () {
const lambdaArguments = arguments const lambdaArguments = arguments
const lambdaScope = input[1].reduce(function (acc, x, i) { const lambdaScope = input[1].reduce(function (acc, x, i) {
acc[x.value] = lambdaArguments[i] acc[x.value] = lambdaArguments[i]
@ -69,23 +69,23 @@ function Lisp (input, lib) {
return interpret(input[2], new Context(lambdaScope, context)) return interpret(input[2], new Context(lambdaScope, context))
} }
}, },
if: function (input, context) { if: async function (input, context) {
if (interpret(input[1], context)) { if (await interpret(input[1], context)) {
return interpret(input[2], context) return interpret(input[2], context)
} }
return input[3] ? interpret(input[3], context) : [] return input[3] ? interpret(input[3], context) : []
} }
} }
const interpretList = function (input, context) { const interpretList = async function (input, context) {
if (input.length > 0 && input[0].value in special) { if (input.length > 0 && input[0].value in special) {
return special[input[0].value](input, context) return special[input[0].value](input, context)
} }
const list = input.map(function (x) { return interpret(x, context) }) const list = await Promise.all(input.map(function (x) { return interpret(x, context) }))
return list[0] instanceof Function ? list[0].apply(undefined, list.slice(1)) : list return list[0] instanceof Function ? list[0].apply(undefined, list.slice(1)) : list
} }
const interpret = function (input, context) { const interpret = async function (input, context) {
if (!input) { console.warn('error', context.scope); return null } if (!input) { console.warn('error', context.scope); return null }
if (context === undefined) { if (context === undefined) {
@ -134,7 +134,7 @@ function Lisp (input, lib) {
return parenthesize(tokenize(input)) return parenthesize(tokenize(input))
} }
this.toPixels = function () { this.toPixels = async function () {
return interpret(this.parse(input)) return interpret(this.parse(input))
} }
} }

View File

@ -47,8 +47,8 @@ function Ronin () {
this.theme.reset() this.theme.reset()
} }
this.log = function (msg) { this.log = function (...msg) {
console.log(msg) console.log(...msg)
this.commander.setStatus(msg.reduce((acc, val) => { return acc + val + ' ' }, '')) this.commander.setStatus(msg.reduce((acc, val) => { return acc + val + ' ' }, ''))
} }

View File

@ -106,7 +106,8 @@ function Surface (ronin) {
// IO // IO
this.open = function (path, callback = () => {}) { this.open = function (path) {
return new Promise(resolve => {
const img = new Image() const img = new Image()
img.src = path img.src = path
img.onload = () => { img.onload = () => {
@ -114,20 +115,19 @@ function Surface (ronin) {
const rect = { x: 0, y: 0, w: img.width, h: img.height } const rect = { x: 0, y: 0, w: img.width, h: img.height }
this.resize(rect, true) this.resize(rect, true)
this.context.drawImage(img, 0, 0, img.width, img.height) this.context.drawImage(img, 0, 0, img.width, img.height)
if (typeof callback === 'function') { resolve()
callback()
}
} }
})
} }
this.draw = function (img, rect = this.getFrame(), callback = () => {}) { this.draw = function (img, rect = this.getFrame()) {
return new Promise(resolve => {
img.onload = () => { img.onload = () => {
ronin.log(`Draw ${img.width}x${img.height}`) ronin.log(`Draw ${img.width}x${img.height}`)
this.context.drawImage(img, rect.x, rect.y, rect.w, rect.h) // no strect: img.height * (rect.w / img.width) this.context.drawImage(img, rect.x, rect.y, rect.w, rect.h) // no strect: img.height * (rect.w / img.width)
if (typeof callback === 'function') { resolve()
callback()
}
} }
});
} }
this.crop = function (rect) { this.crop = function (rect) {

View File

@ -2,15 +2,6 @@
( (
(clear) (clear)
(open "../static/crystal.jpg")
; Filter (crop (rect 100 100 400 400))
(def filter-action
(lambda () (crop (rect 100 100 400 400))))
; Draw photo
(open
"../../PREVIEW.jpg"
filter-action)
) )

View File

@ -3,7 +3,7 @@
( (
(clear) (clear)
; Filter ; Glitch
(defn glitch (defn glitch
(rec) (rec)
@ -19,6 +19,7 @@
(draw (draw
"../static/crystal.jpg" "../static/crystal.jpg"
(rect 0 0 400 400) (rect 0 0 400 400))
(lambda () (glitch 2000)))
(glitch 500)
) )

View File

@ -1,13 +1,9 @@
; scale file ; scale file
( (
; Filter
(def filter-action (open "../static/crystal.jpg")
(lambda () (pixels (pixels
(frame) (frame)
saturation saturation
0.5) 12)
))
(open (path "/Users/VillaMoirai/Desktop/clip.jpg") filter-action)
) )

View File

@ -2,20 +2,11 @@
( (
(clear) (clear)
(draw
; Filter "../../PREVIEW.jpg"
(frame))
(def filter-action (pixels
(lambda () (pixels
(rect 0 0 500 500) (rect 0 0 500 500)
saturation saturation
0.5) 0.5)
))
; Draw photo
(draw
"../../PREVIEW.jpg"
(frame)
filter-action)
) )

View File

@ -6,15 +6,16 @@
(defn place (defn place
(rec) (rec)
(if (gt rec 0) (if (gt rec 0)
((draw "../static/crystal.jpg" (
(draw "../static/crystal.jpg"
(rect (rect
(random 200) (random 200)
(random 200) (random 200)
(random 200) (random 200)
(random 200)) (random 200)))
(lambda () (place (sub rec 1))) (place (sub rec 1))
))) ))
) )
(place 30) (place 30)
) )

View File

@ -2,15 +2,6 @@
( (
(clear) (clear)
(open "../../PREVIEW.jpg")
; Filter (resize 0.5 0.5)
(def filter-action
(lambda () (resize 0.5 0.5)))
; Draw photo
(open
"../../PREVIEW.jpg"
filter-action)
) )