Live Note

Remain optimistic

在使用 Android dev 开发 flutter 应用时,控制台会输出 GPU debug 日志:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[+1001 ms] D/EGL_emulation( 9434): app_time_stats: avg=1001.31ms min=1001.31ms max=1001.31ms count=1
[+1989 ms] D/EGL_emulation( 9434): app_time_stats: avg=994.95ms min=992.16ms max=997.75ms count=2
[+1996 ms] D/EGL_emulation( 9434): app_time_stats: avg=998.40ms min=994.74ms max=1002.06ms count=2
[+2005 ms] D/EGL_emulation( 9434): app_time_stats: avg=1002.85ms min=990.68ms max=1015.03ms count=2
[+1983 ms] D/EGL_emulation( 9434): app_time_stats: avg=991.95ms min=987.00ms max=996.89ms count=2
[+1016 ms] D/EGL_emulation( 9434): app_time_stats: avg=1015.37ms min=1015.37ms max=1015.37ms count=1
[+1002 ms] D/EGL_emulation( 9434): app_time_stats: avg=1002.23ms min=1002.23ms max=1002.23ms count=1
[+1002 ms] D/EGL_emulation( 9434): app_time_stats: avg=1003.33ms min=1003.33ms max=1003.33ms count=1
[+1008 ms] D/EGL_emulation( 9434): app_time_stats: avg=1005.76ms min=1005.76ms max=1005.76ms count=1
[+1997 ms] D/EGL_emulation( 9434): app_time_stats: avg=999.30ms min=994.23ms max=1004.37ms count=2
[+1998 ms] D/EGL_emulation( 9434): app_time_stats: avg=998.66ms min=998.60ms max=998.71ms count=2
[+2000 ms] D/EGL_emulation( 9434): app_time_stats: avg=1000.03ms min=998.15ms max=1001.91ms count=2
[+1998 ms] D/EGL_emulation( 9434): app_time_stats: avg=999.53ms min=999.27ms max=999.79ms count=2
[+1990 ms] D/EGL_emulation( 9434): app_time_stats: avg=995.32ms min=993.38ms max=997.25ms count=2
[+1001 ms] D/EGL_emulation( 9434): app_time_stats: avg=1001.47ms min=1001.47ms max=1001.47ms count=1
[+1001 ms] D/EGL_emulation( 9434): app_time_stats: avg=1001.27ms min=1001.27ms max=1001.27ms count=1
[+1004 ms] D/EGL_emulation( 9434): app_time_stats: avg=1004.90ms min=1004.90ms max=1004.90ms count=1
[+1005 ms] D/EGL_emulation( 9434): app_time_stats: avg=1005.88ms min=1005.88ms max=1005.88ms count=1
[+1979 ms] D/EGL_emulation( 9434): app_time_stats: avg=989.67ms min=979.52ms max=999.82ms count=2

这些日志并不影响应用的运行,但是会占用控制台的空间,可以通过以下方式忽略掉:

1
flutter run --dart-define=ENV=dev --verbose | grep -v "EGL_emulation"

Class in ES2015

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* Class representing a point.
*/
class Point {
/**
* Create a point.
* @param {number} x - The x value.
* @param {number} y - The y value.
*/
constructor(x, y) {}

/**
* Get the x value.
* @return {number} The x value.
*/
getX() {}

/**
* Convert a string containing two comma-separated number into a point.
* @param {string} str - The string containing two comma-separated number.
* @return {Point} A point object.
*/
static fromStringStr(str) {}
}

extends

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* Class representing a point.
* @extends Point
*/
class Dot extends Point {
/**
* Create a dot.
* @param {number} x - The x value.
* @param {number} y - The y value.
* @param {number} width - The width of the dot.
*/
constructor(x, y, width) {
super(x, y)
}

/**
* Get the width of the dot.
* @return {number} The dot's width.
*/
getWidth() {}
}

@abstract

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* Foo.
* @constructor
*/
function Foo() {}

/**
* Check if is solid.
* @abstract
* @return {boolean}
*/
Foo.prototype.isSolid = function () {
throw new Error("Must be implemented by suclass.")
}

/**
* Bar.
* @constructor
* @arguments Foo
*/
function Bar() {}

/**
* Check if is solid.
* @return {boolean} Always return false.
*/
Bar.prototype.isSolid = function () {
return false
}

