Skip to content

Instantly share code, notes, and snippets.

View vldvel's full-sized avatar

Vlad vldvel

  • Amsterdam
View GitHub Profile
function * generator() {
yield 1;
return 2;
yield 3; // we will never reach this yield
}
const gen = generator();
gen.next(); // {value: 1, done: false}
gen.next(); // {value: 2, done: true}
function * generator(arr) {
for (const el in arr)
yield el;
}
const gen = generator([0, 1, 2]);
for (const g of gen) {
console.log(g); // 0 -> 1 -> 2
}
function * generator() {
yield 1;
}
generator.prototype.__proto__; // Generator {constructor: GeneratorFunction, next: ƒ, return: ƒ, throw: ƒ, Symbol(Symbol.toStringTag): "Generator"}
// as Generator is not global variable we have to write something like this
generator.prototype.__proto__.math = function(e = 0) {
return e * Math.PI;
}
@vldvel
vldvel / Notification-request.js
Last active April 14, 2018 02:21
Notification Request API
Notification.requestPermission((permission) => {
switch (permission) {
case 'granted': {
console.log('Now we can send notifications!');
break;
}
case 'denied': {
console.log('User close the request pop-up!')
}
case 'default': {
@vldvel
vldvel / Notification-check.js
Created March 14, 2018 07:27
Notification check
if (!('Notification' in window)) {
// Place notification logic here
}
@vldvel
vldvel / Notification-request-promise.js
Created March 14, 2018 07:51
Notification request promise
Notification
.requestPermission()
.then((permission) => {
if (permission !== 'granted') throw new Error('No access!');
})
.then(() => {
console.log('We have an access!');
// Handle granted permission here
})
.catch((err) => {
@vldvel
vldvel / Notification-simple.js
Last active March 15, 2018 07:21
Notification simple
const title = 'I\'m notification!';
notification(title); // wrong - notification is not defined
Notification(title); // wrong - Failed to construct Notification
new Notification(title); // OK
const notification = new Notification(title); // OK
@vldvel
vldvel / Notification-options.js
Last active March 15, 2018 12:09
Notification with options
const notificationTitle = 'I\'m notification'; // string
const notificationOptions = {
tag: 'notification', // tag for notification
renotify: true, // defalt false, renotifying after old notification
body: 'Here is my desription', // description under title
icon: 'https://avatars1.githubusercontent.com/u/22643415', // image URL
vibrate: [50, 100, 150] // vibration on notification
}
@vldvel
vldvel / Notification-close.js
Created March 14, 2018 12:40
Notification close
const notificationProps = {
title: 'I\'m notification',
body: 'Here is my desription',
icon: 'https://avatars1.githubusercontent.com/u/22643415',
vibrate: [50, 100, 150]
}
const myNotification = new Notification(notificationProps.title, notificationProps);
function closeNotification(notifyID, time) {
@vldvel
vldvel / Notification-permission.js
Created March 14, 2018 13:18
Notification permission
switch (Notification.permission) {
case 'granted': {
new Notification('Hello world!');
break;
}
case 'denied': {
alert('No notification for you!');
break;
}
case 'default': {