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
/** | |
* 构建一个懒对象:该对象只有在被使用(被访问/设置/删除/遍历属性等)时才会被实例化 | |
* 其他方案:采用getter方式,如:module.exports = { get XXX() { return new ClsXXX(); } }; | |
* @param factory 实例化函数 | |
*/ | |
function unstable_makeLazyInstance<T>(factory: () => T): T { | |
let instance: T; | |
function wrap<H extends (...args: any[]) => any>(handle: H): H { | |
return ((...args: any[]) => { |
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 { Store, Unsubscribe } from 'redux'; | |
export class LazyCache<V = any, S = any, M = any> { | |
/** 最后一次更新并转换后的数据 */ | |
private value: V; | |
/** 当前selector后的值,用于作变更对比 */ | |
private currentSelected: M | S; | |
/** 标记对应数据流是否有变更,在使用方使用数据的时候,便可重新生成 */ |
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
/** | |
* Allow define React Component with async dependencies | |
* @param defineClass Return a new class that depends on deps | |
* @param asyncDeps Define what the class depends on | |
*/ | |
export function makeDepsComponent<T extends React.ComponentType<any>, D>( | |
defineClass: (deps: D) => T, | |
asyncDeps: () => Promise<D>, | |
) { | |
return makeLazyComponent(async () => defineClass(await asyncDeps())); |
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
/** | |
* Fill target with default values by source via immutable | |
* Benefits: unlike fp.defaultsDeep(), we don't deep clone target to get the real immutable. | |
* @param target target | |
* @param source source | |
* @param options.ignoreArray whether append elements to array | |
*/ | |
export function immutableDefaultsDeep(target: any, source: any, options?: { ignoreArray?: boolean }): any { | |
const targetType = typeof target; | |
const sourceType = typeof source; |
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
export class WatchObjectOptions { | |
/** How deep do we put a proxy */ | |
depth? = 10; | |
nameKey? = Symbol('$keyName'); | |
nameParent? = Symbol('$parent'); | |
// nameAppliedProxy? = Symbol('$appliedProxy'); |
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
/** | |
* Simple clone JSON-like object (No circular reference, No class inherit, Plain object) | |
* [NOTE] Not tested on all cases | |
* @param obj object to be clone | |
* @param excludeFields exclude fields that will ignored copying | |
*/ | |
export function deepClone<T>(obj: any, excludeFields?: string[]) { | |
if (obj === null || typeof obj !== 'object') { return obj; } | |
if (Object.prototype.toString.call(obj) === '[object Array]') { | |
return obj.map((element) => deepClone(element, excludeFields)); |
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
/** | |
* Recursively iterate table data rows, if iterator() returns false, then will stop iterating | |
* @param rows Data list which may contain children | |
* @param iterator Iterator, return false to stop iterating | |
* @param childrenField Specify the children field's key | |
* @param _parent [Private] The parent of the rows, null by default, used for iterator() ONLY | |
* @return If iterated all items, this method will return true | |
*/ | |
function recurseRows<T>( | |
rows: T[], |
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
/** | |
* Input decorator that handle a prop to do get/set automatically with toBoolean | |
* | |
* Why not using @InputBoolean alone without @Input? AOT needs @Input to be visible | |
* | |
* @howToUse | |
* ``` | |
* @Input() @InputBoolean() visible: boolean = false; | |
* | |
* // Act as below: |
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
/** | |
* Author: Wilson Zeng | |
*/ | |
#define NULL 0 | |
class Element { | |
public: | |
int key; | |
int value; |
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
/** | |
* Author: Wilson Zeng | |
*/ | |
#include <stdio.h> | |
#include <cstdlib> | |
#include <string.h> | |
using namespace std; | |
// DECLARATIONS |
NewerOlder