• Stars
    star
    216
  • Rank 178,154 (Top 4 %)
  • Language
    CoffeeScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Meta-Flux Framework

Arda

Meta-Flux framework for real world.

$ npm install arda --save

Changelog

  • v0.16: Drop React < v15

Concept

Today's Flux is weak at scene transitions. Arda make it simple by router and context(chunk of flux).

Context has Flux features and its stack is very simple.

  • Dispatcher is just EventEmitter
  • View is just React.Component (with mixin)
  • Store should be covered by typesafe steps with promise

I need to develop to make my company's react project simple. Arda is started from extraction of my works and well dogfooded. Thx Increments Inc.

Goals

  • Transition with Promise
  • Loose coupling and testable
  • TypeScript, CoffeeScript, and ES6 friendly
  • Protect mutable state and make it atomic.

Intro

Context, it extends way of react, is just one flux loop and has data flow, Props => State => ComponentProps

simple example

window.React = require('react');
var Arda = require('arda');
var Clicker = React.createClass({
  mixins: [Arda.mixin],
  render() {
    return React.createElement('button', {onClick: this.onClick.bind(this)}, this.props.cnt);
  },
  onClick() {
    this.dispatch('hello:++');
  }
});

class ClickerContext extends Arda.Context {
  get component() {
    return Clicker;
  }

  initState() {
    return {cnt: 0};
  }

  expandComponentProps(props, state) {
    return {cnt: state.cnt};
  }

  delegate(subscribe) {
    super.delegate();
    subscribe('context:created', () => console.log('created'));
    subscribe('hello:++', () =>
      this.update((s) => { return {cnt: s.cnt+1}; })
    );
  }
};

window.addEventListener('DOMContentLoaded', () => {
  var router = new Arda.Router(Arda.DefaultLayout, document.body);
  router.pushContext(ClickerContext, {});
});

Transition

Arda.Router has pushContext, popContext and replaceContext and return promise object.

(coffeescript)

router = new Arda.Router(Arda.DefaultLayout, document.body)
router.pushContext(MainContext, {})             # Main
.then => router.pushContext(SubContext, {})     # Main, Sub
.then => router.pushContext(MainContext, {})    # Main, Sub, Main
.then => router.popContext()                    # Main, Sub
.then => router.replaceContext(MainContext, {}) # Main, Main
.then => console.log router.history

pushContext and replaceContext's second argument is to be context.props as immutable object.

LifeCycle

(coffeescript)

subscriber = (context, subscribe) ->
  subscribe 'context:created', -> console.log 'created'
  subscribe 'context:started', -> console.log 'started'
  subscribe 'context:paused', -> console.log 'paused'
  subscribe 'context:resumed', -> console.log 'resumed'
  subscribe 'context:disposed', -> console.log 'disposed'

class MyContext extends Arda.Context
  component: MyComponent
  subscribers: [subscriber]

static subscribers is automatic delegator on instantiate.

DispatcherButton

This is just utility ReactElement.

(coffeescript)

{DispatcherButton} = arda
React.createClass
  mixins: [Arda.mixin]
  render: ->
    React.createElement 'div', {}, [
      React.createElement DispatcherButton, {
        event: 'foo-event'
        args: ['foo-id-12345']
      }, 'foo' # => button foo
      React.createElement DispatcherButton, {
        event: 'foo-event'
        args: ['foo-id-**']
        className: 'custome-button'
      }, [
        React.createElement 'span', {}, 'text'
      ] # => span.custome-button > span text
    ]

with TypeScript

To achive purpose to make mutable state typesafe, Arda with TypeScript is better than other AltJS.

interface Props {firstName: string; lastName: string;}
interface State {age: number;}
interface ComponentProps {greeting: string;}

class MyContext extends Arda.Context<Props, State, ComponentProps> {
  get component() {
    return React.createClass({
      mixins: [Arda.mixin],
      render: function(){return React.createElement('h1', {}, this.props.greeting);}
    });
  }

  initState(props){
    // Can use promise  (State | Promise<State>)
    return new Promise<State>(done => {
      setTimeout(done({age:10}), 1000)
    })
  }
  expandComponentProps(props, state) {
    // Can use promise  (ComponentProps | Promise<ComponentProps>)
    return {greeting: 'Hello, '+props.firstName+', '+state.age+' years old'}
  }
}

