This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* addGen(sum){ | |
let first= yield sum; | |
sum+=first; | |
let second=yield sum; | |
sum+=second; | |
return sum; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* genRace(){ | |
yield 'first'; | |
yield 'second'; | |
yield 'third'; | |
return "stupid race"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* GenIterator(i=0,counts=10){ | |
let endpoint=i+counts; | |
while(i<endpoint){ | |
yield i++; | |
} | |
} | |
console.log([...GenIterator(24,7)]) // @returns : [24, 25, 26, 27, 28, 29, 30] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mystore.store("books") | |
.add([{book_name:"Eloquent JavaScript"},{book_name:"You don't know JS"}]) | |
.store("users") | |
.add({name:"javascript"}) | |
.then(()=> mystore.store("books").get({book_name:"Eloquent JavaScript"}) ) | |
.then(res=>{ | |
console.log(res.books); //The final result | |
}) | |
.catch(()=>{ | |
console.log("transaction failed"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request=db.transaction(["books","users"],"readwrite"); | |
var bookStore=request.objectStore("books"); | |
bookStore.add({book_name:"Eloquent JavaScript"}); | |
bookStore.add({book_name:"You don't know JS"}); | |
var userStore=request.objectStore("users"); | |
userStore.add({name:"javascript"}); | |
request.oncomplete=()=>{ | |
let getReq=db.transaction("books","readonly"); | |
let bookStore=getReq.objectStore("books"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SimpleStore from "simplestore-indexeddb"; | |
new SimpleStore("mystore").addStores([ | |
{ name: "books", | |
indices: ["book_name"] | |
}, | |
{ | |
name:"users" | |
} | |
]).then(res=>{ let mystore=res.simplestore; }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let openRequest = indexedDB.open("mystore", 3); | |
openRequest.onupgradeneeded = function(event) { | |
let db = openRequest.result; | |
switch(event.oldVersion) { // existing db version | |
case 1: | |
// do changes for update from version 1 to 3... | |
db.createObjectStore("books",{autoIncrement:true}); | |
case 2: | |
// do changes for update from version 2 to 3... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
off(eventname,callback){ | |
if(!this.hasOwnProperty(eventname))return false; | |
if(!(callback && callback.constructor===Function))return false; | |
if(Array.isArray(this[eventname])){ | |
this[eventname]=this[eventname].filter(eventcb=>eventcb!==callback); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
emit(eventname,...args){ | |
if(!this.hasOwnProperty(eventname))return false; | |
if(Array.isArray(this[eventname])){ | |
this[eventname].forEach(callback=>callback(...args)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
on(eventname,callback){ | |
if(!eventname)return false; | |
if(!(callback && callback.constructor===Function))return false; | |
if(!this[eventname]){ | |
this[eventname]=[]; | |
} | |
if(!this[eventname].includes(callback) && Array.isArray(this[eventname]) && this.hasOwnProperty(eventname)){ |