|
| 1 | +/** |
| 2 | + * [main.js] |
| 3 | + * const cookieVal = $persistentStore.read(cookieKey) |
| 4 | + * => |
| 5 | + * const chavy = init() |
| 6 | + * const cookieVal = chavy.getdata(cookieKey) |
| 7 | + * |
| 8 | + * $httpClient.get => chavy.get |
| 9 | + * $notification.post => chavy.msg |
| 10 | + * console.log => chavy.log |
| 11 | + * $done({}) => chavy.done() |
| 12 | + * |
| 13 | + * [main.cookie.js] |
| 14 | + * const cookieVal = $request.headers['Cookie'] |
| 15 | + * => |
| 16 | + * const chavy = init() |
| 17 | + * const cookieVal = $request.headers['Cookie'] |
| 18 | + * |
| 19 | + * $persistentStore.write => chavy.setdata |
| 20 | + * $notification.post => chavy.msg |
| 21 | + * console.log => chavy.log |
| 22 | + * $done({}) => chavy.done() |
| 23 | + */ |
| 24 | + |
| 25 | +function init() { |
| 26 | + isSurge = () => { |
| 27 | + return undefined === this.$httpClient ? false : true |
| 28 | + } |
| 29 | + isQuanX = () => { |
| 30 | + return undefined === this.$task ? false : true |
| 31 | + } |
| 32 | + getdata = (key) => { |
| 33 | + if (isSurge()) return $persistentStore.read(key) |
| 34 | + if (isQuanX()) return $prefs.valueForKey(key) |
| 35 | + } |
| 36 | + setdata = (key, val) => { |
| 37 | + if (isSurge()) return $persistentStore.write(key, val) |
| 38 | + if (isQuanX()) return $prefs.setValueForKey(key, val) |
| 39 | + } |
| 40 | + msg = (title, subtitle, body) => { |
| 41 | + if (isSurge()) $notification.post(title, subtitle, body) |
| 42 | + if (isQuanX()) $notify(title, subtitle, body) |
| 43 | + } |
| 44 | + log = (message) => console.log(message) |
| 45 | + get = (url, cb) => { |
| 46 | + if (isSurge()) { |
| 47 | + $httpClient.get(url, cb) |
| 48 | + } |
| 49 | + if (isQuanX()) { |
| 50 | + url.method = 'GET' |
| 51 | + $task.fetch(url).then((resp) => cb(null, {}, resp.body)) |
| 52 | + } |
| 53 | + } |
| 54 | + post = (url, cb) => { |
| 55 | + if (isSurge()) { |
| 56 | + $httpClient.post(url, cb) |
| 57 | + } |
| 58 | + if (isQuanX()) { |
| 59 | + url.method = 'POST' |
| 60 | + $task.fetch(url).then((resp) => cb(null, {}, resp.body)) |
| 61 | + } |
| 62 | + } |
| 63 | + done = (value = {}) => { |
| 64 | + $done(value) |
| 65 | + } |
| 66 | + return { isSurge, isQuanX, msg, log, getdata, setdata, get, post, done } |
| 67 | +} |
0 commit comments