实现一个事件总线,要求满足一下特性:
- 简单的回调注册和事件触发。
// 注册
bus.on('event', function listener1(arg1, arg2) { ... } )
// 触发
bus.trigger('event', arg1, arg2)- 同一事件下,回调可以指定顺序。要求有检测错误的能力。
实现一个事件总线,要求满足一下特性:
// 注册
bus.on('event', function listener1(arg1, arg2) { ... } )
// 触发
bus.trigger('event', arg1, arg2)| // 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 ( |
| 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; |
| 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) { |
| 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 |
| 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 |
| // 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; | |
| } |