Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Development
[Electron + Angular + TypeScript] Venobo Streaming App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="MayoMayn" data-source="post: 406509" data-attributes="member: 71840"><p>Nah, you're definitely right.</p><p>Relevant or not, it still looks quite nice.</p><p>The design before just looked way too shady (and definitely illegal kinda)</p><p>[doublepost=1491871690,1491869553][/doublepost]<strong>#UPDATES</strong></p><p>I've decided to code an offline mode aswell as a caching system to get faster performance.</p><p>Everytime a HTTP requests gets made, it caches the data for 1 hour otherwise it caches new data, or if the computer is offline, it'll just use the latest cache.</p><p><strong>components/cache.js</strong></p><p>[PHP]</p><p>import config from '../../config'</p><p>import fs from 'fs'</p><p>import path from 'path'</p><p>import rimraf from 'rimraf'</p><p>import md5 from 'crypto-js/md5'</p><p></p><p>export default class Cache {</p><p></p><p> static write(key, data, callback) {</p><p> if(data instanceof String) data = data.toString()</p><p> if(typeof data != "string") data = JSON.stringify(data)</p><p></p><p> this.exists(key, (res) => {</p><p> if(res) this.purgeSync(key)</p><p></p><p> fs.writeFileSync(this.getPath(key), data, {flag: 'wx'})</p><p> callback(data)</p><p> })</p><p> }</p><p></p><p> static read(key, callback) {</p><p> let data = fs.readFileSync(this.getPath(key))</p><p> callback(data.toString())</p><p> }</p><p></p><p> static exists(key, callback) {</p><p> fs.stat(this.getPath(key), (err, stat) => {</p><p> callback(!err, stat)</p><p> })</p><p> }</p><p></p><p> static isExpired(key, callback) {</p><p> this.exists(key, (res, stat) => {</p><p> if(!res) {</p><p> callback(true)</p><p> } else {</p><p> let now = new Date().getTime(),</p><p> endTime = new Date(stat.ctime).getTime() + 3600000</p><p></p><p> callback(now > endTime)</p><p> }</p><p> })</p><p> }</p><p></p><p> static getPath(key) {</p><p> return path.join(config.CACHE_PATH, md5(key) + '.js')</p><p> }</p><p></p><p> static purge(key, callback) {</p><p> fs.unlink(this.getPath(key), callback)</p><p> }</p><p></p><p> static purgeSync(key) {</p><p> return fs.unlinkSync(this.getPath(key))</p><p> }</p><p></p><p> static trash(callback) {</p><p> rimraf(config.CACHE_PATH, callback)</p><p> }</p><p></p><p>}</p><p>[/PHP]</p><p></p><p><strong>components/http.js</strong></p><p>[php]</p><p>import Cache from './cache'</p><p>import Axios from 'axios'</p><p></p><p>export default class HTTP {</p><p></p><p> static get(url, callback) {</p><p> if(navigator.onLine) {</p><p> Axios.get(url)</p><p> .then((res) => {</p><p> Cache.isExpired(url, (is) => {</p><p> if(!is) {</p><p> Cache.read(url, callback)</p><p> } else {</p><p> Cache.write(url, res.data, callback)</p><p> }</p><p> })</p><p> })</p><p> .catch((err) => {</p><p> console.log(err)</p><p> this.parse(url, callback)</p><p> })</p><p> } else {</p><p> this.parse(url, callback)</p><p> }</p><p> }</p><p></p><p> static post() {</p><p></p><p> }</p><p></p><p> static parse(url, callback) {</p><p> Cache.exists(url, (res) => {</p><p> if(res) {</p><p> Cache.read(url, callback)</p><p> } else {</p><p> console.log("something went wrong with get requests, and cache doesnt exist")</p><p> }</p><p> })</p><p> }</p><p></p><p>}</p><p>[/php]</p><p></p><p>Still need some fixes but close being there.</p></blockquote><p></p>
[QUOTE="MayoMayn, post: 406509, member: 71840"] Nah, you're definitely right. Relevant or not, it still looks quite nice. The design before just looked way too shady (and definitely illegal kinda) [doublepost=1491871690,1491869553][/doublepost][B]#UPDATES[/B] I've decided to code an offline mode aswell as a caching system to get faster performance. Everytime a HTTP requests gets made, it caches the data for 1 hour otherwise it caches new data, or if the computer is offline, it'll just use the latest cache. [b]components/cache.js[/b] [PHP] import config from '../../config' import fs from 'fs' import path from 'path' import rimraf from 'rimraf' import md5 from 'crypto-js/md5' export default class Cache { static write(key, data, callback) { if(data instanceof String) data = data.toString() if(typeof data != "string") data = JSON.stringify(data) this.exists(key, (res) => { if(res) this.purgeSync(key) fs.writeFileSync(this.getPath(key), data, {flag: 'wx'}) callback(data) }) } static read(key, callback) { let data = fs.readFileSync(this.getPath(key)) callback(data.toString()) } static exists(key, callback) { fs.stat(this.getPath(key), (err, stat) => { callback(!err, stat) }) } static isExpired(key, callback) { this.exists(key, (res, stat) => { if(!res) { callback(true) } else { let now = new Date().getTime(), endTime = new Date(stat.ctime).getTime() + 3600000 callback(now > endTime) } }) } static getPath(key) { return path.join(config.CACHE_PATH, md5(key) + '.js') } static purge(key, callback) { fs.unlink(this.getPath(key), callback) } static purgeSync(key) { return fs.unlinkSync(this.getPath(key)) } static trash(callback) { rimraf(config.CACHE_PATH, callback) } } [/PHP] [b]components/http.js[/b] [php] import Cache from './cache' import Axios from 'axios' export default class HTTP { static get(url, callback) { if(navigator.onLine) { Axios.get(url) .then((res) => { Cache.isExpired(url, (is) => { if(!is) { Cache.read(url, callback) } else { Cache.write(url, res.data, callback) } }) }) .catch((err) => { console.log(err) this.parse(url, callback) }) } else { this.parse(url, callback) } } static post() { } static parse(url, callback) { Cache.exists(url, (res) => { if(res) { Cache.read(url, callback) } else { console.log("something went wrong with get requests, and cache doesnt exist") } }) } } [/php] Still need some fixes but close being there. [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Development
[Electron + Angular + TypeScript] Venobo Streaming App
Top