gzqby / asprogramer Goto Github PK
View Code? Open in Web Editor NEW博客-编程之旅
博客-编程之旅
Copies non-react specific statics from a child component to a parent component. Similar to Object.assign, but with React static keywords blacklisted from being overridden.
--used by react-redux
This package allows you to test arbitrary values and see if they're a particular React element type.
-- by react
// co(gen)接受generator,是函数绑定this
if (typeof gen === 'function') gen = gen.apply(ctx, args);
// onFulfilled();执行
function onFulfilled(res) {
var ret;
try {
ret = gen.next(res);
} catch (e) {
return reject(e);
}
next(ret);
return null;
}
function next(ret) {
// 判断结束
if (ret.done) return resolve(ret.value);
// 转promise
var value = toPromise.call(ctx, ret.value);
// 再执行onFulfilled
if (value && isPromise(value)) return value.then(onFulfilled, onRejected);
// 以上为正常执行的处理
return onRejected(new TypeError('You may only yield a function, promise, generator, array, or object, '
+ 'but the following object was passed: "' + String(ret.value) + '"'));
}
function toPromise(obj) {
if (!obj) return obj;
if (isPromise(obj)) return obj;
if (isGeneratorFunction(obj) || isGenerator(obj)) return co.call(this, obj);
if ('function' == typeof obj) return thunkToPromise.call(this, obj);
if (Array.isArray(obj)) return arrayToPromise.call(this, obj);
if (isObject(obj)) return objectToPromise.call(this, obj);
// 者一步应该是返回promise.resolve(obj)才更符合async函数的形式
return obj;
}
暂时只是写了几个小项目,并没有总结
#JavaScript 中的 DILOS 原则
高级模块必须依赖低级模块。依赖实体而不是抽象。
function hipAPI(url, httpMethod) {
...
}
hipAPI('http://url.com', 'get');
function hipDifferentAPI(type, httpMethod) {
if(type instanceof initialLoad) {
}else if(type instanceof naBar) {
}else {
}
}
强迫客户端依赖它们不用的 [代码]。
写的函数越少越好。把什么东西封装在一个放在其他地方的新函数里,并抽象化它的逻辑?可别这么干。怎么让人犯迷糊怎么来,需要代码的时候复制粘贴过来就行。
软件各部分的子级和父级不可以互换。
DRY。因为如果我们不遵循里氏分离原则,我们就会构建准确而健壮的继承链。将逻辑抽象为封装好的 base 原型 / 对象。我们还会对原型链中的不同方法按逻辑分组,它们的特定覆盖会让代码路径更加可预期和可发现。
对象应对修改开放,对扩展封闭。
function makeSound(animal) {
if(animal === "dog"){
...
}else if(animal === "cat") {
...
}else if(animal === "crow") {
...
}else if(animal === "sheep") {
...
}else if(animal === "cow") {
...
}else if(animal === "pig") {
...
}else{
...
}
}
确保你的函数 / 对象有多重职责。
优秀的编程人员常常会将他们的代码分成多个不同的对象或模块。但我可搞不清楚这种事情,我记不住它们都负责什么事情。
这种反模式有时被称为“瑞士军刀”,因为就算你要的只是一把剪子,但它也可以是指甲锉、锯子、镊子、开瓶器,也可以是软木钉。
形成参数配置—args2存在opt的选项的话复制到opt
if (arguments.length == 2) {
utils.shallowCopyFromList(opts, data, _OPTS_PASSABLE_WITH_DATA);
}
处理cache—有cache,取,存以template和opt形成的compile
if (options.cache) {
if (!filename) {
throw new Error('cache option requires a filename');
}
func = exports.cache.get(filename);
if (func) {
return func;
}
if (!hasTemplate) {
template = fileLoader(filename).toString().replace(_BOM, '');
}
}
compile—处理旧版scope问题,new Template返回其compile
if (opts && opts.scope) {
if (!scopeOptionWarned){
console.warn('`scope` option is deprecated and will be removed in EJS 3');
scopeOptionWarned = true;
}
if (!opts.context) {
opts.context = opts.scope;
}
delete opts.scope;
}
createRegex—匹配对应的标示符号
function () {
var str = _REGEX_STRING;
var delim = utils.escapeRegExpChars(this.opts.delimiter);
var open = utils.escapeRegExpChars(this.opts.openDelimiter);
var close = utils.escapeRegExpChars(this.opts.closeDelimiter);
str = str.replace(/%/g, delim)
.replace(/</g, open)
.replace(/>/g, close);
return new RegExp(str);
},
GenerateSource—
parseTemplateText—用数组装匹配到的模版标示(<%= %>等)
var result = pat.exec(str);
matchs循环匹配openDelimiter和closeDelimiter,并scanLine(line)
matches.forEach(function (line, index) {
var closing;
// If this is an opening tag, check for closing tags
// FIXME: May end up with some false positives here
// Better to store modes as k/v with openDelimiter + delimiter as key
// Then this can simply check against the map
if ( line.indexOf(o + d) === 0 // If it is a tag
&& line.indexOf(o + d + d) !== 0) { // and is not escaped
closing = matches[index + 2];
if (!(closing == d + c || closing == '-' + d + c || closing == '_' + d + c)) {
throw new Error('Could not find matching close tag for "' + line + '".');
}
}
self.scanLine(line);
});
scanLine生成了source
scanLine: function (line) {
...
this.source +=
}
compile函数根据source并添加函数定义给new Function初始化
fn = new ctor(opts.localsName + ', escapeFn, include, rethrow', src);
最好传入data执行
handleCache(opts, template)(data);
var _REGEX_STRING = '(<%%|%%>|<%=|<%-|<%_|<%#|<%|%>|-%>|_%>)';
匹配文本,以匹配到的内容为临界点分割成数组arr。(这一步很关键,这步后基本思路瞬间清晰了。所以假如要处理其他类型的文本,最关键依然该是分析文本特点,以关键标示形成适合的数据模型,方便进一步处理!)(<%%|%%>|<%=|<%-|<%_|<%#|<%|%>|-%>|_%>)
为依据输出对应执行语句<% arr.forEach(item) { %>
能这样使用函数的)写到的正则就记在这吧,以便后观
// 协议,主机地址,后缀,ip规则可加
/^http[s]?:\/\/([.-A-Za-z0-9]+(?:com|cc|top|cn)|\d{2,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).+/g.exec("https://127.0.0.1/122?231"),
从业务出发,什么是登录?
本系统账号密码
社群账号、微信谷歌
等等
定位到你的userId
生成通关文碟
cookie
session
token
单点登录(Single Sign On),简称为 SSO,是比较流行的企业业务整合的解决方案之一。SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统。
百科的解释,两个关键词:多个应用,登录一次
换句话说,一个通关文碟,访问多个应用
让多个应用服务端都能识别一个通关文碟,它们肯定要是:统一的一个鉴权,或者相互调研验证的鉴权
多个应用客户端能持有一个通关文碟,通关文碟肯定是:放在一个公共读取的地方,或者相互通信、通道去拿到
以上2个条件每个都可以有多个方案,交叉组队方案就很多了,实际情况要安全、准确性等其他条件综合考虑。
客户端:cookie、httpOnly、一个Domain
服务端:一个accounts服务
重大异常
物理因素等,无法处理
一般异常
不捕获,抛出来,让开发者发现问题
需要捕获异常,属于正常发生的不正常情况
文章流程是这样的:首先是题目与几个观点,然后通过几个问题讲一讲mobx的逻辑,然后再回到题目的观点,最后就是对mobx或者其他方案的态度就是没态度。或者说团队开发是软工的事,个人就随心意
题目的来源是在读mobx文档时看到的,即以上链接,给出的几个观点
how can let React render?how can let react escape rerender?
there are useState, setState and force-update
// 一种渲染和规避的方式
class Demo extends React.Component{
state = {
box: 1
}
shouldComponentUpdate(_, state) {
// console.log(arguments);
return state.box>5 ? false : true;
}
render() {
return <div>
<h1>hello world, {this.state.box}</h1>
<button onClick={()=>this.setState(prevState=>({
box: prevState.box+1
}))}>click+</button>
</div>
}
}
how MOBX render?
const box = observable.box(1);
const act = action(()=>{
box.set(box.get()+1)
})
autorun(()=>{
console.log(box.get());
})
act()
act()
act()
act()
what does the inject of Mobx-react do?
基本主要就是做了把context作为props传递给组件
function createStoreInjector(
grabStoresFn: IStoresToProps,
component: IReactComponent<any>,
injectNames: string,
makeReactive: boolean
): IReactComponent<any> {
// Support forward refs
let Injector: IReactComponent<any> = React.forwardRef((props, ref) => {
const newProps = { ...props }
const context = React.useContext(MobXProviderContext)
Object.assign(newProps, grabStoresFn(context || {}, newProps) || {})
if (ref) {
newProps.ref = ref
}
return React.createElement(component, newProps)
})
if (makeReactive) Injector = observer(Injector)
Injector["isMobxInjector"] = true // assigned late to suppress observer warning
// Static fields from component should be visible on the generated Injector
copyStaticProperties(component, Injector)
Injector["wrappedComponent"] = component
Injector.displayName = getInjectName(component, injectNames)
return Injector
}
what does the observe of Mobx-react do?
重要的,首先是Reaction对象,去track(利用getter把reaction传递给observable,最后addObserver)
trackDerivedFunction<T>(derivation: IDerivation, f: () => T, context: any) {
const prevAllowStateReads = allowStateReadsStart(true)
// pre allocate array allocation + room for variation in deps
// array will be trimmed by bindDependencies
changeDependenciesStateTo0(derivation)
derivation.newObserving_ = new Array(derivation.observing_.length + 100)
derivation.unboundDepsCount_ = 0
derivation.runId_ = ++globalState.runId
const prevTracking = globalState.trackingDerivation
globalState.trackingDerivation = derivation
globalState.inBatch++
let result
if (globalState.disableErrorBoundaries === true) {
result = f.call(context)
} else {
try {
// 这里通过globalState同步关联
result = f.call(context)
} catch (e) {
result = new CaughtException(e)
}
}
globalState.inBatch--
globalState.trackingDerivation = prevTracking
// 这个地方是把Reaction 绑定到observable
bindDependencies(derivation)
warnAboutDerivationWithoutDependencies(derivation)
allowStateReadsEnd(prevAllowStateReads)
return result
}
const reaction = new Reaction(`${initialName}.render()`, () => {
if (!isRenderingPending) {
// N.B. Getting here *before mounting* means that a component constructor has side effects (see the relevant test in misc.js)
// This unidiomatic React usage but React will correctly warn about this so we continue as usual
// See #85 / Pull #44
isRenderingPending = true
if (this[mobxIsUnmounted] !== true) {
let hasError = true
try {
setHiddenProp(this, isForcingUpdateKey, true)
if (!this[skipRenderKey]) Component.prototype.forceUpdate.call(this)
hasError = false
} finally {
setHiddenProp(this, isForcingUpdateKey, false)
if (hasError) reaction.dispose()
}
}
}
})
why it doesn't re-render when i update simple context object, and it does when i update MOBX object?
const aaa = {
box: 1,
plus() {
this.box++;
},
}
const ctx = React.createContext(aaa)
const MMMM = () => {
const ctxx = React.useContext(ctx);
const [_, setA] = React.useState(0);
return <div>
<h1>hello mobx, {ctxx.box}</h1>
<button onClick={()=>{
// act();
ctxx.plus();
console.log(ctxx.box, aaa.box);
if(ctxx.box > 5) {
setA(ctxx.box)
}
}}>
click+
</button></div>
}
同步是否易于开发?(就是个个人感知问题!)是不是最优解哪?hooks能不能解决上述问题
我的总结没有答案。我想说的是:基于一点点的不同,一点点的变化,一点点的用户习惯,造轮子有没有必要?前端生态的繁荣有时候就是靠这些一点点,才有我们今天应对各种场景时,选择方案的从容。
so保持开放的心态,持续学习
图片查看
看图说特性:
atom({
key: "atom",
default: "",
})
// 输出什么?
{key: "atom"}
更快的加载,更省资源
用户体验,看到网页,一个正常的网页
要考虑的硬件点:1. 浏览器同时加载资源几路?6,同源。要考虑的软件点:1. SPA网站需要加载的资源巨多
a. 怎么并行最大化?多源6路
b. 怎么串行最大化?一次传输,不拆包,不重复建立通路
c. 怎么不请求
以上这两点,都是理想化的,因为网速、带宽,各个资源大小不一。但是其实还是能说明一个点,4M拆2个有点粗暴
折中方案:
a. 媒体资源不同源
b. 不需要一味优化代码体积,打包工具推荐2M用就完了
c. 缓存,强制缓存,关键要分包,把基础依赖资源利用MD5固定文件路径强制缓存下来,所以这个对应上面的基础包体积大,并不是啥问题
在账户找回业务场景中,需要使用用户的一部分信息,其中有图片信息。然后就出现一个问题,img标签src图片链接是不能显示的,然而用浏览器新开tab的方式是可以正常显示图片的。
登陆态与非登陆态表现不同的原因在,登陆态的可以获取预先协议规则,使用非img标签形式获取资源,而非登陆不能。代码中与浏览器tab表现不同的原因在:有无Referer。
线上image资源作为公开资源,不做一定的限制,面临问题的—盗链,以及带来的流量负担与版权问题
正向:怎么做资源保护与下载:
反向:
由于常规图片资源还是要兼容浏览器查看的,所以屏蔽referer就是应对方案。方法:
<meta name="referrer" content="no-referrer">
也是想了一下午怎么更好的实现,感觉也挺有意思的,或许这就是人们讲算法数据结构才是程序员的终极奥义,不过我不怎么喜欢干刷算法题,未来要多找点有场景的多玩玩
从发展历史可以总结技术发展规律,如何从一个个人项目到W3C标准
为什么把worker拿出来,因为两者可以搭配使用
WebAssembly | Worker | JS | |
---|---|---|---|
性能 | 高 | 中 | 低 |
安全性 | 高 | 中 | 低 |
生态丰富性 | 中 | 低 | 高 |
兼容性 | 高 | 高 | 高 |
适合任务 | 高性能、计算密集型任务 | 异步执行、耗时任务 | 快速开发、通用任务 |
// js胶水代码
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" .
// html模版代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="root"></div>
</body>
<script src="wasm_exec.js"></script>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject)
.then((result) =>{
go.run(result.instance)
});
</script>
</html>
package main
import (
"fmt"
"syscall/js"
"time"
)
// js可以调用的函数
func forJsFunc(this js.Value, args []js.Value) any {
// duration := int(js.Value(args[0]).Int())
duration := args[0].Int()
var cb = args[len(args)-1]
go func() {
// time.Sleep(time.Second * duration)
time.Sleep(time.Duration(duration) * time.Second)
str := fmt.Sprintf("it's %d second", duration)
cb.Invoke(js.ValueOf(str))
}()
return nil
}
func main() {
done := make(chan int, 0)
// js的golbal
var jsGlobal = js.Global()
// Set、Get对象等
var document = jsGlobal.Get("document")
var root = document.Call("getElementById", "root")
root.Set("innerHTML", js.ValueOf("hello world!"))
var alert = jsGlobal.Get("alert")
// 执行
alert.Invoke("alert")
// 设置js的全局函数
jsGlobal.Set("wamsFunc", js.FuncOf(forJsFunc))
<-done
}
// 编译
GOARCH=wasm GOOS=js go build -o test.wasm main.go
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.