Skip to content

Instantly share code, notes, and snippets.

View starandtina's full-sized avatar

Xin(Khalil) Zhang starandtina

View GitHub Profile

实现一个事件总线,要求满足一下特性:

  1. 简单的回调注册和事件触发。
// 注册
bus.on('event', function listener1(arg1, arg2) { ... } )
// 触发
bus.trigger('event', arg1, arg2)
  1. 同一事件下,回调可以指定顺序。要求有检测错误的能力。
@starandtina
starandtina / connect.js
Created December 15, 2018 14:20 — forked from gaearon/connect.js
connect.js explained
// connect() is a function that injects Redux-related props into your component.
// You can inject data and callbacks that change that data by dispatching actions.
function connect(mapStateToProps, mapDispatchToProps) {
// It lets us inject component as the last step so people can use it as a decorator.
// Generally you don't need to worry about it.
return function (WrappedComponent) {
// It returns a component
return class extends React.Component {
render() {
return (
@starandtina
starandtina / unistore.js
Created October 11, 2018 13:02 — forked from developit/unistore.js
Update: the newer & better version of this is published: https://github.com/developit/unistore
import { h, Component } from 'preact';
/** Creates a new store, which is a tiny evented state container.
* @example
* let store = createStore();
* store.subscribe( state => console.log(state) );
* store.setState({ a: 'b' }); // logs { a: 'b' }
* store.setState({ c: 'd' }); // logs { c: 'd' }
*/
/* --------------------------------
Typography
-------------------------------- */
:root {
--font-primary: sans-serif;
--font-secondary: serif;
@starandtina
starandtina / StateDispatcher.js
Created August 4, 2018 14:35 — forked from francisrstokes/StateDispatcher.js
Redux without redux - sharing state and one way data flow using only standard react
import React from 'react';
export class StateDispatcher extends React.Component {
constructor(props) {
super(props);
this.state = props.state || {};
this._dispatch = this.dispatch.bind(this);
}
dispatch(action) {
@starandtina
starandtina / require.js
Created October 13, 2017 02:24 — forked from nitin42/require.js
How Node.js's require() works ??
const some_module = require('some_module')
/**
* require('some_module') calls Module._load
*
* Module._load then tries to load the module with a filename (also save it to the cache) using module.load(filename)
*
* module.load(filename), given a filename, passes it to the proper extension handler ('.js', '.json')
*
* If there were any errors when loading the file, it deletes the file from the cache (delete Module._cache[filename]) and throws an error
@starandtina
starandtina / type.js
Created August 23, 2017 13:35 — forked from kirilloid/type.js
getType
function getType (value) {
let type = typeof value;
if (type === 'object') {
return value ? Object.prototype.toString.call(value).slice(8, -1) : 'null';
}
return type;
}
[NaN, 0, 1, Infinity, // numbers
null, undefined, false, 'str', // other primitives
@starandtina
starandtina / asyncComponent
Last active May 22, 2017 03:38 — forked from acdlite/app.js
Quick and dirty code splitting with React Router v4
// getComponent is a function that returns a promise for a component
// It will not be called until the first mount
function asyncComponent(getComponent) {
return class AsyncComponent extends React.Component {
static Component = null;
state = { Component: AsyncComponent.Component };
componentWillMount() {
if (!this.state.Component) {
getComponent().then(Component => {
var str = 'class ಠ_ಠ extends Array {constructor(j = "a", ...c) {const q = (({u: e}) => {return { [`s${c}`]: Symbol(j) };})({});super(j, q, ...c);}}' +
'new Promise((f) => {const a = function* (){return "\u{20BB7}".match(/./u)[0].length === 2 || true;};for (let vre of a()) {' +
'const [uw, as, he, re] = [new Set(), new WeakSet(), new Map(), new WeakMap()];break;}f(new Proxy({}, {get: (han, h) => h in han ? han[h] ' +
': "42".repeat(0o10)}));}).then(bi => new ಠ_ಠ(bi.rd));';
try {
eval(str);
} catch(e) {
alert('Your browser does not support ES6!')
}
import { connect } from 'react-redux';
import { isFeatureEnabled } from './reducers'
function EnabledFeature({ isEnabled, children }) {
if (isEnabled) {
return children;
}
return null;
}