var router = new Arda.Router(Arda.DefaultLayout, document.body);
// Unfortunately, initial props by router are not validated yet
// If you want, you can create your original router wrapper
router.pushContext(MyContext, {firstName: 'Jonh', lastName: 'Doe'})
.then(context => {
  setInterval(() => {
    context.state(state => {age: state.age+1}) // this is validated
  }, 1000 * 60 * 60 * 24 * 360) // fire once by each year haha:)
});

See typescript working example

Or see mizchi's starter projectmizchi-sandbox/arda-starter-project

Custom Layout (Advanced)

Arda provide default layout to use. It can resolve most cases.

But occasionally you need custom layout.

example.

const Layout = React.createClass({
  childContextTypes: {
    shared: React.PropTypes.object
  },
  contextTypes: {
    ctx: React.PropTypes.object
  },

  getChildContext() {
    return {shared: this.getContext()};
  },

  getContext() {
    return this.state.activeContext || this.context.shared;
  },

  getInitialState() {
    return {
      activeContext: null,
      templateProps: {}
    };
  },

  render() {
    if (this.state.activeContext != null) {
      this.state.templateProps.ref = 'root';
      return React.createElement(
        this.state.activeContext.component,
        this.state.templateProps
      );
    } else {
      return <div/>
    }
  }
})

// use it!
const router = new Arda.Router(Layout, document.body);

Custom layout is required some implementations.

  • implement contextTypes.shared
  • implement childContextTypes.ctx
  • implement getChildContext() to return contextTypes.shared
  • implement getInitialState() to fill contextTypes.
  • optional: render initial case and use context propeties

This implement resolve dispatch mixin behaviour.

Perhaps you can resolve by Copy and Paste and edit manually.

Custom Renderer (Advanced)

Initialize in node.js to use custom renderer.

const React = require('react')
const Arda = require('arda/node')(React);
const {render} from '@mizchi/react-blessed';

// you should prepare custom layout for its environment
// and function to get root component
// (el: ReactElement) => ReactComponent
const router = new Arda.Router(Layout, layout => {
  const screen = render(layout, {
    autoPadding: true,
    smartCSR: true,
    title: 'react-blessed hello world'
  });
  screen.key(['escape'], () => process.exit(0));
  return screen._component;
});

custom layout hs to fill contextTypes specs. See example with react-blessed

Dependencies

  • React v0.14.0-beta3 >=
  • Promise or its poryfill

API

See detail at index.d.ts

LICENSE

MIT

More Repositories

1

next-editor

Standalone Git Editor on Browser
TypeScript
316
star
2

md2react

markdown to react element
JavaScript
155
star
3

flumpt

JavaScript
151
star
4

frontend-boilerplate

JavaScript
143
star
5

uniroll

Opinionated universal frontend bundler in browser
TypeScript
110
star
6

trans-loader

webpack-less frontend with service-worker
TypeScript
100
star
7

mints

lightweight typescript compiler
TypeScript
94
star
8

amdx

Accelarated MDX
JavaScript
82
star
9

next-boilerplate-20200727

TypeScript
72
star
10

mdbuf

TypeScript
65
star
11

warden

Client router with resouce management
JavaScript
62
star
12

swdev

No bundle frontend by service-worker
TypeScript
60
star
13

hard-reducer

Type friendly reducer helper
JavaScript
56
star
14

tsx-source-jump

Jump from the HTML element to the source code of the generator
TypeScript
46
star
15

horn.js

simple reactive view framework inspired by knockout, angular and backbone
CoffeeScript
45
star
16

frontend-gh-action-playground

TypeScript
44
star
17

stone-skin

Isomorphic IndexedDb and in memory db wrapper.
CoffeeScript
43
star
18

use-react-redux-context

Alternative ReactRedux.connect by useContext for performance
TypeScript
43
star
19

dts-parser

CoffeeScript
43
star
20

next-pwa-boilerplate

Next PWA
JavaScript
34
star
21

my-feed-reader

JavaScript
34
star
22

fe-base

JavaScript
32
star
23

modern-js-stack-example-with-react

JavaScript
32
star
24

skin.js

Simple and primitive DOM wrapper
JavaScript
31
star
25