@assets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* @constructor
*/
function Foo() {
/**
* @assets private
* @type {number}
*/
let foo = 0

/**
* @assets protected
* @type {string}
*/
this.bar = "1"

/**
* @assets package
* @type {string}
*/
this.barz = "2"

/**
* @assets public
* @type {string}
*/
this.barm = "3"
}

@author

1
2
3
4
/**
* @author Edward <wang.huiyang@outlook.com>
*/
function MyClass() {}

@callback

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* @class
*/
function Foo() {}

/**
* Send a request.
* @param {requestCallback} cb - The callback than handles the response.
*/
Foo.prototype.send = function (cb) {}

/**
* Callback
* @callback requestCallback
* @param {number} responseCode
* @param {string} responseMessage
*/

@event

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* @constructor
*/
function Foo() {}

/**
* Do some test.
* @fires Foo#test
*/
Foo.prototype.test = function () {}

/**
* Foo test.
* @event Foo#test
* @type {object}
* @property {boolean} isPass - Check if is pass.
*/

Iterator 概念

Iterator 是一种接口,为各种不同的数据结构提供统一的访问机制。任何数据结构,只要部署 Iterator 接口,就可以完成遍历操作。
Iterator 的主要作用:为数据结构提供统一的、简便的访问接口;使得数据结构的成员能够按照某种次序排列;供 for…of 消费。
遍历过程如下:

  1. 创建一个指针对象,指向当前数据结构的起始位置。
  2. 第一次调用指针对象的 next 方法,将指针指向数据结构的第一个成员。
  3. 第二次调用 next 方法,指向第二个成员。
  4. 不断调用 next 方法,直到指针指向数据结构的结束位置。

每次调用 next 方法都会返回数据结构当前成员的信息,返回一个包含 value 的 done 两个属性的对象。value 属性是当前成员的值,done 属性是一个布尔值,表示遍历是否结束。
模拟 next 方法返回值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var it = makeIterator(['a', 'b'])
it.next() // { value : 'a', done : false }
it.next() // { value : 'b', done : false }
it.next() // { value : undefined, done : true }
function makeIterator(array) {
var nextIndex = 0
return {
next: function () {
return nextIndex < array.length
? { value: array[nextIndex++], done: false }
: { value: undefined, done: true }
},
}
}

遍历器与所遍历的数据结构实际上是分开的,完全可以写出没有对应数据结构的遍历器对象,或者用遍历器对象模拟出数据结构。无限运行的遍历器对象的例子:

1
2
3
4
5
6
7
8
9
10
11
12
var it = idMaker()
it.next().value // 0
it.next().value // 1
//...
function idMaker() {
var index = 0
return {
next: function () {
return { value: index++, done: false }
},
}
}
Read more »

eval()

eval 可以解析任何字符串变成 JS:

1
2
3
var str = "function testFunction() {console.log('test');}"
eval(str)
testFunction() //'test'

JSON.parse()

JSON.parse 只能解析 JSON 形式的字符串变成 JS,安全性比 eval 高一些。
字符串中的属性要严格加上引号

1
2
3
let str = '{ "name" : "hello" }'
let json = JSON.parse(str)
json.name //'hello'
Read more »

文件类型

  • 普通文件
  • 目录文件
  • 链接文件:类似 Windows 的快捷方式,分软链接和硬链接
  • 设备文件:一般在/dev 目录下,一种是块设备文件,一种是字符设备文件

文件属性

-rwxrwxrwx

  • r: read
  • w: write
  • x: execute

第一个字符:

  • - : 普通文件
  • d : 目录文件
  • l : 链接文件
  • c : 字符设备
  • b : 块设备
  • p : 命名管道,如 FIFO
  • f : 堆栈文件,如 LIFO
  • s : 套接字

之后的三个三位字符组:

  1. 第一组代表文件拥有者(u)对该文件的权限
  2. 第二组代表文件用户组(g)对该文件的权限
  3. 第三组代表系统其它用户(o)对该文件的权限

文件系统类型

  • ext2 & ext3:ext3 是 ext2 的升级版本
  • swap:交换分区使用
  • vfat:DOS 中的系统(FAT12、FAT16 和 FAT32 等)
  • NFS:网络文件系统
  • ISO9660:光盘文件系统
Read more »