Skip to content

Commit

Permalink
add all non-ui method wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
wpcfan committed Jan 6, 2017
1 parent 4d491fe commit 811ee62
Show file tree
Hide file tree
Showing 10 changed files with 492 additions and 13 deletions.
22 changes: 19 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { xs } from './lib/xstream/index'
import { debounce } from './lib/xstream/extra/debounce'
import xs from './lib/xstream/index'
import debounce from './lib/xstream/extra/debounce'
import concat from './lib/xstream/extra/concat'
import dropRepeats from './lib/xstream/extra/dropRepeats'
import { http } from './wrappers/http'
import { event } from './wrappers/from-event'
import { image } from './wrappers/image'
import { socket } from './wrappers/socket'
import { file } from './wrappers/file'
import { record } from './wrappers/record'
import { voice } from './wrappers/voice'
import { audio } from './wrappers/audio'
import { video } from './wrappers/video'
import { storage } from './wrappers/storage'
import { device } from './wrappers/device'

module.exports = {
xs: xs,
Expand All @@ -13,5 +21,13 @@ module.exports = {
file: file,
image: image,
socket: socket,
debounce: debounce
record: record,
voice: voice,
audio: audio,
video: video,
storage: storage,
device: device,
debounce: debounce,
concat: concat,
dropRepeats: dropRepeats
}
122 changes: 122 additions & 0 deletions wrappers/audio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import xs from '../lib/xstream/index'

let audio = {}

audio.state = () => {
const producer = {
start: listener => {
wx.getBackgroundAudioPlayerState({
success: res => listener.next(res),
fail: res => listener.error(res),
complete: () => listener.complete()
})
},
stop: () => {}
}
return xs.create(producer)
}

audio.play = (dataUrl, title, coverImageUrl) => {
const producer = {
start: listener => {
wx.playBackgroundAudio({
dataUrl: dataUrl,
title: title,
coverImageUrl: coverImageUrl,
success: res => listener.next(res),
fail: res => listener.error(res),
complete: () => listener.complete()
})
},
stop: () => {}
}
return xs.create(producer)
}

audio.pause = () => {
const producer = {
start: listener => {
wx.pauseBackgroundAudio()
listener.next(null) //just need a signal
listener.complete()
},
stop: () => {}
}
return xs.create(producer)
}

audio.seek = (position) => {
const producer = {
start: listener => {
wx.playBackgroundAudio({
position: position,
success: res => listener.next(res),
fail: res => listener.error(res),
complete: () => listener.complete()
})
},
stop: () => {}
}
return xs.create(producer)
}

audio.stop = () => {
const producer = {
start: listener => {
wx.stopBackgroundAudio()
listener.next(null) //just need a signal
listener.complete()
},
stop: () => {}
}
return xs.create(producer)
}

audio.createContext = (audioId) => {
const producer = {
start: listener => {
let ctx = wx.createAudioContext(audioId)
listener.next(ctx)
listener.complete()
},
stop: () => {}
}
return xs.create(producer)
}

audio.onPlay = (callback) => {
const producer = {
start: listener => {
wx.onBackgroundAudioPlay(callback)
listener.next(null)
},
stop: () => {}
}
return xs.create(producer)
}

audio.onPause = (callback) => {
const producer = {
start: listener => {
wx.onBackgroundAudioPause(callback)
listener.next(null)
},
stop: () => {}
}
return xs.create(producer)
}

audio.onStop = (callback) => {
const producer = {
start: listener => {
wx.onBackgroundAudioStop(callback)
listener.next(null)
},
stop: () => {}
}
return xs.create(producer)
}

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

let device = {}

device.sysInfo = () => {
const producer = {
start: listener => {
wx.getSystemInfo({
success: res => listener.next(res),
fail: res => listener.error(res),
complete: () => listener.complete()
})
},
stop: () => {}
}
return xs.create(producer)
}

device.network = () => {
const producer = {
start: listener => {
wx.getNetworkType({
success: res => listener.next(res),
fail: res => listener.error(res),
complete: () => listener.complete()
})
},
stop: () => {}
}
return xs.create(producer)
}

device.onAccChange = () => {
const producer = {
start: listener => {
wx.onAccelerometerChange(res => {
listener.next(res) //it seems an infinite stream so it never ends
})
},
stop: () => {}
}
return xs.create(producer)
}

device.onCompChange = () => {
const producer = {
start: listener => {
wx.onAccelerometerChange(res => {
listener.next(res) //it seems an infinite stream so it never ends
})
},
stop: () => {}
}
return xs.create(producer)
}

device.dial = (phoneNumber) => {
const producer = {
start: listener => {
wx.makePhoneCall({
phoneNumber: phoneNumber,
success: res => listener.next(res),
fail: res => listener.error(res),
complete: () => listener.complete()
})
},
stop: () => {}
}
return xs.create(producer)
}

device.scan = () => {
const producer = {
start: listener => {
wx.scanCode({
success: res => listener.next(res),
fail: res => listener.error(res),
complete: () => listener.complete()
})
},
stop: () => {}
}
return xs.create(producer)
}

module.exports = {
device: device
}
63 changes: 60 additions & 3 deletions wrappers/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ file.save = (tempFilePath) => {
start: listener => {
wx.saveFile({
tempFilePath: tempFilePath,
success: function(res) {
listener.next(res)
},
success: res => listener.next(res),
fail: () => listener.error('file cannot be saved'),
complete: () => listener.complete()
})
Expand All @@ -19,6 +17,65 @@ file.save = (tempFilePath) => {
return xs.create(producer)
}

file.list = () => {
const producer = {
start: listener => {
wx.getSavedFileList({
success: res => listener.next(res),
fail: res => listener.error(res),
complete: () => listener.complete()
})
},
stop: () => {}
}
return xs.create(producer)
}

file.info = (filePath) => {
const producer = {
start: listener => {
wx.getSavedFileInfo({
filePath: filePath,
success: res => listener.next(res),
fail: res => listener.error(res),
complete: () => listener.complete()
})
},
stop: () => {}
}
return xs.create(producer)
}

file.remove = (filePath) => {
const producer = {
start: listener => {
wx.removeSavedFile({
filePath: filePath,
success: res => listener.next(res),
fail: res => listener.error(res),
complete: () => listener.complete()
})
},
stop: () => {}
}
return xs.create(producer)
}

file.open = (filePath) => {
const producer = {
start: listener => {
wx.openDocument({
filePath: filePath,
success: res => listener.next(res),
fail: res => listener.error(res),
complete: () => listener.complete()
})
},
stop: () => {}
}
return xs.create(producer)
}

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

let record = {}

record.start = () => {
const producer = {
start: listener => {
wx.startRecord({
success: res => listener.next(res),
fail: res => listener.error(res),
complete: () => listener.complete()
})
},
stop: () => {}
}
return xs.create(producer)
}

record.stop = () => {
const producer = {
start: listener => {
wx.stopRecord({
success: res => listener.next(res),
fail: res => listener.error(res),
complete: () => listener.complete()
})
},
stop: () => {}
}
return xs.create(producer)
}

module.exports = {
record: record
}
11 changes: 4 additions & 7 deletions wrappers/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ socket.connect = (url, data={}, header={}, method=CONN_METHOD.GET) => {
header: header,
method: method,
success: () => {
wx.onSocketOpen(res => {
listener.next(res)
})
wx.onSocketError(res => {
listener.error('socket connection cannot open and response is: ' + res)
})
wx.onSocketOpen(res => listener.next(res))
wx.onSocketError(res => listener.error(res))
},
fail: () => listener.error('connect failed'),
complete: () => listener.complete()
Expand Down Expand Up @@ -70,7 +66,7 @@ socket.onMsg = () => {
const producer = {
start: listener => {
wx.onSocketMessage(res => {
listener.next(res)
listener.next(res) // it should be an infinite stream
})
},
stop: () => {}
Expand All @@ -84,6 +80,7 @@ socket.close = () => {
wx.closeSocket()
wx.onSocketClose(res => {
listener.next(res)
listener.complete()
})
},
stop: () => {}
Expand Down
Loading

0 comments on commit 811ee62

Please sign in to comment.