react-dispatcher-decorator

JavaScript
29
star
26

atom-git-grep

git-grep in atom editor
CoffeeScript
26
star
27

backbone-test-pattern

JavaScript
26
star
28

mdex-editor

JavaScript
24
star
29

shaker-phobia

TypeScript
24
star
30

deftypes.js

JavaScript Type Definition and Typed Function DSLs
CoffeeScript
23
star
31

dev

TypeScript
23
star
32

reiny

Template engine for React / Mithril / Mercury
CoffeeScript
21
star
33

modern-node-project-2022

TypeScript
20
star
34

remix-d1-bullets

TypeScript
20
star
35

redux-workerized

TypeScript
19
star
36

zodiarg

TypeScript
19
star
37

momic

CoffeeScript
17
star
38

minlink

Minimum(> 1kb) and isomorphic worker wrapper with comlink like rpc.
TypeScript
17
star
39

bower-build

CoffeeScript
17
star
40

rn-expo-firebase-web

JavaScript
17
star
41

flyio-playground

JavaScript
16
star
42

frontend-challenges

TypeScript
16
star
43

gulp-react-jade

CoffeeScript
15
star
44

Wanderer

Websocket Based Diablo Clone
JavaScript
15
star
45

injector.js

JavaScript DCI inspired by Robotlegs
CoffeeScript
14
star
46

vk

CoffeeScript
14
star
47

vistree

TypeScript
13
star
48

mizchi

13
star
49

qwik-svelte

TypeScript
13
star
50

dotfiles-deprecated

Shell
13
star
51

webview-pane

CoffeeScript
12
star
52

tcs

Tiny and typed coffeescript like language
CoffeeScript
12
star
53

react-unite

Editable layout system
TypeScript
12
star
54

toxen

TypeScript
12
star
55

posenet-worker

TypeScript
12
star
56

frontend-deno

HTML
11
star
57

modern-js-stack-example

JavaScript
11
star
58

frontend-starter-react-redux-typescript

TypeScript
11
star
59

gpt-markup-preview

TypeScript
10
star
60

bundle-on-browser

TypeScript
10
star
61

bincr

incremental build helper
JavaScript
10
star
62

whiteboard

JavaScript
10
star
63

ardux

Async + Reducer + Redux Like API
JavaScript
10
star
64

promised-reducer

Fold promise queue and detect sync or async
JavaScript
9
star
65

absurd-sql-example-with-typeorm

TypeScript
9
star
66

summon

CoffeeScript
9
star
67

mz-match

JavaScript
9
star
68

vite-monorepo-example

TypeScript
9
star
69

react-animation

JavaScript
9
star
70

prj

minimum frontend project
HTML
8
star
71

overworld

JavaScript
8
star
72

sabizan

Network proxy by ServiceWorker
JavaScript
8
star
73

lint-with-deno

TypeScript
8
star
74

next-amp-tailwind-purgecss

TypeScript
8
star
75

lizod

TypeScript
8
star
76

next-prisma-example

TypeScript
7
star
77

qwik-microfront-base

TypeScript
7
star
78

flow-require

JavaScript
7
star
79

next-like-frameworks

TypeScript
7
star
80

visual-typescript

TypeScript
6
star
81

web-shell

TypeScript
6
star
82

sketch-board

JavaScript
6
star
83

microfront-base

TypeScript
6
star
84

sqlite-wasm-playground

TypeScript
6
star
85

vue-haxe

Haxe
6
star
86

libretto

CoffeeScript
6
star
87

kawa.js

CoffeeScript
6
star
88

berry-typescript-project

JavaScript
5
star
89

markdown-code-features

TypeScript
5
star
90

bony

JavaScript
5
star
91

frontend-starter

Start your own frontend project
HTML
5
star
92

whirlr.js

Deferred task runner and manager.
CoffeeScript
5
star
93

minfront

JavaScript
5
star
94

bundler

hobby bundler
TypeScript
5
star
95

mytown

TypeScript
5
star
96

underscore.d.ts

5
star
97

virdy

Handy react element DSL
CoffeeScript
5
star
98

noo

CoffeeScript
5
star
99

vite-wasm-pack-starter

Rust
5
star
100

qwik-vue

TypeScript
5
star