Skip to content

Commit

Permalink
add a few more wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
wpcfan committed Jan 5, 2017
1 parent 2b0a7cb commit 50f152f
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 5 deletions.
24 changes: 24 additions & 0 deletions wrappers/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import xs from '../lib/xstream/index'

let file = {}

file.save = (tempFilePath) => {
const producer = {
start: listener => {
wx.saveFile({
tempFilePath: tempFilePath,
success: function(res) {
listener.next(res)
},
fail: () => listener.error('file cannot be saved'),
complete: () => listener.complete()
})
},
stop: () => {}
}
return xs.create(producer)
}

module.exports = {
file: file
}
17 changes: 12 additions & 5 deletions wrappers/http.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import xs from '../lib/xstream/index'

const REQ_METHOD = {
GET: 'GET',
POST: 'POST',
PUT: 'PUT',
DELETE: 'DELETE'
}

let http = {}

http.get = (url, data={}, header={'content-type': 'application/json'}) => {
return http_request(url, 'GET', data, header)
return http_request(url, REQ_METHOD.GET, data, header)
}

http.post = (url, data={}, header={'content-type': 'application/json'}) => {
return http_request(url, 'POST', data, header)
return http_request(url, REQ_METHOD.POST, data, header)
}

http.put = (url, data={}, header={'content-type': 'application/json'}) => {
return http_request(url, 'PUT', data, header)
return http_request(url, REQ_METHOD.PUT, data, header)
}

http.delete = (url, data={}, header={'content-type': 'application/json'}) => {
return http_request(url, 'DELETE', data, header)
return http_request(url, REQ_METHOD.DELETE, data, header)
}

function http_request(
url,
method='GET',
method=REQ_METHOD.GET,
data={},
header={'content-type': 'application/json'}) {
const producer = {
Expand Down
55 changes: 55 additions & 0 deletions wrappers/image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import xs from '../lib/xstream/index'

let image = {}

image.choose = (count=9, size=['original', 'compressed'], soure=['album', 'camera']) => {
const producer = {
start: listener => {
wx.chooseImage({
count: count,
sizeType: size,
sourceType: soure,
success: res => listener.next(res),
fail: () => listener.error('image choosing failed'),
complete: () => listener.complete()
})
},
stop: () => {}
}
return xs.create(producer)
}

image.preview = (urls=[], current='') => {
const producer = {
start: listener => {
wx.previewImage({
current: current,
urls: urls,
success: res => listener.next(res),
fail: () => listener.error('image preview failed'),
complete: () => listener.complete()
})
},
stop: () => {}
}
return xs.create(producer)
}

image.info = (src='') => {
const producer = {
start: listener => {
wx.getImageInfo({
src: src,
success: res => listener.next(res),
fail: () => listener.error('image preview failed'),
complete: () => listener.complete()
})
},
stop: () => {}
}
return xs.create(producer)
}

module.exports = {
image: image
}
96 changes: 96 additions & 0 deletions wrappers/socket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import xs from '../lib/xstream/index'

const CONN_METHOD = {
OPTIONS: 'OPTIONS',
GET: 'GET',
HEAD: 'HEAD',
POST: 'POST',
PUT: 'PUT',
DELETE: 'DELETE',
TRACE: 'TRACE',
CONNECT: 'CONNECT'
}

let socket = {
CONN_METHOD: CONN_METHOD
}

socket.connect = (url, data={}, header={}, method=CONN_METHOD.GET) => {
const producer = {
start: listener => {
wx.connectSocket({
url: url,
data: data,
header: header,
method: method,
success: () => {
wx.onSocketOpen(res => {
listener.next(res)
})
wx.onSocketError(res => {
listener.error('socket connection cannot open and response is: ' + res)
})
},
fail: () => listener.error('connect failed'),
complete: () => listener.complete()
})
},
stop: () => {}
}
return xs.create(producer)
}


socket.sendMsg = (data) => {
const producer = {
start: listener => {
const isArray = Array.isArray(data)
if(isArray){
data.map(element => wx.sendSocketMessage({
element,
success: () => listener.next(element),
fail: () => listener.error('sending ' + element + ' failed'),
complete: () => listener.complete()
}))
} else {
wx.sendSocketMessage({
data,
success: () => listener.next(data),
fail: () => listener.error('sending ' + data + ' failed'),
complete: () => listener.complete()
})
}
},
stop: () => {}
}
return xs.create(producer)
}

socket.onMsg = () => {
const producer = {
start: listener => {
wx.onSocketMessage(res => {
listener.next(res)
})
},
stop: () => {}
}
return xs.create(producer)
}

socket.close = () => {
const producer = {
start: listener => {
wx.closeSocket()
wx.onSocketClose(res => {
listener.next(res)
})
},
stop: () => {}
}
return xs.create(producer)
}

module.exports = {
socket: socket
}
6 changes: 6 additions & 0 deletions wxstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ import xs from './lib/xstream/index'
import debounce from './lib/xstream/extra/debounce'
import http from './wrappers/http'
import fromEvent from './wrappers/from-event'
import image from './wrappers/image'
import socket from './wrappers/socket'
import file from './wrappers/file'

module.exports = {
xs: xs,
http: http,
fromEvent: fromEvent,
file: file,
image: image,
socket: socket,
debounce: debounce
}

0 comments on commit 50f152f

Please sign in to comment.