{"id":16888322,"url":"https://github.com/threepointone/disto","last_synced_at":"2025-07-17T00:06:10.306Z","repository":{"id":30563221,"uuid":"34118110","full_name":"threepointone/disto","owner":"threepointone","description":"mildly opinionated flux","archived":false,"fork":false,"pushed_at":"2016-03-25T18:21:54.000Z","size":1520,"stargazers_count":65,"open_issues_count":1,"forks_count":7,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-04T05:40:15.711Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/threepointone.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-17T13:27:46.000Z","updated_at":"2022-05-30T02:19:51.000Z","dependencies_parsed_at":"2022-08-17T19:45:26.716Z","dependency_job_id":null,"html_url":"https://github.com/threepointone/disto","commit_stats":null,"previous_names":[],"tags_count":47,"template":false,"template_full_name":null,"purl":"pkg:github/threepointone/disto","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/threepointone%2Fdisto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/threepointone%2Fdisto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/threepointone%2Fdisto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/threepointone%2Fdisto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/threepointone","download_url":"https://codeload.github.com/threepointone/disto/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/threepointone%2Fdisto/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265553224,"owners_count":23787036,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T16:50:53.518Z","updated_at":"2025-07-17T00:06:10.215Z","avatar_url":"https://github.com/threepointone.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"disto\n---\n\n(deprecated, consider using [redux](https://github.com/gaearon/redux) instead)\n\n- follows the original [flux](http://facebook.github.io/flux) architecture\n- a simple api, with no new concepts\n- leans heavily on regular functions\n- stores have no setters or ajax / async calls\n- shorthand notation for action creators, with async function / promise support\n- live editing experience across action creators / stores / views\n- timetravel helper\n- includes mixin to polyfill [sideloading data on components](https://github.com/facebook/react/issues/3398)\n- browser / server / react-native compatible, because apparently that's a thing now\n- really tiny - base ~2k, another 2k for dev goodies.\n- [tests](https://github.com/threepointone/disto/blob/master/test/index.js)\n- i love you\n\n`npm install disto --save`\n\n```js\nvar {Dis} = require('disto');\n// Dispatcher class.\n```\n\ndispatcher\n---\n\nThe dispatcher uses the fb dispatcher under the hood,\nbut the api is tweaked for our stores / actions\n\n```js\nvar dispatcher = new Dis();\n\ndispatcher.register(initialState, fn, compare)\n\ndispatcher.unregister(store)\n\ndispatcher.dispatch(action, ...args)\n\ndispatcher.waitFor(...stores)\n\ndispatcher.act(creators)\n```\n\nactions\n---\n\nAction creators can be however you choose. This is how I write them.\n\nThe action creator helper takes a map of key/values,\nand generates a collection of functions that, when each are called,\ndispatches a unique action along with passed arguments\nfurther calling any optional function passed in the map.\n\nIndeed, we use the action creator *itself* as the 'actionType'\nto much convenience\n\nWhat this means, is that you'll likely\nnever have to dispatch a raw action by yourself.\n\nAlso, since these are unique objects (with readable string representations),\nyou also don't have to worry about global namepace clashes.\n\n```js\nvar $ = dispatcher.act({\n  init: '',   // use a blank string for default function\n  a: '',\n  b: function(){\n    console.log('possible fire an ajax request here');\n  },\n  c: function(){\n    // you can alias to another creator like so\n    $.b();\n  },\n  d: function(){\n    // creators can also call an optional .done() action\n    // this is useful for ajax / other async operations\n    setTimeout(function(){\n      $.d.done('any', 'args', 'you', 'like');\n    }, 500);\n  }\n  e: function(){\n    // you can also return a Promise from an action creator,\n    // and .done() gets called when it resolves\n    return new Promise(function(resolve, reject){\n      resolve('success!');\n    });\n  },\n  f: async function(q){\n    // you can use es7 async functions\n    // and .done() will get called when it finishes\n    return await fetch(`/search/${q}`);\n  },\n  g: async function(q){\n    // finally, throwing errors / rejecting promises will call .error()\n    throw new Error('disto');\n  }\n}, 'baconium' /* optional prefix to dev strings */);\n\n// $.a is now a function\n\n$.a(1, 2, 3);  // dispatches [$.a, 1, 2, 3] to all stores\n\nconsole.log($.a.toString())  // baconium:~:a\n\n$.b();  // dispatches [$.b], and then logs \"possibly fire...\"\n\n$.c();  // dispatches [$.c], then [$.b], and then logs \"possibly fire...\"\n\n$.d();  // dispatches [$.d], later [$.d.done, 'any', 'args', 'you', 'like']\n\n$.e();  // dispatches [$.e], then [$.e.done, 'success!']\n\n$.f();  // dispatches [$.f], later [$.f.done, response]\n\n$.g();  // dispatches [$.g], then [$.g.error, Error:disto]\n\n// these actions are consumed by stores,\n// which hold all the 'state'\n```\n\nstores\n---\n\nStores are represented as initial state + a 'reduce' function\nthat get called on every [actions, ...args] message\nthat passes through the \"system\".\n\nWhile this might seem terse, it's a fully open system,\nand you should be able to build any abstraction on top of it.\n\n```js\nvar initialState = {\n  q: '',\n  res: [],     // initial state\n  err: null\n};\n\nfunction reduce(state, action, ...args){\n  switch(action){\n    case $.query:\n      let [q] = args;\n      return {\n        ...state, q\n      };\n\n    case $.query.done:\n      let [err, res] = args;\n      return {\n        ...state, err, res\n      };\n\n    default:\n      return state;\n  }\n}\n\nvar store = dispatcher.register(initialState, reduce);\n\nstore.get()   // returns current value\n\n// you can optionally pass in a custom 'compare' function\n// which decides when to trigger 'change' events\n// analogous to 'shouldComponentUpdate'\n\n// eg, with immutable-js (https://facebook.github.io/immutable-js/)\n// we'd use immutable.is to compare states\n\nvar iStore = dispatcher.register(Immutable.Map({\n  loading: false,\n  err: null,\n  results: []\n}), function(o, action, ...args){\n  // returns immutable structures\n}, Immutable.is);\n\n// stores are also lightweight 'observables',\n\nstore.subscribe(function(state){\n  console.log('store state changed to', state);\n})\n\n// we use this to hook on to react components via the .observe() polyfill\n\nvar mix = require('disto').mix;\n\nvar Component = React.createClass({\n  mixins: [mix],\n  observe: function(props){\n    return {a: store1, b: store2};\n  },\n  render: function(){\n    var data = this.state.data;\n    return \u003cdiv\u003e\n      current value of store 1 : {data.a}\n      current value of store 2 : {data.b}\n    \u003c/div\u003e;\n  }\n};\n\n```\n\nhot loading\n---\n\nto enable hot loading of stores/actions, use hot versions of the dispatcher register/act functions\n```js\nvar {register, act} = require('disto').hot(dispatcher, module);\n\nvar store = register(initial, reduce);\n\n// etc etc\n```\n\n(there are quirks around this that I'll document soon)\n\nworks well with [react-hot-loader](https://github.com/gaearon/react-hot-loader/)\n\ntime travel!\n---\n\n(compatible with hot mode)\n\n```js\n// run this before registering any other stores\nvar r = require('disto/lib/record').setup(dispatcher, module);\n\nvar i = r.snapshot()  // takes a snapshot of current state\nr.goTo(i)             // 'goes' to a particular snapshot\n\nr.record()    // start recording\nr.stop()      // stop recording\nr.play()      // replay the session\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthreepointone%2Fdisto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthreepointone%2Fdisto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthreepointone%2Fdisto/lists"}