Giter VIP home page Giter VIP logo

remold's Introduction

Remold

Remold is extra small (~500B minified and gzipped) object-oriented alternative to the popular libraries for state-management such as Redux, or Mobx. With Remold you no longer need to create a redux-like singleton, fight with boilerplate, and overuse primitives. Instead, you can reuse your domain-specific objects.

Table of Contents

Installation

Remold requires React 15.0 or later.

npm install --save remold

Getting Started

Every object which wants to be represented as a React component has to inherit from Remold class:

import { Remold } from 'remold'

export default class Counter extends Remold {
  count = 0
}

Every method which can modify object's state has to be decorated with act:

import { Remold, act } from 'remold'

export default class Counter extends Remold {
  count = 0
  
  @act increase() { this.count += 1 }
  
  @act decrease() { this.count -= 1 }
}

Every method which connects a React component to an object has to be decorated with mold(aComponent):

import React from 'react'
import { Remold, act, mold } from 'remold'

const CounterView = ({ count, onPlus, onMinus }) => (
  <div>
    <p>{count}</p>
    <button onClick={onMinus}>-</button>
    <button onClick={onPlus}>+</button>
  </div>
)

export default class Counter extends Remold {
  count = 0
  
  @act increase() { this.count += 1 }
  
  @act decrease() { this.count -= 1 }
  
  @mold(CounterView) asView() {
    return {
      count: this.count,
      onPlus: this.increase,
      onMinus: this.decrease,
    }
  }
}

The methods decorated with mold(aComponent) returns an instance of the React component:

import React from 'react'
import Counter from './Counter'

const counter = new Counter()

const App = () => (
  <div>
    {counter.asView()}
  </div>
)

Notes

  • Both act and mold(aComponent) decorators bound decorated method
  • Every molded component has a unique key prop, so you don't need to manage this prop manually

Rationale

The dominant part of our frontend-applications represents a visual description of domain-specific objects. However, modern libraries push us away from objects, so we have to reinvent the wheel, overuse primitives, and write procedural code which mutates one global store.

Let's look at the Facebook 'chat problem'. According to this problem, our app should:

  1. increment unseen thread count
  2. append message in the chat tab
  3. if open, append message in the main messages view
  4. if the chat tab is focused or the main messages view is open, decrement the unseen count

Solving this problem, Facebook reinvented the Object-Oriented Programming and gave it the name 'Flux.'

Flux OOP
Action Method name (selector)
Dispatcher Object
Store Object's internal state

Remold embraces OOP and doesn't add new abstractions. With Remold a solution to this problem can be simple and straightforward. Firstly, we have to represent our domain with objects:

Chat -> Threads -> Messages
// Domain.js
export class Chat {
  // Initialisation is here
  
  markThreadAsSeen(aThread) {
    aThread.markAsSeen()
  }
  
  get unseenThreadsCount() {
    return this.unseenThreads.length
  }
  
  get unseenThreads() {
    return this._threads.filter(t => t.isUnseen())
  }
  
  addMessage(aMessage, { toThread: aThread }) {
    aThread.addMessage(aMessage)
  }
}

export class Thread {
  // Initialisation is here
  
  isUnseen() {
    return this.unseenMessages.length > 0
  }
  
  markAsSeen() {
    this.unseenMessages.forEach(m => m.markAsSeen())
  }
  
  get unseenMessages() {
    return this._messages.filter(m => m.isUnseen())
  }
  
  addMessage(aMessage) {
    this._messages.push(aMessage)
  }
}

export class Message {
  // Initialisation is here
  
  isUnseen() {
    return this._unseen
  }
  
  markAsSeen() {
    this._unseen = false
  }
}

Our domain is ready, and we have to visualize it using React components:

// Components.jsx
import React from 'react'

export const Counter = ({ count }) => (
  // Component markup
)

export const MessageView = ({ content, isUnseen }) => (
  // Component markup
)

export const ChatTab = ({ messages, onFocus }) => (
  // Component markup
)

export const MainMessagesView = ({ messages, onOpen }) => (
  // Component markup
)

export const SmallChatView = ({ threads }) => (
  // Component markup
)

As the last step we need to connect components to domain objects:

//Domain.js
import { Remold, mold, act } from 'remold'
import { 
  Counter, MessageView, ChatTab, 
  MainMessagesView, SmallChatView 
} from './Components'

export class Chat extends Remold {
  // Initialisation is here
  
  @act markThreadAsSeen(aThread) {
    aThread.markAsSeen()
  }
  
  get unseenThreadsCount() {
    return this.unseenThreads.length
  }
  
  get unseenThreads() {
    return this._threads.filter(t => t.isUnseen())
  }
  
  @act addMessage(aMessage, { toThread: aThread }) {
    aThread.addMessage(aMessage)
  }
  
  @mold(Counter) asCounter() {
    return {
      count: this.unseenThreadsCount,
    }
  }
  
  @mold(SmallChatView) asSmallView() {
    return {
      threads: this._threads.map(t => t.asChatTab({ 
        onFocus: () => this.markThreadAsSeen(t) 
      })),
    }
  }
}

export class Thread extends Remold {
  // Initialisation is here
  
  isUnseen() {
    return this.unseenMessages.length > 0
  }
  
  @act markAsSeen() {
    this.unseenMessages.forEach(m => m.markAsSeen())
  }
  
  get unseenMessages() {
    return this._messages.filter(m => m.isUnseen())
  }
  
  @act addMessage(aMessage) {
    this._messages.push(aMessage)
  }
  
  @mold(ChatTab) asChatTab({ onFocus }) {
    return {
      messages: this.messagesView,
      onFocus,
    }
  }
  
  @mold(MainMessagesView) asMainMessagesView({ onOpen }) {
    return {
      messages: this.messagesView,
      onOpen,
    }
  }
  
  get messagesView() {
    return this._messages.map(m => m.asView())
  }
}

export class Message extends Remold {
  // Initialisation is here
  
  isUnseen() {
    return this._unseen
  }
  
  @act markAsSeen() {
    this._unseen = false
  }
  
  @mold(MessageView) asView() {
    return {
      content: this._content,
      isUnseen: this.isUnseen(),
    }
  }
}

That's it. Now we can use the chat in different places of our application:

// App.jsx
import React from 'react';
import { Chat } from './Domain'
import { Header, Body } from './Somewhere'

const chat = new Chat()

export const App = () => (
  <div>
    <Header threadsCounter={chat.asCounter()}/>
    <Body smallChat={chat.asSmallView()}/>
  </div>
)

Now we have all visualization of domain objects in a single place. If you want to visualize message as a preview, add new mold method into message object:

export class Message extends Remold {
  // ...
  @mold(MessagePreview) asPreview() {
    return {
      content: this._content
    }
  }
}

remold's People

Contributors

telichkin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.