Elixir-flavored Redux:
defmodule Store do
def start_link(reducer, preloadedState) do
Agent.start_link(fn -> { reducer, preloadedState } end)
end
def start_link(reducer) do
Agent.start_link(fn -> { reducer, nil } end)
| // A collection Orders contains the following documents: | |
| { "_id" : 1, "item" : "abc", "price" : 12, "quantity" : 2 } | |
| { "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1 } | |
| { "_id" : 3 } | |
| // Another collection Inventory contains the following documents: | |
| { "_id" : 1, "sku" : "abc", description: "product 1", "instock" : 120 } | |
| { "_id" : 2, "sku" : "def", description: "product 2", "instock" : 80 } | |
| { "_id" : 3, "sku" : "ijk", description: "product 3", "instock" : 60 } | |
| { "_id" : 4, "sku" : "jkl", description: "product 4", "instock" : 70 } |
| db.articles.aggregate( | |
| [{ $match: { | |
| $or: [{ | |
| score: { $gt: 90 } | |
| }, { | |
| author: "dave" | |
| }}] | |
| }}] | |
| ); |
| // Given these documents in a collection | |
| { "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80 } | |
| { "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85 } | |
| { "_id" : ObjectId("55f5a192d4bede9ac365b257"), "author" : "ahn", "score" : 60 } | |
| { "_id" : ObjectId("55f5a192d4bede9ac365b258"), "author" : "li", "score" : 55 } | |
| { "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "author" : "annT", "score" : 60 } | |
| { "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "author" : "li", "score" : 94 } | |
| { "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "author" : "ty", "score" : 95 } | |
| // Apply this aggregation |
Elixir-flavored Redux:
defmodule Store do
def start_link(reducer, preloadedState) do
Agent.start_link(fn -> { reducer, preloadedState } end)
end
def start_link(reducer) do
Agent.start_link(fn -> { reducer, nil } end)
| import { createStore } from 'redux' | |
| function counter(state = 0, action) { | |
| switch (action.type) { | |
| case 'INCREMENT': | |
| return state + 1 | |
| case 'DECREMENT': | |
| return state - 1 | |
| default: | |
| return state |
| let currentListeners = [] | |
| let nextListeners = currentListeners | |
| ... | |
| function subscribe(listener) { | |
| if (typeof listener !== 'function') { | |
| throw new Error('Expected listener to be a function.') | |
| } |
| declare type Dispatch<A: { type: $Subtype<string> }> = (action: A) => A; |
| function dispatch(action) { | |
| if (!isPlainObject(action)) { | |
| throw new Error( | |
| 'Actions must be plain objects. ' + | |
| 'Use custom middleware for async actions.' | |
| ) | |
| } | |
| if (typeof action.type === 'undefined') { | |
| throw new Error( |
| ... | |
| let currentState = preloadedState | |
| ... | |
| function getState() { | |
| return currentState | |
| } |
| export default function createStore(reducer, preloadedState) { | |
| let state = preloadedState | |
| let listeners = [] | |
| let dispatching = false | |
| function getState() { | |
| return state | |
| } | |
| function dispatch(action) { |